Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11 Process Management

Similar presentations


Presentation on theme: "Chapter 11 Process Management"— Presentation transcript:

1 Chapter 11 Process Management

2 Objectives  create a child process
 execute a new program within a process  terminate a process  wait for a child process to terminate

3 What is a process ?  a process is an executing program together with
information about the program in some table entries in kernel the program's stack and system stacks  the user area which contains information about a process  a process image is the layout of a process in memory  a process is not a program by itself

4 Components of Process code area - the executable code of a process
data area - static and global variables - heap area stack area - runtime stack to hold activation records including local variables kernel data structures to keep information one process table in kernel a user area per process information like open fds, signal actions, etc

5 Components of Process main() { … TEXT ... } DATA STACK USER AREA

6 Kernel processes and user processes
process in user mode a system call make it into kernel mode  kernel process process in kernel mode the code is linked directly into the kernel ( no executable file)

7 System Calls : Creating a new process
int fork( ) create a new process (child process) an almost-exact duplicate of the parent process code, data, stack, open file descriptors, etc. return value success parent process : child process id child process :0 fail -1

8 System Calls : Creating a new process

9 System Calls Process id int getpid() int exit (int status)
return a process's id int getppid() return a parent process's id int exit (int status) a process may terminate process by executing exit() closes a process's fds, deallocate its code, data, and stack sends its parent a SIGCHLD signal and waiting for its termination code to be accepted (zombie process)

10 System Calls Orphan process
If a parent dies before its child, the child is called an orphan process and is adopted by "init" process, PID 1. it is dangerous for a parent to terminate without waiting for the death of its child.

11 System Calls int wait(int* status)
wait() causes a process to suspend until one of its children terminate a successful call to wait() returns the pid of the child that terminated and places a status code into status

12 Example : fork and wait #include <stdio.h> main( ) {
int pid, status, childPid; printf(“I’m the parent process and my PID is %d\n”, getpid( )); pid = fork( ); if (pid != 0) { printf(“I’m the parent process with PID %d and PPID %d\n” getpid( ), getppid( )); 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”, exit(42); } printf(“PID %d terminates\n”);

13 System Calls:Exec int execl(char* path, char* arg0, char* arg1, ... , char* argn, NULL) int execv ( char* path, char* argv[]) replace the calling process's code, data, and stack with the executable whose pathname is path and starts to execute the new code. return : a successful exec() never returns failure : -1

14 Example : fork and exec call exec( ) after calling fork( )
create a new process and execute a program #include <stdio.h> main( ){ int fork( ); if (fork( ) == 0) { execl("/bin/echo", "echo", "this is message one", NULL); perror("exec one faild"); exit(1); } execl("/bin/echo", "echo", "this is message two", NULL); perror("exec two faild"); exit(2); execl("/bin/echo","echo", "this is message three",NULL); perror("exec three faild"); exit(3); printf("Parent program ending\n");

15 int chdir (char* pathname)
Etc  Changing directories int chdir (char* pathname) set a process's current working directory tot he directory pathname the process must have execute permission from the directory to succeed returns 0 if successful; otherwise -1  Accessing user and group ids int getuid() int setuid(int id) int geteuid() int seteuid(int id) int getgid() int setgid(int id) int getegid() int setgid(int id) returns the calling process's real and effective user or group ids

16 Example #include <stdio.h> main( ) { system(“pwd”); chdir(“/”);
chdir(“/user/faculty/chang”); }

17 Example: Background Processing
Execute a program in the background using fork( ) and exec( ) #include <stdio.h> main(argc, argv) int argc, argv[ ]; { if (fork( ) == 0) { execvp(argv[1], &argv[1]); fprintf(stderr, “Could not execute %s\n”,argv[1]); }

18 Example: Redirection %redirect out ls -l
duplicate the fd of “out” file to the std. Output file(fd=1) execute the command then all standard output will be redirected to the “ls.out” file #include <stdio.h> main(argc, argv) int argc, argv[ ]; { int fd; fd=open(argv[1], O_CREAT|O_TRUNC|O_WRONLY, 0600) dup2(fd, 1); close(fd); execvp(argv[2], &argv[2]); perror(“main”); }

