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.
man sem_overviewon the command line? - Drew McGowen