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晚。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: semget函数是Unix/Linux系统中的一个系统调用函数,用于获取一个信号量集的标识符(也称为信号量集ID)。它通常与其他信号量函数(如semopsemctl)一起使用,用于实现进程间同步和互斥。 semget函数的调用格式为: ``` #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int semget(key_t key, int nsems, int semflg); ``` 其中,key是唯一的键值,用于标识信号量集;nsems指定信号量集中信号量的数量;semflg是标志位,指定semget函数的行为。 semget函数的返回值是一个非负整数,即信号量集的标识符,如果出现错误则返回-1。可以通过该标识符对信号量集进行操作,如设置信号量的值、等待信号量、释放信号量等。 需要注意的是,使用信号量集前需要对其进行初始化,可以使用semctl函数进行初始化操作。同时,在使用完信号量集后,应该使用semctl函数将其删除,避免出现资源泄漏的问题。 ### 回答2: semget函数是在Linux中用于创建或获取一个信号量集的函数。它的函数原型为: int semget(key_t key, int nsems, int semflg); 参数key是唯一标识一个信号量集的键值,可以使用ftok函数生成。参数nsems指定需要创建的信号量数量。参数semflg可以控制信号量集的访问权限和其他操作标志。 当调用semget函数时,它会检查指定键值的信号量集是否已经存在。如果存在,则返回该信号量集的标识符(即信号量集的ID)。如果不存在,则根据指定的键值和信号量数量创建一个新的信号量集,并返回新创建的信号量集的ID。 semget函数返回的ID可以用于后续的信号量操作函数,如semop,用于对信号量集进行加减操作。多个进程可以通过该ID共享对信号量集的访问。在使用完信号量集后,可以使用semctl函数对其进行删除或控制其他属性。 通常,semget函数的返回值为正数表示成功,返回值为-1表示失败。失败的原因可能是给定的键值无效、对信号量集的数量进行了非法的操作等。 总的来说,semget函数是用于创建或获取信号量集的函数。它通过键值来标识一个信号量集,并返回一个标识符用于后续对其进行操作。通过信号量集,可以实现进程间的同步与互斥,实现进程间共享资源的控制。 ### 回答3: semget函数是Linux系统中用于创建或获取一个信号量集的函数。该函数的主要功能是根据给定的key值和标志位创建一个新的信号量集,或者根据给定的key值和标志位获取已存在的信号量集的标识符。 函数原型为: int semget(key_t key, int nsems, int semflg); 其中,key是一个键值,用来唯一标识一个信号量集,nsems是信号量集中信号量的数量,semflg是标志位,在创建新的信号量集时指定该信号量集的权限以及其他的一些标志。 semget函数的返回值是一个整型的信号量集标识符,用于后续对该信号量集进行操作。 当调用semget函数时,会根据给定的key值以及标志位进行以下操作: 1. 如果给定的key对应的信号量集已存在,则semget函数会返回该信号量集的标识符。 2. 如果给定的key对应的信号量集不存在,且同时指定了IPC_CREAT标志,则semget函数会创建一个新的信号量集,并返回其标识符。 3. 如果给定的key对应的信号量集不存在,并且没有指定IPC_CREAT标志,则semget函数会返回一个错误。 通常,semget函数与其他的信号量操作函数(如semopsemctl等)一起使用,用于对信号量集进行锁定、释放等操作。 总之,semget函数是Linux系统中用于创建或获取信号量集的函数,通过给定的key值和标志位,可以创建新的信号量集或获取已存在的信号量集的标识符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值