操作系统之进程同步问题

进程同步的两种形式的制约关系
间接相互制约关系(进程互斥) 进程-进程
直接相互制约关系(进程同步) 进程-资源-进程
此处的资源一般指(临界资源:在一段时间内只允许一个进程访问的资源。临界资源的访问要求互斥的访问。)

讲到进程同步就不得不提生产者-消费者问题了,
int in=0,out=0;-----两个指针,输入输出
item buffer[n];
semaphore mutex=1,empty=n,full=0;
利用互斥信号量mutex,利用信号量empty和full分别表示缓冲池空和缓冲池满的数量
void producer(){ ----生产者
do{
producer an item nextp;

wait(empty); -----测量空间
wait(mutex); -----申请权限
buffer[in]=nextp;
in=(in+1)%n;
signal(mutex);
signal(full);
}while(TURE);
}

void consumer(){
do{
wait(full);
wait(mutex);
nextc=buffer[out];
out=(out+1)%n;
signal(mutex);
signal(empty);
connsumer the item in nextc;

}while(TURE);
}

void main(){
cobegin
producer();consumer();
coend
}

猜你喜欢

转载自blog.csdn.net/qq_43277404/article/details/84536998