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

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

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

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

Chapter 3: Process Concept

Chapter 3: Process Concept

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

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

Processes. Dr. Yingwu Zhu

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Chapter 3: Process Concept

Chapter 3: Processes. Chapter 3: Processes. Process in Memory. Process Concept. Process State. Diagram of Process State

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

Operating Systems. Lecture 05

Chap 4, 5: Process. Dongkun Shin, SKKU

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

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

Part Two - Process Management. Chapter 3: Processes

CSCE 313: Intro to Computer Systems

Processes. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & Apps 1

CHAPTER 2: 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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Lecture 2 Process Management

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

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

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

Fall 2015 COMP Operating Systems. Lab #3

Processes. Operating System Concepts with Java. 4.1 Sana a University, Dr aimen

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

Diagram of Process State Process Control Block (PCB)

Chapter 3: Processes

Chapter 3: Processes

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

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

The Big Picture So Far. Chapter 4: Processes

Chapter 3: Processes

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Unix Processes. What is a Process?

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

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.

Processes and Threads

Processes. Process Concept

CSE 153 Design of Operating Systems Fall 2018

Chapter 4: Processes. Process Concept

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

The Big Picture So Far. Chapter 4: Processes

Chapter 4: Processes

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

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

CHAPTER 3 - PROCESS CONCEPT

Process Concepts 8/21/2014. CS341: Operating System. Scheduling: Theoretical Analysis

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

Mon Sep 17, 2007 Lecture 3: Process Management

CSC 1600 Unix Processes. Goals of This Lecture

Chapter 3: Process Concept

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

Killing Zombies, Working, Sleeping, and Spawning Children

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

Process Concept. Chapter 4: Processes. Diagram of Process State. Process State. Process Control Block (PCB) Process Control Block (PCB)

Chapter 4: Processes

CSC 539: Operating Systems Structure and Design. Spring 2006

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

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

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

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Chapter 4: Processes. Process Concept

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Chapter 3: Processes. Operating System Concepts 8th Edition, modified by Stewart Weiss

CS 537 Lecture 2 - Processes

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

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

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

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Lecture 4: Process Management

CSI Module 2: Processes

Operating System Structure

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

Course: Operating Systems Instructor: M Umair. M Umair

Chapter 3: Processes. Operating System Concepts 8th Edition

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

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

csci3411: Operating Systems

Chapter 3: Processes. Operating System Concepts 9 th Edition

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

Chapter 4: Processes

Processes, PCB, Context Switch

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

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

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

2. PROCESS. Operating System Concepts with Java 8th Edition Silberschatz, Galvin and Gagn

CS240: Programming in C

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Transcription:

CPSC 341 OS & Networks Processes Dr. Yingwu Zhu

Process Concept Process a program in execution What is not a process? -- program on a disk A process is an active object, but a program is just a file It is the unit of execution It is the unit of scheduling It is the dynamic execution context of a program

Process Components A process includes: Program counter (the CPU register holding the addr. of the next instruction) Text section: program code Stack: local variables, function params, return addresses Data section: global variables Heap (optional): dynamically allocated memory A set of general-purpose registers with current values A set of operating system resources Open files, network connections, etc. A process is named using its process ID (PID)

Process Diagram

Process State Processes switch between different states based on internal and external events Each process is in exactly one state at a time As a process executes, it changes state (typical states of processes (varies with OS)) new: The process is being created running: Instructions are being executed (only one process per processor may be running) waiting: The process is waiting for some event (e.g., I/O, signals) to occur ready: The process is waiting to be assigned to a processor terminated: The process has finished execution

Diagram of Process State

Process Data Structures How does the OS represent a process in the kernel? At any time, there are many processes in the system, each in its particular state The OS data structure representing each process is called the Process Control Block (PCB) The PCB contains all of the info about a process The PCB also is where the OS keeps all of a process s hardware execution state (PC, SP, regs, etc.) when the process is not running This state is everything that is needed to restore the hardware to the same configuration it was in when the process was switched out of the hardware

Process Control Block (PCB) PCB stores all of the information about a process info. associated with each process PID Process state Hardware state: PC (program counter), SP (stack pointers) and CPU registers (accumulators, index registers, etc.) CPU scheduling information: priority, etc. Memory-management information: base/limit, page tables, or segment tables Accounting information: CPU, etc I/O status information: a list of I/O devices allocated, a list of open files, etc. Pointers for state queues Etc.

