Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

Similar documents
Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

Processes. Today. Next Time. Process concept Process model Implementing processes Multiprocessing once again. Scheduling processes

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Processes (Intro) Yannis Smaragdakis, U. Athens

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

Processes. CS3026 Operating Systems Lecture 05

Operating System Structure

4. The Abstraction: The Process

CS240: Programming in C

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Operating Systems. Lecture 05

CSE 153 Design of Operating Systems Fall 2018

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Roadmap. Tevfik Ko!ar. CSC Operating Systems Spring Lecture - III Processes. Louisiana State University. Virtual Machines Processes

Processes and Threads

CS 537 Lecture 2 - Processes

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

CS510 Operating System Foundations. Jonathan Walpole

1 Programs and Processes

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Fall 2015 COMP Operating Systems. Lab #3

Killing Zombies, Working, Sleeping, and Spawning Children

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Lecture 4: Process Management

518 Lecture Notes Week 3

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Chapter 3: Processes. Operating System Concepts 8th Edition,

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

csci3411: Operating Systems

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

Chap 4, 5: Process. Dongkun Shin, SKKU

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

COP 4610: Introduction to Operating Systems (Spring 2014) Chapter 3: Process. Zhi Wang Florida State University

System Programming. Process Control II

Mon Sep 17, 2007 Lecture 3: Process Management

Processes. Sanzheng Qiao. December, Department of Computing and Software

CS240: Programming in C

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

CSC 1600 Unix Processes. Goals of This Lecture

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Operating Systems. II. Processes

Processes. Process Concept

2 Processes. 2 Processes. 2 Processes. 2.1 The Process Model. 2.1 The Process Model PROCESSES OPERATING SYSTEMS

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Lecture 2: Processes. CSE 120: Principles of Opera9ng Systems. UC San Diego: Summer Session I, 2009 Frank Uyeda

Chapter 4: Processes. Process Concept

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Computer Systems Assignment 2: Fork and Threads Package

Chapter 3: Process Concept

Chapter 3: Process Concept

Chapter 4: Processes

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

CSE 380 Computer Operating Systems. Instructor: Insup Lee. University of Pennsylvania Fall 2003

Chapter 3: Processes

CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CS Lecture 2! Processes! George Mason University! Fall 2010!

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Chapter 3: Processes. Operating System Concepts Essentials 8 th Edition

Part Two - Process Management. Chapter 3: Processes

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission 1

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Processes. CS439: Principles of Computer Systems January 24, 2018

Operating Systems & Concurrency: Process Concepts

Process Control. Philipp Koehn. 23 April 2018

Chapter 3: Process Concept

Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

SE350: Operating Systems

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Frequently asked questions from the previous class survey

Processes. CS439: Principles of Computer Systems January 30, 2019

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

UNIX Processes. by Armin R. Mikler. 1: Introduction

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

CS 261 Fall Mike Lam, Professor. Processes

Interrupts, Fork, I/O Basics

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CS 550 Operating Systems Spring Process II

What is a Process? Processes and Process Management Details for running a program

Transcription:

Processes & Threads Today! Process concept! Process model! Implementing processes! Multiprocessing once again Next Time! More of the same J

The process model! Most computers can do more than one thing at a time Hard to keep track of multiple tasks How do you call each of them?! Process the OS s abstraction for execution A program in execution a.k.a. job, task! Simplest (classic) case a sequential process An address space abstraction of memory A single thread of execution abstraction of CPU Thread Address space 2

The process model! CPU switches back & forth among processes Pseudo-parallelism Four programs A B C D! Multiprogramming on a single CPU At any instant of time one CPU means one executing task, but over time Every processes as if having its own CPU! Process rate of execution not reproducible One PC A B C D Context switch 3

What s in a process! A process consists of (at least) An address space The code of the running program The data for the running program A thread state Execution stack and stack pointer Program counter A set of general purpose registers A set of OS resources including open files, network connections, Other process metadata (e.g. signal handlers)! I.e. all you need to run or restart a program if interrupted 4

