为什么 semop() 挂起?
当尝试运行此代码时:首先,它为每个信号量打印“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎只生成了一个子进程(我认为这不是有意的),不过,我相信在主进程返回之前缺少等待,这意味着主进程将比子进程更快结束并让它“挂起”(这可能是问题的一部分,但可能不是漏洞问题,请在之前检查循环)。
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).