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

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 & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

Processes (Intro) Yannis Smaragdakis, U. Athens

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

4. The Abstraction: The Process

Processes and Threads

CS 537 Lecture 2 - Processes

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

1 Programs and Processes

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

Fall 2015 COMP Operating Systems. Lab #3

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

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

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

Operating Systems. Lecture 05

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

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

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

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

CSC 1600 Unix Processes. Goals of This Lecture

Operating System Structure

518 Lecture Notes Week 3

csci3411: Operating Systems

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Lecture 4: Process Management

CSCE 313: Intro to Computer Systems

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

3. Process Management in xv6

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

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

Lesson 2. process id = 1000 text data i = 5 pid = 1200

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

A process. the stack

CS3210: Isolation Mechanisms

Process management 1

Processes. Johan Montelius KTH

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

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

CS240: Programming in C

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

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

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

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

Processes. CS3026 Operating Systems Lecture 05

CSE 153 Design of Operating Systems Fall 2018

Interrupts, Fork, I/O Basics

Chap 4, 5: Process. Dongkun Shin, SKKU

CS510 Operating System Foundations. Jonathan Walpole

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

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

System Programming. Process Control III

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Exceptional Control Flow Part I

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

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

Changes made in this version not seen in first lecture:

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

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

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

CS 318 Principles of Operating Systems

Computer Systems Assignment 2: Fork and Threads Package

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

Process Management 1

SE350: Operating Systems

CS5460/6460: Operating Systems. Lecture 9: First process. Anton Burtsev January, 2014

Section 4: Threads and Context Switching

Altering the Control Flow

Assignment 1. Teaching Assistant: Michalis Pachilakis (

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

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

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

CS 318 Principles of Operating Systems

CSC369 Lecture 2. Larry Zhang

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

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

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

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Altering the Control Flow

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Changes made in this version not seen in first lecture:

Processes. Dr. Yingwu Zhu

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

CS 550 Operating Systems Spring Process II

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

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

Mon Sep 17, 2007 Lecture 3: Process Management

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

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Parents and Children

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

Transcription:

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

The process model 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 Conceptually, every process on its own CPU OS creates the illusion by virtualizing the CPU Four programs A B C D In reality, CPU switches back & forth among processes Pseudo-parallelism Multiprogramming on a single CPU At any time one CPU means one executing task, but over time 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 Code & data A thread state Execution stack and stack pointer Program counter General purpose registers A set of OS resources Open files, network connections, Other process metadata (e.g. signal handlers) SP P C Stack (dynamic allocated mem) heap (dynamic allocated mem) Static data, global variables code Stack segment Data segment Code or text segment i.e., all you need to run/restart a program if interrupted 4

Process identifiers Every process has a unique ID The PID namespace is global to the system Operations that create processes return a PID (e.g., fork) Operations on processes take a PID as argument (e.g., kill) Creating process in Unix fork pid_t fork(void); Call once, returns twice Returns 0 in child, pid in parent, -1 on error Special process IDs: 0 swapper, 1 init Since it s unique sometimes used to guarantee uniqueness of other identifiers (tmpnam/tmpfile) 5

Process execution 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 Xv6 enum procstate {UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE}; new admitted interrupt exit terminated ready running I/O or event completion dispatched waiting I/O or event wait 6

Implementing processes OS uses a data struct to keep track of process state The Process Control Block PCB: information associated with each process Process state: ready, waiting, Program counter CPU registers CPU scheduling information: e.g. priority Memory-management information Accounting information I/O status information pointer Process state Process number Program counter registers Memory limits List of open files In Linux: defined in task_struct (include/linux/sched.h) 7

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

PCBs and CPU state 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 in 1996, now is sub-microsecs Context switch Process P 0 Operating system Process P 1 Executing Save state into PCB 0 Interrupt or system call Idle Reload state into PCB 1 Choosing which process to run next scheduling (Next lectures!) Executing Idle Interrupt or system call Save state into PCB 1 Reload state into PCB 0 Executing Idle 9

And now a short break 10

Context switching in xv6 # Context switch # void swtch(struct context **old, struct context *new); # Save current register context in old # and then load register context from new..globl swtch swtch: movl 4(%esp), %eax movl 8(%esp), %edx # Save old callee save registers pushl %ebp pushl %ebx pushl %esi pushl %edi # Switch stacks movl %esp, (%eax) movl %edx, %esp # Load new callee save registers popl %edi popl %esi popl %ebx popl %ebp ret Loads arguments off the stack into %eax and %edx before changing stack pointer Pushes register state creating a context structure on the current stack; %esp is save implicitly to *old; %eip was saved by call instruction that invoked swtch and is above %ebp Switch stacks New stack has same format, so just undo; ret has the %eip at the top 11

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, ) 12

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 frees its PCB 13

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 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) Chicken and egg What creates the first process and when? 14

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? Can you think of an everyday example where fork is enough? 15

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 the fork()call once, returns twice Once into the parent, and once into the child Returns child s PID to the parent And 0 to the child 16

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){ /* Return 0 to the child */ printf( I am %d the child of %d\n, getpid(), ppid); return 0; } else { /* And the child PID to the parent */ printf( I am %d, the parent of %d\n, ppid, pid); return 0; } } } Where does the newly created process start? 17

Testing fork() - output [fabianb@eleuthera tmp]$ gcc o creatone createone.c [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; mypid = getpid(); childpid = fork(); if (childpid == -1) { perror("failed to fork\n"); return 1; } Both IDs should be the same 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

The dangers with sharing [fabianb@eleuthera tmp]$./badpid Child 3948, ID = 3947 Parent 3947, ID = 3947 What?!?... 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; } 20

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 21

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; } } 22

fork() + exec() output [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 23

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 24

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 Parent suspended until child exits or calls execve Child promises not to modify the address space before that Not enforced, use at your own peril Saves the effort of duplicating parent s address space when child is going to exec anyway Uncommon today 25

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 26

UNIX shells... // Read and run input commands. while(getcmd(buf, sizeof(buf)) >= 0){ if(buf[0] == c && buf[1] == d && buf[2] == ){ // Clumsy but will have to do for now. // Chdir has no effect on the parent if run in the child. buf[strlen(buf) 1] = 0; // chop \n if(chdir(buf+3) < 0) printf(2, "cannot cd %s\n", buf+3); continue; } if(fork1() == 0) runcmd(parsecmd(buf)); wait(); }... 27

exec and company exec is not a system call int execl(const char *path, const char *arg,...); int execlp(const char *file, const char *arg,...); int execle(const char *path, const char *arg,..., char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execvpe(const char *file, char *const argv[], char *const envp[]); execve is the only exec-like system call The rest are front-ends to it execve knows whether you have done a fork or a vfork by a flag in the PCB 28

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 Scheduling 29