Process creation! Principal events that cause process creation System initialization Execution of a process creation system User request to create a new process Initiation of a batch job! In all cases a process creates another one Running user process, system process or batch manager process! Process hierarchy UNIX calls this a "process group" No hierarchies in Windows - all created equal (parent does get a handle to child, but this can be transferred) 5

Process creation! Resource sharing Parent and children share all resources, a subset or none! Execution Parent and children execute concurrently or parent waits! Address space Child duplicate of parent or one of its own from the start! Unix example fork() system call creates new process; a clone of parent Both continue execution at the instruction after the fork execve replaces process memory space with new one Why two steps? 6

Process identifiers! Every process has a unique ID! Since it s unique sometimes used to guarantee uniqueness of other identifiers (tmpnam/tmpfile)! Special process IDs: 0 swapper, 1 init! Creating process in Unix fork pid_t fork(void); Call once, returns twice Returns 0 in child, pid in parent, -1 on error 7

Hierarchy of processes in Solaris! sched is first process! Its children pageout, fsflush, init! csh (pid = 7778), user logged using telnet! init pid = 1 pageout pid = 2 Sched pid = 0 fsflush pid = 3 inetd pid = 140 dtlogin pid = 251 telnetdaemon pid = 7776 xsession pid = 294 Csh pid = 7778 std_shel pid = 340 Firefox pid = 7785 xemacs pid = 8105 Csh pid = 1400 ls pid = 2123 cat pid = 2536 8

Process termination Conditions which terminate processes! Normal exit (voluntary) the job is done! Error exit (voluntary) oops, missing file?! Fatal error (involuntary) Referencing non-existing memory perhaps?! Killed by another process (involuntary) kill -9 Unix ways to terminate! Normal return from main, calling exit (or _exit)! Abnormal calling abort, terminated by a signal 9

Process states! Possible process states (in Unix run ps) New being created Ready waiting to get the processor Running being executed (how many at once?) Waiting waiting for some event to occur Terminated finished executing! Transitions between states new admitted interrupt exit terminated ready running dispatched I/O or event completion waiting I/O or event wait Which state is a process in most of the time? 10

Implementing processes! Current activity Program counter & content of processor s registers Stack temporary data including function parameters, return address, Data section global variables Heap dynamically allocated memory SP PC Stack (dynamic allocated mem) heap (dynamic allocated mem) Static data, global variables code Stack segment Data segment Code or text segment! OS uses a data struct to keep track of process state The Process Control Block When a process is unscheduled, state is transferred to PCB 11

Implementing processes! PCB: information associated with each process Process state: ready, waiting, Program counter: next instruction to execute CPU registers CPU scheduling information: e.g. priority Memory-management information Accounting information I/O status information! In Linux: defined in task_struct (include/linux/ sched.h) 12

Processes in xv6 enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE }; // Per-process state struct proc { char *mem; uint sz; char *kstack; enum procstate state; volatile int pid; struct proc *parent; struct trapframe *tf; struct context *context; void *chan; int killed; struct file *ofile[nofile]; struct inode *cwd; char name[16]; }; struct { struct spinlock lock; struct proc proc[nproc]; } ptable; // Start of process memory (kernel address) // Size of process memory (bytes) // Bottom of kernel stack for this process // Process state // Process ID // Parent process // Trap frame for current syscall // Switch here to run process // If non-zero, sleeping on chan // If non-zero, have been killed // Open files // Current directory // Process name (debugging) Statically-size process table 13

Switch between processes! When a process is running, hardware state is loaded on CPU and registers! When process is waiting, state is saved in the PCB! Switching a CPU between process: context switch ~5 microseconds Context switch! Choosing which process to run next scheduling 14

State queues! OS maintains a collection of queues that represent the state of processes in the system Typically one queue for each state PCBs are queued onto/move between state queues according to current/new state of the associated process Ready queue head tail Thunderbird (9764) pdigin (20309) Wait queue head tail evince (10012) term (9089) Firefox (28918)! There may be many wait queues, one for each type of wait (devices, timer, message, ) 15