Process Control Block (PCB)

Maintaining PCBs Keep track of the different processes in the system Collection of PCBs is called a process table How to store the process table? Option 1: P1 P2 P2 P3 P4 P5 Ready Waiting New Term Waiting Ready Problems with Option 1: hard to find processes how to fairly select a process

Solution: State Queues Store processes in queues based on state (state queue) Typically, the OS has one queue for each state Ready, waiting, etc. Each PCB is queued on a state queue according to its current state As a process changes state, its PCB is unlinked from one queue and linked into another. Processes migrate among the various queues Job queue set of all processes in the system Ready queue set of all processes residing in main memory, ready and waiting to execute Device queues set of processes waiting for an I/O device

Ready Queue and Various I/O Device Queues

PCBs vs. State Queues PCBs are data structures dynamically allocated in OS memory When a process is created, the OS allocates a PCB for it, initialized, and placed on the ready queue As the process computes, does I/O, etc., its PCB moves from one queue to another When the process terminates, its PCB is deallocated

Representation of Process Scheduling

Schedulers Long-term scheduler (or job scheduler) selects which processes should be brought into the memory; controls degree of multiprogramming (# of processes in memory) Short-term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU

Addition of Medium-Term Scheduling Swap in/out processes memory, adaptive to memory status and process status

Schedulers (Cont.) Short-term scheduler is invoked very frequently (milliseconds) (must be fast) Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow) The long-term scheduler controls the degree of multiprogramming Processes can be described as either: I/O-bound process spends more time doing I/O than computations, many short CPU bursts CPU-bound process spends more time doing computations; few very long CPU bursts

Short-Term Scheduler Responsible for: saving state into PCB when switching to a new process selecting a process to run (from the ready queue) loading state of another process One of the most time critical parts of the OS

CPU Switch From Process to Process

Selecting a Process to Run called scheduling can simply pick the first item in the queue called round-robin scheduling is round-robin scheduling fair? can use more complex schemes we will study these in the future use alarm interrupts to switch between processes when time is up, a process is put back on the end of the ready queue frequency of these interrupts is an important parameter typically 3-10ms on modern systems need to balance overhead of switching vs. responsiveness

PCB and Hardware State When a process is running, its hardware state (PC, SP, regs, etc.) is in the CPU The hardware registers contain the current values When the OS stops running a process, it saves the current values of the registers into the process PCB When the OS is ready to start executing a new process, it loads the hardware registers from the values stored in that process PCB What happens to the code that is executing? The process of changing the CPU hardware state from one process to another is called a context switch

Context Switch When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process Context-switch time is overhead; the system does NO useful work while switching Time dependent on hardware support Depends on Memory speed, #-of-registers to copy, special instructions (single instruction to load/save all registers) A few milliseconds. This can happen 100 or 1000 times a second!

Process Creation Parent processes create children processes, which, in turn create other processes, forming a tree of processes Resource sharing Parent and children share all resources Children share subset of parent s resources Parent and child share no resources Execution Parent and children execute concurrently Parent waits until children terminate

Process Creation (Cont.) Address space Child duplicate of parent Child has a program loaded into it (exec) UNIX examples fork system call creates new process exec system call used after a fork to replace the process memory space with a new program

Forking a New Process Create a PCB for the new process (Linux: task_struct) copy most entries from the parent clear accounting fields buffered pending I/O allocate a pid (process id for the new process) Allocate memory for it could require copying all of the parents segments however, text segment usually doesn t change so that could be shared might be able to use memory mapping hardware to help More about this in the memory management part Add it to the ready queue

System Call: fork() Creating a process: Creates and initializes a new PCB Creates a new address space Initializes the address space with a copy of the entire contents of the address space of the parent Initializes the kernel resources to point to the resources used by parent (e.g., open files) Places the PCB on the ready queue Fork returns twice Returns the child s PID to the parent, 0 to the child

System Call: fork() Use the return value to determine parent/child process (0 child, nonzero parent, negative failure) The child process duplicates the parent process s address space

More Complete Example

