/*
车站售票系统
#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <sys/types.h>
//车票数量
int ticket1 = 20; int ticket2 = 10; int ticket3 = 30;
//乘客已购买的票
int ticket1_ok; int ticket2_ok; int ticket3_ok;
//线程
void *task1(void *arg); void *task2(void *arg); void *task3(void *arg);
//信号处理函数
void sig_handler(int arg); int key, key1, q = 1; pthread_mutex_t lock; int main(void) { pthread_t tid1, tid2, tid3;
//线程的指针
int fd1, fd2, ds3; //进程的id pthread_mutex_init(&lock, NULL); //动态创建线程互斥锁 printf("欢迎使用xx售票系统!\r\n"); while(1) { printf("请选择需要的服务\r\n"); printf("1、买票, 2、退票, 3、改签, 4、 退出系统\r\n"); scanf("%d", &key); printf("\r\n"); switch(key) { case 1: pthread_create(&tid1, NULL, task1, NULL); //创建线程1,买票 pthread_join(tid1, NULL); //等待线程运行结束 break; case 2: pthread_create(&tid2, NULL, task2, NULL); //创建线程2,退票 pthread_join(tid2, NULL); //等待线程运行结束 break; case 3: pthread_create(&tid3, NULL, task3, NULL); //创建线程3,改签 pthread_join(tid3, NULL); //等待线程运行结束 break; case 4: pthread_mutex_destroy(&lock); //销毁互斥锁 return 0; //结束进程 } } return 0; }
//线程1,买票
void *task1(void *arg) { int num;
while(1) { printf("当前剩余票数:广州:%d 深圳:%d 肇庆:%d\r\n", ticket1, ticket2, ticket3); printf("请问您要购买哪个地方的票\r\n"); printf("1、广州, 2、深圳, 3、肇庆, 4、退出\r\n-----输入相应数字即可\r\n"); scanf("%d", &key1); printf("\r\n"); if(key1 == 4) { printf("\r\n"); pthread_exit(0); //结束当前线程 } printf("请问您需要购买几张\r\n"); scanf("%d", &num); printf("\r\n"); if(key1 == 1) { if((ticket1 - num) >= 0) { ticket1 -= num; //系统剩余的票数 ticket1_ok += num; //乘客手中的票数 printf("购票成功!\r\n"); } else { printf("购票失败---目前前往广州的票还剩%d张!\r\n", ticket1); } printf("\r\n"); } else if(key1 == 2) { if((ticket2 - num) >= 0) { ticket2 -= num; ticket2_ok += num; printf("购票成功!\r\n"); } else { printf("购票失败---目前前往深圳的票还剩%d张!\r\n", ticket2); } printf("\r\n"); } else if(key1 == 3) { if((ticket3 - num) >= 0) { ticket3 -= num; ticket3_ok += num; printf("购票成功!\r\n"); } else { printf("购票失败---目前前往肇庆的票还剩%d张!\r\n", ticket3); } printf("\r\n"); } }
//线程2,退票
void *task2(void *arg) { char temp = 0; //有哪几种票 char flag1 = 0, flag2 = 0, flag3 = 0; //票的种类 char flag11 = 0, flag22 = 0, flag33 = 0; int i, rec; char rec1, rec2, rec3;
if(ticket1_ok > 0) { printf("您购买了%d张前往广州的票!\r\n", ticket1_ok); temp ++; flag1 = 1; } if(ticket2_ok > 0) { printf("您购买了%d张前往深圳的票!\r\n", ticket2_ok); temp ++; flag2 = 1; } if(ticket3_ok > 0) { printf("您购买了%d张前往肇庆的票!\r\n", ticket3_ok); temp ++; flag3 = 1; } if(ticket1_ok == 0 && ticket2_ok == 0 && ticket3_ok == 0) { pthread_mutex_lock(&lock);//加锁 printf("您还没购票!无法为您提供退票服务!\r\n"); printf("10秒后自动关闭该界面!"); signal(14, sig_handler); alarm(10); //10秒后自动结束该线程 for(i = 10; i >= 0; i--) { printf("%d\r\n", i); //倒计时显示 sleep(1); } //while(q); printf("\r\n"); pthread_exit(0); //结束当前线程 } //手中有票 while(1) { rec1 = flag1; rec2 = flag2; rec3 = flag3; printf("请问你需要退的票是?(按4退出该界面!)\r\n"); for(i = 1; i <= temp; i++) //判断已买的票,并排序 { if(rec1 > 0) { printf("%d:广州\t", i); rec1 = 0; flag11 = i; //票的序号 } else if(rec2 > 0) { printf("%d:深圳\t", i); rec2 = 0; flag22 = i; } else if(rec3 > 0) { printf("%d:肇庆\t", i); rec3 = 0; flag33 = i; } } printf("\r\n"); scanf("%d", &rec); //选择要退的票 printf("\r\n"); if(rec == flag11) //退广州的票 { printf("您可以退%d张前往广州的票,请问您需要退几张?\r\n", ticket1_ok); scanf("%d", &rec); //退几张 printf("\r\n"); if(rec > ticket1_ok) { printf("您至多退%d张票\r\n", ticket1_ok); } else { ticket1_ok -= rec; //原有的票减去要退的票 ticket1 += rec; //系统中的票加上退票 if(ticket1_ok > 0) printf("您还有%d张前往广州的票!\r\n", ticket1_ok); else { printf("您已经没有了前往广州的票!\r\n"); flag1 = 0; flag11 = 110; } } printf("\r\n"); } else if(rec == flag22) { printf("您可以退%d张前往深圳的票,请问您需要退几张?\r\n", ticket2_ok); scanf("%d", &rec); printf("\r\n"); if(rec > ticket2_ok) { printf("您至多退%d张票\r\n", ticket2_ok); } else { ticket2_ok -= rec; ticket2 += rec; if(ticket2_ok > 0) printf("您还有%d张前往深圳的票!\r\n", ticket2_ok); else { printf("您已经没有了前往深圳的票!\r\n"); flag2 = 0; flag22 = 120; } } printf("\r\n"); } else if(rec == flag33) { printf("您可以退%d张前往肇庆的票,请问您需要退几张?\r\n", ticket3_ok); scanf("%d", &rec); if(rec > ticket3_ok) { printf("您至多退%d张票\r\n", ticket3_ok); } else { ticket3_ok -= rec; ticket3 += rec; if(ticket3_ok > 0) printf("您还有%d张前往肇庆的票!\r\n", ticket3_ok); else { printf("您已经没有了前往肇庆的票!\r\n"); flag3 = 0; flag33 = 130; } } printf("\r\n"); } else if(rec == 4) { pthread_exit(0);//结束当前线程 } }
//线程3,改签
void *task3(void *arg) { int temp = 0, temp1 = 0; //有几种票 char flag1 = 0, flag2 = 0, flag3 = 0; //票的类型 char flag11 = 0, flag22 = 0, flag33 = 0, flag44 = 0, flag55 = 0, flag66 = 0; char remain1, remain2, remain3; //系统剩下票的标志位 char rec1, rec2, rec3, rec4, rec5, rec6; int local1, local2; //改签的源、目地点 int num = 0; char i;
while(1) { if(ticket1_ok > 0 | ticket2_ok > 0 | ticket3_ok > 0) //手上有票 { printf("您现在有:"); if(ticket1_ok > 0) { printf("%d张前往广州的票\t", ticket1_ok); temp++; flag1 = 1; } if(ticket2_ok > 0) { printf("%d张前往深圳的票\t", ticket2_ok); temp++; flag2 = 1; } if(ticket3_ok > 0) { printf("%d张前往肇庆的票", ticket3_ok); temp++; flag3 = 1; } printf("\r\n"); } else //手上没票 { pthread_mutex_lock(&lock); //加锁 printf("您还没买票,无法为您提供改签服务!如有需要,请到买票窗口买票\r\n"); printf("10秒后自动关闭该界面!"); signal(14, sig_handler); alarm(10); //10秒后自动结束该线程 for(i = 10; i >= 0; i--) { printf("%d\r\n", i); //倒计时显示 sleep(1); } printf("\r\n"); pthread_exit(0); //结束当前线程 } if(ticket1 > 0 | ticket2 > 0 | ticket3 > 0) //系统中还有余票 { printf("现在系统剩余的票有:\r\n"); if(ticket1 > 0) { printf(" 前往广州的票还有%d张\r\n", ticket1); remain1 = 1; temp1++; } if(ticket2 > 0) { printf(" 前往深圳的票还有%d张\r\n", ticket2); remain2 = 1; temp1++; } if(ticket3 > 0) { printf(" 前往肇庆的票还有%d张\r\n", ticket3); remain3 = 1; temp1++; } } else //系统中没有余票 { pthread_mutex_lock(&lock); //加锁 printf("不好意思,现在系统中已经没有余票了!\r\n"); printf("您可以等别人退票或者等加仓!\r\n"); printf("10秒后自动关闭该界面!"); signal(14, sig_handler); alarm(10); //10秒后自动结束该线程 for(i = 10; i >= 0; i--) { printf("%d\r\n", i); //倒计时显示 sleep(1); } printf("\r\n"); pthread_exit(0); //结束当前线程 } /*********************************************************************/ rec1 = flag1; rec2 = flag2; rec3 = flag3; rec4 = remain1; rec5 = remain2; rec6 = remain3; printf("请问您要改签那种票(按4退出该界面!)\r\n"); for(i = 1; i <= temp; i++) { if(rec1 > 0) //flag1 = 0时才不打印 { printf("%d:广州 ", i); rec1 = 0; flag11 = i; //票的序号 } else if(rec2 > 0) { printf("%d:深圳 ", i); rec2 = 0; flag22 = i; } else if(rec3 > 0) { printf("%d:肇庆 ", i); rec3 = 0; flag33 = i; } } printf("\r\n"); scanf("%d", &local1); printf("\r\n"); if(local1 == 4) { pthread_exit(0);//结束当前线程 } printf("现在有票的地点有:\r\n"); for(i = 1; i <= temp1; i++) { if(rec4 > 0) { printf("%d:广州 ", i); rec4 = 0; //flag4 = 0时才不打印 num = 1; flag44 = i; } else if(rec5 > 0) { printf("%d:深圳 ", i); rec5 = 0; num = 1; flag55 = i; } else if(rec6 > 0) { printf("%d:肇庆 ", i); rec6 = 0; num = 1; flag66 = i; } } printf("\r\n"); if(num == 0) //不能改签 { printf("\r\n"); printf("目前没有能改签的地方\r\n"); printf("您可以等别人退票或者等加仓!\r\n"); printf("\r\n"); } else //能改签 { printf("\r\n"); printf("请问您要改签到什么地方?\r\n"); scanf("%d", &local2); //目地点 printf("请问您需要改签几张\r\n"); scanf("%d", &num); if(local1 == flag11) //原地点是广州 { if(ticket1_ok < num) //手上的票数少过要改的数量 { printf("改签失败,您手上没有%d张原地点是广州的票\r\n", num); } else if(local2 == flag44) //目地点是广州 { printf("不能改签到一样的地方!\r\n"); } else if(local2 == flag55) //目地点是深圳 { if(num > ticket2) //系统现有的票数少过要改的数量 { printf("改签失败,现在系统中仅有%d张前往深圳的票\r\n", ticket2); } else { printf("改签成功!\r\n"); ticket1_ok -= num; //手上前往广州的票 ticket1 += num; //系统中前往广州的票 ticket2_ok += num; //手上前往深圳的票 ticket2 -= num; //系统中前往深圳的票 } } else if(local2 == flag66) //目地点是肇庆 { if(num > ticket3) //系统现有的票数少过要改的数量 { printf("改签失败,现在系统中仅有%d张前往肇庆的票\r\n", ticket3); } else { printf("改签成功!\r\n"); ticket1_ok -= num; //手上前往广州的票 ticket1 += num; //系统中前往广州的票 ticket3_ok += num; //手上前往肇庆的票 ticket3 -= num; //系统中前往肇庆的票 } } } else if(local1 == flag22) //原地点是深圳 { if(ticket2_ok < num) //手上的票数少过要改的数量 { printf("改签失败,您手上没有%d张原地点是深圳的票\r\n", num); } else if(local2 == flag44) //目地点是广州 { if(num > ticket2) //系统现有的票数少过要改的数量 { printf("改签失败,现在系统中仅有%d张前往广州的票\r\n", ticket1); } else { printf("改签成功!\r\n"); ticket2_ok -= num; //手上前往深圳的票 ticket2 += num; //系统中前往深圳的票 ticket1_ok += num; //手上前往广州的票 ticket1 -= num; //系统中前往广州的票 } } else if(local2 == flag55) //目地点是深圳 { printf("不能改签到一样的地方!\r\n"); } else if(local2 == flag66) //目地点是肇庆 { if(num > ticket3) //系统现有的票数少过要改的数量 { printf("改签失败,现在系统中仅有%d张前往肇庆的票\r\n", ticket3); } else { printf("改签成功!\r\n"); ticket2_ok -= num; //手上前往深圳的票 ticket2 += num; //系统中前往深圳的票 ticket3_ok += num; //手上前往肇庆的票 ticket3 -= num; //系统中前往肇庆的票 } } } else if(local1 == flag33) //原地点是肇庆 { if(ticket3_ok < num) //手上的票数少过要改的数量 { printf("改签失败,您手上没有%d张原地点是肇庆的票\r\n", num); } else if(local2 == flag44) //目地点是广州 { if(num > ticket2) //系统现有的票数少过要改的数量 { printf("改签失败,现在系统中仅有%d张前往广州的票\r\n", ticket1); } else { printf("改签成功!\r\n"); ticket3_ok -= num; //手上前往肇庆的票 ticket3 += num; //系统中前往肇庆的票 ticket1_ok += num; //手上前往广州的票 ticket1 -= num; //系统中前往广州的票 } } else if(local2 == flag55) //目地点是深圳 { if(num > ticket3) //系统现有的票数少过要改的数量 { printf("改签失败,现在系统中仅有%d张前往深圳的票\r\n", ticket2); } else { printf("改签成功!\r\n"); ticket3_ok -= num; //手上前往肇庆的票 ticket3 += num; //系统中前肇庆圳的票 ticket2_ok += num; //手上前往深圳的票 ticket2 -= num; //系统中前往深圳的票 } } else if(local2 == flag66) //目地点是肇庆 { printf("不能改签到一样的地方!\r\n"); } } printf("\r\n"); } //标志位清零 temp = temp1 = flag1 = flag11 = flag2 = flag22 = flag3 = flag33 = flag44 = flag55 = flag66 = 0; } }
//信号处理函数
void sig_handler(int arg) { q = 0; pthread_mutex_unlock(&lock); //解锁 }