In: Computer Science
Consider the following code segment:
pid_t pid;
pid = fork();
if (pid == 0) { /* child process */
pthread_create( ... );
fork();
}
pthread_create( ... );
fork();
the remaining code (not shown) does not call fork() nor call pthread_create(). Assume each invocation to fork() or pthread_create() was successful.
a. How many unique processes are created (including the first main or root process)? Justify your answer.
b. How many unique threads are created by pthread_create()? Justify your answer.
pid_t pid; pid = fork(); if (pid == 0) { /* child process */ pthread_create( ... ); fork(); } pthread_create( ... ); fork(); Note that, fork created a new process, while the pthread_create creates the thread, which does its own work. The forked process moves to the next instruction after the fork statement, but the thread calls its own subroutine and works there.. a) How many unique processes are created: A main process(M) executes Line 2, and process P1 is created. Only Process P1 executes Line 4, And calls fork(), then creates another process P2. At line 9, We have 3 processes: M, P1, P2. each of which first create a thread, and then create a new process, P3, P4, P5. Hence total of 1 + 2 + 3 = 6 unique processes are created. b) P1 creates one thread on line 5. The M, P1, P2 create one thread each on line 9. Hence total 4 threads
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.