2013年操作系统第2题:
编码实现:
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
sem_t road;
sem_t south;
sem_t north;
int southcount=0,northcount=0;
pthread_t tid1;
void *Southwalker()
{
do{
sleep(2);//这个sleep函数的目的是为了给另外一个线程函数反应的时间以便结束阻塞状态
sem_wait(&south);
if(southcount==0)sem_wait(&road);
southcount++;
sem_post(&south);
printf("\n我向南走\n");
sem_wait(&south);
southcount--;
if(southcount==0)sem_post(&road);
sem_post(&south);
}while(1);
}
void *Northwalker()
{
do{
sleep(2); //这个sleep函数的目的是为了给另外一个线程函数反应的时间以便结束阻塞状态
sem_wait(&north);
if(northcount==0)sem_wait(&road);
northcount++;
sem_post(&north);
printf("\n我向北走\n");
sleep(2);
sem_wait(&north);
northcount--;
if(northcount==0)sem_post(&road);
sem_post(&north);
}while(1);
}
int main()
{
sem_init(&road,0,1);
sem_init(&south,0,1);
sem_init(&north,0,1);
pthread_create(&tid1,NULL,Northwalker,NULL);
pthread_create(&tid1,NULL,Southwalker,NULL);
pthread_join(tid1,NULL);
}
运行截图:
2015年操作系统第2题:
编码实现:
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
sem_t airport;//对机场跑道的使用权限
sem_t fly;//起飞飞机修改共享变量flycount的权限
sem_t land;//着陆飞机修改共享变量landcount的权限
int flycount=0,landcount=0;
pthread_t tid1;
void *Fly()
{
do{
sleep(2);//这个sleep函数的目的是为了给另外一个线程函数反应的时间以便结束阻塞状态
sem_wait(&fly);
if(flycount==0)sem_wait(&airport);
flycount++;
sem_post(&fly);
printf("\n我起飞了!\n");
sem_wait(&fly);
flycount--;
if(flycount==0)sem_post(&airport);
sem_post(&fly);
}while(1);
}
void *Land()
{
do{
sleep(2);//这个sleep函数的目的是为了给另外一个线程函数反应的时间以便结束阻塞状态
sem_wait(&land);
if(landcount==0)sem_wait(&airport);
landcount++;
sem_post(&land);
printf("\n我着陆了!\n");
sem_wait(&land);
landcount--;
if(landcount==0)sem_post(&airport);
sem_post(&land);
}while(1);
}
int main()
{
sem_init(&airport,0,1);
sem_init(&fly,0,1);
sem_init(&land,0,1);
pthread_create(&tid1,NULL,Fly,NULL);
pthread_create(&tid1,NULL,Land,NULL);
pthread_join(tid1,NULL);
}
运行效果:
2016年操作系统第10题:
编码实现:
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#define ElemType int
sem_t mutex1,mutex2,empty_F1,full_F1,empty_F2,full_F2;
int in1=0,out1=0,in2=0,out2=0;
ElemType buffer_F1[10]={0};
ElemType buffer_F2[10]={0};
pthread_t tid1;
void *producerA() //甲车间
{
do{
int A=20;
printf("\n将20定为零件A\n");
sem_wait(&empty_F1);
sem_wait(&mutex1);
buffer_F1[in1]=A;
in1=(in1+1)%10;
sem_post(&mutex1);
sem_post(&full_F1);
sleep(2);
}while(1);
}
void *producerB()//乙车间
{
do{
int B=30;
printf("\n将30定为零件B\n");
sem_wait(&empty_F2);
sem_wait(&mutex2);
buffer_F2[in2]=B;
in2=(in2+1)%10;
sem_post(&mutex2);
sem_post(&full_F2);
sleep(2);
}while(1);
}
void *Equip()//装配车间
{
do{
int nextc1;
sem_wait(&full_F1);
sem_wait(&mutex1);
nextc1=buffer_F1[out1];
out1=(out1+1)%10;
sem_post(&mutex1);
sem_post(&empty_F1);
int nextc2;
sem_wait(&full_F2);
sem_wait(&mutex2);
nextc2=buffer_F2[out2];
out2=(out2+1)%10;
sem_post(&mutex2);
sem_post(&empty_F2);
printf("\nnextc1+nextc2=%d\n",(nextc1+nextc2));
sleep(2);
}while(1);
}
int main()
{
sem_init(&mutex1,0,1);
sem_init(&mutex2,0,1);
sem_init(&empty_F1,0,10);
sem_init(&full_F1,0,0);
sem_init(&empty_F2,0,10);
sem_init(&full_F1,0,0);
pthread_create(&tid1,NULL,producerA,NULL);
pthread_create(&tid1,NULL,producerB,NULL);
pthread_create(&tid1,NULL,Equip,NULL);
pthread_join(tid1,NULL);
}
运行截图:
2017年操作系统第10题:
编码实现:
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
#define ElemType char
int in=0,out=0;
sem_t mutex,empty,Afull,Ofull;
ElemType buffer[20];
pthread_t tid1;
void *Father()
{
do{
printf("\nprepare an apple!\n");
sem_wait(&empty);
sem_wait(&mutex);
buffer[in]='A';
in=(in+1)%20;
sem_post(&mutex);
sem_post(&Afull);
sleep(2);
}while(1);
}
void *Mother()
{
do{
printf("\nprepare an orange!\n");
sem_wait(&empty);
sem_wait(&mutex);
buffer[in]='O';
in=(in+1)%20;
sem_post(&mutex);
sem_post(&Ofull);
sleep(2);
}while(1);
}
void *Son()
{
do{
ElemType nextc;
sem_wait(&Afull);
sem_wait(&mutex);
nextc=buffer[out];
out=(out+1)%20;
printf("\nSon get a %c\n",nextc);
sem_post(&mutex);
sleep(2);
}while(1);
}
void *Daughter()
{
do{
ElemType nextc;
sem_wait(&Ofull);
sem_wait(&mutex);
nextc=buffer[out];
out=(out+1)%20;
printf("\nDaughter get a %c\n",nextc);
sem_post(&mutex);
sleep(2);
}while(1);
}
int main()
{
memset(buffer,'\0',20);
sem_init(&mutex,0,1);
sem_init(&empty,0,20);
sem_init(&Afull,0,0);
sem_init(&Ofull,0,0);
pthread_create(&tid1,NULL,Father,NULL);
pthread_create(&tid1,NULL,Mother,NULL);
pthread_create(&tid1,NULL,Son,NULL);
pthread_create(&tid1,NULL,Daughter,NULL);
pthread_join(tid1,NULL);
}
运行效果:
2018年操作系统第15题:
编码实现:
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
sem_t mutex,umutex,dmutex;
int upcount=0,downcount=0;
pthread_t tid1;
void *Upstairs()
{
do{
sem_wait(&umutex);
if(upcount==0)sem_wait(&mutex);
upcount++;
sem_post(&umutex);
printf("\n我上楼!\n");
sem_wait(&umutex);
upcount--;
if(upcount==0)sem_post(&mutex);
sem_post(&umutex);
sleep(2);
}while(1);
}
void *Downstairs()
{
do{
sem_wait(&dmutex);
if(downcount==0)sem_wait(&mutex);
downcount++;
sem_post(&dmutex);
printf("\n我下楼!\n");
sem_wait(&dmutex);
downcount--;
if(downcount==0)sem_post(&mutex);
sem_post(&dmutex);
sleep(2);
}while(1);
}
int main()
{
sem_init(&mutex,0,1);
sem_init(&umutex,0,1);
sem_init(&dmutex,0,1);
pthread_create(&tid1,NULL,Upstairs,NULL);
pthread_create(&tid1,NULL,Downstairs,NULL);
pthread_join(tid1,NULL);
}
运行效果: