Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The parent process should read an array (size 6) of double (frequency (1)) from the user and creates a pipe to do the inter-process communication and then creates a child process (Child-1). • Child-1 will calculate the velocity (V) when the wavelength (A) is 100 m (constant) and write answers to the pipe. Use the formula is given below, V = fa (V-Velocity, /-frequency, wavelength) Then the Child-1 creates a new child process (Child-2) and the Child 2 should read values from the pipe and print them to the screen


What I have tried:

I just tried this for practice in the internet. But unable to find the answer.
#include <stdio.h>
#include <unistd.h>
#include<stdlib.h>
int main()
{
int status = 0;
int fd1[2], fd2[2], fd3[2], fd4[2], a, b;
pipe(fd1); pipe(fd2);
pipe(fd3); pipe(fd4);
printf("Input two numbers for + and - ");
scanf("%d,", &a); scanf("%d,", &b);
write(fd1[1], &a, sizeof(int));
write(fd2[1], &b, sizeof(int));
printf("Input two numbers for multiplication and division\n");
scanf("%d,", &a); scanf("%d,", &b);
write(fd3[1], &a, sizeof(int));
write(fd4[1], &b, sizeof(int));
pid_t child1 = fork();
if (child1 == 0)
{
    int status = 0, a, b, c;
    read(fd1[0], &a, sizeof(int));
    read(fd2[0], &b, sizeof(int));
    printf("I'm 1st child and calculating + and -\n");
    c = a + b;
    a = a - b;
    write(fd1[1], &c, sizeof(int));
    write(fd2[1], &a, sizeof(int));
    pid_t c3 = fork();
    if (c3 == 0)
    {
        int sum, sub, mul, div;
        read(fd1[0], &sum, sizeof(int));
        read(fd2[0], &sub, sizeof(int));
        read(fd3[0], &mul, sizeof(int));
        read(fd4[0], &div, sizeof(int));
        printf("SUM = %d\n", sum);
        printf("SUB = %d\n", sub);
        printf("MUL = %d\n", mul);
        printf("DIV = %d\n", div);
    }
}
else
{
    pid_t child2 = fork();
    if (child2 == 0) {
        int status = 0, a, b, c;
        read(fd3[0], &a, sizeof(int));
        read(fd4[0], &b, sizeof(int));
        printf("I'm 2nd child and calculating multiplication and division");
        c = a*b;
        a = a / b;
        write(fd3[1], &c, sizeof(int));
        write(fd4[1], &a, sizeof(int));
    }
}
return 0;
}
Posted
Updated 10-Jul-22 19:58pm
Comments
CPallini 23-Mar-22 2:59am    
Try a simpler one to start your practice.

1 solution

You did not tell us what your compiler is or what the compiler errors and warnings are. What is it telling you?

So, a guess would be to

(1.)
Make certain that your includes cover everything that you are doing in this bit of code.

(2.)
Print out to screen, or message box out, or however you want to do it; but show yourself a running result of each (or almost each) line in your program and see if (and where) you are getting the wrong result.

I do not use Linux, but if I did those two things could be what I might do first.

Thank you for asking.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900