Answered step by step
Verified Expert Solution
Link Copied!

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:
1
.
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.
2
.
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
-
.
3
.
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
-
.
4
.
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.
5
.
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
2
7
int
main
(
)
{
int shmid;
key
_
t key;
char
*
shm
,
*
s;
/
*
*
We need to get the segment named
*
"
5
6
7
8
"
,
created by the server.
*
/
key
=
5
6
7
8
;
/
*
*
Locate the segment.
*
/
if
(
(
shmid
=
shmget
(
key
,
SHMSZ
,
0
6
6
6
)
)
<
0
)
{
perror
(
"
shmget
"
)
;
exit
(
1
)
;
}
/
*
*
Now we attach the segment to our data space.
*
/
if
(
(
shm
=
shmat
(
shmid
,
NULL,
0
)
)
=
=
(
char
*
)
-
1
)
{
perror
(
"
shmat
"
)
;
exit
(
1
)
;
}
/
*
*
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
0
;
}
#include
#include
#include
#include
#include
/
*
for exit
*
/
#define SHMSZ
2
7
int
main
(
)
{
char c;
int shmid;
key
_
t key;
char
*
shm
,
*
s;
/
*
*
We'll name our shared memory segment
*
"
5
6
7
8
"
.
*
/
key
=
5
6
7
8
;
/
*
*
Create the segment.
*
/
if
(
(
shmid
=
shmget
(
key
,
SHMSZ
,
IPC
_
CREAT
|
0
6
6
6
)
)
<
0
)
{
perror
(
"
shmget
"
)
;
exit
(
1
)
;
}
/
*
*
Now we attach the segment to our data space.
*
/
if
(
(
shm
=
shmat
(
shmid
,
NULL,
0
)
)
=
=
(
char
*
)
-
1
)
per

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2023 Workshops 34th International Conference Dexa 2023 Penang Malaysia August 28 30 2023 Proceedings

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Atif Mashkoor ,Johannes Sametinger ,Maqbool Khan

1st Edition

303139688X, 978-3031396885

More Books

Students also viewed these Databases questions