• Home
  • Textbooks
  • Operating System Concepts Essentials
  • Threads

Operating System Concepts Essentials

Abraham Silberschatz, Peter B. Galvin, Greg Gagne

Chapter 4

Threads - all with Video Answers

Educators


Chapter Questions

01:24

Problem 1

Provide two programming examples in which multithreading provides better performance than a single-threaded solution.

James Kiss
James Kiss
Numerade Educator
00:59

Problem 2

What are two differences between user-level threads and kernel-level threads? Under what circumstances is one type better than the other?

James Kiss
James Kiss
Numerade Educator
00:59

Problem 3

Describe the actions taken by a kernel to context-switch between kernellevel threads.

James Kiss
James Kiss
Numerade Educator

Problem 4

What resources are used when a thread is created? How do they differ from those used when a process is created?

Check back soon!

Problem 5

Assume that an operating system maps user-level threads to the kernel using the many-to-many model and that the mapping is done through LWPs. Furthermore, the system allows developers to create real-time threads for use in real-time systems. Is it necessary to bind a real-time thread to an LWP? Explain.

Check back soon!

Problem 6

A Pthread program that performs the summation function was provided in Section 4.3.1. Rewrite this program in Java.

Check back soon!
01:24

Problem 7

Provide two programming examples in which multithreading does not provide better performance than a single-threaded solution.

James Kiss
James Kiss
Numerade Educator

Problem 8

Describe the actions taken by a thread library to context-switch between user-level threads.

Check back soon!

Problem 9

Under what circumstances does a multithreaded solution using multiple kernel threads provide better performance than a single-threaded solution on a single-processor system?

Check back soon!
01:03

Problem 10

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

James Kiss
James Kiss
Numerade Educator

Problem 11

Can a multithreaded solution using multiple user-level threads achieve better performance on a multiprocessor system than on a singleprocessor system? Explain.

Check back soon!

Problem 12

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 and Solaris - treat processes and threads differently. Typically, such systems use a notation wherein 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.

Check back soon!
01:01

Problem 13

The program shown in Figure 4.14 uses the Pthreads API. What would be the output from the program at LINE C and LINE P?

James Kiss
James Kiss
Numerade Educator

Problem 14

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 greater 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.
#include <pthread.h>
#include <stdio.h>
int value = 0;
void *runner(void *param); /* the thread */
int main(int argc, char *argv[])
{
int 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 > 0) { /* parent process */
wait(NULL);
printf("PARENT: value = %d",value); /* LINE P */
}
}
void *runner(void *param) {
value = 5;
pthread exit(0);
}
Figure 4.14 C program for Exercise 4.13.
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.

Check back soon!
06:02

Problem 15

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.

Paul Gabriel
Paul Gabriel
Numerade Educator

Problem 16

Modify the socket-based date server (Figure 3.19) in Chapter 3 so that the server services each client request in a separate thread.

Check back soon!
06:04

Problem 17

The Fibonacci sequence is the series of numbers $0,1,1,2,3,5,8, \ldots$ Formally, it can be expressed as:
$$
\begin{aligned}
& f i b_0=0 \\
& f i b_1=1 \\
& f i b_n=f i b_{n-1}+f i b_{n-2}
\end{aligned}
$$
Write a multithreaded program that generates the Fibonacci sequence using either the Java, Pthreads, or Win32 thread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in data that can be shared by the threads (an array is probably the most convenient data structure). When the thread finishes execution, the parent thread will output the sequence generated by the child thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finishes, this will require having the parent thread wait for the child thread to finish, using the techniques described in Section 4.3.

Angela Guo
Angela Guo
Numerade Educator

Problem 18

Exercise 3.17 in Chapter 3 involves designing an echo server using the Java threading API. However, this server is single-threaded, meaning that the server cannot respond to concurrent echo clients until the current client exits. Modify the solution to Exercise 3.17 so that the echo server services each client in a separate request.

Check back soon!