Processes. Process ID (pid) Synopsis #include pid_t getpid(void) – returns the pid of the currently running process. pid_t getppid(void) – returns the.

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CS Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Processes A process is a program in execution A process has (among other things) a number (pid) and a state. The pid enables the OS to distinguish between.
Unix Processes operating systems. The Process ID Unix identifies each process with a unique integer called a process ID. The process that executes the.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
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.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Operating Systems Chapter 2
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Creating and Executing Processes
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
System calls for Process management
Chapter 11 Process Management
Operating Systems Processes 1.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
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
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.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
Correct C Programs. The C compiler cc [filename.c] Options include: -o [output file name] -lm to include the maths library -E to get output from preprocessor.
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)
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.
Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)
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.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
CSCI 4061 Recitation 2 1.
LINUX System : Lecture 8 Programming with Processes
Using Processes.
Unix Process Management
CSC 382: Computer Security
Processes in Unix, Linux, and Windows
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Processes in Unix, Linux, and Windows
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
Tutorial 3 Tutorial 3.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Processes

Process ID (pid) Synopsis #include pid_t getpid(void) – returns the pid of the currently running process. pid_t getppid(void) – returns the pid of the parent of the currently running process. POSIX

PID Example /* Example 2.1 */ #include #include void main(void) { printf("Process ID: %ld\n", (long)getpid()); printf("Parent process ID: %ld\n", (long)getppid()); return 0; }

User/Group ID System managers assign a unique integral user ID and an integral group ID to each user when creating the user’s account The system uses the user and group Ids to retrieve privileges from the system database for that user Each process is identified with a particular user called the owner Each user has a unique ID (uid) Effective User ID (euid) can change during execution and determine access permissions to files System manager has a UID of 0 POSIX

User/Group ID Synopsis SYNOPSIS #include gid_t getegid(void); uid_t geteuid(void); gid_t getgid(void); uid_t getuid(void); POSIX

User/Group ID Example #include int main (void) { printf(“My real user ID is %5ld\n”, (long) getuid()); printf(“My effective user ID is %5ld\n”, (long) geteuid()); printf(“My real group ID is %5ld\n”, (long) getgid()); printf(“My effective group ID is %5ld\n”, (long) getuid()); return 0; }

Process States new blocked readydone running

ps Displays information about processes. The – a option displays information for processes associated with terminals SYNOPSIS ps [-aA] [-G grouplist] [-o format]… [-p proclist] [-t termlist] [-U userlist] POSIX Shells and Utilities -

ps Fields headerMeaning F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME COMMAND Flags associated with the process The process state The user ID of the process owner The process ID The parent process ID The processor utilization used for scheduling The priority of the process The nice value The memory address of the process The size of the process image The address of the event if the process is sleeping The controlling terminal Cumulative execution time Command name

fork System Call Creates child process by copying parent’s memory image SYNOPSIS #include pid_t fork(void) POSIX

fork return values Returns 0 to child Returns child PID to parent Returns –1 on error

Fork Attributes Child inherits: Parent’s memory image Most of the parent’s attributes including environment and privilege. Some of parent’s resources such as open files. Child does not inherit: Parent pid. Parent time clock (child clock is set to 0).

fork Example #include #include #include void main(void) { pid_t childpid; childpid = fork()) if(childpid == -1 { perror(“failed to fork”); return 1; } if(childpid == 0) { fprintf(stderr, "I am the child, ID = %ld\n", (long)getpid()); /* child code goes here */ } else if (childpid > 0) { fprintf(stderr, "I am the parent, ID = %ld\n", (long)getpid()); /* parent code goes here */ } }

fork (Chain) #include #include #include void main(void) { int i; int n; pid_t childpid; n = 4; for (i = 1; i < n; ++i) if (childpid = fork()) break; fprintf(stderr,"This is process %ld with parent %ld\n", (long)getpid(), (long)getppid()); sleep(1); }

fork (Fan) #include #include #include void main(void) { int i; int n; pid_t childpid; n = 4; for (i = 1; i < n; ++i) if ((childpid = fork()) <= 0) break; fprintf(stderr, "This is process %ld with parent %ld\n", (long)getpid(), (long)getppid()); sleep(1); }

