Question

In: Computer Science

Consider the following code segment: pid_t pid; pid = fork(); if (pid == 0) { /*...

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.

Solutions

Expert Solution

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.


Related Solutions

Question 9 What is produced from the following code segment? val = 0; do { val++;...
Question 9 What is produced from the following code segment? val = 0; do { val++; WriteLine(val); } while (val < 5); Nothing is displayed Outputs 5 thru 9 Outputs 10 Outputs 1 thru 4 Outputs 1 thru 5 Outputs 10 thru 14 Outputs 0 thru 4 Question 10 Assuming val was described as an integer and an initial value of 10, what is produced from the following code segment? do { WriteLine(val); } while (val < 10); Nothing is...
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
Consider the following segment table: Segment Base Length 0 219 600 1 2300 14 2 90...
Consider the following segment table: Segment Base Length 0 219 600 1 2300 14 2 90 100 3 1327 580 4 1952 96 What are the physical addresses for the following logical addresses? 0,430 1,10 2,500 3,400 4,112
Consider the following code segment:    count = 1    while count <= 10:       print(count,...
Consider the following code segment:    count = 1    while count <= 10:       print(count, end=" ") Which of the following describes the error in this code? The loop control variable is not properly initialized. The comparison points the wrong way. The loop is infinite. The loop is off by 1. Does this code contain an error? If so, what line is the error on? 0: ans = input("Yes/No? ") 1: if ans == "Yes": 2: print("Confirmed!") 3: else...
Write assembly code for the following machine code. Assume that the segment is placed starting at...
Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual memory addresses represented by such labels. 0010 1010 0000 1000 0000 0000 0000 1010 0001 0001 0000 0000 0000 0000 0000 0010 0000 0010 0001 0001 1000 0000 0010 0000 0000 1000 0000 0000 0100 1110 0010 0101 0000 0010 0001 0010 1000 0000 0010 0000
a. Write machine code for the following assembly code. Assume that the segment is placed starting...
a. Write machine code for the following assembly code. Assume that the segment is placed starting at location 80000. Use decimal numbers to represent each instruction. loop:         beq $s3, $s1, endwhile                  add $t0, $s3, $s4                  lw $t1, 0($t0)                  add $s0, $s0, $t1                  addi $s3, $s3, 4                  j loop endwhile: b. Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual...
Consider the following schema: Suppliers (sid, sname, address) Parts (pid, pname, colour) Catalog(sid, pid, cost) The key for Suppliers is sid, for Parts is pid, and for Catalog is sid and pid The Ca...
Consider the following schema: Suppliers (sid, sname, address) Parts (pid, pname, colour) Catalog(sid, pid, cost) The key for Suppliers is sid, for Parts is pid, and for Catalog is sid and pid The Catalog relation associates prices charged for parts by suppliers. Write the following queries using relational algebra. For items (a) through (e), use the "sequences of assignments" form. For items (f) and (g), use the "expression tree" form. List all assumptions. (Some marks will be given for the quality of your answers.) (a) Find...
We consider a monocentric city represented by a segment x 2 [0,xf ] where 0 stands...
We consider a monocentric city represented by a segment x 2 [0,xf ] where 0 stands for the CBD where everybody works and earns a uniform (exogenous) urban wage w >0 and xf is the city fringe. The rural wage a is normalized to zero. Agents located at a distance x from the CBD pay a rent R(x). The utility of an agent is her disposable income. 1 Fixed commuting cost 1.1 Private transportation Agents drive to the CBD and...
Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) write the following...
Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) write the following queries in SQL: * Find the names of all suppliers who supply a green part. *Find the names of all suppliers who are from Illinois. *Find the names of all suppliers who sell a red part costing less than $100. *Find the names and colors of all parts that are green or red. In writing these queries you may make the following assumptions: 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT