Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is in C , please don't use chatgpt, Write a simple program and show that it runs correctly ( example code available on Canvas;
This is in C
please don't use chatgpt, Write a simple program and show that it runs correctly
example code available on Canvas; can be used as
starting point
where processes synchronize via polling. Consider THREE
processes: A
B and C
Process A is the master process and will print out the
strings from shared memory written by two other writing processes
B first
and then C second
Process A needs to 'wait' by polling until B and C finish
writing their strings to memory. Each of processes A
B and C should be in
different code files. Create THREE different shared memory locations:
i
one
for storing the pid,
ii
for storing the process identifier
A
B or C
and
iii
for
storing the string. Here is the sequence of events that needs to be
implemented:
Process A writes
to the first shared memory location and
A
to
the second location. It also writes the string
I am Process A
into the
string shared memory location. It then prints out the shared memory
location in the following format:
I am Process A
Process A
then waits until B and C completes.
Process B first checks if the second shared memory location has a
A
in it
When true, it writes its into the first shared memory location
and the string "I am Process B
into the third shared memory location
and then signals A & C that it is complete by writing
B
into the second
shared memory location
note: B must wait to write into the process
identifier shared memory location until after process A writes
A
there
Then process B quits. At this point, process
A prints out the shared
memory location in the following format:
I am Process B
Process C writes the string "I am Process C
into the string shared
memory and its pid into the first shared memory location. It then signals
A that it is complete by writing
C
into the second shared memory
location
note: C must wait until process B
writes
B
into the process identifier shared memory
Then process C
quits. At this point, process
A prints out the shared memory location in
the following format:
I am Process C
Process A needs to keep polling the process identifier shared memory
location continuously. After process B writes
I am Process B
process
A should be able to print it out. Similarly, after process C writes
I am
Process C
process A needs to be able to print it out. Here some ad
hoc
synchronization is needed. Specifically, consider using the usleep
function to delay processes B and C between writing the pid, string and
the process identifier to give process A a chance to poll and print. Adjust
the delay such that prints happen properly.
Process A is the last one to quit and prints out a
GoodBye
message at the very end before quitting
#include
#include
#include
#include
#include
#define SHMSZ
int
main
int shmid;
key
t key;
char
shm
s;
We need to get the segment named
created by the server.
key
;
Locate the segment.
if
shmid
shmget
key
SHMSZ
perror
shmget
;
exit
;
Now we attach the segment to our data space.
if
shm
shmat
shmid
NULL
char
perror
shmat
;
exit
;
Now read what the server put in the memory.
for
s
shm;
s
char
NULL; s
putchar
s
;
putchar
;
Finally change the first character of the
segment to
indicating we have read
the segment.
shm
;
return
;
#include
#include
#include
#include
#include
for exit
#define SHMSZ
int
main
char c;
int shmid;
key
t key;
char
shm
s;
Well name our shared memory segment
key
;
Create the segment.
if
shmid
shmget
key
SHMSZ
IPC
CREAT
perror
shmget
;
exit
;
Now we attach the segment to our data space.
if
shm
shmat
shmid
NULL
char
per
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started