computing b^2-4ac using inter process communication
Experiment: Interprocess Communication Using Pipes
Title:
Using Pipe IPC to Evaluate the Expression √(b² – 4ac)
The first process computes b².
The second process computes 4ac and sends it to the first process using a pipe.
The first process then evaluates √(b² – 4ac) and displays the final value.
Aim:
To write a program using Interprocess Communication (IPC) with pipes where two processes cooperate to evaluate the mathematical expression:
sqrt(b^2−4ac)-
Process–1 computes b²
-
Process–2 computes 4ac and sends it to Process–1 through a pipe
-
Process–1 computes the final value and prints it
Theory:
Interprocess Communication using Pipes
A pipe is a unidirectional communication channel that allows data to pass from one process to another.
In Linux, the system call:
creates two file descriptors:
-
fd[0]– read end -
fd[1]– write end
Pipes are commonly used between a parent and child process created using fork().
In this experiment, two related processes communicate through a pipe:
-
The child process computes 4ac and sends it to the parent through the pipe.
-
The parent computes b², receives 4ac, and calculates the final expression.
This demonstrates synchronization, cooperation, and data exchange between processes.
Algorithm:
-
Start the program.
-
Declare variables a, b, c and one integer fd[2] for the pipe.
-
Create a pipe using
pipe(fd). -
Call
fork()to create a child process. -
If
fork()returns 0, it is the child process:-
Close the read end of the pipe.
-
Compute
4ac. -
Write the value to the pipe.
-
Close the write end.
-
-
If
fork()returns a positive value, it is the parent process:-
Close the write end of the pipe.
-
Compute
b². -
Read the value
4acfrom the pipe. -
Compute the final expression:
-
Display the result.
-
Close the read end.
-
-
End the program.
Program
Sample Outputs
Case 1: Positive discriminant
Case 2: Negative discriminant
Result:
The program successfully demonstrates Interprocess Communication using pipes.
The child process computed 4ac, sent it to the parent, and the parent computed b² and evaluated √(b² – 4ac) correctly.
Comments
Post a Comment