Lecture 4: Process Management

Similar documents
CS5460: Operating Systems

CSE 153 Design of Operating Systems Fall 2018

The Big Picture So Far. Today: Process Management

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

Today: Process Management. The Big Picture So Far. What's in a Process? Example Process State in Memory

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSCE 313: Intro to Computer Systems

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Operating System Structure

518 Lecture Notes Week 3

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

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

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

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

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

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

Processes. CS3026 Operating Systems Lecture 05

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

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

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

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

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

The Kernel Abstraction. Chapter 2 OSPP Part I

Mon Sep 17, 2007 Lecture 3: Process Management

CSC 539: Operating Systems Structure and Design. Spring 2006

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

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

CS510 Operating System Foundations. Jonathan Walpole

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

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

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

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

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

csci3411: Operating Systems

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

Process! Process Creation / Termination! Process Transitions in" the Two-State Process Model! A Two-State Process Model!

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

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

Processes and Non-Preemptive Scheduling. Otto J. Anshus

A process. the stack

Wednesday, September 14, Chapter 3: Processes

Chap 4, 5: Process. Dongkun Shin, SKKU

Operating System. Chapter 3. Process. Lynn Choi School of Electrical Engineering

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

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

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

Processes. Johan Montelius KTH

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

W4118 Operating Systems. Junfeng Yang

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Processes and Threads

Processes. Dr. Yingwu Zhu

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

Last time: introduction. Networks and Operating Systems ( ) Chapter 2: Processes. This time. General OS structure. The kernel is a program!

Computer Systems II. First Two Major Computer System Evolution Steps

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

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

Interrupts, Fork, I/O Basics

COMP 3430 Robert Guderian

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

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

Computer Systems Assignment 2: Fork and Threads Package

! The Process Control Block (PCB) " is included in the context,

Operating Systems. Lecture 05

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

Announcements Processes: Part II. Operating Systems. Autumn CS4023

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Processes & Threads. Recap of the Last Class. Layered Structure. Virtual Machines. CS 256/456 Dept. of Computer Science, University of Rochester

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

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

Exceptional Control Flow Part I

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

1/13/2019 Operating Systems. file:///volumes/users/rasit/desktop/comp3430/coursematerial/slides/03_processes/index.html?

CSC 716 Advanced Operating System Fall 2007 Exam 1. Answer all the questions. The maximum credit for each question is as shown.

* What are the different states for a task in an OS?

Process Control. Philipp Koehn. 23 April 2018

Announcement. Exercise #2 will be out today. Due date is next Monday

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

CS153: Process 2. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

What is a Process. Processes. Programs and Processes. System Classification 3/5/2013. Process: An execution stream and its associated state

CSE 153 Design of Operating Systems

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

Processes and Threads

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

Process Management! Goals of this Lecture!

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

IT 540 Operating Systems ECE519 Advanced Operating Systems

Processes. slide 1 kv, gm. Silberschatz chapter 3

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

Process concepts. Processes. Process in Memory. Process Creation. definition of a process (sometimes called a Task) aprogram in execution

Process Description and Control

Transcription:

Lecture 4: Process Management (Chapters 2-3) Process: execution context of running program. A process does not equal a program! Process is an instance of a program Many copies of same program can be running at same time OS manages a variety of activities User programs Batch jobs and scripts System programs (e.g., print spool, file server, network daemons, ) Each activity is encapsulated in a process. All activity is either in the OS or in a process.

OS manages processes: OS creates, deletes, suspends, and resumes processes OS schedules processes to manage CPU allocation ( scheduling ) OS manages inter-process communication and synchronization OS allocates resources to processes Cooperating processes banking example ATM transaction manager Web ASP calculating current account balance Monthly statement generator process Question: If all of these processes try to access the same account information simultaneously, what can happen? Process state consists of: Memory state: code, data, heap, stack Processor state: PC, registers, etc. Kernel state:» Process state: ready, running, etc.» Resources: open files/sockets, etc.» Scheduling: priority, cpu time, etc. Address space consists of: Code Static data (data and BSS) Dynamic data (heap and stack) See: Unix size command Special pointers: PC: current instruction being executed HP: top of heap (explicitly moved) SP: bottom of stack (implicitly moved) 0xFFFFFFFF SP HP PC 0x00000000 Stack Heap (Dynamically allocated) Uninitialized data (BSS segment) Static data (Data segment) Code (Text segment)

On a Unix machine, try: ps Af : lots of info on all running processes kill 9 547 : immediately terminates process with PID 547 top : displays dynamic info on top running jobs Write a program that calls:» getpid() : returns current process s PID» fork(),exec(),wait(): creates new process» sleep() : puts current process to sleep for specified time Similar commands work on Mac OS X (BSD Unix!) On Windows Task managed (CTL-ALT-DEL) Each process has a state: new: OS is setting up process ready: runnable, but not running running: executing instructions on CPU waiting: stalled for some event (e.g., IO) terminated: process is dead or dying OS invariant: At most one running process at a time As program executes, it moves from state to state as a result of program, OS, or extern actions Program: sleep(), IO request, OS action: scheduling External: interrupts, IO completion create process New Ready I/O done schedule deschedule Waiting Terminated exit process Running block on timer, I/O, page fault,

