Question: why isnt my code working? here is the assignment. #include #include #include #include int main ( ) { pid _ t pid; char * message;

why isnt my code working? here is the assignment.
#include
#include
#include
#include
int main()
{
pid_t pid;
char *message;
int n;
printf("fork program starting
");
pid = fork();
switch(pid)
{
case -1:
perror("fork failed");
exit(1);
case 0:
message = "This is the child";
n =5;
break;
default:
message = "This is the parent";
n =3;
break;
}
for(; n >0; n--){
puts(message);
sleep(1);
}
exit(0);
}
Zombie Process
When a child process terminates, an association with its parent survives until the parent in turn either:
calls wait() orterminates normally
The child process entry in the process table is therefore not freed up immediately. Although no longer active, the child process is still in the system because the exit code needs to be stored in case the parent subsequently calls wait(). The child becomes what is known as defunct, or a zombie process.
Orphan Process
If a parent process did not invoke wait () and instead terminated, its child processes remain as orphan processes
Programming:
To compile:
gcc fork1.c -o fork1
To run:
./fork1
Compile and execute fork1.c program. The original process (the parent) finishes before the child has printed all of its messages, so the next shell prompt appears mixed in with the output. Does the child become an orphan? Why or why not?
Convert the fork1.c program t o a zombie process. (Hint: change the number of messages. If the child prints fewer messages than the patent, child will finish first and will exist as a zombie until the parent has finished.)
and this is the code i inputed
#include
#include
#include
#include
int main(){
pid_t pid;
char *message;
int n;
printf("fork program starting
");
pid = fork();
switch (pid){
case -1:
perror("fork failed");
exit(1);
case 0:
message = "This is the child";
n =2;
for (; n >0; n--){
puts(message);
sleep(1);
}
exit(0);
default:
message = "This is the parent";
n =10;
for (; n >0; n--){
puts(message);
sleep(1);
}
exit(0);
}
}
why isnt my code working? here is the assignment.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!