Reading Assignment 4 Chapter 4 Threads, due 2/7 1/31/13 CSE325 - Processes 1
What s Next? 1. Process Concept 2. Process Manager Responsibilities 3. Operations on Processes 4. Process Scheduling 5. Cooperating Processes 6. Interprocess Communication CSE325 - Processes 2
Process starting and ending Processes are created When the system boots By the actions of another process By the actions of a user By the actions of a batch/process manager Processes terminate Normally exit Voluntarily on an error Involuntarily on an error Terminated (killed) by the actions a user or a process 3
Process Creation Parent process creates 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. CSE325 - Processes 4
Process Creation (Cont.) Address space Child duplicate of parent. Child has a program loaded into it. 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. CSE325 - Processes 5
Creating a Process in UNIX pid = fork(); UNIX kernel Process Table Process Descriptor CSE325 - Processes 6
Process Creation Unix & Linux Create a new (child) process fork(); Allocates new PCB Clones the calling process (almost exactly) Copy of parent process address space Copies resources in kernel (e.g. files) Places new PCB on Ready queue Return from fork() call 0 for child child PID for parent 7
Example of fork( ) int main(int argc, char **argv) { char *name = argv[0]; int child_pid = fork(); if (child_pid == 0) { printf( Child of %s sees PID of %d\n, name, child_pid); return 0; } } else { printf( I am the parent %s. My child is %d\n, name, child_pid); return 0; } %./forktest Child of forktest sees PID of 0 I am the parent forktest. My child is 486 8
Starting New Programs Unix & Linux: int exec (char *prog, char **argv) Check privileges and file type Loads program at path prog into address space Replacing previous contents! Execution starts at main() Initializes context e.g. passes arguments *argv Place PCB on ready queue Preserves, pipes, open files, privileges, etc. 9
Executing a New Program fork() followed by exec() Creates a new process as clone of previous one First thing that clone does is to replace itself with new program 10
Fork + Exec shell-like int main(int argc, char **argv) { char *argvnew[5]; int pid; if ((pid = fork()) < 0) { printf( "Fork error\n ); exit(1); } else if (pid == 0) { /* child process */ argvnew[0] = "/bin/ls"; argvnew[1] = "-l"; argvnew[2] = NULL; if (execve(argvnew[0], argvnew, environ) < 0) { printf( "Execve error\n ); exit(1); } } else { /* parent */ wait(pid); /* wait for the child to finish */ } } 11
Creating a Process in NT CreateProcess( ); Handle Table Win32 Subsystem ntcreateprocess( ); ntcreatethread( ); What is the main difference between process creation in UNIX and Windows NT? NT Executive Process Descriptor NT Kernel 12
Process Hierarchies Parent-child relationship may be significant: parent controls children s execution Wait by parent Zombie Sleeping Done Request Uninterruptible Sleep I/O Request I/O Complete Request Running Allocate Schedule Resume Start Runnable Traced or Stopped Device Mgr Application Program fork() wait() exec() Process Mgr Memory Mgr UNIX File Mgr 13
Process Tree on a UNIX System 14
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. Parent is exiting. Operating system does not allow child to continue if its parent terminates. Cascading termination. 15