Threads Flashcards

1
Q

What two things does a process have in a typical operating system?

A

Address space

Single thread of control

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

Where do threads come in?

A

When you separate the idea of resource grouping and execution

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

Describe the way of thinking about a process as a bundle.

A

A process having address space, resources, alarms, handlers, status information. Single process = easy control.

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

Describe a thread in one line.

A

Threads are the entities scheduled for execution on the CPU

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

What do threads allow?

A

Threads allow multiple executions to take place in the same process environment, largely independent of each other.

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

How do threads run on a single CPI?

A

The threads take turns running

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

If three threads are running from different processes, what illusion is provided?

A

That the CPU, despite switching between threads, appears to be working in parallel and multiple processes appear to be being completed.

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

If different threads in the same process are running, what should you be mindful of?

A

They are not indepedent of each other.
They share the same address space. They also share the same global variables.
This means thread one could be working, the CPU switches to handle thread two and thread two could overwrite the data that thread one had been working on.

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

Why should the possibility of threads overwriting each not matter?

A

Because the threads are created in the user space and thus it becomes the user’s concern to ensure the threads work with, rather than in competition against, each other.

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

What’s the overall goal of threads?

A

The ability for multiple executions to share a set of resources.

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

What three states can a thread be in?

A

Running
Ready
Blocked

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

Give an example of how a thread could be blocked.

A

If a thread performs a system call to read data from the keyboard. It is blocked until an action is performed on the keyboard.

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

What do threads share across a process?

A

Address space
Global variables
Open files
Signals

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

What is unique per thread

A

program counter
registers
stack
state

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

What is the typical initialisation for a thread for multithreading?

A

Processes normally start with single thread.
That thread can create new threads.
A parameter is passed to a command that specifies a procedure.
All new threads in same address space.
Sometimes threads are hierarchical. This is rare.

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

Describe how thread initialisation and termination are similar?

A

When a thread finishes, it ‘vanishes’ and is no longer schedulable.

17
Q

What is a common thread_ command and what does it do?

A

thread_yield.

It voluntarily gives up the CPU for another thread.

18
Q

Why is thread_yield important?

A

There is no clock interrupt in threads meaning the CPU cannot switch threads on their own. They need to all be able to run.

19
Q

Describe things that might affect design of threads

A

If there is a parent thread and a child thread, if the parent thread blocks waiting for keyboard input does the child block?

What happens if a thread closes a file another thread needed access to?

If a thread starts allocating more memory, switches to another thread and a new thread starts allocating memory, do you use too much memory?

20
Q

Give reasons to implement threading?

A

1) Decomposing application into smaller entities should, in theory, make programming model easier.
2) Threads are easier to create and destroy. They are 100 times faster than creating a process.
3) Performance: if I/O- and processor-based work, threads allow these activities to overlap.

21
Q

Give an example of threading with a word processor.

A

word processing for an author. three threads could handle separate things for a novelist writing a book.

1) one thread can handle input from the keyboard
2) one thread can handle reformatting other pages in the document after changes are made on another page. Eg, deletions.
3) one thread can handle periodically saving a copy of the book to disk so that if something was to crash, the author could resume their work.

22
Q

Why would threading be good for a word processor?

A

If you kept everything as a monolithic block, then if a backup started, the user wouldn’t be able to type. Conversely, if a user typed, it would stop the backup.

23
Q

How does three threads for a word processor fit with the idea of a thread?

A

Each thread would have its own program counter and stack, tracing what and how it performed certain actions. Importantly, each thread would have access to a shared resource and global variables. In this case, that would be the document the user was editing.

24
Q

Why would threading be good for a web server?

A

Dispatcher: reads incoming requests.
Worker thread is picked by dispatcher to get to work.
If request can be fetched from web cache, then great. Otherwise, it’ll go to memory and collect it.

25
Q

Which two spaces can a thread be created?

A

User space

Kernel space