What happens when two processes are trying to acce

2020-07-26 11:10发布

问题:

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
}

I can have multiple processes that can be invoked from main() (note the fork) .

Thanks

回答1:

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, 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.)

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:

  • 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.