Hello I am working on a C program that uses fork() and i to change my output of
ID: 3673595 • Letter: H
Question
Hello I am working on a C program that uses fork() and i to change my output of my program to be like the following bolded output below:
My problem i need help with: My end child process doesn't end correctly, i need to have that fixed and output something more similar to what is in bold above, my output is short and lacking the proper exit with time and dates
Parent: pid 8606 forked
Parent: child number 0 with pid 8606 exec'd at Sat Jan 31 07:58:31 2015
Child: pid 8606 executing with sleep 2 at Sat Jan 31 07:58:31 2015
Parent: pid 8607 forked
Parent: child number 1 with pid 8607 exec'd at Sat Jan 31 07:58:31 2015
Parent: pid 8608 forked
Parent: child number 2 with pid 8608 exec'd at Sat Jan 31 07:58:31 2015
Child: pid 8607 executing with sleep 1 at Sat Jan 31 07:58:31 2015
Parent: pid 8609 forked
Parent: child number 3 with pid 8609 exec'd at Sat Jan 31 07:58:31 2015
Child: pid 8608 executing with sleep 6 at Sat Jan 31 07:58:31 2015
Parent: pid 8610 forked
Parent: child number 4 with pid 8610 exec'd at Sat Jan 31 07:58:31 2015
Child: pid 8609 executing with sleep 5 at Sat Jan 31 07:58:31 2015
Child: pid 8610 executing with sleep 8 at Sat Jan 31 07:58:31 2015
Parent: child pid 8606 exited normally with status 2 at Sat Jan 31 07:58:33 2015
Parent: child pid 8607 exited normally with status 1 at Sat Jan 31 07:58:33 2015
Parent: child pid 8608 exited normally with status 6 at Sat Jan 31 07:58:37 2015
Parent: child pid 8609 exited normally with status 5 at Sat Jan 31 07:58:37 2015
Parent: child pid 8610 exited normally with status 8 at Sat Jan 31 07:58:39 2015
My program so far:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(){
pid_t myprocessid, child_pid, getpid();
int mystatus;
myprocessid = getpid();
printf("Parent : pid %d. forked", myprocessid);
child_pid = fork();
if (child_pid == 0)
{
printf("Child1 number 0 with pid: %d. exec'd at ", myprocessid);
myprocessid = getpid();
printf("Child2: getpid2() with pid %d. ", getpid());
child_pid = fork();
if ( child_pid == 1)
{
perror("error while fork");
return 1;
}
else if (child_pid == 0)
{
printf("Child3 from parent with pid: %d. ", myprocessid);
myprocessid = getpid();
printf("Child3: pid %d. exec'd ", getpid());
sleep(30);
return 12;
}
else
return 15;
}
else if(child_pid == 1)
{
while ( waitpid(child_pid, &mystatus, WNOHANG) == 0 )
sleep(1);
if ( WIFEXITED(mystatus) )
printf("Child1: Child2 exited with exit status %d. ",WEXITSTATUS(mystatus));
else
printf("Child1: child hasn't ended correctly. ");
}
else {
printf("Parent: pid: %d forked", child_pid);
wait(&mystatus);
if ( WIFEXITED(mystatus))
printf("Parent: child has %d with exit normally ",WEXITSTATUS(mystatus));
else
printf("Parent: child hasn't ended correctly. ");
}
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(){
pid_t myprocessid, child_pid, getpid();
int mystatus;
myprocessid = getpid();
printf("Parent : pid %d. forked", myprocessid);
child_pid = fork();
if (child_pid == 0)
{
printf("Child1 number 0 with pid: %d. exec'd at ", myprocessid);
myprocessid = getpid();
printf("Child2: getpid2() with pid %d. ", getpid());
child_pid = fork();
if ( child_pid == 1)
{
perror("error while fork");
return 1;
}
else if (child_pid == 0)
{
printf("Child3 from parent with pid: %d. ", myprocessid);
myprocessid = getpid();
printf("Child3: pid %d. exec'd ", getpid());
sleep(30);
return 12;
}
else
return 15;
}
else if(child_pid == 1)
{
while ( waitpid(child_pid, &mystatus, WNOHANG) == 0 )
sleep(1);
if ( WIFEXITED(mystatus) )
printf("Child1: Child2 exited with exit status %d. ",WEXITSTATUS(mystatus));
else
printf("Child1: child hasn't ended correctly. ");
}
else {
printf("Parent: pid: %d forked", child_pid);
wait(&mystatus);
if ( WIFEXITED(mystatus))
printf("Parent: child has %d with exit normally ",WEXITSTATUS(mystatus));
else
printf("Parent: child hasn't ended correctly. ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.