Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
15-213, Fall 06 Outline Shell Lab Processes Signals.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UC Santa Barbara Project 1 Discussion Bryce Boe 2011/04/12.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
28-Jun-15Advanced Programming Spring 2002 Unix processes and threads Henning Schulzrinne Dept. of Computer Science Columbia University.
CSSE Operating Systems
Operating Systems Recitation 3, April 7-8 th, 2002.
CSE 451 Section 4 Project 2 Design Considerations.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
1 UNIX System Programming v Objectives –look at how to program UNIX processes –fork( ), exec( ), wait( ) Processes.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
LINUX System Programming with Processes. Overview 1. What is a Process? 2. fork() 3. exec() 4. wait() 5. Process Data 6. File Descriptors across Processes.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Lecture 5 Process, Thread and Task September 22, 2015 Kyu Ho Park.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Processes: program + execution state
Creating and Executing Processes
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
8-Sep Operating Systems Yasir Kiani. 8-Sep Agenda for Today Review of previous lecture Process scheduling concepts Process creation and termination.
System calls for Process management
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
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,
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
Operating Systems Process Creation
Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library 5. Files and Directories 6. System Data Files.
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
ACCESS CONTROL. Components of a Process  Address space  Set of data structures within the kernel - process’s address space map - current status - execution.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
Section 8: Processes What is a process Creating processes Fork-Exec
Using Processes.
Unix Process Management
UNIX System Overview.
UNIX PROCESSES.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
Operating Systems Lecture 6.
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Chien-Chung Shen CIS/UD
The Environment of Unix Process
Lecture 6: Multiprogramming and Context Switching
Presentation transcript:

Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function

Process Identifiers Every process has a PID Unique non negative integer PIDs are re-used Two special PIDs 0 – scheduler process. A.K.A. swapper. System process. 1 – init process. Parent of all other processes.

Process Identifiers Functions pid_t getpid(void); pid_t getppid(void); uid_t getuid(void); uid_t geteuid(void); gid_t getgid(void); gid_t getegid(void); Shell command ps

Process Creation fork function Used to create new processes Every process except swapper and init are created with fork pid_t fork(void); Creates a copy of the current process Returns 0 in the child Returns the PID of the child in the parent Returns -1 on error

Process Creation Child process gets a copy of the parent’s data space, heap and stack Text segment is shared Modern implementations use copy-on- write for data because a fork is often followed by an exec

Buffering and forking Buffers are part of the data of the parent and will be copied to the child Line buffered vs fully buffered are flushed at different times May cause output of program to differ if its re-directed to a file instead of screen (changes from line buffered to fully buffered)

File Sharing File descriptors are copied from parent to child Important that file offsets changed by child are updated for parent as well See Fig. 8.2 on page 214 Normally the parent will wait on the child to finish or close the descriptors that the child will use (and child closes the ones parent will use)

Inherited Properties RUID RGID EUID EGID Supplementary GIDs Process Group ID Session ID Controlling terminal set-user-ID and set-group-ID flags Current working directory Root directory File mode creation mask Signal mask and dispositions close-on-exec flag for any open file descriptors Environment Attached shared memory segment Memory mappings Resource limits

Differences Return value from fork Process IDs Different parent process IDs Child’s tms_utime tms_stime tms_cutime and tms_cstime values are zero File locks not inherited Pending alarms cleared for child Pending signals for child set to empty set

When fork fails Too many processes in the system Too many processes for the real user ID CHILD_MAX specifies the max number of processes per user

vfork Creates a new processes with the express purpose of exec-ing a new program New child process runs in parent’s address space until exec or exit is called vfork guarantees that the child will run before the parent until exec or exit call is reached

exit functions Normal termination Return from main Calling exit Calling _exit or _Exit Abnormal termination Calling abort Receiving some signals

exit functions Special cases Orphaned process If the parent of a process terminates before it does, that process becomes an orphan init becomes the parent of all orphaned processes Zombie process Child terminates, but parent has not yet called the wait or waitpid function to retrieve its exit status Status of zombie processes shown as “Z” by the ps command init always calls wait or waitpid for its children, so orphaned processes never become zombies

wait and waitpid pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); Both return the PID of the child or -1 on error status will hold the return status of the child when the function returns.

wait and waitpid 4 mutually exclusive macros for interpreting the status argument WIFEXITED(status) WEXITSTATUS(status) WIFSIGNALED(status) WTERMSIG(status) WCOREDUMP(status) WIFSTOPPED(status) WSTOPSIG(status) WIFCONTINUED(status)

wait and waitpid options argument specifies the behavior of the parent. Can be zero or binary OR of the following constants WNOHANG – do not block for child to terminate. Function returns zero in this case WUNTRACED – retrieves the status information for the process of a child that has been stopped WCONTINUED – same as WUNTRACED, but for the specified child that is continued

wait and waitpid pid argument has different meanings depending on its value pid > 0 – wait on child whose PID == pid pid == -1 - waits for any child pid == 0 – waits for any child where process group ID equals the calling process group ID pid < -1 - waits for any child where process group ID equals absolute value of pid

Advantages of waitpid Can wait for a specified process Has a non-blocking option Supports job control with the options argument

Race Conditions A race condition occurs when two or more processes are using the same resource with no synchronization. In this case, the result is dependant on the order in which the processes execute Often solved with use of signals and other mechanisms to be discussed later

exec Functions 6 versions of exec Replaces the program in a process with the binary image in a specified file PID remains the same All return -1 on error, no return on success

exec Functions int execl(const char *path, 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 execlp(const char *file, const char *arg,...);

exec Functions p – uses filename parameter and searches the PATH for the executable v – arguments to new program are contained in a vector (array of strings) l – arguments to new program are contained in a list (separate parameters) with final entry being NULL e – function takes a vector of environment values instead of using the current system environment

Changing UID and GID int setuid(uid_t uid); int setgid(gid_t gid); Rules for setting the ID EUID = 0 then setuid sets RUID, EUID and set- user-ID to uid EUID != 0 but uid = RUID or set-user-ID then setuid sets EUID to uid. RUID and set-user-ID remain unchanged Otherwise, sets errno to EPERM (operation not permitted) and return -1

Changing Effective IDs int seteuid(uid_t euid); int setegid(gid_t egid); Sets only effective IDs, not real or saved

System Function int system(const char *command); Executes a shell command string from within a program Causes a fork, exec and waitpid Returns -1 if fork failed 127 if exec failed Exit status of shell otherwise

User Identification char *getlogin(void); Returns a string containing the user’s login name or NULL on error

Process Times clock_t times(struct tms *buf); struct tms clock_t tms_utime clock_t tms_stime clock_t tms_cutime clock_t tms_cstime The clock_t values are measured in seconds To get clock ticks, find the clock ticks per second with sysconf(_SC_CLK_TCK);