Processes, Threads, and Concurrency PDF
Document Details
Uploaded by LawAbidingAgate9551
University at Buffalo
2024
Ethan Blanton, Carl Alphonce, & Eric Mikida
Tags
Summary
These lecture notes describe processes, threads, and concurrency. They explain concepts such as logical control flows, multitasking, and multiprocessing. The document is geared towards an undergraduate computer science course.
Full Transcript
Processes, Threads, and Concurrency CSE 220: Systems Programming Ethan Blanton, Carl Alphonce, & Eric Mikida Department of Computer Science and Engineering University at Buffalo Introduction Concurrency Processes Threads Summary References L...
Processes, Threads, and Concurrency CSE 220: Systems Programming Ethan Blanton, Carl Alphonce, & Eric Mikida Department of Computer Science and Engineering University at Buffalo Introduction Concurrency Processes Threads Summary References Logical Control Flows Computer Systems: A Programmer’s Perspective defines a logical control flow as: [A] series of program counter values that [correspond] exclusively to instructions contained in [a program’s] executable object file or in shared objects linked to [it] dynamically at run time. The system provides each program with the illusion that its logical control flow runs on a dedicated computer. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 2 Introduction Concurrency Processes Threads Summary References Concurrency Concurrency is when more than one logical control flow is present in the system at the same time. Concurrent flows are logical control flows whose execution overlap in time. Concurrent flows can be present even with only one processor. Multiple flows can coexist on one processor via multitasking. Multitasking time slices between multiple logical control flows. Each flow runs for a brief period of time, then is interrupted A context switch changes control to another flow The new flow runs for a brief period of time (repeat) © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 3 Introduction Concurrency Processes Threads Summary References The Process Our fundamental logical control flow abstraction is the process. A process encapsulates: A set of instructions The memory they use The system resources they access … All interactions with other processes are through the OS. This is due to the dedicated computer model. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 4 Introduction Concurrency Processes Threads Summary References Threads Threads provide a conceptually similar abstraction to processes. Threads also represent a logical control flow. However: One process may have multiple threads Two threads within one process are much less isolated than two processes, or threads in different processes In particular, threads within a process share a memory map. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 5 Introduction Concurrency Processes Threads Summary References Lecture Question Ask a review question! © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 6 Introduction Concurrency Processes Threads Summary References Multitasking and Multiprocessing Multitasking Multiprocessing A B C X Y Z Time © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 7 Introduction Concurrency Processes Threads Summary References Multitasking Concurrent flows in a multitasking environment do not execute simultaneously. However, from the point of view of any given flow, other flows are making progress while it executes. Consider: Process A is executing at PC location L A context switch occurs, removing A from the CPU and switching to Process B Process B does something A context switch occurs, switching to Process A at location L Process A will observe progress in Process B before and after L. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 8 Introduction Concurrency Processes Threads Summary References Multiprocessing Concurrent flows in a multiprocessing environment may execute simultaneously. Even with multiprocessing, multitasking may also be used. This is typical for modern systems. The operating system provides the illusion of a dedicated machine even to processes running simultaneously. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 9 Introduction Concurrency Processes Threads Summary References Concurrency and Separation Concurrent flows may be related or unrelated in: Design Implementation Memory space Resource requirements Timing requirements … When concurrent flows are completely unrelated, the dedicated computer abstraction provided by modern systems is both mostly complete and very appropriate. When they are more related, it gets more complicated. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 10 Introduction Concurrency Processes Threads Summary References Motivation for Concurrency There are many reasons to use concurrent flows: Making computational progress while blocked on a slow device Achieving rapid response to a particular condition (e.g., human input, external event) Utilizing multiple physical processors … In addition, simply taking advantage of the dedicated computer model to simplify design and implementation. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 11 Introduction Concurrency Processes Threads Summary References Processes We have already seen process-level concurrency. (Consider the chat client and server!) Multiple processes may: Proceed independently on unrelated tasks Proceed independently on related tasks Cooperate on tasks © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 12 Introduction Concurrency Processes Threads Summary References Designing for Multiple Processes A multi-process design can be robust and reliable. The isolation in memory and resources provided by the system protects processes from certain faults in their neighbors. Communication and cooperation can be expensive, though: Separate memory spaces protect, but also divide Many inter-process communication (IPC) mechanisms require interaction with the OS, which is slow Most modern web browsers use multiple processes for this reason! © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 13 Introduction Concurrency Processes Threads Summary References Lecture Question Do a lecture question! © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 14 Introduction Concurrency Processes Threads Summary References Threads Thread are like processes that share almost everything. They: Share memory Share system resources (such as open files) Run the same executable code … Switching between threads is often less expensive than processes in a multitasking system. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 15 Introduction Concurrency Processes Threads Summary References Threads vs. Processes Processes Threads P1 P2 P3 Kernel Kernel Kernel Stack Stack T1 Stack T2 Stack Heap Heap Heap BSS BSS BSS Data Data Data © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 16 Introduction Concurrency Processes Threads Summary References Threading Advantages Threads are much cheaper than processes: They share memory maps They share permissions and operating system resources Context switches between two threads in the same process are much less involved than between processes Inter-thread communication is trivial, due to shared memory. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 17 Introduction Concurrency Processes Threads Summary References Threading Disadvantages Concurrent access to shared resources is very tricky. Many established APIs are not thread-safe. (Over the next few lectures, think about a thread-safe malloc()!1 ) Breaking down the dedicated computer model makes reasoning about process behavior harder. 1 But you are not required to implement one! © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 18 Introduction Concurrency Processes Threads Summary References Threading Use Cases Threading is often appropriate for tasks which require: Very rapid change of control between parallel tasks Lots of large, shared data structures Blocking operations that do not inhibit other progress More rapid computation than can be performed on a single CPU Multiple processes may also solve some of these problems. The costs of threading must be weighed against its advantages on a case-by-case basis. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 19 Introduction Concurrency Processes Threads Summary References Summary Logical control flows are execution steps through programs. Concurrency is multiple logical control flows at one time. Multiprocessing versus Multitasking Processes versus Threads © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 20 Introduction Concurrency Processes Threads Summary References Next Time … Races and Synchronization © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 21 Introduction Concurrency Processes Threads Summary References References I Required Readings Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau. Operating Systems: Three Easy Pieces. Chapter 26: Intro, 26.1. Arpaci-Dusseau Books. URL: https://pages.cs.wisc.edu/~remzi/OSTEP/. Optional Readings Randal E. Bryant and David R. O’Hallaron. Computer Science: A Programmer’s Perspective. Third Edition. Chapter 8: 8.2; Chapter 12: Intro, 12.1, 12.3. Pearson, 2016. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 22 Introduction Concurrency Processes Threads Summary References License Copyright 2019–2024 Ethan Blanton, All Rights Reserved. Copyright 2024–2024 Eric Mikida, All Rights Reserved. Copyright 2022–2024 Carl Alphonce, All Rights Reserved. Copyright 2019 Karthik Dantu, All Rights Reserved. Reproduction of this material without written consent of the author is prohibited. To retrieve a copy of this material, or related materials, see https://www.cse.buffalo.edu/~eblanton/. © 2024 Ethan Blanton, Carl Alphonce, & Eric Mikida / CSE 220: Systems Programming 23