PCB and state queues! PCB are data structures Dynamically allocated inside OS memory! When a process is created OS allocates and initializes a PCB for it OS places it on the correct queue! As process computes OS moves its PCB from queue to queue! When process terminates PCB may hang around for a while (exit code ) Eventually OS deallocates its PCB 16

Process creation in UNIX! Processes are created by existing processes! UNIX creation through fork()! Creates and initializes a new PCB Creates a new address space and initializes it with content of parent s Initializes kernel resources with those of the parent Places PCB in ready queue! fork() clone me 17

Process creation in UNIX #include <stdio.h> #include <sys/types.h> int main (int argc, char* argv[]) { int pid; int ppid = getpid(); } if ((pid = fork()) < 0){ perror("fork failed"); return 1; } else { if (pid == 0){ printf( I am %d the child of %d\n, getpid(), ppid); return 0; } else { /* parent */ printf( I am %d, the parent of %d\n, ppid, pid); return 0; } [fabianb@eleuthera tmp]$./creatone } I am 6647, the parent of 6648 I am 6648 the child of 6647 18

Process creation in UNIX #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main (void) { pid_t childpid, mypid; [fabianb@eleuthera tmp]$./badpid 4 Child 3948, ID = 3947 Parent 3947, ID = 3947 mypid = getpid(); childpid = fork(); if (childpid == -1) { perror("failed to fork\n"); return 1; } if (childpid == 0) /* child */ printf( Child %d, ID = %d\n, getpid(), mypid); else /* parent */ printf( Parent %d, ID = %d\n, getpid(), mypid); return 0; } 19

Process creation in UNIX + exec()!! Beyond cloning first fork, then exec! int execv(char *prog, char *argv[])! (a family of functions, front-ends for execve) Stops current process Loads prog into the address space (overwriting what s there) Initializes hardware content, args for new program Places PCB onto ready queue! To run a new program, then fork to create a child Child does an exec! Parent can wait for child to complete or not 20

Process creation in UNIX... } if ((pid = fork()) < 0) { perror( fork failed ); return 1; } else { if (pid == 0) { printf( Child before exec now the ls output\n ); execlp("/bin/ls", "ls", NULL); } else { wait(null); /* block parent until child terminates */ printf("child completed\n"); return 0; } } [fabianb@eleuthera tmp]$./creattwo Child before exec... now the ls output copy_shell creatone.c~ p3id skeleton copy_shell.tar creattwo p3id.c uwhich.tar creatone creattwo.c p3id.c~ creatone.c creattwo.c~ Child completed 21

Faster creation! The semantics of fork() says that the child s address space is a copy of the parent s! Expensive (i.e. slow) implementation Allocate physical memory for the new address space Copy one into the other Set up child s page tables to map to new address space! To make it faster 22

Faster creation version 1! Vfork() oldest approach, redefine the problem child address space is a copy of the parent s è child address space *is* the parent s Promise the child won t modify the address space before doing an exec Not enforced, use at your own peril Saves the effort of duplicating parent s address space when child is going to exec anyway 23

Faster creation version 2! Keep old semantic, but implement it differently Copy only what you need, on demand! COW copy on write Create new address space Initialize page tables to the same mappings as parent s and set both parents and child page tables to read-only If either parent or child tries to write page fault When a page fault occurs Allocate new physical page for child Copy content Mark entries as writable Restart process Page are copied only as needed 24

UNIX shells int main(int argc, char **argv) { while (1) { printf( % ); char *cmd = get_next_cmd(); int pid = fork(); if (pid == 0) { exec(cmd); panic( exec failed! ); } else { wait(pid); } } } 25

Summary! Today The process abstraction Its implementation How they are represented How the CPU is scheduled across processes Processes in Unix Perhaps the most important part of the class! Coming up Threads & synchronization 26