fork (Tree) #include #include #include void main(void) { int i; int n; pid_t childpid; for (i = 1; i < 4; ++i) if ((childpid = fork()) == -1) break; fprintf(stderr, "This is process %ld with parent %ld\n", (long)getpid(), (long)getppid()); sleep(1); }

wait Function SYNOPSIS #include pid_t wait (int *stat_loc); pid_t waitpid(pid_t pid, int *stat_loc, int options); POSIX

wait errno errnocause ECHILDcaller has no unwaited-for children (wait) or process or process group specified by pid does not exist (waitpid), or process group specified by pid does not have a member that is a child of caller (waitpid) EINTRfunction was interrupted by a signal EINVALoptions parameter of waitpid was invalid

pid_t wait(int *stat_loc) Causes caller to pause until a child terminates, or stops until the caller receives a signal. If wait returns because a child terminates, the return value (of type pid_t) is positive and is the pid of that child. Otherwise wait returns –1 and sets errno. stat_loc is a pointer to an integer variable. If caller passes something other than NULL, wait stores the return status (terminate status?) of the child. POSIX specifies the following macros for testing the return status: WIFEXITED, WEXITSTUS, WIFSIGNALED, WTERMSIG, WIFSTOPPED, and WSTOPSIG. Child returns status by calling exit, _exit, or return.

Chain with wait #include … void main(void) { int i; int n; pid_t childpid; int status; pid_t waitreturn; n = 4; for (i = 1; i < n; ++i) if (childpid = fork()) break; while(childpid != (waitreturn = wait(&status))) if ((waitreturn == -1) && (errno != EINTR)) break; fprintf(stderr, "I am process %ld, my parent is %ld\n", (long)getpid(), (long)getppid()); }

r_wait Restarts the wait function if it is interrupted by a signal

Status Values SYNOPSIS #include WIFEXITED (int stat_val) WIFEXITSTATUS (int stat_val) WIFSIGNALED (int stat_val) WTERMSIG (int stat_val) WIFSTOPPED (int stat_val) WIFSTOPSIG (int stat_val)

Use of Status Values wait(&status); if(WIFSTOPPED(status)) printf(“Child stopped due to a signal”);

waitpid (pid,status,options) If the pid paramater is: –greater than 0, wait for child with pid number –equal to 0, wait for any child in the same process group as the caller –equal to –1, wait for any child –less than –1, wait for any child in the process group specified by the absolute value of pid

waitpid Status Parameter The second waitpid parameter is used the same way as the wait parameter (i.e., NULL or &status) The second parameter can be tested by status (WIF…) operators

waitpid Options Parameter The options parameter is the bitwise or of one or more flags It is set to 0 if there are no options WNOHANG causes waitpid to return even no child has terminated WUNTRACED causes waitpid to report the status of unreported child processes that have been stopped

waitpid Return Values waitpid returns: –a value of –1 if an error occurs –a value of 0 if there are possible unwaited- for children that have not terminated (applies when WNOHANG option is set) –a value of the terminating child pid if a child terminates

waitpid examples waitreturn = waitpid (apid, &status,0); wait for the specific child apid waitreturn = waitpid (–1, NULL, NOHANG); wait for any child, but do not delay if a child is currently not available waitreturn = waitpid(0, NULL,0) wait for any child in the process group of the calling process waitreturn = waitpid(-4828,NULL,0) wait for any child in the process group 4828

waitpid – WNOHANG # include #include #include #include #include void main(void) { int status; pid_t childpid; pid_t waitreturnpid; childpid = fork(); if (childpid == 0) { fprintf(stderr,"Child %ld will sleep for 5 seconds\n",(long)getpid()); sleep(5); fprintf(stderr,"Child %ld will now exit\n",(long)getpid()); exit(0); } sleep(8); fprintf(stderr,"Parent %ld will wait for any child\n",(long)getpid()); while(waitreturnpid = waitpid(-1, &status, WNOHANG)) if (!((waitreturnpid == -1) && (errno != EINTR))) break; fprintf(stderr,"Parent will exit after receiving %ld from waitpid\n", waitreturnpid); }