Why fork()? Very useful when the child Is cooperating with the parent Relies upon the parent s data to accomplish its task Example: Web server while (1) { int sock = accept(); if ((child_pid = fork()) == 0) { Handle client request } else { Close socket } }

Process Creation Wait a second. How do we actually start a new program? int exec(char *prog, char *argv[]) exec() Stops the current process Loads the program prog into the process address space Initializes hardware context and args for the new program Places the PCB onto the ready queue Note: It does not create a new process What does it mean for exec to return?

exec Family of System Calls Replace the current process with a new process The old process is gone The new process starts The functions return an integer error code. (0=Ok/-1=Fail).

Example: execv/execvp int execv(const char *path, char *const argv[]); //the first element in argv must be command name execvp() perform the same purpose except that it will use environment variable PATH to determine which executable to process. Thus a fully qualified path name would not have to be used. The first argument to the function could instead be "ls".

More: execl() and execlp() int execl(const char *path, const char *arg0, const char *arg1, const char *arg2,... const char *argn, (char *) 0); Example: #include <unistd.h> main() { execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0); } The routine execlp() will perform the same purpose except that it will use environment variable PATH to determine which executable to process. Thus a fully qualified path name would not have to be used. The first argument to the function could instead be "ls. The function execlp() can also take the fully qualified name as it also resolves explicitly.

Example int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); } }

Process Creation

What happened to opened files after fork()?

Note on Preceding Example infile: a text file with the numbers 1 through 7 As one process reads from the file, and the file pointer is moved for both processes. Likewise, when a file is written to, the next character goes to the end of the file. This makes sense, because the kernel keeps track of the open file's information. The file descriptor is merely an identifier for the process. (duplicated after fork!) At least 3 open files shared: 0, 1, 2

Process Termination Process executes last statement and asks the operating system to delete it (exit) Output data from child to parent (via wait) Process resources are deallocated by operating system Parent may terminate execution of children processes (abort) Child has exceeded allocated resources Task assigned to child is no longer required If parent is exiting orphan process Some operating system do not allow child to continue if its parent terminates All children terminated - cascading termination (VMS) in UNIX becomes child of the root process

Process Termination - UNIX example Kernel frees memory used by the process moved PCB to the terminated queue Terminated process signals parent of its death (SIGCHILD) is called a zombie in UNIX remains around waiting to be reclaimed parent process wait system call retrieves info about the dead process exit status accounting information signal handler is generally called the reaper since its job is to collect the dead processes

Linux case: controlling processes part of command regular_command command & jobs Ctrl+Z Ctrl+C %n bg fg kill Meaning Runs this command in the foreground. Run this command in the background (release the terminal) Show commands running in the background. Suspend (stop, but not quit) a process running in the foreground (suspend). Interrupt (terminate and quit) a process running in the foreground. Every process running in the background gets a number assigned to it. By using the % expression a job can be referred to using its number, for instance fg %2. Reactivate a suspended program in the background. Puts the job back in the foreground. End a process (also see Shell Builtin Commands in the Info pages of bash)

Linux: Process Attributes The process ID or PID: a unique identification number used to refer to the process. The parent process ID or PPID: the number of the process (PID) that started this process. Nice number: the degree of friendliness of this process toward other processes (not to be confused with process priority, which is calculated based on this nice number and recent CPU usage of the process). Terminal or TTY: terminal to which the process is connected. User name of the real and effective user (RUID and EUID): the owner of the process. The real owner is the user issuing the command, the effective user is the one determining access to system resources. RUID and EUID are usually the same, and the process has the same access rights the issuing user would have. Real and effective group owner (RGID and EGID): The real group owner of a process is the primary group of the user who started the process. The effective group owner is usually the same, except when SGID access mode has been applied to a file. Cmd: ps -af

More ps -ef grep username This displays all processes owned by a particular user pstree

More? See Supl. Materials online

Exercise How many processes for the segment of code int main() { for (int m = 0; m<2; m++) } pid_t x = fork(); //assume success all the times return 0;

Summary What are the units of execution? Processes How are those units of execution represented? Process Control Blocks (PCBs) How is work scheduled in the CPU? Process states, process queues, context switches What are the possible execution states of a process? New, running, ready, waiting, terminated How does a process move from one state to another? Scheduling, I/O, creation, termination How are processes created? fork/exec (Unix)