40
Process Management CS3320 Spring 2008

Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

Embed Size (px)

Citation preview

Page 1: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

Process Management

CS3320

Spring 2008

Page 2: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

Process• A process is an instance of a running program.

– Not the same as “program” or “processor”

• Process provides each program with two key abstractions:– Logical control flow

• Each program seems to have exclusive use of the CPU.

– Private address space• Each program seems to have exclusive use of main memory.

• How are these Illusions maintained?– Process executions interleaved (multitasking)– Address spaces managed by virtual memory system

Page 3: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

main Function int main(int argc, char *argv[ ])

– The starting point of a C program. Programs written in different languages have a different name.

– A special start-up routine is called to set things up first before call main()

• Set up by the link editor (invoked by the compiler)

Disk Memory

Program Counter

Page 4: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

Booting

• Bootstrap program– Initialize all aspects of the system, e.g., CPU

registers, device controllers, memory, etc.– Load and run the OS

Page 5: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

Process Control• Special Processes

– PID 0 – Swapper (I.e., the scheduler)• Kernel process

• No program on disks correspond to this process

– PID 1 – init responsible for bringing up a Unix system after the kernel has been bootstrapped.

• User process with superuser privileges

• Initializing system processes, e.g., various daemons, login processes, etc.

• /etc/rc* & init or /sbin/rc* & init

– PID 2 - pagedaemon responsible for paging• Kernel process

Page 6: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

A Tree of Processes on a Typical Solaris

Page 7: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 8

Init Process

• When UNIX starts (boots), there is only one process, called init, with process ID = 1.– The only way to create a new process is to duplicate an

existing process

– So init is the ancestor of all subsequent processes.• Initially, init duplicates (forks) several times and each child

process replaces its code (execs) with the code of the executable getty which is responsible for user logins.

Page 8: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

Memory tables

I/O tables

File tables

Primary process table

Process 1

Process 2

Process 3

Memory

Devices

Files

Processes

Process image

Process image

Process 1

Process 3

General Structure of Operating System General Structure of Operating System Control TablesControl Tables

Page 9: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

Process Control Block (PCB)

Information associated with each process• Process state• Program counter• CPU registers• CPU scheduling information• Memory-management information• Accounting information• I/O status information

Page 10: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

Process States

Page 11: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

Process Creation & Termination

Page 12: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

