CSE 542: Operating Systems

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 5 Threads os5.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Threads CSCI 444/544 Operating Systems Fall 2008.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
I/O Systems ◦ Operating Systems ◦ CS550. Note:  Based on Operating Systems Concepts by Silberschatz, Galvin, and Gagne  Strongly recommended to read.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Threads, Thread management & Resource Management.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Department of Computer Science and Software Engineering
Threads, Thread management & Resource Management.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Chapter 4: Threads.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Processes and threads.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
Topic 3 (Textbook - Chapter 3) Processes
Operating System Concepts
Operating System (013022) Dr. H. Iwidat
Chapter 3 Threads and Multithreading
Chapter 4: Multithreaded Programming
Process Management Presented By Aditya Gupta Assistant Professor
Processes Overview: Process Concept Process Scheduling
Chapter 3: Process Concept
Operating Systems: A Modern Perspective, Chapter 6
Intro to Processes CSSE 332 Operating Systems
Chapter 4: Threads.
Threads & multithreading
Chapter 4: Threads.
Operating System Concepts
Applied Operating System Concepts
System Structure and Process Model
Chapter 4: Threads.
Chapter 4: Processes Process Concept Process Scheduling
Lecture 2: Processes Part 1
ICS 143 Principles of Operating Systems
Recap OS manages and arbitrates resources
CSE 542: Operating Systems
System Structure and Process Model
Application Interface
Lecture Topics: 11/1 General Operating System Concepts Processes
Process Description and Control
Threads Chapter 4.
Multithreaded Programming
CSE 542: Operating Systems
Process Description and Control
Chapter 4: Threads.
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
CSE 542: Operating Systems
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
CSE 153 Design of Operating Systems Winter 2019
CSE 542: Operating Systems
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 3: Process Concept
Chapter 4: Threads.
Presentation transcript:

CSE 542: Operating Systems Sunday, June 16, 2019 Review Chapter 4: Processes Chapter 5: Threads Goal: To have an concrete understanding on the process abstraction 16-Jun-19 CSE 542: Operating Systems

Little detour to explain the notion of process CSE 542: Operating Systems Sunday, June 16, 2019 Little detour to explain the notion of process Suppose you want to perform the following work from a machine. For now, ignore any notions of OS and stuff and focus on how to solve this problem read r1 read r2 compute r3= function(r1, r2) write r3 Assume that r1 is available on the hard disk, r2 is available on the serial port and r3 is written into a floppy disk 16-Jun-19 CSE 542: Operating Systems

What operations do we need for “read r1” CSE 542: Operating Systems Sunday, June 16, 2019 What operations do we need for “read r1” Allocate some memory to hold r1 (r1-op1) Reclamation: memory may be full Exclusivity: make sure that “someone” else doesn’t inadvertently share this memory Get the device ready to read r1 (r1-op2) Hard disk maybe “parked”, the disk arms may not be where they should be, how do you know where the arms should be (disk organization etc.) You seek to the right sector and cylinder Read from disk to memory (r1-op3) Ask the drive to read directly into the memory location for r1 Deal with error conditions and delays (r1-op4) Once the DMA controller tells you that read finished you are done 16-Jun-19 CSE 542: Operating Systems

What operations do we need for “read r2” CSE 542: Operating Systems Sunday, June 16, 2019 What operations do we need for “read r2” Allocate some memory to hold r2 (r2-op1) Reclamation: memory may be full Exclusivity: make sure that “someone” else doesn’t inadvertently share this memory Get the device ready to read r2 (r2-op2) Might be a modem Read from port to memory (r2-op3) Serial port controller may be polled or asynchronous Deal with error conditions and delays (r2-op4) 16-Jun-19 CSE 542: Operating Systems

