当两个进程试图访问与信号= 0的一个关键部分,会发生什么? [英] What happens when two processes are trying to access a critical section with semaphore = 0?

查看:132
本文介绍了当两个进程试图访问与信号= 0的一个关键部分,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code我这样做以下初始化:

In my code I do the following initialization :

struct PipeShm myPipe = { .init = 0 , .flag = FALSE , .mutex = NULL , .ptr1 = NULL , .ptr2 = NULL ,
        .status1 = -10 , .status2 = -10 , .semaphoreFlag = FALSE };

int initPipe()
{
    if (!myPipe.init)
    {
        myPipe.mutex = mmap (NULL, sizeof *myPipe.mutex, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0);

        if (!sem_init (myPipe.mutex, 1, 0))  // semaphore is initialized to 0
        {
            myPipe.init = TRUE;
        }
        else
            perror ("initPipe");
    }
    return 1;   // always successful
}

我可以有可以从被调用的main()多进程(注意在)。

感谢

推荐答案

AFAICS你的错误是在你的控制变量。只有你的互斥变量在进程间共享的,不是你的的init 标志变量。这些都是写时复制,所以你不会看到在不同的进程的变化。

AFAICS your error is in your control variables. Only your mutex variable is shared between the processes, not your init or flag variables. These are copy on write, so you wouldn't see the changes in a different process.

您不得不收拾你所有的控制变量所创建的段内。创建一个相应的结构键入您需要的所有领域。

You'd have to pack all of your control variables inside the segment that you create. Create an appropriate struct type for all the fields that you need.

BTW,调用一个信号互斥真是个糟糕的主意。互斥有一个语义是从信号完全不同。 (或者,如果你真的使用它作为一个互斥体,我没有检查,使用 pthread_mutex_t pshared的在初始化。)

BTW, calling a semaphore mutex is really a bad idea. A mutex has a semantic that is quite different from a semaphore. (Or if you really use it as a mutex, I didn't check, use pthread_mutex_t with pshared in the initializer.)

你的编辑后,编辑:否它不会像这样工作。你真的要整结构将在共享段。所以,你的结构PipeShm 必须包含一个 sem_t SEM ,而不是 sem_t *互斥。然后,你会做这样的事情。

Edit after your edit: No it wouldn't work like this. You really have to place the whole struct in the shared segment. So your struct PipeShm must contain a sem_t sem and not a sem_t* mutex. Then you'd do something like

struct PipeShm * myPipe = 0;

int initPipe()
{
    if (!myPipe->init)
    {
        myPipe = mmap (NULL, sizeof *myPipe, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0);

        if (!sem_init (myPipe->sem, 1, 0))  // semaphore is initialized to 0
        {
            myPipe->init = true;
        }
        else
            perror ("initPipe");
    }
    return 1;   // always successful
}

其他的事情你应该知道的:

Other things you should be aware of:


  • sem_t 接口,可以通过任何IO或其他信号的中断。你总是要检查
    这些功能,特别是重新启动功能的返回
    如果它收到 EINTR

  • Mondern C有一个布尔值。这样你可以很容易地通过包括使用
    < stdbool.h> 布尔的名称虚假真正

  • The sem_t interfaces can be interrupted by any kind of IO or other signals. You always have to check the return of these functions and in particular restart the function if it received EINTR.
  • Mondern C has a Boolean. This you can easily use by including <stdbool.h> through names of bool, false and true.

这篇关于当两个进程试图访问与信号= 0的一个关键部分,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