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 .
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

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

int pipe(int fd[2]);

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 , receives 4ac, and calculates the final expression.

This demonstrates synchronization, cooperation, and data exchange between processes.

Algorithm:

  1. Start the program.

  2. Declare variables a, b, c and one integer fd[2] for the pipe.

  3. Create a pipe using pipe(fd).

  4. Call fork() to create a child process.

  5. 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.

  6. If fork() returns a positive value, it is the parent process:

    • Close the write end of the pipe.

    • Compute .

    • Read the value 4ac from the pipe.

    • Compute the final expression:

      b24ac\sqrt{b^2 - 4ac}
    • Display the result.

    • Close the read end.

  7. End the program.

Program

#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>

int main() {
    int fd[2];
    pid_t pid;
    double a, b, c;
    double four_ac, b2, result, discriminant;

    printf("Enter values of a, b, c: ");
    scanf("%lf %lf %lf", &a, &b, &c);

    // Create pipe
    if (pipe(fd) == -1) {
        perror("Pipe failed");
        return 1;
    }

    pid = fork();

    if (pid < 0) {
        perror("Fork failed");
        return 1;
    }

    else if (pid == 0) {
        // Child process: compute 4ac
        close(fd[0]); // Close read end

        four_ac = 4 * a * c;
        write(fd[1], &four_ac, sizeof(four_ac));

        close(fd[1]); // Close write end
    }

    else {
        // Parent process: compute b^2 and final expression
        close(fd[1]); // Close write end

        b2 = b * b;
        read(fd[0], &four_ac, sizeof(four_ac));

        close(fd[0]); // Close read end

        discriminant = b2 - four_ac;

        if (discriminant < 0) {
            printf("Discriminant (b^2 - 4ac) = %lf is NEGATIVE.\n", discriminant);
            printf("Square root of a negative number is imaginary. Cannot compute real value.\n");
        } else {
            result = sqrt(discriminant);
            printf("Value of sqrt(b^2 - 4ac) = %lf\n", result);
        }
    }

    return 0;
}

Sample Outputs

Case 1: Positive discriminant

Enter values of a, b, c: 1 5 6 Value of sqrt(b^2 - 4ac) = 1.000000

Case 2: Negative discriminant

Enter values of a, b, c: 1 2 5 Discriminant (b^2 - 4ac) = -16.000000 is NEGATIVE. Square root of a negative number is imaginary. Cannot compute real value.

Result:

The program successfully demonstrates Interprocess Communication using pipes.
The child process computed 4ac, sent it to the parent, and the parent computed and evaluated √(b² – 4ac) correctly.


Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command