2
votes

Below Server code which a string into shared memory variable.

Client code display the string available in shared memory.

FULL Code : available in this github link

Server.c

int main(int argc, char *argv[])
{
    /* code to create posix shared memory and posix named semaphore */

    /* critical section start */

    snprintf(shared_msg->content, MAX_MSG_LENGTH, "%s", argv[1]);

    /* critical section end */
}

Client.c

int main(int argc, char *argv[])
{
    /* code to open posix shared memory and posix named semaphore */

    for(i=0; i<20; i++){
        /* critical section start */

       //operation of semaphore
        while(loop < 15){
            printf("Type : %d, content : %s\n", shared_msg->type, shared_msg->content);
            sleep(1);
            loop++;
        }

     /* Critical section end */

    loop = 0;
    printf("loop %d finished\n", i);
      }
}

How to use( wait and post) POSIX semaphore in above code, to achieve following requirement

  • When client starts it has to display shared memory data. once inner while loop finished then only client release the shared memory.
  • If server start and try to write data to shared memory, when client while loop running, semaphore will not write allow the to write untill client releases the semaphore.
  • In single line Server must write when client releases the semaphore

Thanks.

2
Have you tried man sem_overview on the command line? - Drew McGowen
yes. Still i getting some struggle. - sujin
What client server communicates via shared memory? Why not use pipes or sockets? - Scotty Bauer
@ScottyBauer : I can use others also. I just need to understand named semaphore so i chose POSIX shared memory and semaphore. - sujin

2 Answers

1
votes

You want a quasi-mutex or binary semaphore i.e. only one process at a time can access the resource, in this case shared memory. So this looks wrong:

mysem = sem_open(SEMOBJ_PATH, O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, 2);

You want the initial value to be 1. The first process that wants to write shared memory calls sem_wait decrementing the ctr to 0 thereby forcing any other process that calls sem_wait to wait until the value is non-zero. In other words, the semaphore is locked until the process holding it does a sem_post.

Your pseudo-code looks basically correct. sem_wait when you enter a critical section and sem_post when you exit. I think your problem, as I understand it, lies in incorrectly initializing the semaphore on sem_open.

0
votes

From the answer of @Duck i rewrite the code. now i getting the expected output.

I initialize the semaphore with 1 as said in @Duck answer

updated server.c

int main(int argc, char *argv[])
{
  /* code to create posix shared memory and posix named semaphore */

  /* critical section start */
  sem_wait(mysem); //update
  snprintf(shared_msg->content, MAX_MSG_LENGTH, "%s", argv[1]);

  /* critical section end */
}

updated client.c

int main(int argc, char *argv[])
{
/* code to open posix shared memory and posix named semaphore */

for(i=0; i<20; i++){
    /* critical section start */

   //operation of semaphore
    while(loop < 15){
        printf("Type : %d, content : %s\n", shared_msg->type, shared_msg->content);
        sleep(1);
        loop++;
    }
    sem_post(mysem);
 /* Critical section end */

    loop = 0;
    printf("loop %d finished\n", i);
   }
}