Question

In: Computer Science

a. What will be the output of LINE A in code “a” and output of code...

a. What will be the output of LINE A in code “a” and output of code “b”? Also write
reasons for the outputs.
a. #include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 3;
int main()
{
pid_t pid;
pid = fork();
if (pid = = 0) {/* child process */}
value += 233;
return 0;
}
else if (pid > 0) {/* parent process */}
wait(NULL);
printf(“PARENT: value = %d”, value); /* LINE
A */
return 0;
} }

b.

#include <stdio.h>
#include <unistd.h>
int main()
{
/* fork a child process */
fork();
/* fork another child process */
fork();
/* fork another child process */
fork();
/* and fork another */
fork();
return 0;
}

Solutions

Expert Solution

Here is your solution if you have any doubts then you can write in the comment section.

Please give feedback.

Solution:

Important points:-

(1) fork() command creates a new process called the child process and this process runs concurrently with the process which creates it called parent process.

(2) fork() will return 0 to the child process and return process id(>0)to the parent process which called fork() and return a negative number if child creation is unsuccessful.

(3)Both the Child process and the Parent process run concurrently in the same program but OS allocates different states and data to the process it means if there is any variable (global, local) 's value is changed in any one process doesn't affect another process.

By considering the above point we will find the output of the above problem.

Solution 1:

Here pid=fork(), a new process is created according to point 1.

so for child process pid=0 and for parent process pid will be a positive number according to point 2.

Now both process runs concurrently and in the child process if(pid==0) condition will be true and its value is incremented by 233 and return 0, and in the parent process if(pid>0) condition will be true and it will print value which is 3 for parent process because variables assigned to child and parent process separately according to point 3.

So it will print PARENT: Value=3

Solution 2:

In this program, there are four fork() called so we will see the result after each fork().

We will consider only point 1 for this program.

In this program there is no print statement means it doesn't print anything but a total of 15 child process will be created and finally, there will be 16 processes including parent process.

I Explained each fork() in the diagram if you have any doubts then you can write in the comment section.

Thank You!


Related Solutions

What is the output of the code below? After each line that has a cout, write...
What is the output of the code below? After each line that has a cout, write what the output of the code is. See example in code below. int i = 2, j = 1, k = i + j; cout << k << endl; ➔ 3 double d = 2.99, e = 1, f = d + e; cout << f << endl; double q = 1.50; cout << q/3 << endl; cout << 6.0/4 << endl; char c...
Write the code to return the output in Rstudio. What is the code? Code: x <-...
Write the code to return the output in Rstudio. What is the code? Code: x <- c(28, 69, 5, 88, 19, 20) Output must be: [1] 4 2 1 6 5 3
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
What is the output of the following code: x = 0 a = 1 b =...
What is the output of the following code: x = 0 a = 1 b = -3 if a > 0:    if b < 0:       x = x + 5    elif a > 5:       x = x + 4    else:       x = x + 3 else:     x = x + 2 print(x) 4 2 5 3 Consider the following code segment:    sum = 0.0    while True:       number = input(“Enter a...
21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
Suppose x is 1 and y is 10. What is the output of the following code?...
Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) && ( y <11)                                     Display statement                         else                                     Display nothing 2. Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) || ( y <11)                                     Display statement                         Else                                     Display nothing 3. Write a java program that prints 1 to infinite numbers. 4. The...
What will be the expected output of the following pseudo code? Write exactly what would display...
What will be the expected output of the following pseudo code? Write exactly what would display when you execute the statements. Module main() Declare Integer a = 5 Declare Integer b = 2 Declare Integer c = 3 Declare Integer result = 0 Display "The value of result is" Display result Set result = a + b * c - a Display "Changed value is: ", result End Module
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT