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

build a C/C++ program to simulate the following scenario. Mother and child enter

ID: 3707477 • Letter: B

Question

build a C/C++ program to simulate the following scenario.
Mother and child enters a grocery store. Mother acts as the Parent Process and the child acts as the Child Process. Mother goes to do her grocery buying while the child goes to the toys or books area. But the deal is that the child must send a message to the mother saying “I am doing all right mom” in every 3 seconds while the mother sends a question to the child “Are you doing all right?” every 10 seconds.
After 55 seconds, mother almost finishing shopping, and sends a message to the child that she will be done in 5 seconds and asks the child to come to the store front (e g: “OK. I am done in 5 seconds, now come to the main door...”. Child receives the message and sends message “OK mom. I am coming”.
However, children are children, they are slow, and waits, and look at anything attracts them. So the child get delayed for 5 seconds. The is losing patience a bit that the child is not at the door yet, so the mother prints a message or two again “I am waiting.....come on.....”.
MAKE SURE to put appropriate print statements to indicate the start of the simulation, when the child process is created and to indicate that the mother and child are on their own directions. Note that, when multiple processes run, print statements get mixed up; so make sure to indent all child’s messages. Also put an indication to identify the owner of the message such as, having a Mom Received: or a Child Received: preceding the messages by the Mother and Child respectively. Here is a full 65 second example simulation

Explanation / Answer

ANSWER:

#include<unistd.h>

int pipes(int file_desc[2]);

pipes() function will be called with the array of file descriptors. Also, pipe() function will incorporate the array with new file desc values and will return zero value. On facing error condition, it will return -1 value and it will define the error number in order to define the reason of failure.

The file descriptors will be connected in such way that the data that is to be written to file_ desc[1] will be read from the file_desc[0].

We should note that, as the process uses file descriptors and not the file streams, we must follow to define the read and write system calls in order to access the required data from the file.)

Pipe processing operation:

The activity of passing information between two different programs can be achieved with the support of popen() and pclose() functions.

#include<stdio.h>

FILE *popen(const char *comnd ,

const char *open-md);

int pclose(FILE *strm_to_cls);

popen() Function:

pclose() Function:

With the help of pclose() function, we will be able to close the filestreams dependent on the popen() function. In particular when the process called by popen() function completes it`s execution.

The pclose() function will call the exit code of the process, that specifies the process that needs to be closed after execution. However, when the process has already executed a wait statement in advance before we call pclose()function, the exit statement result will be lost as the process has already completed its activities. When we close the filestream, pclose() function has to wait till the child process gets terminated.

Pipes can be used as standard inputs and outputs:

We can call the standard programs, that will expect a file descriptor to be called as a parameter.

#include<unistd.h>

int duplicate1(int file_desc);

int duplicate2(int file_desc_1,

int file_desc_2);

The purpose of duplicate call will be to open a new file descriptor, that can relate to the same file as an current file descriptor. In view of duplicate1() function, the value of the new file descriptor will be dealt as the lowest number available within the file

. Also, duplicate2() function will be same as duplicate1() function, or it is treated as the first defined descriptor which will be greater than the parameter file_desc_2.

The below C code explains the IPC pipes operation:

//C program to define IPC pipes operation

#include<unistd.h>

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

int main()

{

int data_procs;

int file_pip[2];

const char samp_data[]= "123";

pid_t fork_res;

if(pip(file_pip)==0)

{

fork_res=fork();

if(fork_res==(pid_t)-1)

{

fprintf(stderr,"fork failure");

exit(EXIT_FAILURE);

}

   if(fork_res==(pid_t)0)

{

close(0);

dup(file_pip[0]);

close(file_pip[0]);

close(file_pip[1]);

execlp("od","od","-c",(char *)0);

exit(EXIT_FAILURE);

}

else

{

close(file_pip[0]);

data_ procs =write(file_pip[1],

samp_data,strlen(samp_data));

close(file_pip[1]);

printf("%d -wrote %d bytes ",(int)getpid(),data_processed);

}

}

exit(EXIT_SUCCESS);

}

The child process will perform close operation on its standard input with close(0) statement and also defines duo(file_pip[0])statement. This activity will create duplicate value of the file descriptor this is associated with the read operation.

Following child process will close its initial file descriptor. It`s a fact that child process will never perform write operation, it will also close the current write file descriptor it is associated (file_pip[1]).

At present there is only one file descriptor 0 that is associated to a pipe as standard input.

Next, child process will use the exec statement to invoke a program that perform read operation on the standard input.

The od command needs to wait for the data that needs to be available from a user terminal.