1. Home
  2. Explore
  3. Computer Science
  4. Concurrency in Operating Systems

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Concurrency in Operating Systems

  • 68Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Concurrency Basics

What does concurrency mean in programming, and what makes program components suitable for concurrent execution?

68 questions

  1. 1

    What does concurrency mean in programming, and what makes program components suitable for concurrent execution?

    Model answer

    Concurrency means doing multiple things at the same time. Program components are suitable when they can run independently without interfering with one another.

    Explanation

    The lecture describes concurrency as decomposing a program into order-independent chunks that can run at the same time.

  2. 2

    Why can concurrency improve operating-system efficiency when a process is waiting for input or output?

    Model answer

    While one process waits, another process can use the CPU. This makes better use of available processing time.

    Explanation

    The operating system can allocate CPU time to processes that are ready instead of leaving the CPU idle for a waiting process.

  3. 3

    What is a system call, and what typically happens to a process when it makes one?

    Model answer

    A system call requests a service from the operating system and transfers control to it. The process typically blocks until the request returns.

    Explanation

    The process pauses at the system call while the operating system performs the requested service.

  4. 4

    How does a C library function differ from a system call?

    Model answer

    A C library function performs a library-level operation, such as calculating a square root or formatting output, without requesting operating-system service in the way a system call does. A system call transfers control to the operating system and may block the process.

    Explanation

    The lecture contrasts ordinary library operations with requests such as reading or writing that require operating-system service.

  5. 5

    What is multiprogramming?

    Model answer

    Multiprogramming is an arrangement in which more than one process can be ready to execute. A process that blocks, such as while waiting for input or output, can hand off control so another process can run.

    Explanation

    In the lecture’s description, processes can give up execution when they cannot proceed, allowing another process to run.

  6. 6

    What is a context switch?

    Model answer

    A context switch is the change from running one process to running another.

    Explanation

    The operating system uses context switches to transfer execution between processes.

  7. 7

    How does timesharing create the appearance that multiple processes are running at once?

    Model answer

    Timesharing divides CPU time among multiple processes, giving each a portion of time to execute. Rapid switching makes them appear to run simultaneously.

    Explanation

    The CPU allocates time among processes even when they have not necessarily blocked.

  8. 8

    How does multiprocessing differ from multiprogramming?

    Model answer

    Multiprocessing executes multiple processes at the same time, often using multiple CPU cores. Multiprogramming allows multiple processes to be ready and switches between them, including when one blocks.

    Explanation

    The key distinction is actual simultaneous execution versus sharing execution through switching.

  9. 9

    How can a multicore CPU combine multiprocessing with multiprogramming and timesharing?

    Model answer

    Each core can execute a process or thread, and each core can also switch among work through multiprogramming or timesharing. This allows a system to use multiple cores while sharing each core’s time.

    Explanation

    The lecture notes that each core can itself be multiprogrammed and timeshared.

  10. 10

    What role can hyper-threading play in a CPU?

    Model answer

    Hyper-threading can make a core appear capable of handling additional processes, creating the effect of more processing capacity per core.

    Explanation

    The lecture describes hyper-threading as a technology that can make a couple more processes appear available per core.

  11. 11

    What is a race condition, and why can it be difficult to detect?

    Model answer

    A race condition is a flaw in which the result depends unexpectedly on the relative timing of events. It can be hard to detect because it may require a rare combination of timing and circumstances.

    Explanation

    A program may pass ordinary tests even though an unusual execution order can produce an incorrect result.

  12. 12

    How can concurrent access to shared data create a race condition?

    Model answer

    If multiple processes access the same resource at overlapping times, their operations can interleave in an unintended way. This can cause incorrect results or damage to the data.

    Explanation

    The lecture identifies simultaneous access to a shared resource as a source of race conditions.

  13. 13

    Explain how two concurrent ATM withdrawals can cause an account balance error.

    Model answer

    Both processes may read the same starting balance, subtract the withdrawal amount independently, and then write back the same reduced balance. One withdrawal is effectively lost from the recorded balance.

    Explanation

    For example, both ATMs could read a balance of 100,subtract100, subtract 100,subtract20, and each write back 80,eventhough80, even though 80,eventhough40 was withdrawn.

  14. 14

    Why does a race condition depend on the relative timing of events rather than only on the individual operations?

    Model answer

    The operations may be valid individually, but their interleaving can determine whether the final result is correct or unexpected. The timing and order of access to shared state therefore matter.

    Explanation

    The defining feature is the unexpected dependence of the output on event timing.

  15. 15

    What safety issue arose from the Therac-25’s reliance on software interlocks?

    Model answer

    The system could be outrun by operators who entered information faster than expected, causing it to skip errors. With hardware interlocks removed and safety depending on software interlocks, patients received dangerous radiation doses.

    Explanation

    The lecture reports that this failure caused three deaths and serious injuries to three other people.

  16. 16

    What general access-control rule should be used when a concurrent process may write to a shared resource?

    Model answer

    While one process is writing to the resource, no other process should access it for either reading or writing until the write is complete.

    Explanation

    Preventing overlapping access protects shared data from inconsistent updates.

  17. 17

    How does a lock provide access control for a shared resource?

    Model answer

    A lock restricts access to the process that owns it. Other processes must wait or be denied access until the lock is released.

    Explanation

    The lecture presents a lock as a general way to ensure that only one process accesses a resource at a time.

  18. 18

    How can a lock-file approach indicate that a resource is already locked?

    Model answer

    A process attempts to create a known lock file in a way that fails if the file already exists. If creation fails because the file exists, the process treats the resource as locked and can wait before trying again.

    Explanation

    The example repeatedly attempts to create the file and sleeps before retrying when another process has already created it.

  19. 19

    What is the kernel, and what are some of its responsibilities?

    Model answer

    The kernel is the central part of the operating system. It contains fundamental routines and hardware-abstraction support, and it provides the scheduler.

    Explanation

    Processes request kernel services through system calls rather than interacting with the kernel directly.

  20. 20

    What does the operating-system scheduler do?

    Model answer

    The scheduler allocates CPU time among processes or threads according to its scheduling policy, which may account for priorities or fairness.

    Explanation

    The scheduler determines which eligible work runs at a given time.

  21. 21

    Why are threads useful for a program such as a chat server?

    Model answer

    A chat server may need to handle user connections, receive messages, and send output. Threads let these different lines of work proceed concurrently even though a single execution line can do only one thing at a time.

    Explanation

    Separating work into threads helps keep a live, interactive program running while different tasks are handled.

  22. 22

    What is a thread?

    Model answer

    A thread is an organized line of execution within a process. A process can contain multiple threads that perform different work.

    Explanation

    The scheduler can control which thread runs at a given time.

  23. 23

    Which resources do threads in the same process share?

    Model answer

    Threads share the program’s code, heap, and data such as global variables and static variables.

    Explanation

    Because these resources are shared, threads can communicate through common program data.

  24. 24

    What execution state is private to each thread, and why?

    Model answer

    Each thread has its own stack so it can track function return locations and its automatic variables as it executes.

    Explanation

    Separate stacks support each thread’s independent line of execution.

  25. 25

    Can one thread access another thread’s stack variables?

    Model answer

    Yes. The lecture states that threads can access the stacks of other threads, including their automatic variables.

    Explanation

    This access is possible because threads within a process share an address space, though sharing can create risks.

  26. 26

    Why can switching between threads be faster than switching between processes?

    Model answer

    Threads share much of their process’s memory and other resources, so switching between them does not require unloading and reloading all that shared state.

    Explanation

    The lecture attributes faster thread switching to the shared code, heap, and data.

  27. 27

    How can threads provide true simultaneous execution on a multicore system?

    Model answer

    Kernel-level threads can be scheduled independently on separate CPU cores, allowing them to execute at the same time.

    Explanation

    This differs from merely taking turns on a single core.

  28. 28

    Why can sharing among threads be both useful and risky?

    Model answer

    Sharing code and memory makes communication simpler, but it can expose sensitive data and allow race conditions or resource conflicts.

    Explanation

    For example, threads may share global data even when some of that data should be restricted.

  29. 29

    What is resource contention in concurrent programs?

    Model answer

    Resource contention occurs when multiple processes or threads need the same resource at the same time. One may have to wait, such as when a system call prevents concurrent access to a file.

    Explanation

    Shared files and other resources can limit what concurrent work can proceed.

  30. 30

    How can sharing a process’s memory create a security concern between threads?

    Model answer

    A thread may be able to access sensitive data used by another thread, such as social security numbers held by a security-related component. This can expose information to parts of the program that should not use it.

    Explanation

    Threads in the same process share data, so the program must consider access to sensitive resources.

  31. 31

    How do kernel-level threads differ from user-level threads in their relationship to the scheduler?

    Model answer

    The kernel knows about kernel-level threads and schedules them. User-level threads are managed within a process and are not individually known to the kernel scheduler.

    Explanation

    The operating system sees a process running when that process uses user-level threads.

  32. 32

    How is switching between user-level threads typically controlled?

    Model answer

    Switching is cooperative and occurs when the program chooses to run a different line of execution.

    Explanation

    The program can move between tasks without the kernel scheduler controlling each user-level thread.

  33. 33

    Why might a program use user-level threads for a recursive system?

    Model answer

    User-level threads can be used to organize and massively parallelize a system with a specialized, heavily recursive objective.

    Explanation

    The lecture identifies this kind of specialized recursion as a primary use for user-level threads.

  34. 34

    Why are user-level threads sometimes called green threads?

    Model answer

    They are sometimes called green threads because they do not occupy kernel time as independently scheduled threads.

    Explanation

    The kernel does not manage them as separate threads.

  35. 35

    What is a key disadvantage of user-level threads when one makes a blocking system call?

    Model answer

    A blocking system call can block the entire process, including all of its user-level threads.

    Explanation

    The kernel sees the process as a whole rather than scheduling its user-level threads independently.

  36. 36

    How can preemption affect a process that contains user-level threads?

    Model answer

    The scheduler can preempt and unload the entire process, so all of its user-level threads stop running until the process is scheduled again.

    Explanation

    User-level threads are not individually scheduled by the kernel.

  37. 37

    Compare kernel-level and user-level thread switching in terms of speed and complexity.

    Model answer

    User-level thread switching can be faster because it can amount to switching between parts of the program. However, user-level threads are more difficult to use, while kernel-level threads are supported by built-in UNIX libraries and are generally the preferred choice in the lecture.

    Explanation

    Kernel threads require scheduler involvement, but they avoid some limitations of user-level threads.

  38. 38

    What is the POSIX threads API, and why is it used?

    Model answer

    The POSIX threads API is a standard interface for creating multithreaded applications on UNIX-like systems. POSIX standards help different UNIX-like operating systems provide compatible interfaces.

    Explanation

    The lecture identifies POSIX threads, or pthreads, as the classic standard approach on UNIX.

  39. 39

    What must a C program include and link to use POSIX thread functions?

    Model answer

    It includes the pthread.h header and links the pthread library when compiling, using the appropriate pthread compiler or linker option.

    Explanation

    The header provides declarations, while linking supplies the pthread function implementations.

  40. 40

    What does pthread_create do?

    Model answer

    It creates a new thread and specifies the thread’s ID storage, attributes, starting routine, and argument.

    Explanation

    The new thread begins executing the function provided as its start routine.

  41. 41

    What is the purpose of the first argument to pthread_create?

    Model answer

    It points to a variable in which the new thread’s ID will be stored.

    Explanation

    The ID can later be used to identify or join that thread.

  42. 42

    What is the purpose of the attributes argument to pthread_create?

    Model answer

    It points to a structure containing thread-option flags. If no special options are needed, the usual practice described is to pass no attributes.

    Explanation

    The attributes parameter configures the thread when options are required.

  43. 43

    What does the start-routine argument to pthread_create specify?

    Model answer

    It specifies the function where the new thread begins execution, and it is passed as a function pointer.

    Explanation

    The new thread starts running that routine after it is created.

  44. 44

    What is the purpose and limitation of the argument passed to a thread’s start routine?

    Model answer

    It passes data to the start routine, but it is a single argument. To pass multiple values, the program can place them in a structure and pass a pointer to that structure.

    Explanation

    The pthread start routine accepts one argument, commonly represented as a void *.

  45. 45

    Why are thread IDs not necessarily simple, human-readable integers?

    Model answer

    A thread ID may be represented by a structure rather than a plain integer, and it is not intended to contain conveniently printable data.

    Explanation

    Programs use thread-ID functions, rather than assuming IDs can be compared or displayed as ordinary integers.

  46. 46

    How can a thread obtain and compare its own ID with another thread’s ID?

    Model answer

    A thread can obtain its own ID using pthread_self and compare thread IDs using the appropriate pthread comparison function.

    Explanation

    These functions allow a program to identify which thread is executing without relying on the ID’s internal representation.

  47. 47

    In a thread routine that accepts a void * argument but expects an integer, how can the integer be recovered?

    Model answer

    Cast the argument to an integer pointer and dereference it to retrieve the integer value.

    Explanation

    The cast tells the program how to interpret the pointer before accessing the value it points to.

  48. 48

    What is the purpose of passing the address of an element in an argument array to a thread?

    Model answer

    It gives the thread routine a pointer to the particular value intended for that thread. The routine can then interpret and read that value.

    Explanation

    In the example, each thread receives the address of its own integer argument.

  49. 49

    What does an assertion checking that a thread-creation result is zero accomplish?

    Model answer

    It verifies that thread creation succeeded according to the expected result. If the check fails, the assertion stops the program.

    Explanation

    The lecture presents assertions as a concise way to check a condition that could also be tested with an if statement.

  50. 50

    Name four ways a thread can terminate.

    Model answer

    A thread can call pthread_exit, finish by returning from or reaching the end of its routine, be canceled by another thread, or be terminated when a thread calls exit and ends the whole process.

    Explanation

    The first three end an individual thread; exit ends the process and therefore all its threads.

  51. 51

    What does pthread_exit do?

    Model answer

    It lets the calling thread end its own execution.

    Explanation

    The thread stops running at the point where it calls the function.

  52. 52

    What happens when a thread reaches the end of its start routine?

    Model answer

    The thread terminates when it exits from the bottom of its routine, unless its execution continues, for example, in a loop.

    Explanation

    The routine’s completion ends that thread’s line of execution.

  53. 53

    How can one thread terminate another thread?

    Model answer

    A thread can request cancellation of another thread using pthread_cancel.

    Explanation

    The lecture lists external cancellation as one way a thread can be stopped.

  54. 54

    What is the effect of calling exit from any thread in a process?

    Model answer

    It terminates the entire process, so all of the process’s threads terminate.

    Explanation

    exit is process-wide rather than a way to end only the calling thread.

  55. 55

    What does pthread_join do, and why is it useful?

    Model answer

    pthread_join makes the calling thread wait until a specified thread terminates. It lets a program coordinate its work with the completion of other threads.

    Explanation

    In the example, the main thread joins each worker before reporting that all threads are complete.

  56. 56

    In a program that creates five worker threads and then joins them in sequence, what determines the order in which the main thread proceeds past the joins?

    Model answer

    The main thread waits for each thread in the order specified by the join loop. A thread that has not finished delays the main thread from proceeding past its join.

    Explanation

    The workers may finish in a different order, but sequential joins determine when the main thread moves through the list.

  57. 57

    Why may output from concurrently running threads appear in an unpredictable order?

    Model answer

    The operating system determines when the threads run, so their output can interleave in different orders depending on the system’s state and scheduling.

    Explanation

    Creating threads one by one does not guarantee that they will report in that same order.

  58. 58

    What is the difference between returning from main and calling exit at the end of a program?

    Model answer

    Returning from main returns the program’s status through the normal return mechanism. Calling exit terminates the process directly and supplies an exit status, so using it at the end instead of returning is considered poor style in the lecture.

    Explanation

    The lecture recommends returning from main when possible, while noting that exit can be useful elsewhere in a program.

  59. 59

    What is a mutex, and what does its name mean?

    Model answer

    A mutex is a mutual-exclusion mechanism that uses a lock to synchronize access to data or resources.

    Explanation

    Mutex is short for “mutual exclusion,” and pthreads provides mutex operations.

  60. 60

    How does a mutex differ from a lock file in the examples?

    Model answer

    A lock file represents a lock using a file, while a mutex is a lock associated with a variable that can protect data or resources.

    Explanation

    Both are forms of access control, but they use different mechanisms.

  61. 61

    How is a pthread mutex initialized and destroyed?

    Model answer

    A mutex can be initialized with PTHREAD_MUTEX_INITIALIZER and later destroyed with the mutex-destroy function.

    Explanation

    Initialization makes the mutex available for use; destruction ends its usable lifetime.

  62. 62

    What happens when a thread locks a mutex that is already locked?

    Model answer

    The locking attempt blocks until the mutex becomes available. This also applies if the attempt comes from the thread that already holds the lock.

    Explanation

    A lock cannot be acquired again while it remains locked.

  63. 63

    Who should unlock a mutex, and what happens when it is unlocked?

    Model answer

    The thread that locked the mutex should unlock it. Unlocking makes the mutex available for another locking attempt.

    Explanation

    Releasing the lock allows blocked contenders to resume when they are selected to proceed.

  64. 64

    What does pthread_mutex_trylock allow a program to do?

    Model answer

    It lets a thread attempt to acquire a mutex without blocking and receive a result indicating whether the lock was available.

    Explanation

    This allows the program to check the lock’s status and decide how to proceed.

  65. 65

    What happens when one thread holds a mutex and another thread tries to lock it?

    Model answer

    The second thread blocks while the first thread continues. When the first thread unlocks the mutex, a waiting thread can resume.

    Explanation

    The mutex prevents the waiting thread from entering the protected section while the lock is held.

  66. 66

    What is a semaphore, and how is a mutex related to it?

    Model answer

    A semaphore is a synchronization mechanism, and a mutex is a specific form of semaphore.

    Explanation

    The lecture describes mutexes as a particular semaphore type used for mutual exclusion.

  67. 67

    How does a counting semaphore differ from a binary semaphore?

    Model answer

    A counting semaphore tracks a quantity of available resources, such as buffers, open files, or connections. A binary semaphore has only two states, such as locked and unlocked.

    Explanation

    Counting semaphores represent a resource count, while binary semaphores represent a two-state condition.

  68. 68

    Who developed the semaphore concept, and what historical point does the lecture make about it?

    Model answer

    Edsger Dijkstra developed the concept around 1962 or 1963. Semaphores and mutexes have therefore been used as concurrency concepts for many decades.

    Explanation

    The lecture emphasizes that these synchronization ideas predate modern multicore systems, even though they are especially useful on them.

1 of 68
Question 1 of 68

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent