Question

Q1) Including the initial parent process, how many processes are created by the program shown in...

Q1) Including the initial parent process, how many processes are created by the program shown in Figure 1? Give an explanation for your answer. You can include an output statement to test and run the program. A sample output statement could be: printf("Testing......");

#include <stdio.h> #include <unistd.h>

int main() { /*fork a child process*/ fork();

/*fork another child process*/ fork();

/*fork another child process*/ fork(); return 0; } Figure 1a - How many processes are created?

Another version, now displaying the process ids.

#include <stdio.h> #include <unistd.h>

int main() { printf("%d\n",getpid()); fork(); printf("%d\n",getpid());   

fork(); printf("%d\n",getpid());

fork(); printf("%d\n",getpid());

return 0; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question 1 - How many processes are created?

Answer: 8

Explanation:

Total Number of Processes = 2n, where n is number of fork system calls.

We have totally 3 fork() calls here. So, 23 = 8 processes.

  

Sample program:

#include <stdio.h>
#include <unistd.h>

int main() {
/*fork a child process*/ fork();
/*fork another child process*/ fork();
/*fork another child process*/ fork();
printf("Testing......\n");
return 0;
}

Sample Output:

Question 2 - with process id

Consider the main process as P0

When the first fork is called, it creates one child(P1) for P0. So we have two process id ( P0 and P1)

When the second fork is called, it creates one child for each process (P0 and P1). So totally it creates two process (P2 for P0 and P3 for P1)

Totally we have 4 processes P0, P1, P2, P3

When the third fork is called, it creates one child for each process (P0, P1, P2, P3). P4 for P0, P5 for P1, P6 for P2 and P7 for P3.

#include <stdio.h> #include <unistd.h>

int main() {
   printf("%d\n",getpid());
   fork();
   printf("%d\n",getpid());   
   fork();
   printf("%d\n",getpid());
   fork();
   printf("%d\n",getpid());

return 0; }

Child process runs concurrently with the parent process. After a new child process is created, both processes will execute the next instructions. So each time fork is getting called, system will have additional child processes which executes the same set of instructions.

So the getpid() printed multiple times as the child processes are executing the codes concurrently as the main process.

Sample Output:

5232  
5232  
5232
5232
5232
5232
5232
5457
5232
5232
5456
5456
5232
5455
5455
5455
5232
5232
5456
5458
5232
5455
5455
5460
5232
5455
5459
5459
5232
5455
5459
5461

Feel free to ask any queries and rate the answer.

Happy Studying !!!

Add a comment
Know the answer?
Add Answer to:
Q1) Including the initial parent process, how many processes are created by the program shown in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT