Threads Flashcards

1
Q

Describe a thread?

A

A thread is a sequence of instructions that are executed by a process. it is a lightweight program.

Each thread that belongs to a process space has a separate stack and registers but they all share the same vritual address space.

So they share the same code, data and and open files

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

Why are multiple threads needed?

A

Because applications do more than one thing at a time.

Multithreading allows us to not have to program the interleaved execution ourselves

We do not have to wait for one task to complete (like IO operations) before doing another task

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

What flag do you use to link the thread library to the user code.

A

Use the “-pthread” flag

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

What is the data type of

  • thread ID:
  • thread attr:
  • thread start routine:
A
  • thread ID: pthread_t – unsigned long
  • thread attr: pthread_attr_t
  • thread start routine: void* (*start_routine) (void*)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the prototype of pthread create, what does it return?

A

int pthread_create(

  • pthread_t* restrict threadid,
  • pthread_attr* restrict threadattr,
  • void* ( *start_routine ) ( void* ),
  • void* restrict arg

)

the restrict keyword is used to make sure that the pointers do not point to any of the same memory locations.

Success: returns 0

Failure: returns an error number

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

In which 3 cases does a thread terminate?

A

A thread terminates on one of the 3 cases.

  1. The start routine function ends
  2. The thread calls the pthread_exit function
  3. The program terminates ( main function terminates )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the prototype for pthread_exit()

A

void pthread_exit(void* )

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

What function is used to get the id of the current thread:

A

Use the pthread_self( ) function to get id of the current thread.

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

What is a thread safe function

A

A thread safe function is a function that executes correctly even when it is executed simultaneously by multiple threads.

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

What are re-entrant functions?

A

These are thread safe functions, they usually have _r at the end.

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

Prototype of the pthread_join

A

void pthread_join(pthread_t threadid, void ** ptr)

threadid: id of the thread that is to be joined.
ptr: the pointer to where the return value of the thread is stored.

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

What is thread cancellation and what is the function that can be used for this?

A

One thread can request the cancellation of another thread.

int pthread_cancel( pthread_t thread)

On success this returns 0 or otherwise it just returns an error value

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

What are the 3 ways in which multithreaded programs can go wrong?

A

The three ways are:

  • Race conditions
  • Thread starvation
  • Deadlock
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are race conditions?

What are critical sections?

A

Simultaneuous access of 2 threads lead to data being stored to be inconsistent

Critical sections are those points in the code that can lead to data race conditions

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

Give an example for race condition.

A

Incrementing of a variable int i = 1

i++;

First thread sees i = 1

Second thread sees i = 1 before the first thread can increment it and write it back

First thread increments i to 2 and writes it back to the memory location of i

Second thread also increments its copy of i ( which was 1 ) to 2 and writes it back

The final value of i is 2, when actually it should’ve been 3.

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

What are mutexes and what do they ensure?

A

Mutexes are variables that can be locked on by threads.

They provide mutual exclusion or non interference between threads that execute the same code.

17
Q

How do you create a mutex?

A

You create mutex using the pthread_mutex_init function

int pthread_mutex_init( pthread_mutex_t* mutex, const pthread_mutexattr_t* attr );

18
Q

How do you create mutexes for global variables?

A

You use the static mutex initialazer

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER

19
Q

How do you destroy a mutex?

Why is it important to destroy a mutex once you are done using it?

A

You use the pthead_mutex_destroy function and this taken in the pointer to the mutex

pthread_mutex_destroy( pthread_mutex_t* mutex );

You must destroy a mutex because the pthread library does not clean up for you.

20
Q

When can the usage of mutexes lead to undefined behaviour

A

The usage of mutexes can lead to undefined behaviour when:

  • trying to lock a mutex that has not been initialized
  • trying to unlock a mutex that has not been locked
  • trying lock a mutex twice
  • trying to lock a mutex that has been locked by another thread
21
Q

What is trylock and what does it return?

A

Trylock is used when you want a thread to attempt to lock on a mutex but not wait wait on the mutex if it is unavailable.

It returns:

  • 0, if it is successful
  • EBUSY if it was unsuccessful
  • EINVAL if the mutex is invalid
22
Q

What is deadlock?

A

Deadlock is when 2 threads that are executing are waiting on each other’s resources.

So if 2 threads A and B are executing and if A is waiting for B’s resources while B is waiting for A’s resources, then the neither of the threads can continue their execution and this condition is called a deadlock.

23
Q

What do you have to keep in mind when you’re working with multiple locks inorder to avoid deadlocks?

A

When you’re working with mutliple locks, you would need to make sure that you lock the mutexes in the same order in the different threads.

24
Q

Why is it advantageous to hold mutexes for as short as possible?

A

When you minimize the time the time that you spend holding onto mutexes, then

  • there is less chance that there are mutex related mistakes, like relocking or forgetting to unlock
  • it makes the code more parallel as more number of threads are allowed into the code block.
25
Q

Elaborate on semaphores.

A

Semaphores are threading objects designed to solve a variety of threading problems

They have an internal state that is always greater than or equal to 0.

There are 2 basic operations on semaphores

  • wait: Wait decrements the internal state of semaphore unless it’s 0, then it waits for it to be incremented.
  • signal: incrementst the value.
26
Q

How do you make one thread wait on another thread without using mutexes?

A

Initialize a semaphore at 0, and make on thread wait on it. When the thread that is being waited upon has finished, it can just increment the value of the semaphore ( post ) and this notifies the other thread that it can begin the execution.

27
Q

What is interprocess communication and what are the different types?

A

IPC is when processes communicate by exchanging data between separate address spaces.

  • Message passing
  • Synchronization
  • Shared Memory
28
Q

What are FIFOs?

A

Fifos ( named pipes ) are just like pipes except they allow communication between unrelated processes.

29
Q

What function is used for creating a fifo and what is the protype of this function?

A

Using the function mkfifo to create a mutex programatically

int mkfifo( char* path, mode_t mode );

path: this is the name of the fifo
mode: this is the permissions that are given to the fifo.

30
Q
  1. FIFOs can be opened for reading and writing. For the blocking versions.
    1. Opening a fifo for reading will cause the fifo to block until the same fifo is opened for writing
    2. Opening a fifo for writing will cause it to block until the fifo is opened for reading
  2. If the fifo is opened in a non blocking using the flag 0_NONBLOCK way then the fifo will fail only if it is opened for writing without being opened for reading.
  3. Closing a named semaphore does not remove it from the system. It remains in the system until it is rebooted.
  4. Name sems can be used for task ordering.
31
Q

Since there are multiple writers allowed for a FIFO then how is interleaving of writes prevented?

A

If there are multiple bytes then interleaving of bytes are ensured with the help of a constant called PIPE_BUF

This specifies the minimum number of bytes that will be written atomically.

Any less than the specified number will not be interleaved.

32
Q

What are named semaphores?

A

Named semaphores are posix semaphores used to synchronize threads and processes.

33
Q

How do you open an existing semaphore ( function prot )?

How do you open an existing semphore or creating one if it doesn’t exist?

A

sem_t* sem_open( const char* name, int flags );

sem_t sem_open(const char* name, int flags, mode_t mode, int value);

name: name of the new semaphore
oflag: set to O_CREAT for creating a new semaphore.
mode: permissions for the new semaphore
value: initial value for the new semaphore.

34
Q

How do you close a semaphore?

How do you remove a semaphore from the system?

A

sem_t* sem_close( sem_t* name )

you need to pass in the actual semaphore variable rather than just the name ( I think )

int sem_unlink ( char* name );

35
Q

Which is the fastest form of interprocess communication and why is this the fastest?

A

The fastest form of interprocess communication is shared memory because no data is actually coppied over.

36
Q

What happens in the shared memory ( type of IPC )?

What should you watch out for?

A

In this type of IPC, the processes write to a shared memory region which is visible to all the processes.

You need to make sure that access is synchronized to avoid data inconsistency

37
Q

What is Amdahl’s law?

A

Amdahl’s law says that a program has p parts which are parellelisable and ( 1 - p ) non-parellisable parts.