CS 3204 Operating Systems Lecture 5 Godmar Back.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

Processes and Schedulers. What is a Process Process: An execution stream and its associated state Execution Stream – Set of instructions – “Thread of.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
CS 140 Lecture Notes: Processes and ThreadsSlide 1 UNIX Fork/Exec Example int pid = fork(); if (pid == 0) { exec("foo"); } else { waitpid(pid, &status,
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
1 Processes Professor Jennifer Rexford
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
CS 3214 Computer Systems Lecture 13 Godmar Back.
CS 3214 Computer Systems Godmar Back Lecture 12. Announcements Exercise 6 coming up Project 3 milestone due Oct 8 CS 3214 Fall 2010.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
8-Sep Operating Systems Yasir Kiani. 8-Sep Agenda for Today Review of previous lecture Process scheduling concepts Process creation and termination.
CS333 Intro to Operating Systems Jonathan Walpole.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
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.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n Multithreading.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
1 OS Review Processes and Threads Chi Zhang
Notes on Processes, Context, Context Switching The following slides contain partially quoted statements from various Wikipedia pages.
Operating System Concepts
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Lecture 5 Page 1 CS 111 Online Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Chapter 4 – Thread Concepts
CS 3214 Introduction to Computer Systems
Multi-threading and Concurrency
Chapter 4 – Thread Concepts
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Threads.
CS 5204 Operating Systems Review Basic Concepts
Chapter 4: Multithreaded Programming
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
ICS 143 Principles of Operating Systems
Linux Fork/Exec Example
UNIX Fork/Exec Example
Processes in Unix, Linux, and Windows
Operating Systems Lecture 6.
Process & its States Lecture 5.
UNIX Fork/Exec Example
Tutorial: The Programming Interface
Jonathan Walpole Computer Science Portland State University
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
Linux Fork/Exec Example
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 4 Threads “Most modern applications are multithreaded”
Chapter 4: Threads.
Presentation transcript:

CS 3204 Operating Systems Lecture 5 Godmar Back

Announcements Group membership due tomorrow (Sep 10), 11:59pm Project 1 help session tonight, 6pm-8pm, Rand 211 Project 1 due on Sep 29, 11:59pm Assigned reading on lectures page Please follow submission instructions New UTA: Nick Ryan (10h/week) CS 3204 Fall 2008 4/21/2017

Processes & Threads (continued)

Overview Have discussed: Process States Priority Scheduling User vs Kernel Mode Context Switching Process States Priority Scheduling Process/Thread API Examples Fork/join model CS 3204 Fall 2008 4/21/2017

Process States Only 1 process (per CPU) can be in RUNNING state READY BLOCKED Process must wait for event Event arrived Scheduler picks process preempted Only 1 process (per CPU) can be in RUNNING state CS 3204 Fall 2008 4/21/2017

Process Events What’s an event? External event: disk controller completes sector transfer to memory network controller signals that new packet has been received clock has advanced to a predetermined time Events that arise from process interaction: a resource that was previously held by some process is now available (e.g., lock_release) an explicit signal is sent to a process (e.g., cond_signal) a process has exited or was killed a new process has been created CS 3204 Fall 2008 4/21/2017

Process Lists All ready processes are inserted in a “ready list” data structure Running process typically not kept on ready list Can implement as multiple (real) ready lists, e.g., one for each priority class All blocked processes are kept on lists List usually associated with event that caused blocking – usually one list per object that’s causing events Most of scheduling involves simple and clever ways of manipulating lists CS 3204 Fall 2008 4/21/2017

Priority Based Scheduling MAX Only threads with the highest priority run If more than one, round-robin 6 Higher Priority 3 2 MIN Done in Linux (pre 2.6.23), Windows, Pintos (after you complete Project 1), … CS 3204 Fall 2008 4/21/2017

Priority Based Scheduling (2) Advantage: Dead simple: the highest-priority process runs Q.: what is the complexity of finding which process that is? Disadvantage: Not fair: lower-priority processes will never run Hence, must adjust priorities somehow Many schedulers used in today’s general purpose and embedded OS work like this Only difference is how/whether priorities are adjusted to provide fairness and avoid starvation Exception: Linux “completely-fair scheduler” uses different scheme, will discuss that later in semester CS 3204 Fall 2008 4/21/2017

Reasons for Preemption Generally two: quantum expired or change in priorities Reason #1: A process of higher importance than the one that’s currently running has just become ready Reason #2: Time Slice (or Quantum) expired Question: what’s good about long vs. short time slices? CS 3204 Fall 2007 4/21/2017

I/O Bound vs CPU Bound Procs Processes that usually exhaust their quanta are said to be CPU bound Processes that frequently block for I/O are said to be I/O bound Q.: what are examples of each? What policy should a scheduler use to juggle the needs of both? CS 3204 Fall 2007 4/21/2017

Process States w/ Suspend Can be useful sometimes to suspend processes By user request: ^Z in Linux shell/job control By OS decision: swapping out entire processes (Solaris & Windows do that, Linux doesn’t) CS 3204 Fall 2007 4/21/2017

Windows XP Thread state diagram in a industrial kernel Source: Dave Probert, Windows Internals – Copyright Microsoft 2003 CS 3204 Fall 2007 4/21/2017

Windows XP Priority scheduler uses 32 priorities Scheduling class determines range in which priority are adjusted Source: Microsoft® Windows® Internals, Fourth Edition: Microsoft Windows Server™ CS 3204 Fall 2007 4/21/2017

Process Creation Two common paradigms: Cloning: (Unix) Cloning vs. spawning Cloning: (Unix) “fork()” clones current process child process then loads new program Spawning: (Windows, Pintos) “exec()” spawns a new process with new program Difference is whether creation of new process also involves a change in program CS 3204 Fall 2007 4/21/2017

fork() #include <sys/types.h> #include <unistd.h> #include <stdio.h> int main(int ac, char *av[]) { pid_t child = fork(); if (child < 0) perror(“fork”), exit(-1); if (child != 0) { printf ("I'm the parent %d, my child is %d\n", getpid(), child); wait(NULL); /* wait for child (“join”) */ } else { printf ("I'm the child %d, my parent is %d\n", getpid(), getppid()); execl("/bin/echo", "echo", "Hello, World", NULL); } CS 3204 Fall 2007 4/21/2017

Fork/Exec Model Fork(): Exec(): Advantage: simple, clean Clone most state of parent, including memory Inherit some state, e.g. file descriptors Important optimization: copy-on-write Some state is copied lazily Keeps program, changes process Exec(): Overlays current process with new executable Keeps process, changes program Advantage: simple, clean Disadvantage: does not optimize common case (fork followed by exec of child) CS 3204 Fall 2007 4/21/2017

The fork()/join() paradigm After fork(), parent & child execute in parallel Purpose: Launch activity that can be done in parallel & wait for its completion Or simply: launch another program and wait for its completion (shell does that) Pintos: Kernel threads: thread_create (no thread_join) exec(), you’ll do wait() in Project 2 Parent: fork() Parent process executes Child process executes Child process exits Parent: join() OS notifies CS 3204 Fall 2007 4/21/2017

CreateProcess() See also system(3) on Unix systems // Win32 BOOL CreateProcess( LPCTSTR lpApplicationName, LPTSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation ); See also system(3) on Unix systems Pintos exec() is CreateProcess(), not like Unix’s exec() CS 3204 Fall 2007 4/21/2017

Thread Creation APIs How are threads embedded in a language? POSIX Threads Standard (in C) pthread_create(), pthread_join() Uses function pointer Java/C# Thread.start(), Thread.join() Java: Using “Runnable” instance C#: Uses “ThreadStart” delegate C++ No standard has emerged as of yet see ISO C++ Strategic Plan for Multithreading CS 3204 Fall 2007 4/21/2017

Example pthread_create/join static void * test_single(void *arg) { // this function is executed by each thread, in parallel } /* Test the memory allocator with NTHREADS concurrent threads. */ pthread_t threads[NTHREADS]; int i; for (i = 0; i < NTHREADS; i++) if (pthread_create(threads + i, (const pthread_attr_t*)NULL, test_single, (void*)i) == -1) { printf("error creating pthread\n"); exit(-1); } /* Wait for threads to finish. */ pthread_join(threads[i], NULL); Use Default Attributes – could set stack addr/size here 2nd arg could receive exit status of thread CS 3204 Fall 2007 4/21/2017

Java Threads Example public class JavaThreads { public static void main(String []av) throws Exception { Thread [] t = new Thread[5]; for (int i = 0; i < t.length; i++) { final int tnum = i; Runnable runnable = new Runnable() { public void run() { System.out.println("Thread #"+tnum); } }; t[i] = new Thread(runnable); t[i].start(); for (int i = 0; i < t.length; i++) t[i].join(); System.out.println("all done"); Threads implements Runnable – could have subclassed Thread & overridden run() Thread.join() can throw InterruptedException – can be used to interrupt thread waiting to join via Thread.interrupt CS 3204 Fall 2007 4/21/2017

Why is taking C++ so long? Java didn’t – and got it wrong. Took years to fix What’s the problem? Compiler must know about concurrency to not reorder operations past implicit synchronization points See also Pintos Reference Guide A.3.5 Memory Barriers See Boehm [PLDI 2005]: Threads cannot be implemented as a library lock (&l); flag = true; unlock (&l); lock (&l); unlock (&l); flag = true; CS 3204 Fall 2007 4/21/2017