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

2. The following questions are regarding fork() operation: (a.) When a parent pr

ID: 3753691 • Letter: 2

Question

2. The following questions are regarding fork() operation: (a.) When a parent process creates a child process using the fork() operation, is there any part of memory shared between the parent process and the child process during their ongoing executions? Explain it. (b.) Identify the values of pid at Lines A, B, C, and D in the following program. (Assume that the actual pids of the parent and child are 54321 and 54322, respectively.) #include #include #include unistd.b* int main() pidt pid1, pid2; pidl = fork(); if (pidl

Explanation / Answer

Here are the points needed to solve this question

1) When fork is called new process is made which is called child process and there is no sharing of any sort by default

2) fork returns 0 to the child process and "childs process id" to the parent process. -ive values if fork fails

3)getpid() is a function which return process id of calling process

So, Output will be

A: pid1 = 0 {Child process - fork return 0}

B: pid2 = 54322 {Child process - getpid returns id of process which calls it. Child process calls it}

C: pid1 = 54322 {Parent process - fork returns process if of child}

D: pid2 = 54321 {Parent process - getpid returns id of process which calls it. Parent process calls it}

Above explanation contains ans to both parts

Heres code I have to understand the code better

#include <stdio.h>

#include <sys/types.h> //NOTE: Added

#include <unistd.h>

int main() {

pid_t p1, p2;

p1 = fork();

if(p1 < 0) {

printf("fork faliled ");

} else if(p1 == 0) {

printf("Child process ");

//getpid return process of child

p2 = getpid();

printf("Inside Child Process %d p2 : ", p2);

printf("Inside Child Process %d p1 : ", p1);

} else {

printf("Parent process ");

//Id of Child is p1

//getPid return process id of parent

p2 = getpid();

printf("Inside Parent process %d p2 : ", p2);

printf("Inside Parent process %d p1 : ", p1);

}

}

Please comment for further assistance

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