semget, semop and semctl函数小记

简介:

SEMGET FUNCTION

    The prototype of semget is

        int semget(key_t key, intnsems,int semflg);

    We use semget() toget a semaphore set ID. After we call semget() successfully, it returns a non-negative integer, often calledsem_id, which other semaphore functions use to access the semaphore set. On error, it returns -1.

    Note that the phrase is "semaphore set". That means it is an array of semaphores, and the 2nd argumentnsems decides how many elements the array owns, though almost we just need 1.

    I was a little confused by the 1st argument key at first, because I don't know how it works.

    Then I looked up the linux manual page.

    Here comes the partial description of semget():

        The semget() system call returns the semaphore set identifier associated with the argument key. A new set of nsems semaphores is created if key has the value IPC_PRIVATE or if no existing semaphore set is associated with key and IPC_CREAT is specified in semflg.

    And then I know the parameter key is an integer associated to the special semaphore. When the system plans to create a new semaphore, it checks the key first to see if another one connected to the key has existed, if so, the system fails with errno.

    On another occasion, if we set the key to IPC_PRIVATE, then we can obtain a private semaphore, which means the semaphore can only be accessed by one particular process or thread, so we know it is useless. Always we should provide a unique and non-zero integer for key.


SEMOPFUNCTION

    The prototype of semop is

        int semop(int semid, struct sembuf *sops, unsigned nsops);

    We could easily know that this function is used toperform some operation on semaphores from the name of it.

    But how it actually works?

    As is refered to above, the function semget() returns an ID pointing to an array of semaphores, and the argument nsems in semget() indicates how many they are. So when we decide to make some change on semaphores, we should know where the semaphores are first, so we get thesemid pointing to the array. And then we should know how many operations will be performed, that is why the 3rd parameternsops exists.

    Finally,how we make changes on the semaphores? We usestruct sembuf.

    The structure comes below:

        unsigned short sem_num; /*semaphore number */

        short sem_op; /* semaphore operation */

        short sem_flg; /* operation flags */

    sem_num indicates which semaphore in the set would be operate, usually it is set to 0.

    sem_op holds the value by which the semaphore would change. Generally, +1 is used for P operation and -1 for V to say that the critical section is available now.

    It is good to set thesem_flg to SEM_UNDO, so that when the process terminates, the operation could be undone automatically.

    On the whole, we use semid to point to the destination set, sops to indicates where the operation array is, then the system performs nsops operation to make changes on the semaphores in the sops array order.

    For instance, we call semop(sem_id, buf, 3). The 2nd argument buf is a pointer pointing to an array of struct sembuf owning at least 3 elements. The system catch the 3rd argument 3 to know 3 operations will be performed with the information given by the2nd argument buf. We said that these operations will be performed in the sops array order, so the system would just make changes on semaphores whose ID is among buf[0].sem_num, buf[1].sem_num and buf[2].sem_num, and of course, each operation will be done according to the buf[i].sem_op, in which i is among 0, 1, 2.

    Note that the operation is done atomically and automatically.


SEMCTL FUNCTION

    The prototype of the semctl is:

        int semctl(int semid, intsemnum, int cmd, ...);

    This functionperforms an direct operation on the semnum-th semaphore in the semaphore set whose ID is semid with the argument cmd.

    There are many kinds of cmd, which affects the 4th argument's existence.Usually we use two command, SETVAL and IPC_RMID.

    For command SETVAL we need a 4th argument to indicate the value to set to the semnum-th semaphore in the set owning the ID semid.

    The4th argument structure follows:

         union semun {

               int val; /* Value for SETVAL */

               struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */

               unsigned short *array; /* Array for GETALL, SETALL */

               struct seminfo *__buf; /* Buffer for IPC_INFO

                                        (Linux-specific)*/

          };

     And for command IPC_RMID, we aims at removing the semaphore set whose ID is semid, and awakening the processes blocked for the set. Under this circumstance, the argument semnum is ignored.


JasonLee

2009-11-14p.m


————————————————————————————————————————

[博客整理补充] 这篇文章是在校期间用ubuntu时,中文输入法不好使,就用英文写了。现在回头看,发现还是读过过去的。偷笑

时光飞逝啊,一晃都两年多了。 2012-07-30晚。

目录
相关文章
|
4月前
|
程序员 编译器 C++
函数介绍
函数介绍
23 1
|
1月前
|
Java 测试技术 Python
为什么要用函数
在编程中,函数是一种重要的抽象工具,它使我们能够组织和复用代码,提高代码的可读性、可维护性和效率。函数允许我们将一段代码块封装起来,给它一个名字,并通过参数和返回值来与外部世界交互。下面,我们将深入探讨为什么要使用函数,并附上相应的代码示例。
15 1
|
3月前
函数(三)
函数(三)
20 0
|
3月前
|
数据库
什么是纯函数
纯函数是指在相同的输入下,总是返回相同的输出,且没有副作用的函数。具体来说,纯函数不会改变任何传入的参数,也不会在函数外部改变全局变量、文件系统、数据库等状态,它只是接收输入并返回输出,不会产生任何可观察的副作用。
18 0
|
4月前
|
存储 编译器
函数(下)
函数(下)
60 0
|
9月前
|
编译器
函函函函函函函函函函函数——two
函函函函函函函函函函函数——two
69 0
函函函函函函函函函函函数——two
|
10月前
基本初等函数 对数函数
基本初等函数 对数函数
54 0
|
SQL 分布式计算 关系型数据库
not_in函数致错
Not in 函数-致错 我们开发当中有些业务逻辑会用到not in()这个函数, 岗位角度:不管是后端开发还是大数据开发还是数据分析师… 技术角度:不管是Mysql、Hive、Maxcompute…
121 0
|
存储 编译器
函数
函数
66 0
|
C语言
可变长参数函数
可变长参数函数
100 0