C 问题 sys/sem 中锁定信号量

发布于 2024-08-30 23:24:46 字数 1783 浏览 4 评论 0原文

编辑:这种和平的代码非常好(所以将其作为信号量的示例;)。我的程序中的错误在另一个地方 - 由我的朋友发现。

我的功能有问题。有时两个进程进入临界区。经过10个小时的调试,我没有发现问题。我应该瞄准什么?这段代码中是否有可能出现 bud?

    // lock semaphore
    static int P(int sem_id)
    {
        struct sembuf sem_b;
        sem_b.sem_num = 0;
        sem_b.sem_op = -1; /* P() */
        sem_b.sem_flg = 0;
        if (semop(sem_id, &sem_b, 1) == -1) {
              // error
                 return(0);
        }
        return(1);
    }

    // unlock semaphore
    static int V(int sem_id)
    {
        struct sembuf sem_b[1];
        sem_b.sem_num = 0;
        sem_b.sem_op = 1; /* V() */
        sem_b.sem_flg = 0;
        if (semop(sem_id, &sem_b, 1) == -1) {
              // error
            return(0);
        }
        return(1);
    }

    static int set_semval(int sem_id)  {
        // set to 1 (opened semaphore)
        if (semctl(sem_id, 0, SETVAL, 1) == -1) {
              // error
            return(0);
        }
        return(1);
    }

    static int get_val(int sem_id)
    {
        union semun sem_union;
        //sem_union.val = 0; ?
        return semctl(sem_id, 0, GETVAL, sem_union);
    }

动作循环:

// semaphores init
int mutex;
if ((mutex=semget(key+2, 1, 0666))>=0) {
    // semaphore exists
    fprintf(stderr,"semaphore exists for key %d\n", key+2);
}

if ((mutex=semget(key+2, 1, 0666 | IPC_CREAT)) == -1) { 
    exit(EXIT_FAILURE);
}
if (!set_semval(mutex)) {
    exit(EXIT_FAILURE);
}
fork()    // some times with good conditionals

// in some children
while(1) {
        P(mutex);   
        assert(get_val(mutex)==0); // always ok
        action();  // sometimes made by two processes at same time - fault
        V(mutex);
}   

请随意编辑我的问题。

非常感谢

EDIT: This peace of code is pretty fine (so take it as example of semaphores ;). Bug in my program was in another place - found by my friend.

I have problem with my functions. Sometimes two processes enter into critical section. I can't find problem in this after I spent 10 hours by debugging. On what I should aim? Is there any possibility to have bud in this piece of code?

    // lock semaphore
    static int P(int sem_id)
    {
        struct sembuf sem_b;
        sem_b.sem_num = 0;
        sem_b.sem_op = -1; /* P() */
        sem_b.sem_flg = 0;
        if (semop(sem_id, &sem_b, 1) == -1) {
              // error
                 return(0);
        }
        return(1);
    }

    // unlock semaphore
    static int V(int sem_id)
    {
        struct sembuf sem_b[1];
        sem_b.sem_num = 0;
        sem_b.sem_op = 1; /* V() */
        sem_b.sem_flg = 0;
        if (semop(sem_id, &sem_b, 1) == -1) {
              // error
            return(0);
        }
        return(1);
    }

    static int set_semval(int sem_id)  {
        // set to 1 (opened semaphore)
        if (semctl(sem_id, 0, SETVAL, 1) == -1) {
              // error
            return(0);
        }
        return(1);
    }

    static int get_val(int sem_id)
    {
        union semun sem_union;
        //sem_union.val = 0; ?
        return semctl(sem_id, 0, GETVAL, sem_union);
    }

The action loop:

// semaphores init
int mutex;
if ((mutex=semget(key+2, 1, 0666))>=0) {
    // semaphore exists
    fprintf(stderr,"semaphore exists for key %d\n", key+2);
}

if ((mutex=semget(key+2, 1, 0666 | IPC_CREAT)) == -1) { 
    exit(EXIT_FAILURE);
}
if (!set_semval(mutex)) {
    exit(EXIT_FAILURE);
}
fork()    // some times with good conditionals

// in some children
while(1) {
        P(mutex);   
        assert(get_val(mutex)==0); // always ok
        action();  // sometimes made by two processes at same time - fault
        V(mutex);
}   

Pls, feel free to edit my question.

Many thanks

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

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

发布评论

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

评论(2

谈下烟灰 2024-09-06 23:24:46

在你的“动作循环”中,如果你的信号量不存在,你会怎么做?

目前,semget 的第三个参数是 0666 或 PERMISSION_RW 常量。您可能想使用:

shmget(key, 1, PERMISSION_RW | IPC_CREAT);

这样,如果您的信号量不存在,它将创建一个。

in your 'action loop' what do you do if your semaphore does not exist?

currently your 3rd parameter to semget is 0666 or the PERMISSION_RW constant. You may want to use:

shmget(key, 1, PERMISSION_RW | IPC_CREAT);

this way if your semaphore does not exist it will create one.

我不是你的备胎 2024-09-06 23:24:46

所以错误在另一个地方......

So bug was at another place...

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