为什么 semop(semid,&wait[1],2);在一组三个信号量上减少第 0 个信号量的值而不是第 1 和第 2 个信号量?

发布于 2024-10-18 04:41:39 字数 2672 浏览 5 评论 0原文

我有一个代码正在处理一组 3 个信号量。我有两套 struct sembuf wait[3],signal[3];

我已经初始化了它们中的每一个。 wait 初始化为 -1,signal 初始化为 1,

然后我使用函数 semctl(semid,0,SETALL,2); 将它们的值设置为 2,该函数成功运行。然后我检查它们的值是否已设置,并且它们已设置。

然后我执行 semop(semid,&wait[1],2); 。这应该等待两个信号量并减少它们的值。因此,我预计此时三个信号量的值为 2,1,1,但令我惊讶的是,它使第一个信号量的值减少了两次,我看到的值为 0,2,2。

谁能告诉我为什么会发生这种情况。

这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>

int main(int argc,char *argv[]){

    key_t key1 = 12345;
    int semid;
    unsigned short *semval;

    struct sembuf wait[3],signal[3];
    semval = (unsigned short*) malloc(sizeof(unsigned short) * 3);

    wait[0].sem_num = 0;
    wait[0].sem_op = -1;
    wait[0].sem_flg = SEM_UNDO;

    signal[0].sem_num = 0;
    signal[0].sem_op = 1;
    signal[0].sem_flg = SEM_UNDO;

    wait[1].sem_num = 0;
    wait[1].sem_op = -1;
    wait[1].sem_flg = SEM_UNDO;

    signal[1].sem_num = 0;
    signal[1].sem_op = 1;
    signal[1].sem_flg = SEM_UNDO;

    wait[2].sem_num = 0;
    wait[2].sem_op = -1;
    wait[2].sem_flg = SEM_UNDO;

    signal[2].sem_num = 0;
    signal[2].sem_op = 1;
    signal[2].sem_flg = SEM_UNDO;

    semid = semget(key1,3,IPC_CREAT);
    printf("ALLOCATING THE SEMAPHORES = %s\n",strerror(errno));

    semval[0] = semval[1] = semval[2] = 2;
    semctl(semid,0,SETALL,semval);
    printf("SETTING SEMAPHORE VALUES = %s\n",strerror(errno));

    semctl(semid,0,GETALL,semval);
    printf("Initialized Semaphore values : %d--%d--%d\n",semval[0],semval[1],semval[2]);

    semop(semid,&wait[1],2);
    printf("WAITING ON SEMAPHORES 2 AND 3 = %s\n",strerror(errno));

    semctl(semid,0,GETALL,semval);
    printf("VALUES AFTER WAITING ON SEMAPHORES 2 AND 3 : %d--%d--%d\n",semval[0],semval[1],semval[2]);

    semctl(semid,0,IPC_RMID);
    printf("SEMAPHORE REMOVED = %s\n",strerror(errno));
    return 0;
}

这是输出

anirudh@anirudh-Aspire-5920:~/Desktop/testing$ gcc -g -o sem3 sem3.c
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./sem3
ALLOCATING THE SEMAPHORES = Success
SETTING SEMAPHORE VALUES = Success
Initialized Semaphore values : 2--2--2
WAITING ON SEMAPHORES 2 AND 3 = Success
VALUES AFTER WAITING ON SEMAPHORES 2 AND 3 : 0--2--2
SEMAPHORE REMOVED = Success

终于成功地写了一篇博客。 http://systemsdaemon.blogspot.com/2011/02 /system-v-semaphores-for-babies.html

I have a code where I am working on a set of 3 semaphores. I have a two sets
struct sembuf wait[3],signal[3];

I have initialized each one of them. wait is initialized to -1 and signal is initialized to 1

I then set their values as 2 using the function semctl(semid,0,SETALL,2); which works successfully. I then check if their values are set, and they are set.

Then I do semop(semid,&wait[1],2); . this should wait on both the semaphores and decrease their values. So I expect the values of three semaphores to be 2,1,1 by this time, but to my surprise it decreased the value of 1st semaphore twice and I see the values as 0,2,2.

Can anyone tell me why is this happening.

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>

int main(int argc,char *argv[]){

    key_t key1 = 12345;
    int semid;
    unsigned short *semval;

    struct sembuf wait[3],signal[3];
    semval = (unsigned short*) malloc(sizeof(unsigned short) * 3);

    wait[0].sem_num = 0;
    wait[0].sem_op = -1;
    wait[0].sem_flg = SEM_UNDO;

    signal[0].sem_num = 0;
    signal[0].sem_op = 1;
    signal[0].sem_flg = SEM_UNDO;

    wait[1].sem_num = 0;
    wait[1].sem_op = -1;
    wait[1].sem_flg = SEM_UNDO;

    signal[1].sem_num = 0;
    signal[1].sem_op = 1;
    signal[1].sem_flg = SEM_UNDO;

    wait[2].sem_num = 0;
    wait[2].sem_op = -1;
    wait[2].sem_flg = SEM_UNDO;

    signal[2].sem_num = 0;
    signal[2].sem_op = 1;
    signal[2].sem_flg = SEM_UNDO;

    semid = semget(key1,3,IPC_CREAT);
    printf("ALLOCATING THE SEMAPHORES = %s\n",strerror(errno));

    semval[0] = semval[1] = semval[2] = 2;
    semctl(semid,0,SETALL,semval);
    printf("SETTING SEMAPHORE VALUES = %s\n",strerror(errno));

    semctl(semid,0,GETALL,semval);
    printf("Initialized Semaphore values : %d--%d--%d\n",semval[0],semval[1],semval[2]);

    semop(semid,&wait[1],2);
    printf("WAITING ON SEMAPHORES 2 AND 3 = %s\n",strerror(errno));

    semctl(semid,0,GETALL,semval);
    printf("VALUES AFTER WAITING ON SEMAPHORES 2 AND 3 : %d--%d--%d\n",semval[0],semval[1],semval[2]);

    semctl(semid,0,IPC_RMID);
    printf("SEMAPHORE REMOVED = %s\n",strerror(errno));
    return 0;
}

and here is the output

anirudh@anirudh-Aspire-5920:~/Desktop/testing$ gcc -g -o sem3 sem3.c
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./sem3
ALLOCATING THE SEMAPHORES = Success
SETTING SEMAPHORE VALUES = Success
Initialized Semaphore values : 2--2--2
WAITING ON SEMAPHORES 2 AND 3 = Success
VALUES AFTER WAITING ON SEMAPHORES 2 AND 3 : 0--2--2
SEMAPHORE REMOVED = Success

Finally managed to write a blog on it.
http://systemsdaemon.blogspot.com/2011/02/system-v-semaphores-for-babies.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲墙角沉默 2024-10-25 04:41:39

您在所有参数中使用了 sem_num=0 。

You used sem_num=0 in all of your parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文