Commit 8471c08b authored by Taddeüs Kroes's avatar Taddeüs Kroes

- Changed 'child_id' to 'getpid()' at gup() call in child process, now it works :).

parent 5fb9c53e
......@@ -52,24 +52,18 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
if( c == 'P' )
{
fprintf(log1, "Child %d eats poison...", myNumber);
fprintf(log2, "Child %d eats poison...", myNumber);
fprintf(log3, "Child %d eats poison...", myNumber);
if( !antidote )
{
fprintf(log1, "no antidote, child will exit\n");
fprintf(log2, "no antidote, child will exit\n");
fprintf(log3, "no antidote, child will exit\n");
break;
fprintf(log1, "Child %d has no antidote for poison and will exit \n", myNumber);
fprintf(log2, "Child %d has no antidote for poison and will exit \n", myNumber);
fprintf(log3, "Child %d has no antidote for poison and will exit \n", myNumber);
}
else
{
antidote--;
fprintf(log1, "antidote used (%d left) \n", antidote);
fprintf(log2, "antidote used (%d left) \n", antidote);
fprintf(log3, "antidote used (%d left) \n", antidote);
break;
fprintf(log1, "Child %d uses antidote for poison (%d left) \n", myNumber, antidote);
fprintf(log2, "Child %d uses antidote for poison (%d left) \n", myNumber, antidote);
fprintf(log3, "Child %d uses antidote for poison (%d left) \n", myNumber, antidote);
}
}
else if( c == 'A' )
......@@ -188,16 +182,17 @@ main(int argc, char * argv[])
/* When you fork, be sure to print the process id of the child
(if the fork succeeds) and an error message otherwise */
switch( (child_id = fork()) )
switch( fork() )
{
case -1:
perror("Child could not be created \n");
exit(1);
case 0:
gup(log1, log2, pipe_id, child_id);
gup(log1, log2, pipe_id, getpid());
break;
default:
puts("Parent process continuing...");
fputs("Parent process continuing... \n", log1);
fputs("Parent process continuing... \n", log2);
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment