Download presentation
Presentation is loading. Please wait.
Published byBarry Byrd Modified over 9 years ago
1
scis.regis.edu ● scis@regis.edu CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1
2
Topics Midterm review Systems Programming Process Management Homework 3 Q&A 2
3
Process Management First process is init Process are created by processes ▫Calling fork() ▫One of few calls that return twice Child process inherits parent’s process table entries, except for PID, PPID, and running times Call to fork returns the PID of the child process to the parent and 0 to the child Best practice is to test the return of fork() 3
4
First Processes PID 0 (sched) – created at boot PID 0 executes fork and exec twice to create processes 1 (init) and 2 (pageout) 4 Process 1 fork/exec process 48 process 12 process 34 fork/exec
5
Process States 5 RunningZombiedRunnableIdle Sleeping Suspended Initialize Signal Exit Wait on Event Occurs Allocated CPU
6
Parent process A process creates a child process Parent waits until children processes terminate He return code from the child is passed to the parent to take appropriate action If the parent process terminates unexpectedly, all its children processes are inherited by init When a child terminates, it waits to die until parent acknowledges its termination 6
7
Unix process-oriented system calls fork – duplicates a process getpid – obtains process ID number getppid – obtains parent process ID number exit – terminates a process wait – waits for child process exec – replaces the code, data, and stack of a process 7
8
New process with fork 8 The parent calls fork and two return from it ▫The parent and child continue to run the same code concurrently ▫Each has a different stack and data spaces If fork succeeds, it returns the PID of the child to the parent and returns 0 to the child If fork fails, it returns -1 and there is no child process created
9
Process IDs A process can get its process ID and parent ID ▫getpid = returns process ID ▫getppid = returns parent process ID See example program on page 475 in UPU You can try the program if you have a C/C++ compiler For cygwin, you can use g++ Some examples follow 9
10
Orphan program 10
11
Executing Orphan.c 11
12
Orphan processes If a parent dies before its children processes, init adopts them The original PPID becomes 1 (init) Note how the child inherits PPID after parent terminates 12
13
Basic fork 13
14
Compiling and running fork 14
15
Running fork multiple times 15
16
Executing other programs Can call other programs with exec When running exec, your process current image is replaced with the program’s image When calling a program, your current process never terminates ▫It is replaced by the called program 16
17
Exec program 17
18
Compile and run program 18
19
Explanation Displays message on the screen Calls one of exec function ▫execl means to pass an argument ▫execlp means to search the path First argument is the program to run The rest of the parameters are passed to the programs as arguments (argv) ▫argv[0] is the name of the program (duplicated) ▫argv[1] is the directory list ▫argv[2] null pointer – end of arguments 19
20
Exec family of programs Exec followed by one or more characters ▫e = array of pointes to variables ▫l = command line ▫p = use PATH variable ▫v = command line arguments passes as array of pointers Arguments (arv[x]): ▫0 – name of program ▫Rest depend on the program being executed 20
21
Notes on executed program New program inherits no code or data from current process Signals and signal handlers are cleared Security information and PID of process are retained ▫Including UID of owner ▫File descriptors remain open and available to new program 21
22
Child process clean up If parent terminates right away, no problem If parent continues to execute and the child exits, parent must clean up ▫Or zombie processes hang around 22
23
Zombie example 23
24
Zombie run 24
25
Waiting for a child 25
26
Executing mywait 26
27
Waiting for a process to continue When we exec a program, the original process is no longer active How does the shell remain active when we execute commands? We want the shell to remain dormant while the command executes and then we want to return to the shell 27
28
Exec and Wait – first part 28
29
Exec and wait – main 29
30
Exec and wait – aux functions 30
31
Exec and wait run 31
32
What happened? The code invokes the shell in the child ▫PS1 sets the prompt for the shell The child invokes the shell We get interactive access to the shell while the child waits When we exit, we return to the child, and the child exits Parent notices child exited and then parent exits Parent is blocked until child is done 32
33
Other programs myexec.c – pp. 480-481 chdir.c - pp 481-482 nice.c pp482-483 background.c – pp.484 33
34
Signals Sometimes called interrupts Called when unexpected things happen: ▫Floating point errors ▫Power failures ▫Alarm clock ‘ring’ ▫Death of child process ▫User termination ▫User suspend request Signal handler is called to act on it 34
35
Signals 1 – SIGHUP: hangup 2 – SIGINT: interrupt 3 – SIGQUIT: quit 4 – SIGILL: invalid instruction 5 – SIGTRAP: trace trap 6 – SIGABRT: abort 7 – SIGEMT: emulator trap instruction 8 – SIGFPE: arithmetic exception 9 – SIGKILL: kill 35
36
Signals (Cont’d) 10 – SIGBUS: bus error 11 – SIGSEGV: segmentation violation 12 – SIGSYS: bad argument to system call 13 – SIGPIPE: write on pipe and no reader 14 – SIGALRM: alarm clock 15 – SIGTERM: software termination 16/17 – SIGUSR1/1: user signal 1/2 18 – SIGCHLD: child status changed 19 – SIGPWR: power fail or restart 36
37
Signals (Cont’d) 20 – SIGWINCH: window size changed 21 – SIGURG: urgent socket condition 22 – SIGPOLL: pollable event 23 – SIGSTOP: stopped (signal) 24 – SIGSTP: stopped (user) 25 – SIGCONT: continued 26/27 – SIGTTIN/SIGTTOU: stopped (in/out) 28 – SIGVTALRM: virtual timer expired 29 – SIGPROF: profiler timer expired 30/31 – SIGXCPU/SIGFSZ: CPU/File limit exceeded 37
38
Suspend/ Resume Processes UPU pp. 497-498 38
39
Suspend/ Resume Processes (Cont’d) UPU pp. 497-498 39
40
Pulse.exe 40
41
Control Terminals - terminates a process and its children Every process is a member of a process group When a process forks, children inherit its process group from its parent Every process has a control terminal – the process where the process was started When - is detected, the terminal sends the signal to all processes in the process group 41
42
Pipe example (p.505) 42
43
Pipe example (Cont’d) 43
44
Pipe execution 44
45
Questions? 45
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.