Concurrency Flashcards
What is multiprogramming?
It is the concurrent execution of multiple processes (tasks) on one CPU.
Does each task run knowing there are other tasks running with them?
No, each task runs as if they are the only task running on the CPU
What are the benefits of multiprogramming?
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.
What does a Python web-server do in terms of multiprogramming?
It handles web requests and creates/generates jobs.
What does a multi-process C job-server do?
It processes the jobs in parallel. Then, workers in the job server compete to get jobs to process.
How does the web-server and job-server communicate?
They communicate through the file system.
What is the term when two processes attempt to get the same job?
Race condition
How many workers can get the job?
Only one.
Is it better to have two workers complete the same job or only one worker succeed in getting the job?
Only one worker getting and completing the job is better.
Describe the architecture of a web application.
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.
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?
The worker renames the filename by appending a “locked” to the front.
Can renaming fail? Describe what happens.
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.
Define race condition.
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.
Why do we want to avoid racing condition?
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.
Is racing condition a bug?
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.