Operating System Structure

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CSC 1600 Unix Processes. Goals of This Lecture

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

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

Processes. CS3026 Operating Systems Lecture 05

CS240: Programming in C

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

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

CS 201. Processes. Gerson Robboy Portland State University

fork System-Level Function

Operating systems and concurrency - B03

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

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

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

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

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

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

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

Lecture 4: Process Management

CSE 153 Design of Operating Systems Fall 2018

CSC 252: Computer Organization Spring 2018: Lecture 19

OS Interaction and Processes

CS240: Programming in C

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Operating Systems. II. Processes

Introduction to Processes

Process Management! Goals of this Lecture!

Process Control. Philipp Koehn. 23 April 2018

Process Management 1

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

University of Washington What is a process?

Process Management 1

Assignment 1. Teaching Assistant: Michalis Pachilakis (

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

Processes: Introduction. CS 241 February 13, 2012

Today. Exceptional Control Flow Processes. Exceptions and Processes. Control Flow. Altering the Control Flow

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

W4118 Operating Systems. Junfeng Yang

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

Unix Processes 1 / 31

Chap 4, 5: Process. Dongkun Shin, SKKU

CSC209 Fall Karen Reid 1

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?

Altering the Control Flow

Operating Systems & Concurrency: Process Concepts

Fall 2015 COMP Operating Systems. Lab #3

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

UNIX. Session 2. UNIX processes and forking fork system call exec system call death of process kill fork and I/O using it UNIX.

Exceptional Control Flow: Exceptions and Processes

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

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.

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

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

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

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

SE350: Operating Systems

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

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

Altering the Control Flow

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

Unix Processes. What is a Process?

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

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

CS510 Operating System Foundations. Jonathan Walpole

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

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

Parents and Children

Processes COMPSCI 386

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

The Kernel Abstraction. Chapter 2 OSPP Part I

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

Carnegie Mellon. Processes. Lecture 12, May 19 th Alexandre David. Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

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

Computer Systems Assignment 2: Fork and Threads Package

Exceptional Control Flow Part I

Control Flow. Systemprogrammering 2007 Föreläsning 2 Exceptional Control Flow Part I. Exceptional Control Flow. Altering the Control Flow

Exceptional Control Flow Part I

Exceptional Control Flow Part I September 22, 2008

System Programming. Process Control II

CS 550 Operating Systems Spring Process II

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

Processes, Exceptional

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

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

Mon Sep 17, 2007 Lecture 3: Process Management

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Exceptional Control Flow Part I Oct. 17, 2002

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

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

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

Transcription:

Operating System Structure CSCI 4061 Introduction to Operating Systems Applications Instructor: Abhishek Chandra Operating System Hardware 2 Questions Operating System Structure How does the OS manage different resources, applications, and events? What kind of functions and services does the OS provide to applications? User Programs Shells, Tools and Utilities Processes, File System, Virtual Memory, Threads, Sockets CPU, Memory, Disks, Devices Applications Operating System Hardware 3 4 1

Processes: Outline Process Definition Process Structure and States Process Creation and Execution Process Termination and Waiting What is a Process? A program in execution Basic unit of work Enables multiprogramming Provides a protection boundary 5 6 Program vs. Process Program: Passive entity Set of instructions A binary file Process: Active entity Executing path of instructions Live set of resources (CPU cycles, memory, files) Multiple processes can correspond to the same program What does a process contain? Program text: Binary code Program Counter: Pointer to current instruction Other registers (e.g., stack pointer) Data: Memory required for variables, functions Other objects: File descriptors, signals, locks Accounting information 7 8 2

Process Memory Layout Process Context High Address Args, environment Stack Heap Activation Records (function params, local vars, saved registers) Dynamic Memory Current state of process activity Determined by: Program counter value Function call stack Data values (data segment, bss, and heap) Register values bss Uninitialized Static Data Data Segment Initialized Static Data Low Address Program Text Binary Code 9 10 Multiprogramming OS multiplexes system resources among multiple processes Each process is allowed to run on the CPU for a short duration (quantum) Process has to give up the CPU if: its time quantum expires it has to wait for an event (I/O, signal, lock) Process States New: Being created Running: Executing instructions on the CPU Blocked: Waiting for an event Ready: Waiting to be assigned a CPU Done: Finished executing 11 12 3

Process Life Cycle new created quantum expired ready running done terminated Process Identity Each process has a unique identifier called pid OS keeps track of each process through its pid pid_t getpid(); Returns the pid for the current process event completion scheduled blocked wait for event 13 14 Processes in Unix Parent-child relation Each process has a parent init process: grand-daddy of all processes pid=1, ever-running process pid_t getppid(); Returns pid of parent Process Tree in Unix 1 2 5 3 4 6 7 8 15 16 4

Process Lifecycle Process is created Parent clones itself Process executes a program Loads program from a file Executes the code Process exits Parent might wait for the child to finish Process Creation: fork pid_t fork(); Parent process executes fork It creates an (almost) identical copy of itself New (child) process inherits a new copy of the parent s whole state and context: Code, data, open files Program counter, stack Two clones exist immediately after fork Most systems employ copy-on-write 17 18 Process Creation: fork Returning from fork A rgs, env ironment Stack Heap Data Segment, bss Program Text fork parent child A rgs, env ironment Stack Heap Data Segment, bss Program Text A rgs, env ironment Stack Heap Data Segment, bss Program Text fork() returns twice Once in parent and once in child Value returned by fork is different for each process Returns 0 to child process Returns pid of child to parent Returns -1 to parent if error Both processes resume from same point after fork(), but with different return values 19 20 5

Process Creation: fork pid_t child_pid; int x=1; child_pid=fork(); if (child_pid>0) /* This is parent process */ printf ( I m parent process: x=%d\n, x); else if (child_pid==0) /* This is child process */ printf ( I m child process: x=%d\n, x); Process Creation Example 1 pid_t child_pid; int i, n=3; for (i=0; i<n; i++) { child_pid=fork(); if (child_pid == 0) break; } 21 22 Process Creation Example 2 pid_t child_pid; int i, n=3; for (i=0; i<n; i++) { child_pid=fork(); if (child_pid!= 0) break; } Process Execution: exec fork creates identical clones But what if the child wants to execute some other program? int execv(char *path, char *argv[]); 6 variations of exec Use the same system call execve Differ in parameters 23 24 6

Process Execution: exec Process Execution: exec exec overwrites the memory image inherited from the parent Process starts from scratch with a new state: New code, program counter, data, stack New arguments, (sometimes) environment Retains some things from before exec: Open files parent A rgs, env 1 Stack 1 Heap 1 Data Segment 1, bss 1 Program Text 1 A rgs, env 1 Stack 1 exec A rgs, env 2 Stack 2 child Heap 1 Data Segment 1, bss 1 Program Text 1 Heap 2 Data Segment 2, bss 2 Program Text 2 25 26 Process Execution: exec pid_t child_pid; int x=1; child_pid=fork(); if (child_pid>0) /* This is parent process */ printf ( I m parent process: x=%d\n, x); else if (child_pid==0) /* This is child process */ { execv( /bin/ls, ls, l, NULL); printf ( I m child process: x=%d\n, x); } Normal Process Termination main function falls off the end return from main exit, _Exit or _exit void exit(int status); status value of 0 corresponds to successful completion OS cleans up all process state Releases memory, file pointers, registers, etc. Flushes print buffers 27 28 7

Abnormal Process Termination Call to abort() Receives a signal it does not catch Signals are software interrupts E.g.: Ctrl-C, segmentation fault OS does not call user-installed exit handlers Core dump may be produced Waiting for a Child: wait Sometime parent wants to wait for the child to finish execution Example: ls l Shell waits until command is executed pid_t wait(int *status); The parent suspends execution wait() returns when a child exits Returns pid of exited child status is pointer to child s exit status 29 30 Waiting for a Child: wait Waiting for a Specific Child: waitpid parent wait A process may have many children, but may want to wait for a specific child pid_t waitpid(pid_t pid, int *status, int opt); fork child exec exit pid: process id of specific child status: exit status of child opt: WNOHANG means no waiting, return 0 if child still running 31 Time 32 8

Process Groups Process group: Multiple processes having a single group leader By default, each process belongs to the process group of its parent waitpid allows you to wait for any process belonging to a process group: Negative pid indicates group_pid waitpid(-group_pid, NULL, 0); Uses of wait Synchronization Allows parent to synchronize its execution with the child/children Useful for interactive applications like the shell Reaping OS removes a process only when its parent waits for it Need to notify the exit status of the process 33 34 Orphans and Zombies Orphan: Running process whose parent dies before it finishes Zombie: Terminated process whose parent hasn t waited on it System does not remove child completely until parents does a wait Orphans are adopted by init process init does wait from time to time Eventually reaped Background Processes Shell spawns a process but does not wait for it E.g.: mozilla & Here, the parent does not wait for the child parent fork child exec 35 36 9

Daemons Forever running background processes Similar to shell Get some input Do something useful Print results, log errors if required Differences from shell: Each implements specific service May not be interactive Examples: Web server (httpd), print server (lpd), ssh daemon (sshd) Processes Summary Process Definition and Layout Process States fork() and exec() exit() and wait() 37 38 10