Question
Create a project with the following program listing. This Lab will require an analysis of the output data to answer several of the questions. just
Create a project with the following program listing. This Lab will require an analysis of the output data to answer several of the questions.
just Looking for (MISSING CODE) , Please help
** Needed Includes
*1
#include
#include /* for printf() */
#include /* for fork() */
#include /* for wait() */
#include
#include #include
#include #include
// NO_OF_ITERATIONS
#define NO_OF_ITERATIONS 20
#define SHAREDMEMORY NAME "Lab5"
// The datatype that will be shared typedef struct
int Index;
int Accessed[NO_OF_ITERATIONS*2]; SharedMemoryType;
// The main program
int main( int argc, char *argv[] )
// Shared memory variables
int SharedMemoryFileDescriptor; SharedMemoryType *SharedMemory;
// Common variables int i;
int rtn;
int count;
//Create the shared memory
<MISSING CODE> = shm_open(<MISSING CODE>,O_RDWR I O_CREAT,S_IRWXU S_IRWXG I S_IRWXO); if(SharedMemoryFileDescriptor == -1)
fprintf(stderr,"Can't create shared memory segment! "); fprintf(stderr,"shm_open: %s ",strerror(errno));
exit(1);
// Set the memory size
if(ftruncate(SharedMemoryFileDescriptor, sizeof(SharedMemoryType)) == -1)
{
fprintf(stderr,Cant set memory size! ");
fprintflstderr,"ftruncate: %s ",strerror(errno));
exitl(1);
}
// Map the memory, allowing read/write
<MISSING CODE> = (SharedMemoryType *) mmap{NULL,sizeof{SharedMemoryType},PROT_READ | PROT_WRITE,
MAP_SHARED,SharedMemoryFileDescriptor, 0};
if[ SharedMemory == MAP_FAILED )
{
fprintf(stderr,"Cant Map memory! "};
fprintflstderr,"mmag failed: %s ,strerror(errno)];
exitll);
}
ff Shared memory in now mapped, lets initialize it to all zeros.
If Index will point to the zero element
SharedMemory->Index=0;
forli:0;i
SharedMemory->Accessed[i]=0;
3! Create children
rtn = 1;
for( count = 0; count < 2; count++)
{
ifl rm != 0 }
{
rtn = fork();
}
else
break;
}
ff Check return value to determine if process is parent or child
if[ rtn == 0) If Child
{
{,1 We'll write to the shared memory the NO_OF_ITERAT|ONS
for(i=0;i
{
I] Write to the Shared Memory the child number (will be 1 or 2)
SharedMemory-)AccessecharedMemory->lndex]=count;
delayllO]; // To let the other processes run, problems?
SharedMemory-blndeXH;
}
// We're done with the child
}
else // Parent
l
waithULLl; // Wait for children to die
waithULL};
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