What operations do we need for “r3=function(r1,r2)” CSE 542: Operating Systems Sunday, June 16, 2019 What operations do we need for “r3=function(r1,r2)” Allocate some memory to hold r3 (r3-op1) Reclamation: memory may be full Exclusivity: make sure that “someone” else doesn’t inadvertently share this memory Perform the function operation by passing r1 and r2 to function (call by value, call by reference etc.) (r3-op2) Function can be shared with others (example, sqrt() function) locally (math library) or remotely (sqrt server) You can write your own sqrt function Write function return value to memory r3 (r3-op3) Deal with error conditions (r3-op4) Math overflow error 16-Jun-19 CSE 542: Operating Systems

What operations do we need for “write r3” CSE 542: Operating Systems Sunday, June 16, 2019 What operations do we need for “write r3” Get the floppy device ready to write r3 (r4-op1) How do we know where to write in the floppy. You can’t randomly write into any sector Start the write process (r4-op2) Do we wait till the process finishes slowly? We don’t need to (asynchronous I/O) because we already have r3 in memory, if we need to use it again. But if we change r3 before it is written out, then the write semantics is broken (I that cast, we make a copy of r3 and write that copy) We can wait just to be sure Deal with error conditions and delays (r4-op4) General cleanup (r5-op1) 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Operation flow Start R2-op1 R3-op1 R4-op1 R1-op1 R1-op2 R2-op2 R3-op2 R4-op2 R1-op3 R2-op3 R3-op3 R4-op3 R5-op1 R1-op4 R2-op4 R3-op4 End 16-Jun-19 CSE 542: Operating Systems

