8. Threads and Thread Implementations Flashcards

1
Q

What makes up a thread?

A

Registers and a stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are problems with the CPU that the OS tries to correct?

A

There is only one CPU (or usually fewer than there are processes needing to be run) and it is way faster than the other parts of the system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is batch scheduling?

A

Running jobs sequentially until completion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the operating system create the illusion of concurrency?

A

By rapidly switching the CPU between multiple running tasks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does the OS ensure that it retains control over what threads are using the CPU?

A

By using a periodic timer to generate hardware interrupts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the cost of doing a context switch?

A

Context switches require executing many instructions and saving a lot of state (registers and thread stack). This takes time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does the cost of context switching affect the rate at which the hardware timer fires to allow the kernel to potentially switch between threads?

A

The rate of timer firing cannot be too high, otherwise the overhead from the context switches will start to dominate the CPU and drag the system performance down

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are registers shared between threads or processes?

A

They are not. Registers are private to an individual thread. [?]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How is the stack shared between threads or processes?

A

The stack is shared by all the threads of a process. [?] Process’s stack is not shared between processes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is the address space shared between threads or processes?

A

The address space is shared by all the threads of a process [?] A process’s address space is not shared with other processes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is the file descriptor table shared between threads or processes?

A

The file descriptor table is shared by all the threads of a process. The file descriptor table is copied over if fork is called on a process, but not shared directly with another process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why do we use threads and not just processes?

A

Threads can be a good way of thinking about applications that do multiple things “simultaneously”. Threads may help applications hide or parallelize delays caused by slow devices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Lay out an analogy for the difference between threads and a process.

A

Multiple threads within a single process are like multiple cooks trying to prepare the same meal together.

Each one is doing one thing. They are probably doing different things. They all share the same recipe (instructions) but may be looking at different parts of it. They have private state but can communicate with each other easily enough. They must coordinate with shared resources!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does a web server use multithreading?

A

Web servers use a separate thread to handle each incoming request

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do web browsers use multithreading?

A

Web browsers use separate threads for each open tab.

When loading a page, separate threads are used to request and receive each unique part of the page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does SCIENCE use multithreading?

A

One example is to execute a divide-and-conquer routine on a highly parallelizable data set (lots of threads computing different parts of the data set)

17
Q

Why threads within a process and not just a bunch of processes?

A

Threads get to take advantage of lots of shared state to communicate with each other. Interprocess communication (IPC) is more difficult because the kernel tries to protect processes from each other. Additionally, the state associated with processes doesn’t scale well.

18
Q

Describe the M:1 model of thread implementation.

A

Threads can be implemented in user space by unprivileged libraries such that M user threads are created and look like 1 thread to the OS kernel

19
Q

Describe the 1:1 model of thread implementation

A

Threads can be implemented by the kernel directly

20
Q

Why is it possible to implement threads in user space?

A

Creating threads does not involve multiplexing hardware between processes, so no kernel privilege is required.

21
Q

How do we save and restore context when implementing threads in userspace (without the kernel)?

A

This is just saving and restoring registers. The C library has an implementation called setjmp()/longjmp()

22
Q

How do we preempt other threads from userspace?

A

We use periodic signals delivered by the OS to activate a user space thread scheduler

23
Q

What are the PROS of the M:1 (userspace) threading model of thread implementation?

A

Threading operations are much faster because they do not have to cross the user/kernel boundary, AND thread state can be smaller (because you’re only saving register values?)

24
Q

What are the CONS of the M:1 (userspace) threading model of thread implementation?

A

We can’t use multiple cores, because the process is assigned to a single core (usually).

The OS also may not schedule the application correctly because it doesn’t know about the fact that it contains more than one thread.

Lastly, a single thread may block the entire process in the kernel when there are other threads that could run.

25
Q

What are the PROS of the 1:1 (kernel) threading model of thread implementation?

A

Scheduling might improve because the kernel can schedule all threads in the process

26
Q

What are the CONS of the 1:1 (kernel) threading model of thread implementation?

A

Context switch overhead for all threading operations can get costly.