Operating system concepts(Seventh edition) 2008.3
performance than a single-threaded solution Answer: (1) Any kind of sequential program is not a good candidate to be threaded. An example of this is a program that calculates an individual tax return. (2) Another example is a \
the C-shell or Korn shell. Such a program must closely monitor its own working space such as open files, environment variables, and current working directory.
4.2 Describe the actions taken by a thread library to context switch between user-level threads. Answer:
Context switching between user threads is quite similar to switching between kernel threads, although it is dependent on the threads library and how it maps user threads to kernel threads. In general, context switching between user threads involves taking a user thread of its LWP and replacing it with another thread. This act typically involves saving and restoring the state of the registers.
4.3 Under what circumstances does a multithreaded solution using multiple kernel threads provide better performance than a single-threaded solution on a single-processor system? Answer:
When a kernel thread suffers a page fault, another kernel thread can be switched in to use the interleaving time in a useful manner.
A single-threaded process, on the other hand, will not be capable of performing useful work when a page fault takes place. Therefore, in scenarios where a program might suffer from frequent page faults or has to wait for other system events, a multi-threaded solution would perform better even on a single-processor system.
4.4 Which of the following components of program state are shared across threads in a multithreaded process?
a. Register values b. Heap memory
c. Global variables d. Stack memory Answer:
The threads of a multithreaded process share heap memory and global variables. Each thread has its separate set of register values and a separate stack.
4.5 Can a multithreaded solution using multiple user-level threads achieve better performance on a multiprocessor system than on a single-processor system? Answer:
A multithreaded system comprising of multiple user-level threads cannot make use of the different processors in a multiprocessor system simultaneously. The operating system sees only a single process and will not schedule the different threads of
Operating system concepts(Seventh edition) 2008.3
the process on separate processors. Consequently, there is no performance benefit associated with executing multiple user-level threads on a multiprocessor system.
4.6 As described in Section 4.5.2, Linux does not distinguish between processes and threads. Instead, Linux treats both in the same way, allowing a task to be more akin to a process or a thread depending on the set of flags passed to the clone() system call. However, many operating systems—such as Windows XP and Solaris—treat processes and threads differently. Typically, such systems use a notation where in the data structure for a process contains pointers to the separate threads belonging to the process. Contrast these two approaches for modeling processes and threads within the kernel. Answer:
On one hand, in systems where processes and threads are considered as similar entities, some of the operating system code could be simplified. A scheduler, for instance, can consider the different processes and threads in equal footing without requiring special code to examine the threads associated with a process during every scheduling step. On the other hand, this uniformity could make it harder to impose process-wide resource constraints in a direct manner. Instead, some extra complexity is required to identify which threads correspond to which process and perform the relevant accounting tasks.
4.7 The program shown in Figure 4.11 uses the Pthreads API. What would be output from the program at LINE C and LINE P? include
int value=0;
void *runner(void *param); /* the thread */
int main() {
int pid;
pthread_t tid;
pthread_attr_t attr; pid=fork(); if(pid==0) {
pthread_attr_init(&attr);
pthread_create(&tid, &attr, runner, NULL); pthread_join(tid, NULL);
printf(\ }else if(pid>0){ wait(NULL);
printf(\/* LINE P */
Operating system concepts(Seventh edition) 2008.3
} }
void *runner(void *param) {
value=5;
pthread_exit(0); }
Answer:
Output at LINE C is CHILD: value=5. Output at LINE P is PARENT: value=0.
4.8 Consider amultiprocessor 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.
b. The number of kernel threads allocated to the program is equal to the number of processors.
c. The number of kernel threads allocated to the program is greater than the number of processors but less than the number of userlevel threads. Answer:
When the number of kernel threads is less than the number of processors, then some of the processors would remain idle since the scheduler maps only kernel threads to processors and not user-level threads to processors. When the number of kernel threads is exactly equal to the number of processors, then it is possible that all of the processors might be utilized simultaneously. However, when a kernelthread blocks inside the kernel (due to a page fault or while invoking system calls), the corresponding processor would remain idle. When there are more kernel threads than processors, a blocked kernel thread could be swapped out in favor of another kernel thread that is ready to execute, thereby increasing the utilization of the multiprocessor system. 4.9 Write a multithreaded Java, Pthreads, or Win32 program that outputs prime numbers. This program should work as follows: The user will run the program and will enter a number on the command line. The program will then create a separate thread that outputs all the prime numbers less than or equal to the number entered by the user. Answer:
(1) // a multithreaded Java program
class PrimesThread implements Runnable {
private int num;
private int[] primeNums;
Operating system concepts(Seventh edition) 2008.3
public PrimesThread(int num) { if (num < 2)
throw new IllegalArgumentException(); this.num = num; }
public void run() { int i, j;
primeNums = new int[num + 1]; primeNums[1] = 0;
for (i = 2; i <= num; i++) primeNums[i] = 1;
for (i = 2; i <= num/2; i++) for (j = 2; j <= num/i; j++) primeNums[i*j] = 0; for (i = 1; i <= num; i++) if (primeNums[i] > 0)
System.out.println(i); } }
public class Primes {
public static void main(String args[]) { if (args.length == 0) {
System.out.println(\ System.exit(0); }
else
new Thread(new
PrimesThread(Integer.parseInt(args[0]))).start(); } }
(2) Win32 program that outputs prime numbers #include
/** we will only allow up to 256 prime numbers */ #define MAX_SIZE 256
int primes[MAX_SIZE];
Operating system concepts(Seventh edition) 2008.3
/* the thread runs in this separate function */ DWORD WINAPI Summation(PVOID Param) {
DWORD upper = *(DWORD *)Param; int i, j;
primes[1] = 0;
for (i = 2; i <= upper; i++) primes[i] = 1;
for (i = 2; i <= upper/2; i++) for (j = 2; j <= upper/i; j++) primes[i*j] = 0; return 0; }
int main(int argc, char *argv[]) {
DWORD ThreadId;
HANDLE ThreadHandle; int Param;
// do some basic error checking if (argc != 2) {
fprintf(stderr,\ return -1; }
Param = atoi(argv[1]); if (Param < 2) {
fprintf(stderr, \ return -1; }
// create the thread
ThreadHandle = CreateThread(NULL, 0, Summation, &Param, 0, &ThreadId);
if (ThreadHandle != NULL) {
WaitForSingleObject(ThreadHandle, INFINITE); CloseHandle(ThreadHandle);
/** now output the prime numbers */ for (int i = 1; i <= Param; i++) if (primes[i] > 0) printf(\ } }