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

Goal: IPC and Simulations Using system calls you have learned so far in the cour

ID: 3705093 • Letter: G

Question

Goal: IPC and Simulations Using system calls you have learned so far in the course and paying special attention to system calls in chapters1 5 and 10, 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 "l 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. This is a real world simple simulation. In your code, it is a MUST you use the system calls fork() and IPC system calls such as pipe(). You can use sleep() to control the execution speed and timers (if necessary) in addition to the other system calls you have learnt so far in the course. Just do not limit to fork( and pipe() other and Child enter a grocery store. other is now doing shopping hild process with id 0 is created hild is now an his awn.possibly in toys... on Received: I a doing all right mom on Received: I am doing all right mom on Received: Iam doing all right mom ors Received: 1 at, doing a11 right mom Child Received: Are you doing all right? on Received: I am doing all right mon om Received: I am doing all right mon on Received: I an doing all right mon on Received: I am doing all right mom Child Received: Are you doing all right? Note: IF YOU NEED FULL CREDIT, YOU MUST STRICTLY SHOW/USE IN CODE PROPER SYSTEM CALLS AND MUST ADHERE TO THE MESSAGE CONTROL DESCRIBED IN THIS DOCUMENT. IF YOU DO NOT USE SYSTEM CALLS AND TIMING AND STILL PRODUCE AN OUTPUT TO MATCH THE GIVAN OUTPUT YOU GET ONLY 30% OF THE GRADE. om Received: I am doing all right mon on Received: I am doing all right mon on Received: I am doing all right mon on Received: I am doing all right mom child Received: Are you doing al1 right7 on Received: 1 ?, doing all right mom on Received: I am doing all right mo on Received: I a doing all right mom om Received: I am doing all right mon Child Received: Are you doing all right? on Received: I am doing all right mom on Received: I am doing all right mon on Received: I am doing al right mon 55 5econd simulation c?pl eting.. rom sends a res sage Child Received: oK. I an done in 5 seconds, now cone to the main door on Received: I am coning mon :I a waiting

Explanation / Answer

PIPES:

A pipe is a set of serial interaction device (i.e., the data will be read in the order in which the way it will be written), that will allow a one directional communication. The data written at the end will be read back from the other point of the device.

The pipe will be mainly used for communication between 2 different threads within 1 process or the interaction is from parent and child process. Pipes task is to only connect the related process. Within shell, the symbol | will be used for creating a pipe.

Within pipes the capacity of data will be always limited. Which means if the activity of writing process takes place faster than the reading process that cosumes large amount of data, the pipe cannot store further data. In those scenarios the writer process will be blocked till more memory capacity will be available in the near future. In addition, when the reading process tries to read data in a condition when there is no data available to be read, the same activity will be blocked until further data becomes available for reading activity. Due to this, pipes gets automatically synchronized between the two different processes.

Pipes Creation Process:
The pipes() function defines a way of transmitting data between two different programs and will also allows to read and write the data during transmission activity

.

#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:


The popen function permits a program to call a different program as a new process and either it will write the data to a program or it will read the data from the program. The parameter command is defined as the name of the program to be executed. The open_mode parameter will specify the mode that needs to be invoked, it can be treated as either "r" or "w". On returning failure popen() function will returns a NULL pointer value. However, if we want to perform two process communications we have to use two pipes.

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.

We can transmit the data between various processes by first removing the file desc 0 and the call to the same descriptor will be dealt as duplicate. By defining the new file descriptor we will have the number as 0. However, the new descriptor is considered to be the duplicate of an already existing descriptor and standard input will be changed to have the access of the current descriptor. At the end of activity, we will be creating two file descriptors for one file or pipe and one of them is considered to be as standard input.

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 program will create a pipe and then set of forks. However, both parent and child process can have its own file descriptors for reading and writing operation. Finally there are four file descriptors in total.

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.

Finally, the parent process will never perform the read operation on the pipe it is associated. However, the parent process starts it`s execution by closing the read end operation that is read as file_pip[0] statement. When writing activity of data has been finished, the write end operation of the parent process will be closed and terminated. When there are no file descriptors available to be written to the pipe, the od command will read the three bytes of data from the file and following it will write the data to the pipe, in process the reading process i.e parent process will return 0 bytes specifying the end of the file.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote