为什么 semop() 挂起?

发布于 2024-10-09 07:47:20 字数 1493 浏览 5 评论 0原文

当尝试运行此代码时:首先,它为每个信号量打印“Process some_id BEFORE Enter”(2次)。然后它就挂了。什么是不正确的?

# include <sys/ipc.h>
# include <sys/sem.h>
# include <unistd.h>
# include <errno.h>
# include <stdio.h>
# include <stdlib.h>
# include <iostream>

int seminit()
{
    key_t key = ftok("/bin", 1);
    int semid = semget (key, 1, IPC_CREAT | IPC_EXCL | 600);
    if(-1 == semid)
        if(EEXIST == errno)
            semid = semget(key, 1, 0);
    return semid;
}

void uninit(int semid)
{
        semctl(semid, 0, IPC_RMID);
}

void semlock(int semid)
{
    struct sembuf p_buf;
    p_buf.sem_num = 0;
    p_buf.sem_op = -1;
    p_buf.sem_flg = SEM_UNDO;
    if(semop(semid, &p_buf, 1) == -1)
        printf("semlock failed: ERRNO: %d\n", errno);
}
void semunlock(int semid)
{
    struct sembuf v_buf;
    v_buf.sem_num = 0;
    v_buf.sem_op = 1;
    v_buf.sem_flg = SEM_UNDO;
    if(semop(semid, &v_buf, 1) == -1)
        printf("semunlock failed: ERRNO: %d\n", errno);
}
void some_function()
{
    int semid = seminit();
    pid_t pid = getpid();
    printf("Process %d BEFORE enter\n", pid);
    semlock(semid);
    printf("Process %d IN Critical section\n", pid);
    sleep(10);
    semunlock(semid);
    printf("Process %d AFTER leave\n", pid);
    uninit(semid);
}

int main(int argc, char** argv) 
{
    for(int i = 0; i < 2; ++i)
        if(0 == fork())
            some_function();
    return (EXIT_SUCCESS);
}

When trying run this code: At first it prints "Process some_id BEFORE enter" for each semaphor (2 times). Then it hangs. What is incorrect?

# include <sys/ipc.h>
# include <sys/sem.h>
# include <unistd.h>
# include <errno.h>
# include <stdio.h>
# include <stdlib.h>
# include <iostream>

int seminit()
{
    key_t key = ftok("/bin", 1);
    int semid = semget (key, 1, IPC_CREAT | IPC_EXCL | 600);
    if(-1 == semid)
        if(EEXIST == errno)
            semid = semget(key, 1, 0);
    return semid;
}

void uninit(int semid)
{
        semctl(semid, 0, IPC_RMID);
}

void semlock(int semid)
{
    struct sembuf p_buf;
    p_buf.sem_num = 0;
    p_buf.sem_op = -1;
    p_buf.sem_flg = SEM_UNDO;
    if(semop(semid, &p_buf, 1) == -1)
        printf("semlock failed: ERRNO: %d\n", errno);
}
void semunlock(int semid)
{
    struct sembuf v_buf;
    v_buf.sem_num = 0;
    v_buf.sem_op = 1;
    v_buf.sem_flg = SEM_UNDO;
    if(semop(semid, &v_buf, 1) == -1)
        printf("semunlock failed: ERRNO: %d\n", errno);
}
void some_function()
{
    int semid = seminit();
    pid_t pid = getpid();
    printf("Process %d BEFORE enter\n", pid);
    semlock(semid);
    printf("Process %d IN Critical section\n", pid);
    sleep(10);
    semunlock(semid);
    printf("Process %d AFTER leave\n", pid);
    uninit(semid);
}

int main(int argc, char** argv) 
{
    for(int i = 0; i < 2; ++i)
        if(0 == fork())
            some_function();
    return (EXIT_SUCCESS);
}

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

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

发布评论

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

评论(1

未蓝澄海的烟 2024-10-16 07:47:20

似乎只生成了一个子进程(我认为这不是有意的),不过,我相信在主进程返回之前缺少等待,这意味着主进程将比子进程更快结束并让它“挂起”(这可能是问题的一部分,但可能不是漏洞问题,请在之前检查循环)。

Seems that only a child is generated (I think that is not intended), still, I believe there is a missing wait before the return of the main process, which means that the main process will end faster than the child process and let it "hanged" (this can be part of the issue but is not maybe the hole issue, check that for cycle before).

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