Threads Flashcards

1
Q

What is a thread?

A

A path execution

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

What is the default c/c++ thread?

A

main thread

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

How can you create a thread in c?

A

pthread_create( &thr_id, attr, func, arg )

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

What does every thread have its own of?

A

◼ Stack
◼ PC – Program counter
◼ Set of registers
◼ State
Function calls, local vars

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

What are some common examples of using multiple threads?

A

Server, serving multiple requests. Using multiple cores of the cpu

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

What are the advantages of using threads over processes?

A

Faster creation, faster context switch, faster communication.

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

What are the disadvantages of using threads and not processes?

A

Less robust, a crash brings whole application down. More synchronization problems because shared global variables.

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

What is an Atomic Section (Critical)?

A

A portion of the code that only one thread should execute on.

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

How do you use a mutex lock?

A

pthread_mutex_lock(&mutex);
int tmp = counter;
tmp=tmp+1;
counter=tmp;
pthread_mutex_unlock(&mutex);

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

What happens when you use different mutex locks on two different sections of code?

A

Sequences can interleave each other.

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

What are the two approaches to implementing mutex locks?

A

Disable interupts, Spin Locks

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

Why isn’t disabling interrupts for mutex locks popular?

A

Other interrupts may be lost, only available in Kernel mode

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

How do spin locks work?

A

Make the thread “spin” busy waiting until unlocked.

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

What is the difference between our basic spin lock and an actual mutex lock?

A

Spin lock wastes cpu cycles. Mutex lock just puts thread in wait state.

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

What happens to threads when fork is called?

A

They are not duplicated, unless it is the thread calling fork.

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

Why is it bad to mix threads and forking?

A

Locking can get blocked if tried in child process.

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

What is a Race Condition?

A

When multiple threads edit the data structure at the same time.

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

What is a semaphore?

A

A generalization of mutexes. Allows a specific number of threads between mutexes.

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

What is the initial counter of a semaphore?

A

0 or greater

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

What are semaphores used for?

A

Mutex lock (init to 1), N resource case. Waiting for an event (init to 0)

21
Q

How to decrement a semaphore?

A

sema_wait(sema_t *sem)

22
Q

How to increment a semaphore?

A

sema_post(sema_t *sem)

23
Q

Why must sema_wait be done outside of the mutex_lock/unlock section?

A

If you lock then wait, the value will never change so it won’t pass the wait.

24
Q

What are Read/Write locks?

A

Allow multiple lookups at the same time but only one write.

25
How do read/write locks work?
Separate lock/unlock calls for both
26
What is a deadlock?
When a thread has to wait for a resource that will never be available.
27
What is starvation?
When a thread might need to wait a long time before a resource becomes available ex: Read/Write Locks
28
How to list all threads in gdb.
info thread
29
how to switch to a thread in gdb
thread
30
how to print the stack trace in gdb?
where
31
How can we prevent a deadlock with our locks?
Stick to an ordering, ex: m1->m2->m3
32
Does the order of unlocking mutexes matter?
No because mutex_unlock does not force threads to wait
33
What is the philosophers problem?
If I philosophers at a table need to eat spaghetti and use the two forks to both sides of them they can't share them all.
34
What are condition variables?
A mechanism like semaphores, slightly more expensive.
35
How are atomic sections implemented in Java?
With the keyword synchronized.
36
How do synchronized classes work in Java?
All statements in the method become Atomic?
37
If object is the lock in java what does object.wait() do?
The caller waits for notify to be called on the object.
38
If object is the lock in java what does object.notify() do?
Wake up one thread waiting for object.wait()
39
If object is the lock in java what does object.notifyAll() do?
Wake up all threads waiting for object.notifyAll()
40
What does the command time give?
User Time, System Time (Kernel time), Real time
41
Why are are user + systems time less than the real time?
Overhead of other processes running.
42
What is the equation for user time + system time and N (number processors) if program is using the processors 100%
User time + System time = N* Real time.
43
What is Symmetric Multiprocessing?
When each cpu runs a copy of the OS.
44
What is a cluster of computers?
Collection of inexpensive computers connected through a fast network.
45
What is the cost of both parallel and clusters of computers?
O(n^2) and O(n). Parallel is more expensive because it is more complicated to combine parts.
46
What is a good example of clusters of computers working together?
Google datacenter. Or Computers rendering animated films.
47
What is a grid of computers?
A collection of inexpensive computers across the internet running the same application to accomplish a collective task.
48
What do some people do to contribute to a grid of computers?
Lend the idle time of their computers to give the grid computation powers.
49
What are some applications of VMs?
Hosting multiple server environments, using multiple OS.