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

Implement the producer-consumer in such a way that the parent is the producer an

ID: 3707489 • Letter: I

Question

Implement the producer-consumer in such a way that the parent is the producer and the thread is the consumer and the parent and thread update the values of a queue in the parent’s memory. Are there race or deadlock conditions with this solution? Would you need to add a semaphore or mutex to protect the memory object? By modifying following files,

producer.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <fcntl.h>

#include <sys/shm.h>

#include <sys/stat.h>

#include <sys/mman.h>

#include <sys/types.h>

#include <errno.h>

void display(char *prog, char *bytes, int n);

int main(void)

{

const char *name = "/shm-example"; // file name

const int SIZE = 4096; // file size

  

const char *message0 = "Studying ";

const char *message1 = "Operating Systems ";

const char *message2 = "Is Fun!";

const char *msg_end = " ";

  

int shm_fd; // file descriptor, from shm_open()

char *shm_base; // base address, from mmap()

char *ptr; // shm_base is fixed, ptr is movable

  

/* create the shared memory segment as if it was a file */

shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);

if (shm_fd == -1) {

printf("prod: Shared memory failed: %s ", strerror(errno));

exit(1);

}

  

/* configure the size of the shared memory segment */

ftruncate(shm_fd, SIZE);

  

/* map the shared memory segment to the address space of the process */

shm_base = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);

if (shm_base == MAP_FAILED) {

printf("prod: Map failed: %s ", strerror(errno));

// close and shm_unlink?

exit(1);

}

  

/**

   * Write to the mapped shared memory region.

   *

   * We increment the value of ptr after each write, but we

   * are ignoring the possibility that sprintf() fails.

   */

display("prod", shm_base, 64);

ptr = shm_base;

ptr += sprintf(ptr, "%s", message0);

ptr += sprintf(ptr, "%s", message1);

ptr += sprintf(ptr, "%s", message2);

ptr += sprintf(ptr, "%s", msg_end);

display("prod", shm_base, 64);

  

/* remove the mapped memory segment from the address space of the process */

if (munmap(shm_base, SIZE) == -1) {

printf("prod: Unmap failed: %s ", strerror(errno));

exit(1);

}

  

/* close the shared memory segment as if it was a file */

if (close(shm_fd) == -1) {

printf("prod: Close failed: %s ", strerror(errno));

exit(1);

}

  

return 0;

}

void display(char *prog, char *bytes, int n)

{

printf("display: %s ", prog);

for (int i = 0; i < n; i++)

{ printf("%02x%c", bytes[i], ((i+1)%16) ? ' ' : ' '); }

printf(" ");

}


consumer.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/shm.h>

#include <sys/stat.h>

#include <sys/mman.h>

#include <errno.h>

#include <string.h>

void display(char *prog, char *bytes, int n);

int main(void)

{

const char *name = "/shm-example"; // file name

const int SIZE = 4096; // file size

  

int shm_fd; // file descriptor, from shm_open()

char *shm_base; // base address, from mmap()

  

/* open the shared memory segment as if it was a file */

shm_fd = shm_open(name, O_RDONLY, 0666);

if (shm_fd == -1) {

printf("cons: Shared memory failed: %s ", strerror(errno));

exit(1);

}

  

/* map the shared memory segment to the address space of the process */

shm_base = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);

if (shm_base == MAP_FAILED) {

printf("cons: Map failed: %s ", strerror(errno));

// close and unlink?

exit(1);

}

  

/* read from the mapped shared memory segment */

display("cons", shm_base, 64); // first as bytes, then as a string

printf("%s", shm_base);

  

/* remove the mapped shared memory segment from the address space of the process */

if (munmap(shm_base, SIZE) == -1) {

printf("cons: Unmap failed: %s ", strerror(errno));

exit(1);

}

  

/* close the shared memory segment as if it was a file */

if (close(shm_fd) == -1) {

printf("cons: Close failed: %s ", strerror(errno));

exit(1);

}

  

/* remove the shared memory segment from the file system */

if (shm_unlink(name) == -1) {

printf("cons: Error removing %s: %s ", name, strerror(errno));

exit(1);

}

  

return 0;

}

void display(char *prog, char *bytes, int n)

{

printf("display: %s ", prog);

for (int i = 0; i < n; i++)

{ printf("%02x%c", bytes[i], ((i+1)%16) ? ' ' : ' '); }

printf(" ");

}

Explanation / Answer

.ANSWER:

We need to add a semaphore but not mutex.Mutex will lock it but Semaphore will be the exact solution for avoiding deadlock.

Here,the key value will only be added by producer and decreased by thread causing the exact amount of threads to use the source as much as resource is available,.

Then, value will be decreased by 2 even though parent is readily available but waiting and the another example is just vice versa for it.