hw4

advertisement
4003-440 and 4003-713 Operating Systems
Homework #4
Due January 15, 2007
Name: ___David Oguns_________________Section: ____02________________
1. The program shown in Figure 4.11 of your textbook (page 148) uses the Pthreads API.
What would be output from the program at LINE C and LINE P? Explain your answer
(just stating that you ran the program and that was what was output is not acceptable –
you must explain why the program produced the output, in terms of threads).
CHILD = 5
PARENT = 0
The parent forks a child process which has its own memory space, then waits for the
forked child process to complete. The child process creates a new thread with the
same memory space as the child process, and changes the value of the global
variable to 5. Then it outputs the value of that variable which is 5 and the child
process finishes. In the parent process, the value of the global variable remains
unchanged and its value is outputted, which is 0.
2. Consider a multiprocessor system and a multithreaded program written using the
many-to-many threading model. Let the number of user-level threads in the program be
more than the number of processors in the system. Discuss the performance implications
of the following scenarios:
a. The number of kernel threads allocated to the program is less than the number of
processors.
The scheduler can only schedule user level processes to kernel threads and since
some of the processors aren’t mapped to kernel threads, they will be idle.
b. The number of kernel threads allocated to the program is equal to the number of
processors.
All processors will be working simultaneously assuming there are enough user
threads and none of the kernels threads are blocked.
c. The number of kernel threads allocated to the program is greater than the number of
processors but less than the number of user level threads.
All of the processors will be working simultaneously assuming there are enough user
threads. If a kernel thread is blocked, it can be swapped out for one that isn’t
blocked.
3. A certain program uses a thread pool, which is initially empty. When the program tells
the thread pool to run a task in a separate thread, the thread pool reuses an existing idle
thread if possible otherwise the thread pool creates and uses a new thread. Once a thread
has finished running a task, the thread goes back into the thread pool as an idle thread.
The program runs 50 tasks in sequence. Each task starts 50 milliseconds (1 millisecond =
10-3 sec) after the previous task started. Each task finishes 1 second after it starts. After
all the tasks have finished, how many idle threads will be sitting in the thread pool?
(Ignore any overhead apart from the task execution time.) Explain your answer.
20 threads will be in the thread pool because the number of concurrent threads peaks at
20 during the program’s execution. Since it never exceeds 20, the thread pool size never
grows larger than 20.
Download