2.2 Thread PDF - Bahir Dar University OS(ITec2022)

Document Details

FastPacedWilliamsite8162

Uploaded by FastPacedWilliamsite8162

Bahir Dar University

2022

Bahir Dar University

Tags

operating systems threads computer science programming

Summary

This document from Bahir Dar University details threads in operating systems, covering concepts, usage, libraries, and implementation techniques. The document focuses on aspects of OS(ITec2022).

Full Transcript

2.2 Thread Bahir Dar University 9/1/2024 1 OS(ITec2022) Contents:  Thread concept  Thread usage  Thread library  Thread implementation Bahir Dar University 9/1/2024...

2.2 Thread Bahir Dar University 9/1/2024 1 OS(ITec2022) Contents:  Thread concept  Thread usage  Thread library  Thread implementation Bahir Dar University 9/1/2024 2 OS(ITec2022) Thread concept process model is based on two independent concepts: resource grouping and execution. One way of looking at a process is that it is a way to group related resources together. A process has an address space containing program text and data, as well as other resources. These resource may include open files, child processes, pending alarms, signal handlers, accounting information, and more. By putting them together in the form of a process, they can be managed more easily. The other concept a process has is a thread of execution, usually shortened to just thread. The thread has a program counter that keeps track of which instruction to execute next. Bahir Dar University 9/1/2024 3 OS(ITec2022) Thread concept(con’t..) It has registers, which hold its current working variables. It has a stack, which contains the execution history, with one frame for each procedure called but not yet returned from. Processes are used to group resources together; threads are the entities scheduled for execution on the CPU. The term multithreading is also used to describe the situation of allowing multiple threads in the same process. Bahir Dar University 9/1/2024 4 OS(ITec2022) Thread concept(con’t..) o A thread consists of: thread id program counter register set stack o Threads belonging to the same process share: its code its data section other OS resources Bahir Dar University 9/1/2024 5 OS(ITec2022) Processes and Threads Similarities Differences Both share CPU and only one Unlike processes, threads thread/process is active (running) are not independent of one at a time. another. Like processes, threads within a Unlike processes, all process execute sequentially. threads can access every address in the task. Like processes, thread can create children. Unlike processes, thread are design to assist one Like process, if one thread is other. blocked, another thread can run. Bahir Dar University 9/1/2024 6 OS(ITec2022) Thread usage o several reasons for having multiple threads: o many applications need multiple activities are going on at once.  decomposing such an application into multiple sequential threads that run in quasi-parallel, the programming model becomes simpler. o they are lighter weight than processes, they are easier (i.e., faster) to create and destroy than processes. o Having multiple threads within an application provide higher performance argument. If there is substantial computing and also substantial I/0, having threads allows these activities to overlap, thus speeding up the application. o Threads are useful on systems with multiple CPUs Bahir Dar University 9/1/2024 7 OS(ITec2022) Thread library o Thread libraries provide programmers an API to create and manage threads There are three basic libraries used: POSIX pthreads They may be provided as either a user or kernel library, as an extension to the POSIX standard Systems like Solaris, Linux and Mac OS X implement pthreads specifications WIN32 threads These are provided as a kernel-level library on Windows systems. Java threads » Since Java generally runs on a Java Virtual Machine, the implementation of threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads or Win32 threads depending on the system. Bahir Dar University 9/1/2024 8 OS(ITec2022) Thread implementation o There are two main ways to implement a threads package: in user space and in the kernel. Implementing Threads in User Space  All code and data structure are reside in user space.  Invoking a function in the library results in a local prodecuder call in user space not system call.  the kernel is not aware of the existence of threads. Advantage:  To do thread switching, it calls a run-time system procedure, which is least an order of magnitude-may be more-faster than trapping to the kernel  They allow each process to have its own customized scheduling algorithm. Disadvantage:  problem of how blocking system calls are implemented  Problem of page faults  no other thread in that process will ever run unless the first thread voluntarily gives up the CPU. Bahir Dar University 9/1/2024 9 OS(ITec2022) Thread implementation Implementing Threads in kernel Space o All code and data structure are reside in kernels pace. o Invoking a function in the library results system call. o the kernel is aware of the existence of threads. Advantage:  All calls that might block a thread are implemented as system calls  if one thread in a process causes a page fault, the kernel can easily check to see if the process has any other runnable threads, and if so, run one of them while waiting for the required page to be brought in from the disk. o kernel threads solve some While problems, they do not solve all problem o what happens when a multithreaded process forks?  In many cases, the best choice depends on what the process is planning to do next. Bahir Dar University 9/1/2024 10 OS(ITec2022) Implementing Threads in kernel Space(con’t..) o When a signal comes in, which thread should handle it?  Possibly threads could register their interest in certain signals but there may be two or more threads register for the same signal. Hybrid Implementations o use kernel-level threads and then multiplexes user-level threads onto some or all of the kernel threads. Bahir Dar University 9/1/2024 11 OS(ITec2022) Bahir Dar University 9/1/2024 12 OS(ITec2022)

Use Quizgecko on...
Browser
Browser