19 Example: Redirection %command > output
1) The parent shell forks and then waits for the child shell to terminate 2) The child shell opens the file "output" and 3) The child shell duplicates the fd of "command" to the standard output fd, and close the fd of the original file 4) The child shell then exec's the “command”. All of the standard output of the command goes to "output" 5) When the child shell terminates, the parent resumes

20 Signals Unexpected events a floating-point error a power failure
an alarm clock the death of a child process a termination request from a user (Ctrl-C) a suspend request from a user (Ctrl-Z) Process Signal #8

21 Predefined signals 31 signals /usr/include/signal.h
See the table in page 427 Actions of the default signal handler terminate the process and generate a core(dump) ignores and discards the signal(ignore) suspends the process (suspend) resume the process

22 Terminal signal Terminal signals Control-C SIGINT signal Control-Z
SIGSTP signal Requesting an alarm signal SIGALRM signal Default handler display message “Alarm clock”

23 Example : Alarm Alarm.c #include <stdio.h> main( ) { alarm(3);
printf(“Looping forever …\n”); while (1); printf(“This line should never be executed\n”); }

24 Handling Signals: signal( )
signal(int sigCode, void (*func)( ))) specify the action for a signal sigCode func func SIG_IGN SIG_DFL user-defined function return the previous func

25 Handling Signals: Example
#include <stdio.h> #include <signal.h> int alarmFlag=0; alarmHandler( ); main( ) { signal(SIGALRM,alarmHandler); alarm(3); printf(“Looping …\n”); while(!alarmFlag) { pause( ); } printf(“Loop ends due to alarm signal \n”); } /* main */ alarmHandler( ) { printf(“An alarm clock signal was received\n”); alarmFlag = 1;

26 Example: Protecting Critical Code
#include <stdio.h> #include <signal.h> main ( ) { int (*oldHandler)( ); printf(“I can be Control-C’ed\n”); sleep(3); oldHandler = signal(SIGINT, SIG_IGN); printf(“I’m proctected from Control-C now\n”); signal(SIGINT, oldHandler); printf(“I can be Control-C’ed again\n”); }

27 Sending signals: kill()
int kill(int pid, int sigCode) send signal with value sigCode to the process with PID pid condition for success the owner of the sending process = the owner of pid the owner of the sending process = super-user

28 Example: Time Limit #include <stdio.h> #include <signal.h>
int delay; childHandler( ); main(argc, argv) int argc; char *argv[ ]; { int pid; signal(SIGCHLD,childHandler); pid = fork(); if (pid == 0) { execvp(argv[2], *argv[2]); perror(“Limit”); } else { sscanf(argv[1], “%d”, &delay); sleep(delay); printf(“Child %d exceeded limit and is being killed\n”, pid); kill(pid, SIGINT); } }

29 Example: Time Limit Usage %limit 5 ls %limit 4 sleep 100
childHandler( ) /* Executed if the child dies before the parent */ { int childPid, childStatus; childPid = wait(&childStatus); printf(“Child %d terminated within %d seconds\n”, childPid, delay); exit(0); } Usage %limit 5 ls %limit 4 sleep 100

30 Example: Suspending and Resume
#include <signal.h> #include <stdio.h> main( ) { int pid1, pid2; pid1 = fork( ); if (pid1 == 0) { while(1) { printf(“pid1 isalive\n”); sleep(1); } pid2 = fork( ); if (pid2 == 0) { while (1) { printf(“pid2 is alive\n”); sleep(1); } sleep(3); kill (pid1, SIGSTOP); sleep(3); kill(pid1, SIGCONT); sleep(3); kill(pid1, SIGINT); kill(pid2, SIGINT);

31 Booting Procedure The First Process
the first process with pid =0, called sched, is created by UNIX during boot time, and fork/exec twice immediately PID Name 0 sched 1 init 2 pagedaemon init process(/etc/init) is the root process creates processes based upon script files /etc/inittab, /etc/rc login process, mouting file systems, start daemon processes create getty's on each line that a user may login.

32 The process hierarchy  getty process(/etc/getty)  login process
detects line activity and prompts with login exec /bin/login, after entering a response to the login prompt.  login process checks a user's login and password against /etc/passwd exec /bin/sh or /bin/csh set the evnironment variables like HOME, LOGNAME, PATH...  shell process initially read commands form start-up files like /etc/.cshrc and $HOME/.cshrc shell starts its job

33 The process hierarchy


Download ppt "Chapter 11 Process Management"

Similar presentations


Ads by Google