If ((pid=fork()) == 0){{ …

}else {

}exit(0);

if ((pid=fork() == 0){{ …

}else {

}exit(0);

Parent Child

fork()

What’s a Fork()

• Child is an exact copy of the parent process. • They have their own memory space.

Page 13: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

fork#include <sys/types.h>

#include <unistd.h>

pid_t fork(void);– The only way beside the bootstrap process to create a

new process.– Call once but return twice

• 0 for the child process (getppid)• Child pid for the parent (1:n)

– Cannot predict which process runs first• Process scheduling

– Run the same code concurrently, but have completely separate stack and data space

Page 14: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 15

Example Program with getpid()#include <stdio.h>main () { int pid; printf ("I'm the original process with PID %d and PPID %d.\n", getpid (), getppid ()); pid = fork (); /* Duplicate. Child & parent continue from here */ if (pid != 0) /* pid is non-zero, so I must be the parent */ { printf ("I'm the parent process with PID %d and PPID %d.\n", getpid (), getppid ()); printf ("My child's PID is %d\n", pid); } else /* pid is zero, so I must be the child */ { printf ("I'm the child process with PID %d and PPID %d.\n", getpid (), getppid ()); } printf ("PID %d terminates.\n", getpid () ); /* Both processes execute this */}

Page 15: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 16

Example Program with getpid()$ myforkI'm the original process with PID 639 and PPID 416.I'm the parent process with PID 639 and PPID 416.My child's PID is 640PID 639 terminates.I'm the child process with PID 640 and PPID 1.PID 640 terminates. $

Page 16: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

fork• Normal cases in fork:

– The parent waits for the child to complete.– The parent and child each go their own way (e.g.,

network servers).

• Inherited properties:– Real/effective [ug]id, supplementary gid, process group ID,

session ID, controlling terminal, set[ug]id flag, current working dir, root dir, file-mode creation mask, signal mask & dispositions, FD_CLOEXEC flags, environment, attached shared memory segments, resource limits

• Differences on properties:– Returned value from fork, process ID, parent pid, tms_[us]time,

tms_c[us]time, file locks, pending alarms, pending signals

Page 17: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

fork• Reasons for fork to fail

– Too many processes in the system– The total number of processes for the real uid

exceeds the limit• CHILD_MAX

• Usages of fork– Duplicate a process to run different sections of code

• Network servers

– Want to run a different program• shells (spawn = fork+exec)

Page 18: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 19

Orphan Processes

• If a parent process terminates before its child terminates, the child process is automatically adopted by the init process

• The following program shows this.

Page 19: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 20

Example Program of Orphan#include <stdio.h>main () { int pid; printf ("I'm the original process with PID %d and PPID %d.\n", getpid (), getppid ()); pid = fork (); /* Duplicate. Child & parent continue from here */ if (pid != 0) /* Branch based on return value from fork () */ { /* pid is non-zero, so I must be the parent */ printf ("I'm the parent process with PID %d and PPID %d.\n", getpid (), getppid ()); printf ("My child's PID is %d\n", pid); } else { /* pid is zero, so I must be the child */ sleep (5); /* Make sure that the parent terminates first */ printf ("I'm the child process with PID %d and PPID %d.\n", getpid (), getppid ()); } printf ("PID %d terminates.\n", getpid () ); /* Both processes execute this */}

Page 20: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 21

Example Program of Orphan$ orphanI'm the original process with PID 680 and PPID 416.I'm the parent process with PID 680 and PPID 416.My child's PID is 681PID 680 terminates.$ I'm the child process with PID 681 and PPID 1.PID 681 terminates.

Page 21: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 22

Example of exit()% cat myexit.c#include <stdio.h>#include <stdlib.h> /* needed for exit */

main() { printf("I am going to exit with status code 42\n"); exit(42);}

% myexitI am going to exit with status code 42% echo status is $?42

Page 22: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 23

Zombie Processes• A process cannot leave the system until parent

process accepts its termination code– even if it has exit-ed

• If parent process is dead; init adopts process and accepts code

• If the parent process is alive but is unwilling to accept the child's termination code – because it never executes wait()– the child process will remain a zombie process.

Page 23: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 24

Zombie Processes

• Zombie processes do not take up system resources – But do use up an entry in the system's fixed-

size process table– Too many zombie processes is a problem

Page 24: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 25

Zombie Example#include <stdio.h>main () { int pid;

pid = fork (); /* Duplicate */

/* Branch based on return value from fork () */ if (pid != 0) { /* Never terminate, never execute a wait () */ while (1) sleep (1000); } else { exit (42); /* Exit with a silly number */ }}

Page 25: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 26

Zombie Example

$ zombie &[1] 684$ ps 684 p4 S 0:00 zombie 685 p4 Z 0:00 (zombie <zombie>) 686 p4 R 0:00 ps$ kill 684[1]+ Terminated zombie$ ps 688 p4 R 0:00 ps$

Page 26: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 27

Waiting for a Child: wait()

• pid_t wait(int *status)

• Causes a process to suspend until one of its child processes terminates.

• A successful call to wait() – returns the PID of the child process that

terminated– places a status code into status

Page 27: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 28

Waiting for a Child: wait()

• If a process executes a wait() and has no children, wait() returns immediately with a value of -1

• If a process executes a wait(),– And one or more children are already zombies, – Then wait() returns immediately with the status

of one of the zombies

Page 28: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 29

Example Program using wait()#include <stdio.h>main () { int pid, status, childPid; printf ("I'm the parent process and my PID is %d\n", getpid()); pid = fork (); /* Duplicate */ if (pid != 0) /* Branch based on return value from fork () */ { printf ("I'm the parent process with PID %d and PPID %d\n", getpid (), getppid ()); /* Wait for a child to terminate. */ childPid = wait (&status); printf("A child with PID %d terminated with exit code %d\n", childPid, status >> 8); } else { printf ("I'm the child process with PID %d and PPID %d\n", getpid (), getppid ()); exit (42); /* Exit with a silly number */ } printf ("PID %d terminates\n", getpid () );}

Page 29: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 30

Example Program using wait()$ mywaitI'm the parent process and my PID is 695I'm the parent process with PID 695 and PPID 418I'm the child process with PID 696 and PPID 695A child with PID 696 terminated with exit code 42PID 695 terminates

Page 30: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 31

Differentiating a Process: exec

• With the exec family, a process replaces– Its current code – Data– Stack

• PID and PPID stay the same

• Only the code that the process is executing changes

Page 31: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 32

Differentiating a Process: exec

• Use the absolute or relative name– int execl(const char* path, const char* arg0, ...,

const char* argn, NULL)– int execv(const char* path, const char* argv[ ])

Page 32: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 33

Differentiating a Process: exec

• Use $PATH variable to find the executable:– int execlp(const char* path, const char*

arg0, ..., const char* argn, NULL)– int execvp(const char* path, const char*

argv[ ])

Page 33: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 34

Differentiating a Process: exec

• argi or argv[i]: ith command line argument for executable (arg0: name of executable)

• If executable is not found, -1 is returned – otherwise the calling process replaces its code,

data, and stack with those of the executable and starts executing the new code

• A successful exec() never returns

Page 34: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 35

Example Using execl()#include <stdio.h>main () { printf ("I'm process %d and I'm about to exec an ls -l\n", getpid ()); execl ("/bin/ls", "ls", "-l", NULL); /* Execute ls */ printf ("This line should never be executed\n");}

$ myexecI'm process 710 and I'm about to exec an ls -ltotal 38-rw-rw-r-- 1 raj raj 187 Jul 22 20:24 alarm.c-rw-rw-r-- 1 raj raj 226 Jul 22 20:22 background.c-rw-rw-r-- 1 raj raj 284 Jul 22 20:22 mychdir.c-rw-rw-r-- 1 raj raj 2058 Jul 22 20:23 vount.c-rwxrwxr-x 1 raj raj 4174 Jul 24 12:08 zombie-rw-rw-r-- 1 raj raj 298 Jul 22 20:20 zombie.c

Page 35: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 36

Changing Directories: chdir()

• Every process has a current working directory – Used when processing a relative path name – A child process inherits the current working

directory from its parent

• Example: when a utility is run from a shell, the utility process inherits the shell's current working directory

Page 36: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 37

Changing Directories: chdir()

• int chdir(const char* pathname)

• Sets a process' current working directory to pathname

• The process must have execute permission from the directory for chdir() to succeed

• chdir() returns – -1 if it fails– 0 if it succeeds

Page 37: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 38

Example Using chdir()#include <stdio.h>main () { /* Display current working directory */ system ("pwd"); /* Change working directory to root directory */ chdir ("/"); /* Display new working directory */ system ("pwd"); chdir ("/home/naveen"); /* Change again */ system ("pwd"); /* Display again */}

$ mychdir/home/raj/3320/ch12//home/naveen$

Page 38: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 39

Changing Priorities: nice()• Child process inherits priority of parent process • Can change the priority using nice()• int nice(int delta)• Adds delta to the priority

– Legal values of priority -20 to +19– Only super user can change to negative priority.– Smaller the priority value, faster the process will run.

Page 39: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

23/4/21 cs3320 40

Getting and Setting User and Group IDs:

• The get functions always succeed

• The set methods will succeed only when executed by super user or if id equals real or effective id of the process.

uid_t getuid() uid_t setuid(uid_t id)uid_t geteuid() uid_t seteuid(uid_t id)gid_t getgid() uid_t setgid(gid_t id)gid_t getegid() uid_t setegid(gid_t id)

Page 40: Process Management CS3320 Spring 2008. Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides

exit• Termination

– The same code in the kernel is finally executed.• Close all open descriptors, release memory, and the

like.

– Exit status vs. termination status• Exit status (arg from exit, _exit, or return)

termination status– In abnormal case, the kernel generates it.

• wait & waitpid to retrieve the termination status.