信号量和同步中的分段错误
有时该程序会因分段错误而出错。什么是分段错误?为什么会发生这种情况?我该如何解决它?
我期望输出为:
I am consumer
I am producer
I am consumer
I am producer
(etc.)
但是,情况并非如此。谁能向我解释一下吗?
#include<stdio.h>
#include<semaphore.h>
#include<sys/stat.h>
#include<fcntl.h>
sem_t* mutex;
sem_t* null;
main()
{
int temp;
int pid;
pid = fork();
sem_unlink("/mutex");
sem_unlink("/null");
null = (sem_t*)sem_open("/null",O_CREAT,S_IWUSR|S_IWGRP|S_IWOTH,0);
mutex =(sem_t*)sem_open("/mutex",O_CREAT,S_IWUSR|S_IWGRP|S_IWOTH,1);
if (pid != 0)
while(1)
{
sem_post(null);
sem_wait(mutex);
printf("\nIam In Producer\n");
scanf("%d",&temp); // just for my verification that where i am during execution
sem_post(mutex);
sem_wait(null);
}
else
while(1)
{
sem_post(null);
sem_wait(mutex);
printf("\nIam In consumer\n");
scanf("%d",&temp); // just for my verification that where i am during execution
sem_post(mutex);
sem_wait(null);
}
}
Sometimes this program errors with a segmentation fault. What is a segmentation fault? Why is it happening? And how do I fix it?
I am expecting the output as:
I am consumer
I am producer
I am consumer
I am producer
(etc.)
However, this is not the case. Can anyone explain this to me?
#include<stdio.h>
#include<semaphore.h>
#include<sys/stat.h>
#include<fcntl.h>
sem_t* mutex;
sem_t* null;
main()
{
int temp;
int pid;
pid = fork();
sem_unlink("/mutex");
sem_unlink("/null");
null = (sem_t*)sem_open("/null",O_CREAT,S_IWUSR|S_IWGRP|S_IWOTH,0);
mutex =(sem_t*)sem_open("/mutex",O_CREAT,S_IWUSR|S_IWGRP|S_IWOTH,1);
if (pid != 0)
while(1)
{
sem_post(null);
sem_wait(mutex);
printf("\nIam In Producer\n");
scanf("%d",&temp); // just for my verification that where i am during execution
sem_post(mutex);
sem_wait(null);
}
else
while(1)
{
sem_post(null);
sem_wait(mutex);
printf("\nIam In consumer\n");
scanf("%d",&temp); // just for my verification that where i am during execution
sem_post(mutex);
sem_wait(null);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要将
sem_unlink()
和sem_open()
调用移至调用fork()
之前 。您可能正在取消链接您不想取消的事物。说明 - 您调用
fork()
,现在您有两个进程正在运行。假设父级在子级获得任何处理器时间之前完成了sem_unlink()
和sem_open()
调用。现在孩子开始运行,并立即取消与父母的信号量的链接!I think you need to move the
sem_unlink()
andsem_open()
calls to before you callfork()
. You're probably unlinking things you don't want to.Explanation - you call
fork()
, now you have two processes running. Let's say the parent gets through thesem_unlink()
andsem_open()
calls before the child gets any processor time. Now the child starts running, and immediately unlinks the parent's semaphores!问题是您在子级和父级中都使用了 sem_open() ,但是第一个调用创建了具有错误权限的信号量,因此第二个 sem_open() 失败,因为访问被信号量拒绝。您不检查调用是否成功,因此信号量将无效,这就是为什么当您尝试使用它时会出现段错误。尝试在创建信号量后移动
fork()
,或更改信号量权限。并且不要忘记检查任何可能失败的调用的结果:)The problem is that you
sem_open()
both in the child and the parent, but the first call creates the semaphore with wrong permissions, so the secondsem_open()
fails because access is denied to the semaphore. You don't check whether the call succeeds, so the semaphore will be invalid, that's why you get the segfault when you try to use it. Try to movefork()
after the semaphores are created, or change the semaphore permissions. And don't forget to check the result of any call that might fail :)