Question
3.11 Including the initial parent process, how many processes are created by the program shown in Figure 3.32? Explain your answer. #include #include { int
- 3.11 Including the initial parent process, how many processes are created by the program shown in Figure 3.32? Explain your answer.
#include
#include
{
int main()
{
int i;
{
for (i = 0; i < 4; i++)
fork();
{
return 0;
}
Figure E3.32 How many processes are created?
- 3.13 Using the program in Figure E3.34, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)
#include
#include
#include
int main()
{
pid_t pid, pid1;
/* fork a child process */
pid = fork();
if (pid lt; 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
pid1 = getpid();
printf("child: pid = %d",pid); /* A */
printf("child: pid1 = %d",pid1); /* B */
}
else { /* parent process */
pid1 = getpid();
printf("parent: pid = %d",pid); /* C */
printf("parent: pid1 = %d",pid1); /* D */
wait(NULL);
}
return 0;
}
Figure E3.34 What are the pid values?
- 3.16 Using the program shown in Figure E3.35, explain what the output will be at lines X and Y.
#include
#include
#include
#define SIZE 5
int nums[SIZE] = {0,1,2,3,4};
int main()
{
int i;
pid_t pid;
pid = fork();
if (pid == 0) {
for (i = 0; i lt; SIZE; i++) {
nums[i] *= -i;
printf("CHILD: %d ",nums[i]); /* LINE X */
}
}
else if (pid gt; 0) {
wait(NULL);
for (i = 0; i lt; SIZE; i++) {
printf("PARENT: %d ",nums[i]); /* LINE Y */
}
}
return 0;
}
Figure E3.35 What output will be at Line X and Line Y?
4. 3.17 What are the benefits and the disadvantages of each of the following? Consider both the system level and the programmer level.
a. Synchronous and asynchronous communication
b. Automatic and explicit buffering
c. Send by copy and send by reference
d. Fixed-sized and variable-sized messages
5. 4.10 Which of the following components of program state are shared across threads in a multithreaded process?
- Register values
- Heap memory
- Global variables
- Stack memory
6. 4.17 Consider the following code segment:
pid_t pid;
pid = fork();
if (pid == 0) { /* child process */
fork();
thread_create( . . .);
}
fork();
- How many unique processes are created?
- How many unique threads are created?
7. 4.19 The program shown in Figure E4.23 uses the Pthreads API. What would be the output from the program at LINE C and LINE P?
#include
#include
int value = 0;
void *runner(void *param); /* the thread */
int main(int argc, char *argv[])
{
pid_t pid;
pthread_t tid;
pthread_attr_t attr;
pid = fork();
if (pid == 0) { /* child process */
pthread_attr_init(&attr);
pthread create(&tid,&attr,runner,NULL);
pthread_join(tid,NULL);
printf("CHILD: value = %d",value); /* LINE C */
}
else if (pid gt; 0) { /* parent process */
wait(NULL);
printf("PARENT: value = %d",value); /* LINE P */
}
}
void *runner(void *param) {
value = 5;
pthread_exit(0);
}
Figure E4.23 C program for Exercise 4.19.
8. 4.20 Consider a multicore system and a multithreaded program written using the many-to-many threading model. Let the number of user-level threads in the program be greater than the number of processing cores in the system. Discuss the performance implications of the following scenarios.
- The number of kernel threads allocated to the program is less than the number of processing cores.
- The number of kernel threads allocated to the program is equal to the number of processing cores.
- The number of kernel threads allocated to the program is greater than the number of processing cores but less than the number of user-level threads.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started