execl, execlp, execle “l” – Passes command directly as a parameter in exec. execl searches for command in fully qualified pathname passed as exec parameter or in current directory execlp uses PATH environment variable to find command execle uses environment passed as exec parameter to find command

execv, execvp,execve “v” – Passes command as member of argument array (i.e., argv[] or makeargv[]) execv searches for arg array command in fully qualified pathname passed in exec or in current directory execvp uses PATH environment variable to find arg array command execve uses environment passed as exec parameter to find arg array command

execl Example /* Program 2.6 */ #include #include #include #include #include void main(void) { pid_t childpid; int status; if ((childpid = fork()) == -1) { perror("Error in the fork"); exit(1); } else if (childpid == 0) { /* child code */ if (execl("/usr/bin/ls", "ls", "-l", NULL) < 0) { perror("Exec of ls failed"); exit(1); } } else if (childpid != wait(&status)) /* parent code */ perror("A signal occurred before the child exited"); exit(0); }

execvp Example /* Program 2.7 */ #include #include #include #include #include void main(int argc, char *argv[]) { pid_t childpid, waitreturn; int status; if ((childpid = fork()) == -1) { perror("The fork failed"); exit(1); } else if (childpid == 0) { /* child code */ if (execvp(argv[1], &argv[1]) < 0) { perror("The exec of command failed"); exit(1); } } else /* parent code */ while(childpid != (waitreturn = wait(&status))) if ((waitreturn == -1) && (errno != EINTR)) break; exit(0); }

errno for exec errno E2BIG EACCESEINVAL ELOOP ENAMEOOLONG ENOENT ENOEXEC ENOTDIR

Use of makeargv #include #include #include #include #include #include “restart.h” int makeargv(char *s, char *delimiters, char ***argvp); void main(int argc, char *argv[]) { char **myargv; char delim[] = " \t"; pid_t childpid; if (argc != 2) { fprintf(stderr, "Usage: %s string\n", argv[0]); return 1; } childpid = fork(); if (childpid == -1) { perror(“Failed to fork“); return 1 } if (childpid == 0) { /* child code */ if (makeargv(argv[1], delim, &myargv) == -1) { perror (“Child failed to constuct argument array”); } else { execvp(myargv[0], &myargv[0]); perror(“Child failed to exec command"); } return 1; } } /* parent code */ if (childpid != r_wait(NULL))) { perror(“Parent failed to wait”); return 1; } return 0 }

Attributes Preserved by Calls to exec Preserved AttributeRelevant System Call Process ID Parent process ID Process group ID Session ID Real user ID Real group ID Supplementary group IDs Time left on alarm signal Current working directory Root directory File mode creation mask File size limit* Process signal mask Pending signals Time elapsed getpid() getppid() getpgid() getsid() getuid() getgid() getgroups() alarm() getcwd() unmask() ulimit() sigprocmask() sigpending() times() Continued on next slide

exec Attributes (Continued) Preserved AttributeRelevant System Call resource limits* controlling terminal* interval timers* nice values* semadj values* getrlimit(), setrlimit() open(), tcgetpgrp() ualarm() nice() semop() An * indicates an attribute that is inherited in the POSIX:XSI Extension

Background Processes Child process becomes background process when it executes setsid(). Child that becomes background process never returns to parent. Background processes cannot be interrupted with ctrl-c.

Daemon A daemon is a background process that runs indefinitely. Examples: Solaris 2 pageout daemon Mailer daemon

Background Processes #include #include #include #include #include “restart.h” int makeargv(char *s, char *delimiters, char ***argvp); void main(int argc, char *argv[]) { char **myargv; char delim[] = " \t"; pid_t childpid; if (argc != 2) { fprintf(stderr, "Usage: %s string\n", argv[0]); return 1; } childpid = fork()); if (childpid == -1) { perror("The fork failed"); return 1 } if (childpid == 0) { /* child becomes a background process */ if (setsid() == -1) perror("Could not become a session leader"); else if (makeargv(argv[1], delim, &myargv) == =1) fprintf(stderr, "Argument array could not be constructed\n"); else { execvp(myargv[0], &myargv[0]); perror(“Child failed to exec command"); } return 1; } /* child should never return */ return 0 } /* parent exits */

Critical Sections Interleaved printing. Multi-process to shared memory where at least one process is writing.