信号量和同步中的分段错误

发布于 2024-10-20 14:33:12 字数 1037 浏览 1 评论 0原文

有时该程序会因分段错误而出错。什么是分段错误?为什么会发生这种情况?我该如何解决它?

我期望输出为:

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 技术交流群。

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

发布评论

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

评论(2

卖梦商人 2024-10-27 14:33:12

我认为您需要将 sem_unlink()sem_open() 调用移至调用 fork() 之前 。您可能正在取消链接您不想取消的事物。

说明 - 您调用 fork(),现在您有两个进程正在运行。假设父级在子级获得任何处理器时间之前完成了 sem_unlink()sem_open() 调用。现在孩子开始运行,并立即取消与父母的信号量的链接!

I think you need to move the sem_unlink() and sem_open() calls to before you call fork(). 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 the sem_unlink() and sem_open() calls before the child gets any processor time. Now the child starts running, and immediately unlinks the parent's semaphores!

流星番茄 2024-10-27 14:33:12

问题是您在子级和父级中都使用了 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 second sem_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 move fork() after the semaphores are created, or change the semaphore permissions. And don't forget to check the result of any call that might fail :)

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