
linux系统编程模拟火车售票
#include<semaphore.h>
#include<pthread.h>
pthread_mutex_t lock;//定义互斥锁
pthread_cond_t cond;//定义条件变量
sem_t sem;//定义信号量
int t=20;
void *pth_1(void *arg)
{
while(1)
{
sem_wait(&sem);
if(t>0)
{
printf("站点1:售出第%d张票n",t);
t--;
printf("剩余%d张票n",t);
if(t==0)
{
pthread_cond_signal(&cond);
}
sleep(1);
}
else
{
printf("售票点1:票已售完!n");
sem_post(&sem);
pthread_exit(0);
}
sem_post(&sem);//释放信号量
sleep(1);
"signe.c" 102L, 2016C 4,17 4%