Prévia do material em texto
PROCESSES AND THREADS Prof. Dr. Everton Cavalcante https://www.docente.ufrn.br/everton.cavalcante https://www.docente.ufrn.br/everton.cavalcante GOALS • To present the concepts of processes and threads from the perspective of concurrent programming • To point out the differences between processes and threads • To discuss the benefits and the problems from using threads 2 THE CONCEPT OF PROGRAM A program is a sequence of statements in source code or machine code 3 PROCESS It is a program in execution. A process is the unit of work in a modern computing system. Abraham Silberschatz, Peter Baer Galvin, Greg Gagne. Operating system concepts, 10th ed. United Kingdom: John Wiley & Sons, Inc., 2018. 4 THE CONCEPT OF PROCESS (1/9) A given program can be instantiated in one or more processes 5 Program X Process A Process B Process N ... THE CONCEPT OF PROCESS (2/9) Multiple chrome.exe processes in Task Manager 6 https://superuser.com/questions/174652/multiple-chrome-exe-processes-in-task-manager THE CONCEPT OF PROCESS (3/9) Main events that cause the creation of a process • System initialization • Execution of system call to create processes by a running process (fork in Unix-based systems) o Process hierarchy (parent process, child process) 7 THE CONCEPT OF PROCESS (4/9) •The execution of a process encompasses resource allocation (CPU time, memory, files, input/output devices, etc.) for its task to be executed •Each process receives an identifier (PID) and an address space, being controlled by the operating system during its existence •Concurrent processes compete for the available resources and are scheduled by the operating system 8 THE CONCEPT OF PROCESS (5/9) Loading a process into memory results in assigning it to an address space • Every process has its own virtual address space, and the operating system maps it to the physical address space in memory • The process’ address space comprises o Code (text), the compiled program code o Data with global variables allocated and initialized before execution o Heap, used for dynamic memory allocation o Stack with local variables 9 THE CONCEPT OF PROCESS (6/9) Events that cause a process to terminate 10 Voluntary termination • Normal termination when the process finishes its task • Termination due to the occurrence of an error Involuntary termination • Fatal error, usually a programming one • Cancellation by a request from other process to the operating system THE CONCEPT OF PROCESS (7/9) States of a process 11 Andrew S. Tanembaum, Hebert Bos. Modern operating systems – 5th ed. USA: Pearson, 2023 THE CONCEPT OF PROCESS (8/9) •Despite process representing an important building block in operating systems, operations involving processes (creation, termination, communications) are relatively costly •Context switch performed by the operating system o Storing the state (context) of the process o Later restoration of the process’ context o Transfer of control from one process to another 12 THE CONCEPT OF PROCESS (9/9) 13 Context switch between processes THE CONCEPT OF THREAD (1/4) A thread is an independent execution flow of a program within a process • A thread is also known as a lightweight process • Threads can be executed simultaneously, either concurrently or in parallel 14 THE CONCEPT OF THREAD (2/4) Traditional processes versus multithreaded processes • A traditional process has at least a single thread that controls the resources held by the process • When a process has more than one thread (i.e., it is multithreaded) o more than one thread may be simultaneously active in the process o threads share resources held by the process o one of the threads starts running when the process is scheduled for execution 15 THE CONCEPT OF THREAD (3/4) 16 Traditional processes versus multithreaded processes Process A Process B single thread multithread call(routine x) detour return single execution flow three concurrent execution flows create(threads x and y) Routine x main thread thread ythread x THE CONCEPT OF THREAD (4/4) 17 Traditional processes versus multithreaded processes one process | one thread one process | multiple threads multiple processes | one thread per process multiple processes | multiple threads per process PROCESSES VERSUS THREADS (1/3) •Threads within a process are typically invisible from outside it •A thread within a process is scheduled and runs independently from other threads o Depending on the number of processors available in the system, different threads may run in different processors o In single-processor computers, threads must have their execution interleaved by context switching •Threads within a given process share the same address space and hence the same data o Concurrent among threads may exist 18 PROCESSES VERSUS THREADS (2/3) •Communication among threads within a process takes place through shared memory •Protected address space in processes o Grouping resources as a process eases their management •Threads receive an amount of CPU time (time slice or quantum) at which they perform their task 19 PROCESSES VERSUS THREADS (3/3) •All threads have access to globally shared data •Threads also have their own private data •The responsible for synchronizing access to shared data is the programmer 20 THE LIFECYCLE OF THREADS (1/4) A thread starts its lifecycle at the state and remains in that state until the program starts it, putting it at the state running to perform its task 21 new running wait time wait terminated program starts the thread ti m e e x p ir e s a w a it s le e p THE LIFECYCLE OF THREADS (2/4) A thread may go to a wait state while waiting for another thread to perform a task. Once at that state, the thread returns to the state running only when another thread signals the waiting thread to resume its execution. 22 new running wait time wait terminated program starts the thread ti m e e x p ir e s a w a it s le e p THE LIFECYCLE OF THREADS (3/4) A thread may go to a synchronized (time) wait for a given time interval. A thread in that state returns to the state running when the time interval expires or when the event is waiting to happen. 23 new running wait time wait terminated program starts the thread ti m e e x p ir e s a w a it s le e p THE LIFECYCLE OF THREADS (4/4) A running thread goes to the state terminated when it completes its task, otherwise it is finished. 24 new running wait time wait terminated program starts the thread ti m e e x p ir e s a w a it s le e p THREAD PRIORITY AND SCHEDULING •Each thread has a priority that supports the operating system in determining the order in which threads should be scheduled • Informally, threads with higher priority are more important in a program and hence they must be assigned to the CPU time before threads with lower priority o Thread priority may not guarantee the order in which threads are executed 25 26 The need for using a process is clear since a process is a program in execution. So why divide the execution flow of a process into threads? WHY THREADS? BENEFITS FROM USING THREADS (1/4) Simplification of concurrent programming • Reduction of the problem’s complexity: divide and conquer • Representation of the tasks to be performed as distinct lines of execution control 27 BENEFITS FROM USING THREADS (2/4) Throughput (processing capacity) • Gains in terms of performance since multithreaded systems can better exploit the machine’s processing capabilities o Example: a thread waits for the response from an input/output request while another thread executes another task • Threads allow performing operations in background • When compared to the cost of creating and managing processes,a thread can be created with much less overhead at the operating system level • Managing threads requires fewer system resources than managing processes due to resource sharing 28 BENEFITS FROM USING THREADS (3/4) Servers • Server programs in client-server applications may receive multiple requests simultaneously • If the server program has a single thread, then requests are handled sequentially o In case of many requests, such a behavior makes clients to wait • Multithreaded servers can handle requests faster o Multiple requests are handled simultaneously o Independent scheduling of each request 29 BENEFITS FROM USING THREADS (4/4) An example: loading videos on the Internet • Users may not want to wait for loading all the video to start playing it • It is possible to use multiple threads: a thread loads the video, another plays it o Tasks executed concurrently 30 BENEFITS FROM USING THREADS (?) 31 WHY THREADS MAY BE NOT GOOD (1/5) Comparison between programming with multiple threads and events (single-threaded) Keynote handouts 32 Keynote handouts WHY THREADS MAY BE NOT GOOD (2/5) Problems with using threads • Challenges to programming o The programmer is responsible for ensuring mutual exclusion o Possibility of deadlocks • Challenges to debugging o Challenges to reproducing execution conditions o Non-determinism 33 WHY THREADS MAY BE NOT GOOD (3/5) Problems with using threads • Challenges to synchronization • Challenges to obtaining an actually good performance o Explicit mutual exclusion mechanisms reduce the potential of concurrency o Mutual exclusion mechanisms increase complexity and may reduce performance o Not every program can benefit from using threads 34 WHY THREADS MAY BE NOT GOOD (4/5) 35 Md. Aminul Haque Chowdhury, S. M. Saif Shams, Ki-Hyung Kim, Seung-wha Yoo. An empirical study of I/O bounded processes and threads in operating system. Proceedings of the Third International Conference on Covergence and Hybrid Information Technology. USA: IEEE, 2008, pp. 712-717 WHY THREADS MAY BE NOT GOOD (5/5) Problems with using threads • Breaking the idea of abstraction and modularity o It is not possible to design modules in an independent way • Relatively low portability o Different implementations o Dependency on the programming language 36 37 That is the question! TO USE, OR NOT TO USE THREADS? 37 38 Pushing the Limits of Windows: Processes and Threads (Microsoft Tech Community) https://techcommunity.microsoft.com/t5/windows-blog-archive/pushing-the-limits-of-windows-processes-and-threads/ba-p/723824 PROCESSES AND THREADS Prof. Dr. Everton Cavalcante https://www.docente.ufrn.br/everton.cavalcante https://www.docente.ufrn.br/everton.cavalcante Slide 1: PROCESSES AND THREADS Slide 2: GOALS Slide 3: The concept of program Slide 4: PROCESS Slide 5: The concept of process (1/9) Slide 6: The concept of process (2/9) Slide 7: The concept of process (3/9) Slide 8: The concept of process (4/9) Slide 9: The concept of process (5/9) Slide 10: The concept of process (6/9) Slide 11: The concept of process (7/9) Slide 12: THE CONCEPT of PROCESS (8/9) Slide 13: THE CONCEPT OF PROCESS (9/9) Slide 14: THE CONCEPT of THREAD (1/4) Slide 15: THE CONCEPT of THREAD (2/4) Slide 16: THE CONCEPT of THREAD (3/4) Slide 17: THE CONCEPT of THREAD (4/4) Slide 18: PROCESSES VERSUS THREADS (1/3) Slide 19: PROCESSES VERSUS THREADS (2/3) Slide 20: PROCESSES VERSUS THREADS (3/3) Slide 21: THE LIFECYCLE OF THREADS (1/4) Slide 22: THE LIFECYCLE OF THREADS (2/4) Slide 23: THE LIFECYCLE OF THREADS (3/4) Slide 24: THE LIFECYCLE OF THREADS (4/4) Slide 25: THREAD PRIORITY AND SCHEDULING Slide 26: WHY THREADS? Slide 27: BENEFITS FROM USING THREADS (1/4) Slide 28: BENEFITS FROM USING THREADS (2/4) Slide 29: BENEFITS FROM USING THREADS (3/4) Slide 30: BENEFITS FROM USING THREADS (4/4) Slide 31: BENEFITS FROM USING THREADS (?) Slide 32: Why Threads MAY BE NOT GOOD (1/5) Slide 33: Why Threads MAY BE NOT GOOD (2/5) Slide 34: Why Threads MAY BE NOT GOOD (3/5) Slide 35: Why Threads MAY BE NOT GOOD (4/5) Slide 36: Why Threads MAY BE NOT GOOD (5/5) Slide 37: To use, or not to use threads? Slide 38 Slide 39: PROCESSES AND THREADS