- 论坛徽章:
- 1
|
请教:一个关于共享内存读阻塞等待的问题
可以使用信号灯,
给你个框架,自己慢慢看吧
- /* created */
- data->;semid = semget((key_t)data->;semkey, data->;nsems,
- sem_qperm|IPC_CREAT|IPC_EXCL);
- /* lock */
- struct sembuf semlock = {0, -1, 0|SEM_UNDO};
- semlock.sem_num = sem_no; // 信号灯集中的信号灯序号, 0based
- if (!waitforlock) {
- semlock.sem_flg |= IPC_NOWAIT;
- }
- if(semop(semid, &semlock, 1) == 0)
- return TRUE;
- else
- return FALSE;
- /* unlock */
- struct sembuf semunlock = {0, 1, 0|SEM_UNDO};
- semunlock.sem_num = sem_no;
- if (!waitforunlock) {
- semunlock.sem_flg |= IPC_NOWAIT;
- }
- if(semop(semid, &semunlock, 1) == 0)
- return TRUE;
- else
- return FALSE;
- /* islock? */
- struct sembuf semchk = {0, 0, 0};
- semchk.sem_num = sem_no; // 信号灯集中的信号灯序号, 0based
- if (!waitforlock) {
- semchk.sem_flg |= IPC_NOWAIT;
- }
- if(semop(semid, &semchk, 1) == 0) {
- return TRUE;
- }
- else
- return FALSE;
复制代码 |
|