简体   繁体   中英

Isn't there a function like sem_wait in System V Semaphore?

I'm making one way chatting program. But in client program, It works very fast, so it show so many text to me. So I'm using System V semaphore,but It can't use sem_wait Should I use POSIX semaphore to use like sem_wait?

I can't write sleep. Because I have to send a large amount of files separately.

Yes, there is... but it happens when you decrement a semaphore under 0. Sys V semaphores are a variant of Dijkstra semaphores in which you can order a set of operations to be made in order on a set of semaphores atomically, so you will wait if any of them blocks on the operations you requested. As you will probably know, a semaphore waits(blocks) when you decrement it's valuee below 0, and only when it goes above again it allows blocked processes to pass. In SysV you order increments or decrements to the individual semaphores (in case you are handling only one, and decrement only by one, the operation will be equivalent to a P() Dijkstra's call, while if you do it by N, the operation will be equal to N calls to P() in sequence (and atomically, as warranted by Unix kernel) the increment operations are equivalent to V() Dijkstra's calls, and are also done in an atomic way. The kernel also maintains the net amount a process has done to a semaphore, in order to undo all the operations done by this process to the semaphores, in case this process dies.

I expect this has answered your question.

The function to operate on a System V IPC semaphore set is semop() . It can do both wait and signal operations in a single call if the semaphore set has multiple members.

The macOS manual says:

 #include <sys/sem.h> int semop(int semid, struct sembuf *sops, size_t nsops);

DESCRIPTION The semop() system call atomically performs the array of operations indicated by sops on the semaphore set indicated by semid . The length of sops is indicated by nsops . Each operation is encoded in a struct sembuf , which is defined as follows:

 struct sembuf { u_short sem_num; /* semaphore # */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */ };

It goes on to detail the behaviours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM