Lecture 06 - Threads Flashcards

1
Q

What does each thread get its own copy of?

A

Automatic variables (function variables)

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

What data do threads share?

A
  • Open file handles and sockets
  • Global variables
  • Memory allocated with malloc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give four reasons threads are useful

A
  • Increased responsiveness
  • Ease of communication between threads
  • Scalability
  • Handling asynchronous events
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do threads increase the responsiveness of a program?

A

There can be separate threads for IO while others do computation.

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

What library implements POSIX threads?

A

pthreads

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

What is the function used to create a new thread in C?

A

pthread_create()

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

What is the function prototype of pthread_create?

A
int pthread_create(
        pthread_t *restrict thread,
        const pthread_attr_t *restrict attr,
        void *(*start_routine)(void*),
        void *restrict arg);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are two ways for a thread to exit?

A
  • It returns from its start routine

- It calls pthread_exit

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

What is the function prototype for pthread_exit?

A

void pthread_exit(void *value_ptr);

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

What happens to threads when a process terminates?

A

They also terminate.

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

What function is used to get the current thread’s ID?

A

pthread_self

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

What is the function prototype for pthread_self?

A

pthread_t pthread_self(void);

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

What makes a function thread-safe?

A

When the correct result is produced in the face of simultaneous execution.

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

What gcc parameter is used to link the pthread library?

A

-pthread

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

What function is used to wait for a thread to terminate?

A

pthread_join

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

What does the pthread_join function do?

A

Waits for a thread to terminate and receives its return value.

17
Q

What is the function prototype for pthread_join?

A

int pthread_join(pthread_t thread, void **value_ptr);

18
Q

What function is used to request termination of another thread?

A

pthread_cancel

19
Q

What is the function prototype of pthread_cancel?

A

int pthread_cancel(pthread_t thread);

20
Q

What does the pthread_cancel function do?

A

Requests the termination of another thread.