Page 1 of 1

CIS 345/CIS 545 Assignment 1 Two-way Inter-process communication Points: 35 Write an inter process communication program

Posted: Thu Jul 14, 2022 2:12 pm
by answerhappygod
CIS 345/CIS 545 Assignment1
Two-way Inter-processcommunication
Points: 35
Write an inter process communicationprogram in C language to exchange message between parent processand child process. This Assignment is aimed to learn one-way andtwo-way inter-process communication using fork() and pipe()functionalities. This assignment has two parts.
One –way communication using a singlepipe
Write a program in Cthat will write and read a message, “I am your Parent – Group#”through the pipe using the parent and the child processes.Algorithm:
Step 1 − Create a pipe.
Step 2 − Create a child process.
Step 3 − Parent process writes to thepipe.
Step 4 − Child process retrieves themessage from the pipe and writes it to the standard output.
Write a program in Cfor two-way inter-process communication where parent process willwrite a message and child process will read and will display on thescreen using the first pipe. Child process will write a message andparent process will read and will display on the screen using thesecond pipe.
.
Two-way Communication using pipes:
Anonymous pipe is aunidirectional communication channel which means either the parentprocess writes and the child process reads or vice-versa but notboth. However, when both the parent and the child want to write andread from the pipes simultaneously, they need to use two pipes fora two-way communication. The steps to achieve two-waycommunication:
Second one is for the child to writeand parent to read (call pipe2).
Algorithm
gcc Assignment1bgroup1.c –oAssignment1bgroup1
Execution (Expected Output)
In Parent: Writing to pipe 1 – Messageis Parent
In Child: Reading from pipe 1 –Message is Parent
In Child: Writing to pipe 2 – Messageis Child
In Parent: Reading from pipe 2 –Message is Child
Help:
// Fork() declaration section
if(pid==0)
{
printf("thechild\n" );
}
elseif(pid>0)
{
printf("theparent\n");
wait();
}
// Sample instructions to close unwanted ends of parent andchild process for two-way communications using two pipes
if (pid != 0) // Parent process {
close(pipefd1[0]); // Close the unwanted pipe1 read side close(pipefd2[1]); // Close the unwanted pipe2write side
…….
else { //childprocess close(pipefd1[1]); //Close the unwanted pipe1 write side close(pipefd2[0]); // Close the unwanted pipe2 read side
……..