CSC 322 Operating Systems Concepts Lecture - 8: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,

Slides:



Advertisements
Similar presentations
1 Processes and Threads Creation and Termination States Usage Implementations.
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Threads, SMP, and Microkernels
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Chapter 3 Process Description and Control
1 Implementations: User-level Kernel-level User-level threads package each u.process defines its own thread policies! flexible mgt, scheduling etc…kernel.
Chapter 2 Processes and Threads
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Mutual Exclusion.
Interprocess Communication
Computer Systems/Operating Systems - Class 8
EEE 435 Principles of Operating Systems Interprocess Communication Pt I (Modern Operating Systems 2.3)
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
CSC 322 Operating Systems Concepts Lecture - 15: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Processes and Threads.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
CSC 322 Operating Systems Concepts Lecture - 25: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
Threads G.Anuradha (Reference : William Stallings)
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Operating System Concepts
操作系统原理 OPERATING SYSTEM Chapter 2 Processes and Threads 进程与线程.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Interprocess Communication Race Conditions
CS 6560: Operating Systems Design
CSC 322 Operating Systems Concepts Lecture - 16: by
Lecture 11: Mutual Exclusion
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Implementing Threads in User Space
Thread Implementations; MUTEX
Process Description and Control
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Thread Implementations; MUTEX
Lecture 11: Mutual Exclusion
CS510 Operating System Foundations
CS333 Intro to Operating Systems
Presentation transcript:

CSC 322 Operating Systems Concepts Lecture - 8: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. (Chapter-1) Ahmed Mumtaz Mustehsan, CIIT, Islamabad

No run-time system is needed Also, there is no thread table in each process. Threads do not require any new, non-blocking system calls. Context switching to threads is still possible in case of blocking system calls or event of page fault. Advantages; Implementing Threads in kernel space Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad2

In addition to process table, kernel maintains thread- table, to keep track of all the threads in the system. To create a new or destroy existing thread, it makes a kernel call, which does the creation or destruction by updating the kernel thread table. All calls that might block a thread are implemented as system calls. If thread blocks, kernel just picks another one Not necessarily from same process! To save time recycling of threads is possible. Expensive to manage the threads in the kernel and takes valuable kernel space Implementing threads in kernel space Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad3

kernel threads do not solve all problems! What happens when a multithreaded process forks (creates a child process)? Should child have the same number of threads? Signals are sent to processes, not to threads. When a signal comes in, which thread should handle it? The thread that register that? What if it clashes with another thread? Do Kernel Level Threads solve all the Problems? Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad4

How do we get the advantages of both approaches, without the disadvantages? Multiplex user-level threads onto kernel level threads Programmer determines how many user level and how many kernel level threads to use. The threads expected to issue blocking system call could be made kernel level thread Kernel is aware of kernel threads only User level threads are scheduled, created destroyed independently by RTS without the involvement of kernel. Implementing Threads; Hybrid approach. Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad5

Hybrid approach Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad6

kernel threads are better but are indisputably slower. The scheduler activation mimic the functionality of kernel threads, in user space. No special non-blocking system calls or check safe calls required. The kernel assigns a certain number of virtual processors to each process and lets the (user-space) run-time system allocate threads to processors. Mechanism can also be used on a multiprocessor where the virtual processors may be real CPUs. The user-space run-time system can block the synchronizing thread and schedule a new one by itself. Scheduler activations-Upcalls Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad7

Virtual processors allocation could be initially one, but the process can dynamically request and release. When any thread executes a blocking system call the control traps to the kernel. Kernel notifies the run time system that a thread has blocked passes info to RTS (thread id…)and start RTS RTS can start any thread from ready state. Up-call violates the fundamental principal of Layered System Scheduler activations (upcalls) Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad8

How to handle message arrivals in distributed systems? Could use thread which blocks on a receive system call and processes messages when they arrive. Restore the history of the thread each time a message arrives and execute thread. Instead of restoring thread create a new thread to handle message. The newly created thread is called Pop ups threads. Pop up thread does not requires restoring history and hence faster Popup Threads Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad9

Create a new thread when a message arrives Pop-Up Threads Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad10

Many existing programs were written for single-threaded processes. Converting these to multithreading is tricky and pose the following problems: How do we implement variables which should be global to a thread but not to the entire program? Example: errno, errno Thread wants access to a file, Unix grants access via global errno, thread -1 and thread-2 uses errno Race condition cause thread-1 to get the wrong permission because thread-2 over-writes it. Making Single Threaded Code Multithreaded Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad11

Thread 1 gets the wrong permission Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad12 Solutions: Prohibit global variables altogether; But it conflicts with existing software. Assign each thread its own private global variables.

