Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

As noted above, the program should use fork to start each new process and wait t

ID: 3820978 • Letter: A

Question

As noted above, the program should use fork to start each new process and wait to collect the return status (and possibly check the PID) of each. At present the assignment does not contain any sample output, but I will update it shortly with outputs to demonstrate each of the cases in the rubric below Your assignment will be graded according to the rubric on the following page; partial credit may be given if you successfully complete part of a given objective. Please note that you should not submit separate files for each objective-your sole submission will be judged on how many of the tasks below it accomplishes successfully. The rubric may also be used as an outline for developing your program-for example, first write a program that accomplishes objective A, then modify it to accomplish objective B, and so on. In the rubric, objective A is the base case-you cannot complete any of the other objectives without completing that one, and the number of points listed with that objective is essentially the minimum you can earn with a working program. For each additional objective, the number of points is an additional number

Explanation / Answer

PROG A:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>

int main()
{
pid_t pid = 0;
int status = 0;

printf("Parent process with pid : %d ",getpid());

pid = fork();
if(pid < 0){
printf("Error: Unable to create child process ");
return -1;
}

if(pid == 0){
printf("Child process with pid : %d ",getpid());
}
else{
waitpid(pid,&status,0);
printf("Child process exited ");
}
  
return 0;
}

PROG B:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>

int main()
{
pid_t pid = 0;
int status = 0;

printf("Parent process with pid : %d ",getpid());

printf("Creating child process 1 ");

pid = fork();
if(pid < 0){
printf("Error: Unable to create child process 1 ");
return -1;
}

if(pid == 0){
printf("Child process 1 with pid : %d ",getpid());
}
else{
waitpid(pid,&status,0);
printf("Child process 1 exited ");
  
printf("Creating child process 2 ");
pid = fork();
if(pid < 0){
printf("Error: Unable to create child process 2 ");
return -1;
}

if(pid == 0){
printf("Child process 2 with pid : %d ",getpid());
}
else{
waitpid(pid,&status,0);
printf("Child process 2 exited ");
}
}
  
return 0;
}

PROG C:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>


int main()
{
pid_t pid = 0;
int status = 0, i=1;

printf("Parent process with pid : %d ",getpid());

while(i <= 10){

printf(" Creating child process %d : ",i);

pid = fork();
if(pid < 0){
printf("Error: Unable to create child process %d ",i);
return -1;
}

if(pid == 0){
printf("Child process %d with pid : %d ",i,getpid());
break;
}
else{
waitpid(pid,&status,0);
printf("Child process %d exited ", i);
}

i++;
}
  
return 0;
}

PROG D:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>


int main(int argc, char* argv[])
{
pid_t pid = 0;
int status = 0, i=1;
int count = 0;

if(argc < 2){
printf("Usage: please provide the number of process to be created ");
return -1;
}

count = atoi(argv[1]);
if(count == 0){
return -1;
}

printf("Parent process with pid : %d ",getpid());

while(i <= count){

printf(" Creating child process %d : ",i);

pid = fork();
if(pid < 0){
printf("Error: Unable to create child process %d ",i);
return -1;
}

if(pid == 0){
printf("Child process %d with pid : %d ",i,getpid());
break;
}
else{
waitpid(pid,&status,0);
printf("Child process %d exited ", i);
}

i++;
}
  
return 0;
}

PROG E:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>


int main(int argc, char* argv[])
{
pid_t pid = 0;
int status = 0, i=1;
int count = 0;

if(argc < 2){
printf("Usage: please provide the number of process to be created ");
return -1;
}

count = atoi(argv[1]);
if(count == 0){
return -1;
}

printf("Parent process with pid : %d ",getpid());

while(i <= count){

printf(" Creating child process %d : ",i);

pid = fork();
if(pid < 0){
printf("Error: Unable to create child process %d ",i);
return -1;
}

if(pid == 0){
printf("Child process %d with pid : %d ",i,getpid());
break;
}
else{
waitpid(pid,&status,0);
printf("Child %d (PID %d) finished ", i, pid);
}

i++;
}
  
return 0;
}

PROG F:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>

int main(int argc, char* argv[])
{
pid_t pid = 0;
int status = 0, i=1;
int count = 0;

if(argc < 2){
printf("Usage: please provide the number of process to be created ");
return -1;
}

count = atoi(argv[1]);
if(count == 0){
return -1;
}

printf("Parent process with pid : %d ",getpid());

while(i <= count){

printf(" Creating child process %d : ",i);

pid = fork();
if(pid < 0){
printf("Error: Unable to create child process %d ",i);
return -1;
}

if(pid == 0){
//Each child process exec() "ls" program to print all directories in the root("/") directory
execl("/bin/ls","/bin/ls","/");
break;
}
else{
waitpid(pid,&status,0);
}

i++;
}
  
return 0;
}

PROG G:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>

int main(int argc, char* argv[])
{
pid_t pid = 0;
int status = 0, i=1, ret = 0;
int count = 0;

char* programs[5] = { "/bin/ls" , "/bin/touch", "/bin/rm" , "/bin/cat", "/bin/stat"};
char* args[5] = { "/", "./temp.txt" , "./temp.txt", "./temp.txt" , "./temp.txt" };

if(argc < 2){
printf("Usage: please provide the number of process to be created ");
return -1;
}

count = atoi(argv[1]);
if(count == 0){
return -1;
}

printf("Parent process with pid : %d ",getpid());

while(i <= count){

printf(" Creating child process %d : ",i);

pid = fork();
if(pid < 0){
printf("Error: Unable to create child process %d ",i);
return -1;
}

if(pid == 0){
ret = (getpid() % 5);
execlp((const char*)programs[ret], (const char*)programs[ret], (const char*)args[ret]);
break;
}
else{
waitpid(pid,&status,0);
}

i++;
}
  
return 0;
}