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

3) What will be the output in LINE X and LINE Y? #include #include #include #def

ID: 3744156 • Letter: 3

Question

3) What will be the output in LINE X and LINE Y? #include #include #include #define SIZE 6 int nums[SIZE] = {0,1,2,3,4,5}; int main() { int i; pid t pid; pid = fork(); if (pid == 0) { for (i = 0; i < SIZE; i++) { nums[i] = -i%2; printf("CHILD: %d ",nums[i]); /* LINE X */ } }else if (pid > 0) { wait(NULL); for (i = 0; i < SIZE; i++) nums[i]* = -i printf("PARENT: %d ",nums[i]); /* LINE Y */ } return 0; }

3) What will be the output in LINE X and LINE Y? #include #include #include #define SIZE 6 int nums[SIZE] 10,1,2,3,4,5]; int main() int i pid t pid pid = fork(); if (pid-= 0) { for (i = 0; i

Explanation / Answer

ANSWER :

fork is used to create a new child process.

pid=fork();

if pid==0 then it is child otherwise parent.

The output depends upon which process runs first, child or parent. and this depends upon the scheduler.

Here both process modifying the global variable.

When child process runs first

------------------------------------------------------------

The output at lineX is...

------------------------- -----

it will multiply array nums with -i

so inside for loop it prints.

0,-1,-4,-9,16

The output at lineY

0,-1,-4,-9,16,25

-------------------------------------------------------------

When parent process runs first then

------------------------------------------------------

At lineX output will be.

0,-1,-4,-9,16

At lineY output is

0,1,2,3,4,5

#include #include #include #include #include #define SIZE 6 int nums[SIZE] = {0,1,2,3,4,5};

int main()

{

int i;

pid_t pid;

pid = fork();

if (pid == 0)

{

for (i = 0; i < SIZE; i++)

{

nums[i] *= -i;

printf("CHILD: %d ",nums[i]); / LINE X /

}

}

else if (pid > 0)

{

wait(NULL);

for (i = 0; i < SIZE; i++)

printf("PARENT: %d ",nums[i]); / LINE Y /

}

return 0;

}

Please rate the best and understandable answer

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