Solution-Create private global variables Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad13

New library procedures can be introduced to create, set, and read these thread wide global variables. Create_global ("bufptr"); set_global ("bufptr", &buf); bufptr = read_global("bufptr"); Global is then unique to each thread How? Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad14

Library routine which is not reentrant. (Not designed to call again when previous call is incomplete) Example: Sending a message; one thread puts message in a buffer, context switch takes place, new thread appears and over writes message. Memory allocation programs may be (temporarily) in an inconsistent state when context switching takes place. malloc Example: malloc is busy updating, link list to maintain memory chunks, may tem­porarily be in an inconsistent state, with pointers that point nowhere while another thread access that! Single thread to Multithread; More problems Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad15

How do we implement signals which are thread specific ? If threads are in user space, kernel can’t address right thread to pass the signal. Keyboard signals are not thread specific! Who should catch that? One thread? All threads? Or newly created popup thread? What will happen when one thread wants to catch signal through (Ctrl- C) and other wants to terminate the process using (Ctrl- C)? Signals are difficult to handle in a single thread! More complex handling them with multithread environment. Same issue with stack dynamic growth. Single thread to Multithread; More problems Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad16

MORAL OF THE STORY Need to rework the semantics of system calls and library routines are to be rewritten in order to add threads to a system Must be done in such a way as to remain backward compatible with exist­ing programs Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad17

Three problems How to actually do it How to deal with process conflicts (2 airline reservations for same seat) How to do correct sequencing when dependencies are present. Aim the gun before firing it SAME ISSUES FOR THREADS AS FOR PROCESSES SAME SOLUTIONS AS WELL Inter-process Communication Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad18

In In is local variable containing pointer to next free slot Out Out is local variable pointing to next file to be printed Race Conditions Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad19

Mutual exclusion: only one process at a time can use a shared variable/file Critical Regions: Define shared memory which leads to race conditions Solution: Ensure that two processes can’t be in the Critical Region at the same time How to avoid Races Conditions Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad20

1.No two processes may be simultaneously inside their critical regions. 2. No assumptions may be made about speeds or the number of CPUs. 3. No process running outside its critical region may block other processes to enter critical region. 4. No process should have to wait forever to enter its critical region. Properties of a good solution Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad21

What we are trying to do Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad22

Examine various proposals for achieving mutual exclusion, so that while one process is busy updating shared memory in its critical region, no other process will enter the critical region: Disabling interrupts Lock variables Strict alternation Peterson's solution The TSL instruction Solutions based upon Busy Waiting Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad23

Idea: Process disables interrupts, enters Critical Region, enables interrupts when it leaves Critical Region Problems Process DI then might never enable interrupts, crashing system Won’t work on multicore chips as Disabling Interrupts only effects one CPU at a time Disabling Interrupts Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad24

A software solution everyone shares a lock When lock is 0, process turns it to 1 and enters Critical Region When exit Critical Region, turn lock to 0 Problem: Race condition. How? Lock variables Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad25

Strict Alternation Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad26 A proposed solution to the critical region problem. a)Process 0. b)Process I. In both cases, be sure to note the semicolons terminating the while statements.

Employs busy waiting; while waiting for the Critical Region, a process spins. If one process is outside the Critical Region and it is its turn, then other process has to wait until outside process finishes both outside AND inside (Critical Region ) work Problems with strict alternation Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad27

Problems with strict alternation Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad28 Critical Region Process 0Process 1

. Peterson's Solution Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad29

Process 0 & 1 try to get in simultaneously Last one in sets turn: say it is process 1 Process 0 enters (turn= = process is False) Peterson Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad30

TSL reads lock into register and stores NON ZERO VALUE in lock (e.g. process number) Instruction is atomic: done by freezing access to bus line (bus disable) TSL Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad31

Using TSL Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad32 TSL reads lock into register and stores NON ZERO VALUE in lock (e.g. process number) TSL Instruction is atomic: done by freezing access to bus line (bus disable)

XCHG instruction xchg a,b exchanges a & b Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad33

Both Peterson's solution and the solutions using TSL or XCHG are correct, but both requiring busy waiting. A process to enter critical region, checks if allowed, enters CR otherwise sits in a tight loop waiting until allowed to enter CR. Problem: Wastage of CPU resource. Priority Inversion Problem. Solution: Replace busy waiting by blocking system calls Sleep blocks process Wakeup unblocks process What’s wrong with Peterson, TSL,XCHG? Lecture -8Ahmed Mumtaz Mustehsan, CIIT, Islamabad34