简体   繁体   English

System V Semaphore中没有像sem_wait这样的function吗?

[英]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?所以我正在使用 System V 信号量,但它不能使用 sem_wait 我应该使用 POSIX 信号量来像 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.是的,有...但是当您将信号量减到 0 以下时会发生这种情况。Sys V 信号量是 Dijkstra 信号量的一种变体,您可以在其中以原子方式对一组信号量进行排序操作,因此您如果其中任何一个阻塞您请求的操作,将等待。 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.正如您可能知道的那样,当您将信号量的值减小到 0 以下时,信号量会等待(阻塞),并且只有当它再次高于 0 时,它才允许阻塞的进程通过。 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.在 SysV 中,您对单个信号量下令递增或递减(如果您只处理一个,并且只递减一个,则该操作将等效于 P() Dijkstra 的调用,而如果您按 N 执行此操作,则该操作将等于按顺序对 P() 的 N 次调用(并且原子地,由 Unix 内核保证)增量操作等效于 V() Dijkstra 的调用,并且也以原子方式完成。kernel 还保持净额 a进程已经对信号量进行了操作,以便撤消该进程对信号量所做的所有操作,以防该进程死亡。

I expect this has answered your question.我希望这已经回答了你的问题。

The function to operate on a System V IPC semaphore set is semop() .在 System V IPC 信号量集上运行的 function 是semop() It can do both wait and signal operations in a single call if the semaphore set has multiple members.如果信号量集有多个成员,它可以在单个调用中执行等待和信号操作。

The macOS manual says: macOS 手册说:

 #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 .描述semop()系统调用在semid指示的信号量集上以原子方式执行由sops指示的操作数组。 The length of sops is indicated by nsops . sops的长度由nsops指示。 Each operation is encoded in a struct sembuf , which is defined as follows:每个操作都编码在struct sembuf中,定义如下:

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

It goes on to detail the behaviours.它继续详细说明行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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