设置两个信号量:
A. semid_writer:写者之间和第一个读者之间的互斥信号量的键
B. semid_mutex:修改 readcount 的互斥信号量的键
二:P 和 V 两个操作的实现:
这两个操作我们通过系统的函数调用实现,而非直接编写代码!具体细节
如下:
.系统调用:semop()
返回值:0---成功。-1---失败:errno=E2BIG(nsops 大于最大的 ops 数目)
调用原型:int semop(int semid,struct sembuf*sops,unsign ednsops)
参数解析:
A. 第一个参数是关键字值。
B.第三个参数是数组中的操作的个数。
C.第二个参数是指向将要操作的数组的指针。参数 sops 指向由 sembuf
组成的数组。
此数组是在 linux/sem.h 中定义的:
/*semop systemcall takes an array of these*/
structsembuf{
ushort sem_num;/*semaphore index in array*/
short sem_op;/*semaphore operation*/
short sem_flg;/*operation flags*/
}
1--sem_num 将要处理的信号量的个数。
2--sem_flg 操作标志。
3--sem_op 要执行的操作。
A. 如果 sem_op 是负数,那么信号量将减去它的值。这和信号量控制
3
评论0