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

This is a linux activity Write a simple C or C++ program to call fork() and prin

ID: 3855072 • Letter: T

Question

This is a linux activity

Write a simple C or C++ program to call fork() and print a message with its PID (e.g., "This is process PID") where PID is the process ID of the process. You may write one program to do all cases (or a separate program to do each case).

Case1. fork(); // followed by a print statement

Case2. fork(); fork(); // followed by a print statement

Case3. fork(); fork(); fork(); // followed by a print statement

Case4.

if (pid=fork() && pid2 = fork()) {fork();}

if (pid=fork() && pid2 = fork()) {fork();}

if (pid=fork() && pid2 = fork()) {fork();}

// followed by a print statementCase 5.

for (int i=1; i<=5; i++) {fork();}

// followed by a print statement

Explanation / Answer

Given below is C program to print pid after fork. If the answer helped, please rate it. Thank you.

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main()

{

pid_t pid, pid2;

int n;

printf("Select a number [1-5]: ") ;

scanf("%d", &n);

printf(" ");

switch(n)

{

case 1:

fork();

printf("This is process %d ", getpid());

break;

  

case 2:

fork(); fork();

printf("This is process %d ", getpid());

break;

  

case 3:

fork(); fork(); fork();

printf("This is process %d ", getpid());

break;

  

case 4:

if((pid=fork()) && (pid2 = fork())) {fork();}

if((pid=fork()) && (pid2 = fork())) {fork();}

if ((pid=fork()) && (pid2 = fork())) {fork();}

printf("This is process %d ", getpid());

break;

case 5:

for (int i=1; i<=5; i++)

{

fork();

}

printf("This is process %d ", getpid());

break;

default:

printf("Number not in range [1-5] ! ");

}

}

output

Select a number [1-5]: 3

This is process 25381

This is process 25395

This is process 25394

This is process 25393

This is process 25397

This is process 25398

This is process 25396

This is process 25399

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote