Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add a common semaphore, to shared1.cpp and shared2.cpp to protect the shared mem

ID: 3839943 • Letter: A

Question

Add a common semaphore, to shared1.cpp and shared2.cpp to protect the shared memory so that when a process accesses the shared area, the other is excluded from doing so. You may use the POSIX semaphore

  //shared1.cpp  /*  After the headers the shared memory segment   (the size of our shared memory structure) is created with a call to shmget,   with the IPC_CREAT bit specified. It reads data from the shared memory. */    #include <unistd.h>  #include <stdlib.h>  #include <stdio.h>  #include <string.h>    #include <sys/types.h>  #include <sys/ipc.h>  #include <sys/shm.h>    #define TEXT_SZ 2048    struct shared_use_st {      int written_by_you;      char some_text[TEXT_SZ];  };    int main()  {      int running = 1;      void *shared_memory = (void *)0;      struct shared_use_st *shared_stuff;      int shmid;        srand((unsigned int)getpid());            shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);        if (shmid == -1) {          fprintf(stderr, "shmget failed ");          exit(EXIT_FAILURE);      }    /* We now make the shared memory accessible to the program. */        shared_memory = shmat(shmid, (void *)0, 0);      if (shared_memory == (void *)-1) {          fprintf(stderr, "shmat failed ");          exit(EXIT_FAILURE);      }        printf("Memory attached at %X ", (long)shared_memory);    /* The next portion of the program assigns the shared_memory segment to shared_stuff,   which then prints out any text in written_by_you. The loop continues until end is found   in written_by_you. The call to sleep forces the consumer to sit in its critical section,   which makes the producer wait. */        shared_stuff = (struct shared_use_st *)shared_memory;      shared_stuff->written_by_you = 0;      while(running) {          if (shared_stuff->written_by_you) {              printf("You wrote: %s", shared_stuff->some_text);              sleep( rand() % 4 ); /* make the other process wait for us ! */              shared_stuff->written_by_you = 0;              if (strncmp(shared_stuff->some_text, "end", 3) == 0) {                  running = 0;              }          }      }    /* Lastly, the shared memory is detached and then deleted. */        if (shmdt(shared_memory) == -1) {          fprintf(stderr, "shmdt failed ");          exit(EXIT_FAILURE);      }        if (shmctl(shmid, IPC_RMID, 0) == -1) {          fprintf(stderr, "shmctl(IPC_RMID) failed ");          exit(EXIT_FAILURE);      }        exit(EXIT_SUCCESS);  }  

Explanation / Answer

Hi,

Please find the edited code below:-

The code canbe seen in bold:-

For shared1.cpp

It can be found below:-

===============================================================================


/* After the headers the shared memory segment
(the size of our shared memory structure) is created with a call to shmget,
with the IPC_CREAT bit specified. It reads data from the shared memory. */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<stdio.h> // using semaphore
#include<pthread.h>
#include<semaphore.h>
#define TEXT_SZ 2048

struct shared_use_st {
int written_by_you;
char some_text[TEXT_SZ];
};
sem_t full,empty;/*declaring the semaphore variable.
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
int shmid;
sem_init(&empty,0,1);
sem_init(&full,0,0);

srand((unsigned int)getpid());

shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);

if (shmid == -1) {
fprintf(stderr, "shmget failed ");
exit(EXIT_FAILURE);
}
  
/* We now make the shared memory accessible to the program. */

shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed ");
exit(EXIT_FAILURE);
}

printf("Memory attached at %X ", (long)shared_memory);

/* The next portion of the program assigns the shared_memory segment to shared_stuff,
which then prints out any text in written_by_you. The loop continues until end is found
in written_by_you. The call to sleep forces the consumer to sit in its critical section,
which makes the producer wait. */

shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff->written_by_you = 0;
while(running)
{
sem_wait(&full);
if (shared_stuff->written_by_you)
{
printf("You wrote: %s", shared_stuff->some_text);
/* sleep( rand() % 4 ); /* make the other process wait for us ! */
shared_stuff->written_by_you = 0;
if (strncmp(shared_stuff->some_text, "end", 3) == 0)
{
running = 0;
}
}
sem_post(&empty); /* up semaphore */
}

/* Lastly, the shared memory is detached and then deleted. */

if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed ");
exit(EXIT_FAILURE);
}

if (shmctl(shmid, IPC_RMID, 0) == -1) {
fprintf(stderr, "shmctl(IPC_RMID) failed ");
exit(EXIT_FAILURE);
}

exit(EXIT_SUCCESS);
}

============================================================================

Shared2.cpp

=================================================================================

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<pthread.h>
#include<semaphore.h>

#define TEXT_SZ 2048
sem_t full,empty;
struct shared_use_st {
int written_by_you;
char some_text[TEXT_SZ];
};

int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[BUFSIZ];
int shmid;
sem_init(&empty,0,1);
sem_init(&full,0,0);

shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);

if (shmid == -1) {
fprintf(stderr, "shmget failed ");
exit(EXIT_FAILURE);
}

shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1) {
fprintf(stderr, "shmat failed ");
exit(EXIT_FAILURE);
}

printf("Memory attached at %X ", (long)shared_memory);

shared_stuff = (struct shared_use_st *)shared_memory;
while(running)
{
sem_wait(&empty);
/*while(shared_stuff->written_by_you == 1)
{
sleep(1);
printf("waiting for client... ");
}*/
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);

strncpy(shared_stuff->some_text, buffer, TEXT_SZ);
shared_stuff->written_by_you = 1;

if (strncmp(buffer, "end", 3) == 0) {
running = 0;
}
sem_post(&full);
}

if (shmdt(shared_memory) == -1) {
fprintf(stderr, "shmdt failed ");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
===============================================================================

Please let me know in case of any clarification.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote