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

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

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

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

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

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

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

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

Processes (Intro) Yannis Smaragdakis, U. Athens

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

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

4. The Abstraction: The Process

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

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

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

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

Fall 2015 COMP Operating Systems. Lab #3

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

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

518 Lecture Notes Week 3

CSE 153 Design of Operating Systems Fall 2018

Operating System Structure

CS 537 Lecture 2 - Processes

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Operating Systems. Lecture 05

CS240: Programming in C

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

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

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

csci3411: Operating Systems

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

CSCE 313: Intro to Computer Systems

Processes and Threads

CSC 1600 Unix Processes. Goals of This Lecture

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

Processes. CS3026 Operating Systems Lecture 05

1 Programs and Processes

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.

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

Lecture 4: Process Management

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

Frequently asked questions from the previous class survey

SE350: Operating Systems

Process management 1

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

CS510 Operating System Foundations. Jonathan Walpole

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

System Programming. Process Control III

Chap 4, 5: Process. Dongkun Shin, SKKU

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

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

Killing Zombies, Working, Sleeping, and Spawning Children

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

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

Computer Systems Assignment 2: Fork and Threads Package

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Process Management 1

A process. the stack

Unix Processes 1 / 31

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

System Programming. Process Control II

CS240: Programming in C

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

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

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

Processes. Johan Montelius KTH

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

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

Parents and Children

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

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

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

University of Washington What is a process?

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Interrupts, Fork, I/O Basics

fork System-Level Function

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Exceptional Control Flow Part I

Introduction. This project will focus primarily on processes.

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

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

OS Lab Tutorial 1. Spawning processes Shared memory

CS 550 Operating Systems Spring Process II

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

Processes. Dr. Yingwu Zhu

Announcement (1) sys.skku.edu is now available

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

CS 261 Fall Mike Lam, Professor. Processes

Process Management! Goals of this Lecture!

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

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

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

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

Transcription:

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

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! Conceptually, every process has its own CPU Four programs OS creates the illusion by virtualizing the CPU A B C D! In reality, CPU switches back & forth among processes Pseudo-parallelism! Multiprogramming on a single CPU At any instant of 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 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

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 5

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) 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! In Linux: defined in task_struct (include/linux/ sched.h) 7

Processes in xv6 (~x86-port of Unix v6) 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 There s no magic to it! 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! Choosing which process to run next scheduling (Next lectures!) Executing Idle Reload state into PCB 1 Interrupt or system call Save state into PCB 1 Reload state into PCB 0 Executing Idle 9

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! 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

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

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 12

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

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? 14

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 15

And now a short break 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! 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! 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; } } } Where does the newly created process start? 18

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 19

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; } if (childpid == 0) /* child */ printf( Child %d, ID = %d\n, getpid(), mypid); else /* parent */ printf( Parent %d, ID = %d\n, getpid(), mypid); return 0; } Both IDs should be the same 20

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

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 22

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

fork() + exec() output [fabianb@eleuthera tmp]$./creattwo Child before exec... now the ls output copy_shell creatone.c~ p3id skeleton copy_shell.tar creattwo creatone creattwo.c p3id.c~ creatone.c Child completed creattwo.c~ p3id.c uwhich.tar 24

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 25

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 Uncommon today 26

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 27

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); } } } 28

Fork and Linux clone! Clone is replacing fork (and vfork) #include <sched.h>! int clone (int (*fn) (void *), void *child_stack, int flags, void *arg);! Starting at a different point, with a given stack, and after inheriting something from the parent! Not typically called directly 29

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 30

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 31