火车售票


分别用互斥锁、条件变量、信号灯;用到线程

火车售票-互斥锁

#include<stdio.h>
#include<pthread.h>pthread_mutex_t mutex;
int ticketCount = 20;void *saleWindow1(void *args)
{while(1){pthread_mutex_lock(&mutex);if(ticketCount != 0){printf("Salewindow1 start sale tickets! The number of tickets is %d\n", ticketCount);sleep(2);   // 2秒卖一张票--ticketCount;printf("Salewindow1 sale tickets finish! The number of tickets is %d\n", ticketCount);}else{printf("Ticket is over!\n");pthread_mutex_unlock(&mutex);pthread_exit(NULL);}pthread_mutex_unlock(&mutex);sleep(1);}
}void *saleWindow2(void *args)
{while(1){pthread_mutex_lock(&mutex);if(ticketCount != 0){printf("Salewindow2 start sale tickets! The number of tickets is %d\n", ticketCount);sleep(2);--ticketCount;printf("Salewindow2 sale tickets finish! The number of ticket is %d\n", ticketCount);}else{printf("Ticket is over!\n");pthread_mutex_unlock(&mutex);pthread_exit(NULL);}pthread_mutex_unlock(&mutex);sleep(1);}
}int main(void)
{pthread_t pthid1 = 0, pthid2 = 0;pthread_mutex_init(&mutex, NULL);pthread_create(&pthid1, NULL, saleWindow1, NULL);pthread_create(&pthid2, NULL, saleWindow2, NULL);pthread_join(pthid1, NULL);pthread_join(pthid2, NULL);pthread_mutex_destroy(&mutex);return 0;
}

火车售票2-条件变量

售票售完后通过条件变量重设车票数量,然后继续卖票

#include<stdio.h>
#include<pthread.h>pthread_mutex_t mutex;
pthread_cond_t cond;
int ticketCount = 3;void *setTicket(void *arg)
{pthread_mutex_lock(&mutex);if(ticketCount != 0)pthread_cond_wait(&cond, &mutex);ticketCount = 3;pthread_mutex_unlock(&mutex);pthread_exit(NULL);
}void *saleWindow1(void *arg)
{while(1){pthread_mutex_lock(&mutex);if(ticketCount != 0){printf("Salewindow1 start sale tickets! The number of tickets is %d\n", ticketCount);sleep(2);   // 2秒卖一张票--ticketCount;printf("Salewindow1 sale tickets finish! The number of tickets is %d\n", ticketCount);if(ticketCount == 0)pthread_cond_signal(&cond);}else{printf("Ticket is over!\n");pthread_mutex_unlock(&mutex);pthread_exit(NULL);}pthread_mutex_unlock(&mutex);sleep(1);}
}void *saleWindow2(void *arg)
{while(1){pthread_mutex_lock(&mutex);if(ticketCount != 0){printf("Salewindow2 start sale tickets! The number of tickets is %d\n", ticketCount);sleep(2);--ticketCount;printf("Salewindow2 sale tickets finish! The number of ticket is %d\n", ticketCount);if(ticketCount == 0)pthread_cond_signal(&cond);}else{printf("Ticket is over!\n");pthread_mutex_unlock(&mutex);pthread_exit(NULL);}pthread_mutex_unlock(&mutex);sleep(1);}
}int main(void)
{pthread_t pthid1 = 0, pthid2 = 0, pthid3 = 0;pthread_mutex_init(&mutex, NULL);pthread_cond_init(&cond, NULL);pthread_create(&pthid3, NULL, setTicket, NULL);pthread_create(&pthid1, NULL, saleWindow1, NULL);pthread_create(&pthid2, NULL, saleWindow2, NULL);pthread_join(pthid1, NULL);pthread_join(pthid2, NULL);pthread_join(pthid3, NULL);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0;
}

火车售票3-信号灯

当信号灯灯值为1时,类似于互斥锁

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>int ticketCount = 10;
sem_t lock;void *saleWindow1(void *arg)
{while(1){sem_wait(&lock);if(ticketCount != 0){printf("SaleWindow1 sale ticket begin! The number of tickets is %d\n", ticketCount);sleep(2);--ticketCount;printf("SaleWindow1 sale ticket finish! The number of tickets is %d\n", ticketCount);}else{sem_post(&lock);printf("Ticket is over!\n");pthread_exit(NULL);}sem_post(&lock);sleep(1);}
}void *saleWindow2(void *arg)
{while(1){sem_wait(&lock);if(ticketCount != 0){printf("SaleWindow2 sale ticket begin! The number of tickets is %d\n", ticketCount);sleep(2);--ticketCount;printf("SaleWindow2 sale ticket finish! The number of ticket is %d\n", ticketCount);}else{sem_post(&lock);printf("Ticket is over!\n");pthread_exit(NULL);}sem_post(&lock);sleep(1);}
}int main(void)
{pthread_t pthid1, pthid2;sem_init(&lock, 0, 1);pthread_create(&pthid1, NULL, saleWindow1, NULL);pthread_create(&pthid2, NULL, saleWindow2, NULL);pthread_join(pthid1, NULL);pthread_join(pthid2, NULL);sem_destroy(&lock);return 0;
}

火车售票-线程3种实现相关推荐

  1. Java售票方式_Java多线程之火车售票系统

    Java多线程之火车售票(Thread和Runnable的区别) java中实现多线程的方式有两种 继承Thread类 实现Runnable接口 继承了Thread类的类,使用对象.start()启动 ...

  2. java多线程同步 多窗口卖票实例_java多线程之火车售票系统模拟实例

    1.前言 为了学习多线程共享与通信,我们模拟一个火车售票系统,假设有10张火车票,三个窗口(也就是三个线程)同时进行售票. 2.非同步代码 package com.tl.skyLine.thread; ...

  3. c语言 feof_C语言 实现简单功能的12306火车售票系统【附源码】

    本文福利在文末! 学习迷茫遇到问题了吗?现在关注微信公众号:C程序编程 免费获取进阶指导,资料工具以及源码 程序设计要求用C语言写一个简单的火车售票系统,主要实现的功能为: 录入班次信息 浏览班次信息 ...

  4. 火车票售票系统C语言大作业,基于C语言实现简单的12306火车售票系统

    程序设计要求用C语言写一个简单的火车售票系统,主要实现的功能为: 录入班次信息 浏览班次信息 按班次号查询 按终点站查询 按余票数量排序保存 售票 退票 更新班次信息 退出系统 所有的班次信息保存在n ...

  5. pthread售票多窗口共同C语言,基于C语言实现简单的12306火车售票系统

    程序设计要求用C语言写一个简单的火车售票系统,主要实现的功能为: 录入班次信息 浏览班次信息 按班次号查询 按终点站查询 按余票数量排序保存 售票 退票 更新班次信息 退出系统 所有的班次信息保存在n ...

  6. 火车售票系统c语言编程,基于C语言实现简单的12306火车售票系统.pdf

    基基于于C语语言言实实现现简简单单的的12306火火车车售售票票系系统统 程序设计要求用C语言写一个简单的火车售票系统,主要实现的功能为: 入班次信息 浏览班次信息 按班次号查询 按终点站查询 按余票 ...

  7. 火车售票排队系统 c语言,【C语言】实现12306火车售票系统!【附源码】

    程序设计要求用C语言写一个简单的火车售票系统,主要实现的功能为: ● 录入班次信息 ● 浏览班次信息 ● 按班次号查询 ● 按终点站查询 ● 按余票数量排序保存 ● 售票 ● 退票 ● 更新班次信息 ...

  8. linux环境下,实现火车售票系统

    1.作业题目 1.实现火车售票系统,火车车次.价格.余票.中途站点.票价等信息以文件的形式存储: 2.服务端可以实现车次增加.价格调整: 3.客户端实现车次查询.余票数目查询及购买操作: 4.客户端与 ...

  9. 实现线程哪种方法更好_实施数据以实现更好的用户体验设计的4种方法

    实现线程哪种方法更好 Gone are the days when design used to rely mainly on the color palettes and the creativit ...

最新文章

  1. 2017年度NLP领域论文TOP10(附链接)
  2. 兄弟会-全栈工程师 第一天笔记
  3. 品牌网络推广方案浅析网站改版时如何更好地规避降权风险?
  4. Caffe官方教程翻译(8):Brewing Logistic Regression then Going Deeper
  5. SpringBoot-AOP切面处理
  6. 飞机票应该如何选择更安全
  7. Tomcat(三):日志
  8. Xmind 常用快捷键列表(官方推荐)
  9. Windows Service开发点滴20130622
  10. SQL Server将DataTable传入存储过程(Table Value Parameter)
  11. 怎么卸载虚拟机中的mysql_虚拟机卸载mysql数据库
  12. 未来教育计算机操作题没分,计算机二级题目要求存为PowerPoint.pptx,我按照要求存了但是没有分,我做的未来教育的题...
  13. 数值计算软件有哪些?一款国产软件非常亮眼。
  14. ESP8266物联网开发入门教程
  15. FastDFS原理及维护
  16. AI技术的苹果iPhone XS Max双卡双待7纳米6.5寸512GB顶配12799元(公号回复“苹果AI”下载PDF资料)
  17. android studio项目总结,Android Studio 使用总结
  18. 台式计算机全网页截图,电脑如何截图整个网页并保存?实现整个网页截图的最简单办法...
  19. 6-6 采用邻接表创建无向图
  20. 1、使用类与接口的知识完成如下要求:(1)定义一个接口CanFly,描述会飞的方法public void fly();(2)分别定义类飞机和鸟,实现CanFly接口。(3)定义一个测试类,测试飞

热门文章

  1. python之arp欺骗
  2. c语言md5函数 linux,【转】MD5校验C语言实现源代码
  3. Hbase——练习4
  4. 生死大PK:软路由是否会威胁到硬路由
  5. 2022山东省安全员C证操作证考试题及答案
  6. Kanzi学习之路(6):属性绑定
  7. Semantic-UI-React (称 stardust) 对比 Antd
  8. 昭和女神异闻录——中山美穗
  9. 面试-android
  10. 怎么搭建自己的内测分发平台?