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

Speaker 1Speaker 2

Concurrency in Operating Systems

12:18
0:00-12:18
  1. Michael

    Welcome. Today we’re talking about concurrency: an informal term for doing multiple things at the same time. In programming, that means breaking work into parts that can run independently, without relying on a particular order or interfering with one another.

  2. Anna

    And UNIX systems are built to handle multiple processes. More than one process can be running, including multiple copies of the same program, and the operating system shares CPU time among them.

  3. Michael

    That sharing is useful because processes often have to wait. If one is waiting for input or output, another can use the CPU instead. The operating system can distribute its available processing time among whichever processes need it.

  4. Anna

    Before we compare the ways a system creates that sense of simultaneous activity, let’s distinguish two things that can look alike in C: a system call and a library function.

  5. Michael

    A system call is a request from a running process for the operating system to do something. Control passes to the operating system, and the process may become blocked—stalled at that point until the request returns. Reading or writing data are typical examples, because the process needs the operating system to handle an input-output operation.

  6. Anna

    A C library function, by contrast, is a function provided by a library for the program to use. The lecture’s examples are calculating a square root or formatting output for printing. Both kinds of operations can appear as functions in C, but a system call hands control to the operating system, while a library function is a different kind of service.

  7. Michael

    One way to organize multiple processes is multiprogramming. More than one process can be ready to execute, and when one has to wait—often because it is blocked on input or output—the operating system can switch to another.

  8. Anna

    That change from one process to another is a context switch. The blocked process can’t continue until its system call returns, so handing CPU time to another process lets the system keep making progress.

  9. Michael

    Timesharing creates a related illusion in a different way: the CPU divides its time among processes, giving each a turn. A user may feel that many programs are running at once, even though each process is getting only a portion of the CPU’s time.

  10. Anna

    So multiprogramming can let a process hand off the CPU when it can’t proceed, while timesharing divides CPU time among processes whether or not they are waiting. And multiprocessing is the stronger contrast: multiple processes really do execute at the same time.

  11. Michael

    Modern CPUs make multiprocessing possible with multiple cores. Each core can act like a small CPU, and each can itself use multiprogramming and timesharing. The lecture also mentions hyper-threading as a technology that can make a core appear to handle additional processes.

  12. Anna

    Concurrency is powerful, but it creates complications when processes share data or resources. Sharing can raise security concerns if one process should not be able to access another’s sensitive information. And when multiple processes access the same resource, their timing can produce a race condition.

  13. Michael

    Here’s the classic bank-account example. Two ATMs both read a balance of one hundred dollars. Each subtracts twenty from its own copy, then writes back eighty dollars. The customer has withdrawn forty dollars, but the account has only been reduced by twenty.

  14. Anna

    The problem is that the result depends on the relative timing of events: both processes read the old balance before either one’s update takes effect. That matches the lecture’s definition of a race condition—a flaw where the output depends unexpectedly and critically on how events are timed.

  15. Michael

    Race conditions can be hard to find because the particular timing that triggers one may happen only rarely. A program can appear to work in testing and still fail under a special combination of circumstances.

  16. Anna

    The lecture also describes the Therac-25 radiation therapy machine as a devastating example. Its designers relied on software interlocks after removing hardware interlocks, and the system could be outrun as operators entered information more quickly. Errors could be skipped over, and the lecture reports that three people died and three others were seriously injured.

  17. Michael

    The practical lesson is access control. If multiple processes may access a resource and at least one may write to it, the processes need a way to prevent conflicting access. In the approach discussed here, while one process is writing, no other process should read or write that resource until the update is complete.

  18. Anna

    A lock provides that kind of control: the process that owns the lock is the one allowed to access the resource. One simple example is a lock file. A process tries to create a file at a known path; if it succeeds, it treats that as permission to proceed. If the file already exists, another process may hold the lock, so the process waits briefly and tries again.

  19. Michael

    There can also be a serious error, in which case the program should stop trying and report or handle the failure. The details depend on how the file is opened, but the key idea is that the file’s creation acts as a shared signal to other processes.

  20. Anna

    Now let’s define the kernel. It’s the central part of the operating system, where fundamental routines and hardware-abstraction layers help the operating system communicate with hardware through drivers.

  21. Michael

    A major kernel responsibility is the scheduler. Processes don’t usually interact with the kernel directly; they make system calls asking it to do things. The scheduler then distributes CPU time according to its priorities and scheduling rules.

  22. Anna

    That brings us to threads. Imagine a chat server that must watch for users connecting, receive messages, and send messages to users. A single line of execution can only do one of those things at a time, so threads let a program organize separate lines of execution for different kinds of work.

  23. Michael

    Threads have useful sharing properties. They share the program’s code, heap, global variables, and static data, which makes communication between threads simpler than communication between separate processes.

  24. Anna

    Each thread has its own stack, because it needs its own function-return information and automatic variables. But threads in the same process can access one another’s stacks too. That sharing can help threads work together, but it also means they must be careful with shared data.

  25. Michael

    Switching between threads can be faster than switching between processes because the threads share so much of their program and memory. And on a multicore CPU, kernel-level threads can run on separate cores, which means they can genuinely execute at the same time.

  26. Anna

    The same sharing creates risks. Threads can have race conditions, contend for resources, or expose sensitive information to other threads in the process. If a thread is using a file, for example, another thread may not be able to use it at the same time.

  27. Michael

    There are two broad types to know: kernel-level threads and user-level threads. Kernel-level threads are known to the kernel and scheduled by it. They’re the kind people usually mean when they say a program splits its work into threads.

  28. Anna

    User-level threads, sometimes called green threads, are managed inside the process and aren’t individually visible to the kernel scheduler. The program switches between them cooperatively, while the kernel sees the process as a whole.

  29. Michael

    Switching between user-level threads can be very fast, because it resembles choosing a different part of the program to run. But there are trade-offs: the scheduler can pre-empt the entire process, and a blocking system call can block the process containing all those user-level threads. The lecture describes user-level threads as uncommon and kernel-level threads as the usual, generally preferable choice.

  30. Anna

    On UNIX-like systems, a common way to create kernel-level threads is the POSIX threads API, usually called Pthreads. POSIX provides a standard interface across UNIX-like systems. In C, you include the pthread header and link the pthread library when compiling.

  31. Michael

    The central creation function is pthread_create, which takes four arguments. First is a place to store the new thread’s ID. Second is an attribute structure, usually set to no special options. Third is a function pointer naming the thread’s starting routine. Fourth is a single argument to pass to that routine.

  32. Anna

    That thread ID may be stored in a structure rather than a simple, readable integer. And because pthread_create accepts only one argument for the starting routine, you can package several values together in a structure and pass a pointer to it.

  33. Michael

    A typical starting routine accepts a void pointer and returns a void pointer. That lets the routine receive and return different kinds of values, but the program must agree on what those values mean. If you pass an integer through a void pointer, for example, the routine has to interpret it as the intended type.

  34. Anna

    In the lecture’s example, perform_work receives an argument, interprets it as an integer, and prints a message with that number. The main function creates five threads and gives them the values zero through four, so each thread can report which argument it received.

  35. Michael

    The example checks whether each pthread_create call succeeded. It also illustrates function pointers: you pass the name of the starting function, without parentheses, because you’re giving pthread_create the function to run rather than calling it immediately.

  36. Anna

    A thread can end by calling pthread_exit, by reaching the end of its starting routine, or by being cancelled by another thread with pthread_cancel. And if any thread calls exit, the whole process ends, so all its threads end too.

  37. Michael

    To identify the currently running thread, a thread can call pthread_self. To compare thread IDs, use the Pthreads comparison functions rather than assuming the IDs are simple integers you can interpret directly.

  38. Anna

    The five-thread example also shows why pthread_join matters. The main thread calls join for each created thread, which makes it wait until that particular thread has finished before continuing past that join.

  39. Michael

    The threads’ messages can appear in different orders because the operating system determines when they run. But if main joins them in order from the first thread ID to the fifth, it waits for each one in that order, even if some later thread has already finished.

  40. Anna

    Once all the joins have completed, main can report that all the threads are done. The example ends by calling exit with EXIT_SUCCESS, but the lecture points out that this is poor style at the end of main: returning from main is preferable, while exit is useful when a program needs to terminate from elsewhere.

  41. Michael

    The final topic is mutexes, short for mutual exclusion. A mutex is a lock for coordinating access among threads. The API gives you the lock mechanism, but your program has to decide where to use it to protect shared data or resources.

  42. Anna

    A mutex begins with initialization, which can use PTHREAD_MUTEX_INITIALIZER, and it can be cleaned up with pthread_mutex_destroy. To acquire it, a thread calls pthread_mutex_lock. If another thread already holds it, the caller waits rather than continuing into the protected section.

  43. Michael

    When the thread holding the mutex is done, it calls pthread_mutex_unlock. That releases the lock so a waiting thread can proceed. The lecture also describes pthread_mutex_trylock, which lets a thread check whether it can acquire the mutex without simply waiting for it.

  44. Anna

    Mutexes connect to the broader idea of semaphores. A counting semaphore tracks a quantity of available resources, such as buffers, open files, or connections. A binary semaphore has two states; for a mutex, those states correspond to locked and unlocked.

  45. Michael

    The lecture credits Edsger Dijkstra with inventing semaphores around 1962 or 1963. These ideas have been around for decades, and they’re especially important now that multicore systems make high-speed parallel programming a practical part of everyday software.

  46. Anna

    So the big picture is that concurrency can keep systems productive and responsive, but shared resources make coordination essential. Understanding processes, threads, scheduling, race conditions, and locks gives us the tools to reason about what can run at once—and how to keep that work safe.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent