#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<semaphore.h>
#include<pthread.h>
#define
Max 5pthread_t E[Max];//从东方来的汽车。每辆汽车对应一个线程。
pthread_t W[Max];//从西方来的汽车
pthread_t S[Max];//从南方来的汽车pthread_mutex_t s1;//线程互斥信号量,对应C路段
pthread_mutex_t s2; //线程互斥信号量,对应B路段
pthread_mutex_t s3; //线程互斥信号量,对应A路段
pthread_mutex_t s4; //线程互斥信号量,对应D路段
int i;void *s(void *a1) //南边来的汽车的线程执行体
{ //从C进入,从A驶出,靠右行驶。
int serialNum=*((int*)a1);//取当前汽车序号 pthread_mutex_lock(&s1);
printf("S %d enter C\n",serialNum);
sleep(2);//驶入C
pthread_mutex_lock(&s2); printf("S %d enter B\n",serialNum); sleep(2);//从C驶入B pthread_mutex_unlock(&s1); pthread_mutex_lock(&s3); printf("S %d enter A\n",serialNum); sleep(2);//从B驶入A pthread_mutex_unlock(&s2); printf("S %d leave A\n",serialNum); sleep(2);//从A驶出 pthread_mutex_unlock(&s3); printf("!!!S finished one\n");}void *e(void *a2) //东边来的汽车{ //从B进入,从D驶出,靠右行驶。 int serialNum=*((int*)a2); pthread_mutex_lock(&s2); printf("E %d enter B\n",serialNum); sleep(2);//驶入B pthread_mutex_lock(&s3); printf("E %d enter A\n",serialNum); sleep(2);//从B驶入A pthread_mutex_unlock(&s2); pthread_mutex_lock(&s4); printf("E %d enter D\n",serialNum); sleep(2);//从A驶入D pthread_mutex_unlock(&s3); printf("E %d leave D\n",serialNum); sleep(2);//从D驶出 pthread_mutex_unlock(&s4); printf("!!!E finished one \n");}void *w(void *a3) //西边来的汽车{ //从D进入,从C驶出,靠右行驶。 int serialNum=*((int*)a3); pthread_mutex_lock(&s4); printf("W %d enter D\n",serialNum);sleep(2);//驶入D pthread_mutex_lock(&s1); printf("W %d enter C\n",serialNum); sleep(2);//从D驶入C pthread_mutex_unlock(&s4); printf("W %d leave C\n",serialNum); sleep(2);//从C驶出 pthread_mutex_unlock(&s1); printf("!!!W finished one \n");}int main(int argc,char *argv[]){ int c; printf("Please input the integer number,less than 5: ");//车流量 scanf("%d",&c); if( c > Max ) { printf("You enter the wrong number,try again.\n"); exit(1); } int i1 = pthread_mutex_init(&s1,NULL); //线程互斥信号量创建及初始化 if(i1 != 0) { printf("Create error."); exit(1); } int i2 = pthread_mutex_init(&s2,NULL); if(i2 != 0) { printf("Create error."); exit(1); } int i3 = pthread_mutex_init(&s3,NULL); if(i3 != 0) { printf("Create error."); exit(1); } int i4 = pthread_mutex_init(&s4,NULL); if(i4 != 0) { printf("Create error."); exit(1); } for(i = 1;i <= c; i++) { int* iaddr=&i;//汽车序号 pthread_create(&W[i-1],NULL,(void *)w,(void *)iaddr);//创建线程 pthread_create(&E[i-1],NULL,(void *)e,(void *)iaddr); pthread_create(&S[i-1],NULL,(void *)s,(void *)iaddr); sleep(5); } for(i = 0; i < c; i++) { pthread_join(W[i],NULL);//当前线程等待W[i]线程终止 pthread_join(E[i],NULL); pthread_join(S[i],NULL); } exit(0); //main进程结束}
#include<stdlib.h>
#include<unistd.h>
#include<semaphore.h>
#include<pthread.h>
#define
Max 5pthread_t E[Max];//从东方来的汽车。每辆汽车对应一个线程。
pthread_t W[Max];//从西方来的汽车
pthread_t S[Max];//从南方来的汽车pthread_mutex_t s1;//线程互斥信号量,对应C路段
pthread_mutex_t s2; //线程互斥信号量,对应B路段
pthread_mutex_t s3; //线程互斥信号量,对应A路段
pthread_mutex_t s4; //线程互斥信号量,对应D路段
int i;void *s(void *a1) //南边来的汽车的线程执行体
{ //从C进入,从A驶出,靠右行驶。
int serialNum=*((int*)a1);//取当前汽车序号 pthread_mutex_lock(&s1);
printf("S %d enter C\n",serialNum);
sleep(2);//驶入C
pthread_mutex_lock(&s2); printf("S %d enter B\n",serialNum); sleep(2);//从C驶入B pthread_mutex_unlock(&s1); pthread_mutex_lock(&s3); printf("S %d enter A\n",serialNum); sleep(2);//从B驶入A pthread_mutex_unlock(&s2); printf("S %d leave A\n",serialNum); sleep(2);//从A驶出 pthread_mutex_unlock(&s3); printf("!!!S finished one\n");}void *e(void *a2) //东边来的汽车{ //从B进入,从D驶出,靠右行驶。 int serialNum=*((int*)a2); pthread_mutex_lock(&s2); printf("E %d enter B\n",serialNum); sleep(2);//驶入B pthread_mutex_lock(&s3); printf("E %d enter A\n",serialNum); sleep(2);//从B驶入A pthread_mutex_unlock(&s2); pthread_mutex_lock(&s4); printf("E %d enter D\n",serialNum); sleep(2);//从A驶入D pthread_mutex_unlock(&s3); printf("E %d leave D\n",serialNum); sleep(2);//从D驶出 pthread_mutex_unlock(&s4); printf("!!!E finished one \n");}void *w(void *a3) //西边来的汽车{ //从D进入,从C驶出,靠右行驶。 int serialNum=*((int*)a3); pthread_mutex_lock(&s4); printf("W %d enter D\n",serialNum);sleep(2);//驶入D pthread_mutex_lock(&s1); printf("W %d enter C\n",serialNum); sleep(2);//从D驶入C pthread_mutex_unlock(&s4); printf("W %d leave C\n",serialNum); sleep(2);//从C驶出 pthread_mutex_unlock(&s1); printf("!!!W finished one \n");}int main(int argc,char *argv[]){ int c; printf("Please input the integer number,less than 5: ");//车流量 scanf("%d",&c); if( c > Max ) { printf("You enter the wrong number,try again.\n"); exit(1); } int i1 = pthread_mutex_init(&s1,NULL); //线程互斥信号量创建及初始化 if(i1 != 0) { printf("Create error."); exit(1); } int i2 = pthread_mutex_init(&s2,NULL); if(i2 != 0) { printf("Create error."); exit(1); } int i3 = pthread_mutex_init(&s3,NULL); if(i3 != 0) { printf("Create error."); exit(1); } int i4 = pthread_mutex_init(&s4,NULL); if(i4 != 0) { printf("Create error."); exit(1); } for(i = 1;i <= c; i++) { int* iaddr=&i;//汽车序号 pthread_create(&W[i-1],NULL,(void *)w,(void *)iaddr);//创建线程 pthread_create(&E[i-1],NULL,(void *)e,(void *)iaddr); pthread_create(&S[i-1],NULL,(void *)s,(void *)iaddr); sleep(5); } for(i = 0; i < c; i++) { pthread_join(W[i],NULL);//当前线程等待W[i]线程终止 pthread_join(E[i],NULL); pthread_join(S[i],NULL); } exit(0); //main进程结束}