Hello, i am working on a fork() program in C and need to make some modifications
ID: 3673907 • Letter: H
Question
Hello, i am working on a fork() program in C and need to make some modifications to further match my assignment.
My question is how do i get the program to run via the command line n (from 1 to 10) like my assignment outlines below:
"Write a parent program to fork(2) n processes where n is passed to the program on the command line, and n can be from 1 to 10. All output should be made with fprintf to the stdout or stderr (followed immediately by an fflush). Follow these directions exactly!"
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
The below program will accept 1 argument as command line and the same can be used in ur program. The argument will be stored in the variable 'n' as mentioned in the program.
Example of execution
>>>gcc progam.c -o program
>>>./progam 5
Now n = 5, and the same can be used in any loop for calling fork multiple times.
--------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include<ctype.h>
int main(int argc, char *argv[]){
if ( argc != 2 ) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf( "usage: %s filename", argv[0] );
}
else{
int n = atoi(argv[1]);
printf("%d",n) ;
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.