Reputation: 37
I have to make a short program that, creates two child processes, each one accepting an integer from the keyboard and writes them to a pipe, from where the parent process summarizes them and displays the total on the screen.
I've written one with scanf(), but it freezes up and it doesn't give me the sum. How do I make it work with scanf or any other way if possible?
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int pipe(int pd[2]);
int main(int argc, char *argv[])
{
int pd[2], sum=0, num=0;
if(pipe(pd) == -1)
for(int i = 0; i < 2; i++)
{
if(fork() == 0)
{
scanf("%d", num);
if(write(pd[1], &num, sizeof(int)) == -1)
printf("Error: Write()");
}
}
for(int j = 0; j < 2; j++)
{
wait(NULL);
if(read(pd[0], &num, sizeof(int)) == -1)
printf("Error: Read()");
sum += num;
}
printf("Total: %d\n", sum);
}
Upvotes: 0
Views: 498
Reputation: 48592
Lots of problems here:
if(pipe(pd) == -1)
, and I assume you meant to have an error handler as the "then" clause for it, but you don't, so children will only spawn if the pipe fails, which is basically the opposite of what you want.scanf("%d", num);
. You need &num
since num
isn't already a pointer.return
or exit
from the child processes, or they'll fall into the next loop and consume the output.With just those things fixed, it's enough to make it work:
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int pipe(int pd[2]);
int main(int argc, char *argv[])
{
int pd[2], sum=0, num=0;
if(pipe(pd) == -1)
{
perror("pipe");
return 1;
}
for(int i = 0; i < 2; i++)
{
if(fork() == 0)
{
scanf("%d", &num);
if(write(pd[1], &num, sizeof(int)) == -1)
printf("Error: Write()");
return 0;
}
}
for(int j = 0; j < 2; j++)
{
wait(NULL);
if(read(pd[0], &num, sizeof(int)) == -1)
printf("Error: Read()");
sum += num;
}
printf("Total: %d\n", sum);
}
There's a few other things you should fix too, but they're not complete show-stoppers. Here's what jumped out at me:
pipe
prototype. The one from unistd.h is fine.fork
or scanf
fail.Upvotes: 1