Question: Here is the program to be modified for this speific output: #include semun.h /* Definition of semun union */ #include svshm_xfr.h int main(int argc, char

 Here is the program to be modified for this speific output:

#include "semun.h" /* Definition of semun union */ #include "svshm_xfr.h" int main(int

Here is the program to be modified for this speific output:

#include "semun.h" /* Definition of semun union */

#include "svshm_xfr.h"

int

main(int argc, char *argv[])

{

int semid, shmid, bytes, xfrs;

struct shmseg *shmp;

union semun dummy;

/* Create set containing two semaphores; initialize so that

writer has first access to shared memory. */

semid = semget(SEM_KEY, 2, IPC_CREAT | OBJ_PERMS);

if (semid == -1)

errExit("semget");

if (initSemAvailable(semid, WRITE_SEM) == -1)

errExit("initSemAvailable");

if (initSemInUse(semid, READ_SEM) == -1)

errExit("initSemInUse");

if(semop(semid, BUF_SIZE, 1))

errExit("semop");

/* Create shared memory; attach at address chosen by system */

shmid = shmget(SHM_KEY, sizeof(struct shmseg), IPC_CREAT | OBJ_PERMS);

if (shmid == -1)

errExit("shmget");

shmp = shmat(shmid, NULL, 0);

if (shmp == (void *) -1)

errExit("shmat");

/* Transfer blocks of data from stdin to shared memory */

for (xfrs = 0, bytes = 0; ; xfrs++, bytes += shmp->cnt) {

if (reserveSem(semid, WRITE_SEM) == -1) /* Wait for our turn */

errExit("reserveSem");

shmp->cnt = read(STDIN_FILENO, shmp->buf, BUF_SIZE);

if (shmp->cnt == -1)

errExit("read");

if (releaseSem(semid, READ_SEM) == -1) /* Give reader a turn */

errExit("releaseSem");

/* Have we reached EOF? We test this after giving the reader

a turn so that it can see the 0 value in shmp->cnt. */

if (shmp->cnt == 0)

break;

}

/* Wait until reader has let us have one more turn. We then know

reader has finished, and so we can delete the IPC objects. */

if (reserveSem(semid, WRITE_SEM) == -1)

errExit("reserveSem");

if (semctl(semid, 0, IPC_RMID, dummy) == -1)

errExit("semctl");

if (shmdt(shmp) == -1)

errExit("shmdt");

if (shmctl(shmid, IPC_RMID, 0) == -1)

errExit("shmctl");

fprintf(stderr, "Sent %d bytes (%d xfrs) ", bytes, xfrs);

exit(EXIT_SUCCESS);

}

(70 points) Modify the program (svshm/svshm_xfr_ writer.c) so that two processes can read from the same shared memory at the same time. Writer process Reader process Shared memory reserueSem(WRITE SEM) reserveSem(READ SEM) copy data block from stdin to shared memory copy data block from shared memory to stdout releaseSem(READ SEM) releaseSem(WRITE SEM) Reader process reserueSem(READ SEM); copy data block from shared memory to stdout releaseSem(WRITE SEM) Hint: Modify binary semaphore protocols so that they can operate on multiple reads rather than using binary (use 2 for the value of semaphores). The actual working flow looks like following. When you type 'abe while executing the ../svshm-writer', the abc' is written in the shared memory. (70 points) Modify the program (svshm/svshm_xfr_ writer.c) so that two processes can read from the same shared memory at the same time. Writer process Reader process Shared memory reserueSem(WRITE SEM) reserveSem(READ SEM) copy data block from stdin to shared memory copy data block from shared memory to stdout releaseSem(READ SEM) releaseSem(WRITE SEM) Reader process reserueSem(READ SEM); copy data block from shared memory to stdout releaseSem(WRITE SEM) Hint: Modify binary semaphore protocols so that they can operate on multiple reads rather than using binary (use 2 for the value of semaphores). The actual working flow looks like following. When you type 'abe while executing the ../svshm-writer', the abc' is written in the shared memory

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!