Process control block (PCB) One per process, allocated in kernel memory Tracks state of a process, typically including:» Process state (running, waiting, )» PID» PC, SP, registers» Memory management info» Open file table (open socket table)» Queue pointers (waiting queue, I/O, sibling list, parent, )» Scheduling info (e.g., priority, time used so far, ) When process created, new PCB allocated, initialized, and put on ready queue (queue of runnable processes) When process terminates, PCB deallocated and process state cleaned up (e.g., files closed, parent informed of death, ) OS tracks PCBs using queues Ready processes on readyq Each I/O device has a queue Queue traversed when I/O interrupt handled Processes linked to parents and siblings Needed to support wait() Need to be comfortable managing data on multiple queues (pointers galore!) Ready Queue Disk Wait Queue head tail PID=532 PID=12 PID=119 PID=73 head tail PID=48

Context switch: change from one process to another Select another process to execute ( scheduling ) Store CPU state of running process (PC, SP, regs, ) in its PCB» Requires extreme care: some values from exception stack Load most of CPU state for next process s PCB in to CPU» What can you not just load directly? Set up pseudo-exception stack containing state you want loaded for next process (e.g., PC, SP, PSW, ) Perform (privileged) return from exception instruction» Restores sensitive CPU state from exception stack frame Context switches are expensive Time sharing systems do 100-1000 context switches per second When? Timer interrupt, packet arrives on network, disk I/O completes, user moves mouse, In Windows, CreateProcess(): Creates new process running specified program In Unix, fork(): Creates new process that is near-clone of forking parent Return value of fork() differs: 0 for child, child_pid for parent Many kernel resources are shared, e.g., open files and sockets To spawn new program, use some form of exec() Question: Where does first process (init) come from? Question: Why fork/exec versus CreateProcess?

Stack: Stack: SP COPY COPY COPY SP HP PC Data: pid: 334 Code: pid=fork(); This is the only difference between parent and child! HP PC Data: pid: 0 Code: pid=fork(); Fork(), exit(), and exec() are weird! fork() returns twice once in each process exit() does not return at all exec() usually does not return: overwrites current process with new one! int main (int argc, char** argv) { while (1) { pid_t pid = fork(); if (pid == 0) { char** arguments = 0; for (int i = 1; i < argc; i++) { cout << argv[i] << endl; arguments[i-1] = argv[i]; cout << arguments[i-1]; else { What will happen when you run this program? What might a sysadmin do to prevent this?

When process dies, OS reclaims its resources On Unix: A process can terminate itself using exit() system call A process can kill its child using the kill() system call #include <signal.h> #include <unistd.h> #include <stdio.h> int main(void) { int parentid = getpid(); int cid = fork(); if (cid == 0) { printf( Child exiting!\n ); exit(0); printf( Impossible!\n ); else { printf( Type to kill child\n ); char answer[10]; gets(answer); if (!kill(cid,sigkill)) { printf( Child dead!\n ); How can you speed up fork()? Hint: Think about high cost of copying large address space Homework: Read about different fork/exec variants.

Sign up for cs5460@cs already! Project one assignment out: Simple shell with I/O redirection Due Sept 10 th (one week from today at midnight Weds-Thu) INDIVIDUAL project not a group effort! For next time, read Chapter 5 (finish Chapter 4)! Threads vs processes Brief discussion of concurrency control issues Intro to dispatching and scheduling Processes are heavyweight Memory mappings: expensive to swap Cache/TLB state: flushing expensive, especially side effects Lots of kernel state Context switching between processes is EXPENSIVE Threads are lightweight Multiple threads share process state» Same address space» Same open file/socket tables Goal: make context switching between threads CHEAP Not all OSes support threads efficiently (e.g., older Unixes)» Had to fake it using things like select() and signal()» Good news for you your OS will not support threads

Address space shared by threads Code Data and heap Thread private state: Registers (inc. pc, sp, psw) Stack Key issue: How do you safely access shared state?» Read-only (e.g., code) easy» Writable hard Whole section of course dedicated to this» Synchronization» Concurrency control Context switch between threads Save/restore registers (tricky) SP0 SP1 HP PC1 PC0 Stack: Stack: Data: Code: Kernel threads: OS is aware of thread abstraction Process: address space, privileges, kernel resources Thread: execution state, scheduling entity (PC, SP, registers) Each thread has OS state (ready, blocked, ) Kernel context switches between threads (and processes) User threads: OS is unaware of thread abstraction Thread context switch performed entirely within user space Requires cooperation between threads to ensure fairness What issues arise? Kernel threads: overhead of context switches, User threads: blocking I/O,

Concurrency control How do you write programs/threads that can cooperate to solve a problem? How do you ensure safe access to shared resources? What kinds of problems arise? Dispatching and scheduling Dispatching: operations involved in performing a context switch Scheduling: deciding what process/thread to run next (Backup slides follow )

fork/forkl: Creates a new process that is almost identical copy of current one Only difference is return code from fork call (parent: PID, child: 0) In Unix, only way to make a new process! exec/execv/execl/ : Replaces running program with one named as argument Typically loads new program from disk into address space Calling context is LOST (if successful) READ THE Shares open file table with original process (important!) MANUAL wait: PAGES!!! Waits for any child process to exit() Returns child process s exit code, along with some other status exit (val): Terminates running process with specified exit code