What happens if you are not the only one in system CSE 542: Operating Systems Sunday, June 16, 2019 What happens if you are not the only one in system I don’t trust you to do red items right (malicious intent, fairness, security, protection) Start R2-op1 R3-op1 R4-op1 R1-op1 R1-op2 R2-op2 R3-op2 R4-op2 R1-op3 R2-op3 R3-op3 R4-op3 R5-op1 R1-op4 R2-op4 R3-op4 End I still don’t trust you for green items. At least you wouldn’t destroy the system 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Solution Put everything into a process abstraction so that the kernel can know what is allowed and what is not, how much resources to allocate how to charge you for resources consumed etc. Put red items inside OS kernel OS kernel basically does this part of what you want - items that you cannot trust users to use and allocate fairly by themselves Use system calls to communicate between processes and kernel (this context switch is not free) Put green items inside dynamic libraries, remote procedure calls (RPC/RMI) [Swizzling arguments, return values, pointers (-endian, word size etc.) 16-Jun-19 CSE 542: Operating Systems

What happens if you are not the only one in system CSE 542: Operating Systems Sunday, June 16, 2019 What happens if you are not the only one in system Start r1 allocated from local stack Call read() system call r2 allocated from local stack Call read() system call r3 allocated from local stack Call function() from library. system will load library or make RPC call Call write() system call R2-op1 R3-op1 R4-op1 R1-op1 R1-op2 R2-op2 R3-op2 R4-op2 R1-op3 R2-op3 R3-op3 R4-op3 R5-op1 R1-op4 R2-op4 R3-op4 End 16-Jun-19 CSE 542: Operating Systems

What do OS researchers do CSE 542: Operating Systems Sunday, June 16, 2019 What do OS researchers do Whether r1-op1 stays inside the kernel (you pay the cost to cross from user to kernel space [protected space] or you want performance (and can deal with issues that the kernel provides you for free) The more the OS hides, the more easier it is for the user and (usually) lesser performance Kernel functionality Monolithic kernel (Kernel provides more functionality) Micro kernel (Kernel provides less functionality) Nano kernel (Kernel provides even less functionality) 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems The fundamental problem is: does the OS do it for you or do you do it in user space? 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Process abstraction Process is a program in execution: It’s the job of the OS to provide the environment required by the application to execute fruitfully Program code Data section Execution context: Program counter, registers, stack Process has thread(s) of control Many processes “run” concurrently: Process scheduling Fair allocation of CPU and I/O bound processes Context switch Many users may run many tasks: Protect users from other users as well as from themselves. 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Process States 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Process Control Block Information associated with each process Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information 16-Jun-19 CSE 542: Operating Systems

Process context switch CSE 542: Operating Systems Sunday, June 16, 2019 Process context switch 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Process creation Creating new processes is expensive Resource allocation issue Fork mechanism: UNIX, Windows NT Duplicate the parent process Shares file descriptors, memory is copied Exec to create different process Various optimizations to avoid copying the entire parent context (Copy on write (COW), etc..) Exec mechanism: VMS, Windows NT New process is specifically loaded 16-Jun-19 CSE 542: Operating Systems

Interprocess communication CSE 542: Operating Systems Sunday, June 16, 2019 Interprocess communication Processes need to communicate with each other Naming Message-passing Direct (to process) or indirect (port, mailbox) Symmetric or asymmetric (blocking, nonblocking) Automatic or explicit buffering (capacity) Send by copy or reference Fixed size or variable size messages Shared memory/mutexes Remote Procedure Call (RPC/RMI) Bounded buffer problem Producer buffers, consumer takes from buffer - finite buffer 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 CPU scheduling Interleave processes so as to maximize utilization of CPU and I/O resources Scheduler should be fast as time spent in scheduler is wasted time Switching context (h/w assists – register windows [sparc]) Switching to user mode Jumping to proper location Preemptive scheduling: Context switch without waiting for application to relinquish Process could be in the middle of an operation Especially bad for kernel structures Non-preemptive (cooperative) scheduling: Can lead to Starvation 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Threads Applications require concurrency. Threads provide a neat abstraction to specify concurrency E.g. word processor application Needs to accept user input, display it on screen, spell check and grammar check Implicit: Write code that reads user input, displays/formats it on screen, calls spell checked etc. while making sure that interactive response does not suffer. May or may not leverage multiple processors Threads: Use threads to perform each task and communicate using queues and shared data structures Processes: expensive to create and do not share data structures and so explicitly passed 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Threaded application 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Threads - Benefits Responsiveness If one “task” takes too long, other “tasks” can still proceed Resource sharing: (No protection between threads) Grammar checker can check the buffer as it is being typed Economy: Process creation is expensive (spell checker) Utilization of multiprocessor architectures: If we had four processors (say), the word processor can fully leverage them Pitfalls: Shared data should be protected or results are undefined Race conditions, dead locks, starvation (more later) 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Thread types Continuum: Cost to create and ease of management User level threads (e.g. pthreads) Implemented as a library Fast to create Cannot have blocking system calls Scheduling conflicts between kernel and threads. User level threads cannot do anything is kernel preempts the process Kernel level threads Slower to create and manage Blocking system calls are no problem Most OS’s support these threads 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Threading models One to One model Map each user thread to one kernel thread Many to one model Map many user threads to a single kernel thread Cannot exploit multiprocessors Many to many Map m user threads to n kernel threads 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Threading Issues: Cancellation: Asynchronous or deferred cancellation Signal handling: which thread of a task should get it? Relevant thread Every thread Certain threads Specific thread Pooled threads (web server) Thread specific data 16-Jun-19 CSE 542: Operating Systems

Wizard ‘ps -cfLeP’ output CSE 542: Operating Systems Sunday, June 16, 2019 Wizard ‘ps -cfLeP’ output UID PID PPID LWP PSR NLWP CLS PRI STIME TTY LTIME CMD root 0 0 1 - 1 SYS 96 Aug 03 ? 0:01 sched root 1 0 1 - 1 TS 59 Aug 03 ? 7:12 /etc/init - root 2 0 1 - 1 SYS 98 Aug 03 ? 0:00 pageout root 3 0 1 - 1 SYS 60 Aug 03 ? 275:46 fsflush root 477 352 1 - 1 IA 59 Aug 03 ?? 0:0 /usr/openwin/bin/fbconsole -d :0 root 62 1 14 - 14 TS 59 Aug 04 ? 0:00 /usr/lib/sysevent/syseventd 16-Jun-19 CSE 542: Operating Systems

CSE 542: Operating Systems Sunday, June 16, 2019 Discussion Constant tension between moving functionality to upper layers; involving the application programmer and performing automatically at the lower layers Automatically create/manage threads by compiler/ system? (open research question) 16-Jun-19 CSE 542: Operating Systems