1 餐厅分为等待区、用餐区、厨房。等待区中有n个椅子,顾客来了有位置则坐下,没有就离开。厨房互斥使用,进入厨房后椅子后椅子空出来供顾客坐下。用餐处有m个位置,用完厨房看用餐处是否有空位,如果有空位则顾客从厨房出来并叫清洁员A清扫,没有则坐在厨房等待。顾客到用餐处用餐后,通知清洁员B进行打扫。
#include<stdio.h>
#include<conio.h>
typedef int semaphore;
struct process
{
};
typedef struct
{
int value; //资源个数
struct process *L;
}ss;
void P(int *n)
{
(*n)--; //申请一个资源
printf("apply a resource! n=%d\n",(*n));
if((*n)<0)printf("no resources available,block the process!\n");
}
void V(int *n)
{
(*n)++; //释放一个资源
printf("release a resource! n=%d\n",(*n));
if((*n)<=0)printf("still another process is blocked, wakeup one from the block queue.\n");
}
void Student(int *kitchen,int *seats,int *desks,int *cleaner_A,int *cleaner_B)
{
while(1)
{
printf("Student process\n");
P(seats);
P(kitchen); //等待厨房
printf("enter kitchen...\n");
V(seats); //离开椅子
P(desks); //等待用餐桌
V(cleaner_A); //可以打扫厨房
printf("eating...\n");
V(cleaner_B); //可以打扫餐桌
getch();
}
}
void Cleaner_A(int *cleaner_A,int *kitchen)
{
while(1)
{
printf("Cleaner_A process\n");
printf("having break...\n");
P(cleaner_A); //等待打扫
printf("cleaning...\n");
V(kitchen); //离开厨房
getch();
}
}
void Cleaner_B(int *cleaner_B,int *desks)
{
while(1)
{
printf("Cleaner_B process\n");
printf("having break...\n");
P(cleaner_B); //等待打扫
printf("cleaning...");
V(desks); //离开餐桌
getch();
}
}
int main()
{
int n=4,m=3;
semaphore seats=n,desks=m,kitchen=1,cleaner_A=0,cleaner_B=0;
Student(&kitchen,&seats,&desks,&cleaner_A,&cleaner_B);
Cleaner_A(&cleaner_A,&kitchen);
Cleaner_B(&cleaner_B,&desks);
return 0;
}
2.订票与查票,可多个查票,订票者与查票者不可同时操作且按请求顺序提供服务
#include<stdio.h>
#include<conio.h>
typedef int semaphore;
struct process
{
};
typedef struct
{
int value; //资源个数
struct process *L;
}ss;
void P(int *n)
{
(*n)--; //申请一个资源
printf("apply a resource! n=%d\n",(*n));
if((*n)<0)printf("no resources available,block the process!\n");
}
void V(int *n)
{
(*n)++; //释放一个资源
printf("release a resource! n=%d\n",(*n));
if((*n)<=0)printf("still another process is blocked, wakeup one from the block queue.\n");
}
void Order(int *w,int *rw)
{
while(1)
{
P(w);//在无查询者时可以进入
P(rw);//互斥访问系统
printf("Order ticket...\n");
V(rw); //释放系统访问
V(w); //表示查询者可以访问
getch();
}
}
void Check(int *w,int *rw,int *mutex,int *count)
{
while(1)
{
P(w);
P(mutex);
if(count==0) //当第一个查询者进入系统时
P(rw); //阻止订票者订票
count++;
V(mutex);
V(w);
printf("checking...\n");
P(mutex);
count--;
if(count==0)
V(rw);
V(mutex);
getch();
}
}
int main()
{
int count=0;//用于记录当前的查询者数量
semaphore mutex=1; //用于对count变量的互斥访问
semaphore rw=1; //用于保证查询者和订票者互斥访问系统
semaphore w=1; //用于保证查询者和订票者都不优先
Order(&w,&rw);
Check(&w,&rw,&mutex,&count);
return 0;
}
3.一个果盘可以放两个水果,父亲放苹果,母亲放橘子,女儿取水果。要求只有一个人可以操作果盘,只有果盘里同时有苹果和橘子时,女儿才可以取水果,并且同时把两个水果取走。 这个程序有问题会造成死锁,解决方法是采取同步机制,dad放完苹果后要释放mutex信号量,然后要等到果盘空了才能继续放,mom要等到apple信号量才能放,释放apple,释放mutex,女儿进程没有问题,所以应当再增加一个信号量来解决同步问题
#include<stdio.h>
#include<conio.h>
typedef int semaphore;
struct process
{
};
typedef struct
{
int value; //资源个数
struct process *L;
}ss;
void P(int *n)
{
(*n)--; //申请一个资源
printf("apply a resource! n=%d\n",(*n));
if((*n)<0)printf("no resources available,block the process!\n");
}
void V(int *n)
{
(*n)++; //释放一个资源
printf("release a resource! n=%d\n",(*n));
if((*n)<=0)printf("still another process is blocked, wakeup one from the block queue.\n");
}
void dad(int *empty,int *apple,int *mutex)
{
printf("dad process\n");
while(1)
{
printf("prepare an apple...\n");
P(empty);
P(mutex);
printf("put an apple...\n");
V(apple);
getch();
}
}
void mom(int *empty,int *mutex,int *orange)
{
printf("mom process\n");
while(1)
{
printf("prepare an orange...\n");
P(empty);
P(mutex);
printf("put an orange...\n");
V(orange);
getch();
}
}
void daughter(int *apple,int *orange,int *empty,int *mutex)
{
printf("duaghter process\n");
while(1)
{
P(apple);
P(orange);
P(mutex);
printf("take fruits...\n");
V(mutex);
V(empty);
V(empty);
getch();
}
}
int main()
{
semaphore mutex=1,apple=0,orange=0,empty=2;
dad(&empty,&apple,&mutex);
mom(&empty,&mutex,&orange);
daughter(&apple,&orange,&empty,&mutex);
return 0;
}
4.N个生产者进程和M个消费者进程共享大小为K的缓冲区,规则如下,进程之间互斥访问缓冲区,对每条放入缓冲区的数据,所有消费者必须接受一次,缓冲区满时,生产者必须阻塞,缓冲区空时,消费者必须阻塞。
#include<stdio.h>
#include<conio.h>
#define M 10
#define k 5
typedef int semaphore;
struct process
{
};
typedef struct
{
int value; //资源个数
struct process *L;
}ss;
void P(int *n)
{
(*n)--; //申请一个资源
printf("apply a resource! n=%d\n",(*n));
if((*n)<0)printf("no resources available,block the process!\n");
}
void V(int *n)
{
(*n)++; //释放一个资源
printf("release a resource! n=%d\n",(*n));
if((*n)<=0)printf("still another process is blocked, wakeup one from the block queue.\n");
}
void producer(int *empty,int *mutex,int *full)
{
int i;
while(1)
{
for(i=1;i<M;i++)
P(&empty[i]);
P(mutex);
printf("put data...\n");
V(mutex);
for(i=1;i<=M;i++)
V(&full[i]);
getch();
}
}
void consumer(int *mutex,int *full,int *empty)
{
int i;
while(1)
{
P(&full[i]);
P(mutex);
printf("take out data...\n");
V(mutex);
V(&empty[i]);
getch();
}
}
int main()
{
semaphore mutex=1;
semaphore empty[M]={k};
semaphore full[M]={0};
producer(empty,&mutex,full);
consumer(&mutex,full,empty);
return 0;
}
本文深入探讨了四个经典并发控制场景:餐厅顾客与清洁员的互动、订票与查票的互斥操作、家庭成员间水果放置与取用的协调,以及生产者与消费者对共享缓冲区的管理。通过具体代码实现,揭示了信号量、互斥锁等同步机制在避免死锁、饥饿等问题中的关键作用。
1万+

被折叠的 条评论
为什么被折叠?



