Concurrency Flashcards

1
Q

What is multiprogramming?

A

It is the concurrent execution of multiple processes (tasks) on one CPU.

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

Does each task run knowing there are other tasks running with them?

A

No, each task runs as if they are the only task running on the CPU

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

What are the benefits of multiprogramming?

A

Increased efficiency: when one task is waiting for the I/O, another task can use the CPU so the processor can switch to another task.
The main goal of multiprogramming is to make use of the CPU time.

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

What does a Python web-server do in terms of multiprogramming?

A

It handles web requests and creates/generates jobs.

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

What does a multi-process C job-server do?

A

It processes the jobs in parallel. Then, workers in the job server compete to get jobs to process.

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

How does the web-server and job-server communicate?

A

They communicate through the file system.

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

What is the term when two processes attempt to get the same job?

A

Race condition

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

How many workers can get the job?

A

Only one.

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

Is it better to have two workers complete the same job or only one worker succeed in getting the job?

A

Only one worker getting and completing the job is better.

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

Describe the architecture of a web application.

A

From the web browser, the user submits a HTTP get request, which is sent to the web-server (Python). The created list of jobs (files) are sent to the dick, which passes the jobs to the job-server (C). The workers in the job-server will look for jobs (files) on the disk and process them.

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

If a worker finds a job that meets all the criteria, how does it secure the job so that no one else works on it?

A

The worker renames the filename by appending a “locked” to the front.

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

Can renaming fail? Describe what happens.

A

Yes. If worker 1 and 2 both found the same file and worker 1 finished renaming first. When worker 2 tries to rename it, it will fail. This is racing condition because multiple processes are trying to access the same resources.

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

Define race condition.

A

It is a situation where concurrent operations access data in a way where the output depends on the order of the operations’ execution. Which process will execute first is not guaranteed.

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

Why do we want to avoid racing condition?

A

Because it causes very nasty bugs in concurrent systems and is often overlooked by programmers assuming the order of execution does not affect the output.

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

Is racing condition a bug?

A

No, but it can cause bugs and/or unexpected behaviour. For example, in the web-app example, because ultimately the job is done, so two workers fighting to lock a file is not a bug but part of the process.

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

What is a process in OS-sense?

A

It is OS abstraction of what is needed to run a single program. It is also known as “heavyweight process” because we need pipes to communicate between processes.

17
Q

Describe the two parts of a process.

A

The passive part contains protected resources: code, data, files. The main memory state which is the contents of address space is also here. The I/O state are the file descriptors.

The active part contains: registers, PC, stack, and the thread. The thread represents some code executing.

18
Q

When we fork a process, what parts of the process are the same between the parent and child?

A

They have the same passive part. The active part differs depending on order of execution.

19
Q

What is a heavyweight process? What is a lightweight process?

A

A process is heavyweight. A thread is lightweight.

20
Q

What is multithreading?

A

It is a single program made up of different concurrent activities. In other words, one process with multiple threads where each thread is executing a different task. It is also called “multitasking”.

21
Q

Which part is shared across all thread? What about the other part?

A

The passive part is shared, so the content of memory (global variables, heap) and the I/O state (file descriptors, etc). Each thread has its own active part, so their own registers, stack, PC.

22
Q

Do threads still need pipes to exchange data?

A

No.

23
Q

Can racing condition still happen to threads? Give an example with I/O.

A

Yes. For example, if t1 and t2 both want to print to stdout, since I/O is shared and they are both accessing the same variable in RAM, this would be a racing condition.

24
Q

Process vs thread.

A

A process is a unit of ownership of resources. It owns all the threads under this process. It provides an execution context for the program. The resources it has include memory, I/O, console, etc. The process pretends like it is a single entity controlling the execution environment.

A thread is a single execution unit. It is a unit of scheduling. All threads of a process share the same memory space.

25
Q

What is IPC?

A

IPC stands for Inter Process Communication. It is mechanisms like pipes, shared memory, etc, used to communicate between processes. These are super fast but difficult to set up.

26
Q

How many threads are there in a process sharing how many physical CPUs? Describe the trend from the ancient time to now.

A

Ancient time: a process has one thread running on one CPU
Old days: a process had multiple threads running on one CPU
Now: a process has multiple threads running on multiple CPUs (multicore)

27
Q

How do we implement multithreading in code? Describe the brief flow.

A

Define the functions we want to execute concurrently.
Define one thread for each function with “pthread_t t1, t2, …;”
Start the concurrent execution by running “pthread_create(&t1, defaultAttribute, functionName, input );” for each thread.
The main thread waits for all the threads to finish by running “pthread_join(t1, defaultAttribute);” for each thread.

28
Q

If we define two threads t1 and t2, start the execution with pthread_create and wait for them to finish with pthread_join, how many threads do we have in total?

A
  1. There is always a main thread.
29
Q

Instead of using pthread_join() to wait for the other threads, what else can the main thread do?

A

It can sleep while waiting. Use sleep(time);

30
Q

When we run pthread_create(&t1, …) for n threads, how do we determine which thread runs first?

A

We cannot. Who runs first is dependent on the system.

31
Q

If there is a global variable in the RAM and two threads are both trying to access and change its value, but one thread changes the value and the system switches to the other thread to print the value which does not reflect the change, what is this interruption of tasks called? What is a potential solution?

A

Context switching. Do not declare a global variable, keep the variable local in the thread.

32
Q

What is CRC?

A

CRC stands for cyclic redundancy check. It is used to identify error in data.

33
Q

Describe the active part in detail.

A

The active part holds the resources and components that change over time as the process runs.

CPU context includes PC, registers. The execution state includes the current state of the process, e.g. waiting, running, ready. It also tracks what the process is doing at any moment.