Title: TDC368 UNIX and Network Programming
1TDC368UNIX and Network Programming
- Week 2
- UNIX process concepts
-
- Camelia Zlatea, PhD
- Email czlatea_at_cs.depaul.edu
2Course Outline
- UNIX process concepts.
- Process control primitives
- fork, exec, exit, wait, waitpid
- getpid, getppid
- library functions system
3UNIX basic commands
- Changing password
- passwd
- Information
- date
- cal
- who
- man
- Shells
- program that handles user interactions
- Bourne shell (sh) default prompt is dollar sign
- Korn shell (ksh) a superset of the Bourne shell
- C shell (csh) developed at UC Berkeley, part of
BSD version of UNIX default prompt is the
percent sign
File Manipulation cat file - displays the
content of file ls - list a directory cd -
change directory mkdir - create a new
directory rm file - delete file rm r file -
delete recursively a catalog
4PID0 swapper
PID1 init
init process activates a new process and
executed a program called getty for each
terminal port of the systems getty displays
the login prompt at the assigned terminal waits
for user to type userID execute a new program
called login
PID x gtty
fork - by init exec gtty
5PID0 swapper
PID1 init
login displays the password prompt at the
assigned terminal waits for user to type
password verifies userID and password executes
the user default shell
PID x login
fork - by init exec gtty exec login
6PID0 swapper
PID1 init
sh shell displays the prompt ( example ) and
is ready to accepts user commands
PID x sh
fork - by init exec gtty exec login exec sh
7PID0 swapper
PID1 init
fork - by init exec gtty exec login exec sh
PID x sh
PID y ls -l
fork - by sh exec ls
Example a shell command is
executed
8Shell Processes
- Execution of a Shell command (will explain this
in the next slides) - fork new process
- exec command
- wait shell waits for child process to terminate
- Displaying process table (process status)
- (PID process ID, TTYterminal. TIMEduration
the process is running, COMMANDname of the
command) - ps
- ps al
- Sequential execution of processes using shell
operator semicolon - ps ls
- Timing a delaying of a process
- sleep 20 --- delays the shell for 20 seconds
- sleep 120 echo I am awake!
9Background Processes
- Shell does not wait for child process to complete
- Returns prompter immediately
- shell fork fork exec
- shell child exits immediately after the second
fork, returns prompter - shell grand-child exec the command in
background - it is adopted by process init
- when user logout the background process is
terminated - sleep 30
- (sleep 120 echo I am awake!)
- The command nohup causes the background process
to be immune to terminating signals - nohup (sleep 120 echo I am awake!) and
then logout - the command process will continue execution in
background - where is the echo output displayed, since the
process is no longer associated with any
terminal? - The output is automatically save to nohup.out
10Terminating a process
- The kill command
- kill command sends a termination signal to the
specified process, with the given PID - Signal is an asynchronous event (identified by an
integer number) - Signals 1 to 15 are implemented on the majority
of UNIX systems - SIGTERM 15 (normal termination)
- SIGKILL9 (forced termination)
- Example
- sleep 30
- ps
- PID TTY TIME COMMAND
- 24000 tty001 005 csh
- 24001 tty001 000 sleep 30
- 24002 tty001 002 ps
- kill 9 24001
11UNIX Processes
- Process - system support
- Fork system call
- Process states
- Exec system call
- Exit function
- Background processes
- Parent-Child Synchronization (wait)
12Fork - System Call
- pid_t pid
- pid fork()
- if (pid -1)
-
- / fork failure, no more entries in process
table/ -
- else
- if (pid0)
- /child process/
-
- else
- / parent process /
-
13Fork - System Call
- The user process calls fork()
- Store system call parameters
- arguments, return address, local variables) into
the user stack. - Call corresponding kernel service
- execution of a trap instruction
- In kernel space save
- parameters, return address and local variables
for kernel routine - Execute kernel routine
- Normal Return
- cleans-up kernel stack
- switch from kernel to user mode
14Fork - System Call
- Successful return from system call
- parent and child processes
- share same text segment
- identical data segments
- identical user kernel stack segments
- same user structure
- parent normal return
- restores return address from user stack
- child pseudo return
- restores same return address as the parent from
its user stack
15 File Table
- Parent Process
- Child Process
text
data
stack
I-node Table
Open Files Current Directory
text
data
stack
Open Files Current Directory
16 Process States
- Initiated - fork()
- Ready-to-Run
- in ready queue for CPU access
- Running
- process quantum
- Blocked
- sleep(n) / deterministic delay /
- pause() / non-deterministic delay /
- Terminated
- exit(int status)
- Shell command ps options
17Process Model
New
created
Ready
wakeup
Quantum Expired
dispatch
Blocked/ Suspended
Running User Mode
sleep
exit
System Call Interrupt
Return
Running Kernel Mode
Zombie
Terminated
Interrupt Interrupt return
18Process Image Map
Process Table
Process Image
Proc. n
Proc. 1
Text/Code Segment
Data Segment
Stack Segment
Process Control Block
19(No Transcript)
20- User mode
- processes in use mode can access their own
instructions and data NOT kernel or other
processs code or data - Kernel mode
- process can access system(kernel) code and data
and user addresses - Kernel is part of each process
- Kernel executes on behalf of the process
- P1 P2 P3 P4
OS
HW
Kernel Mode User Mode
K
K
U
U
21 Process information
- include ltsys/types.hgt
- include ltunistd.hgt
- pid_t getpid(void)
- / get current process ID /
- pid_t getppid(void)
- / get ID of the parent of the process /
22- pid_t pid
- static int x
- x1
- pid fork()
- if (pid lt 0 ) perror(fork failure)
- else if (pid0)
- x
- else
- x--
- printf(process d xd\n, getpid(), x)
- What value(s) are printed for variable x?
23- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- void main(void)
- fork() printf(A\n)
- fork() printf(B\n)
- fork() printf(C\n)
-
- Comment on the above program output.
24- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- void main(void)
- int i
- for (i1 ilt3 i)
- fork()
- printf(PIDd id\n, getpid(), i)
-
- printf(PIDd id\n, getpid(), i)
-
- Comment on the above program output.
25- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- void main(void)
- int i
- for (i1 ilt3 i)
- if (fork()0) break
- printf(PIDd id\n, getpid(), i)
-
- printf(PIDd id\n, getpid(), i)
-
- Comment on the above program output.
26- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- void main(void)
- int i
- for (i1 ilt3 i)
- if (fork()gt0) break
- printf(PIDd id\n, getpid(), i)
-
- printf(PIDd id\n, getpid(), i)
-
- Comment on the above program output.
27- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- include ltstring.hgt
- void main(void)
- static char buf2
- if (fork()0) strcpy(buf,A\n)
- else strcpy(buf,B\n)
- sleep(10)
- write(1,buf,sizeof(buf))
-
- Comment on the above program execution
28Exec - System Call
- include ltunistd.hgt
- int execl(const char file, const char arg0, .. ,
const argn) - int execv(const char file, char argv)
- the process executes the the binary code from
file - if failure return -1
- if success does not return
29- File test1.c
- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- void main(void)
- int i
- for (i1 ilt3i)
- if (fork()0) / process child /
- execv(/bin/ps,ps -el)
-
- else / process partent /
- printf(Parent d\n,getpid())
-
-
- cc -o test1 test1.c
- test1
30- File test2.c
- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- void main(void)
- int i
- for (i1 ilt3i)
- if (fork()0) / process child /
- execv(child,child)
-
- else / process partent /
- printf(Parent d\n,getpid())
-
-
- cc -o test2 test2.c
31- File child.c
- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- void main(void)
- pid_t pid
- printf(Parent d\n,getpid())
- sleep(30)
-
- cc -o test2 test2.c
- cc -o child child.c
- test2
32Exit - Function Call
- include ltstdlib.hgt
- void exit(int status)
- status - parameter returned to parent process
- status0 - normal termination
- status - abnormal termination
- Effects
- close all open file descriptors
- notifies the parent (by signal)
- return status info to parent
- if parent no longer exists then PPD1 (is
adopted by process init)
33_exit - System Call
- include ltunistd.hgt
- void _exit(int status)
- status - process exit status code
- status0 - normal termination
- status0 - abnormal termination
- Effects
- terminates a process
- closes all open file descriptors
- De-allocates all process data and stack segment
- process becomes a zombie (it is no longer
scheduled to run) - init process clean up process table slot
34_exit - System Call
- include ltiostream.hgt
- include ltunistd.hgt
- int main()
-
- cout ltlt "Test for _exit" ltlt endl
- _exit(0)
- return 0
-
- CC -o test test.c test
- Test for _exit
- echo status
- 0
35Wait - System Call
- include ltsys/types.hgt
- include ltsys/wait.hgt
- pid_t wait(int status)
- returns -,1 if failure (check errno)
- no unwaited-for child process
- interrupt signal
- returns the child PID, is success
- synchronization
- parent waits for a child to terminate
- if more child-processes, waits for any
36Waitpid - System Call
- include ltsys/types.hgt
- include ltsys/wait.hgt
- pid_t waitpid(pid_t p, int status, int opt)
- Return value - a child process ID or -1
- Argument pid_t p - a child PID
- -1 waits for any child (same as wait)
- 0 waits for any child in same group w/ parent
- gt0 waits for child process with this PIDp
- lt-1 waits for any child with GID p
37Waitpid - System Call
- include ltsys/types.hgt
- include ltsys/wait.hgt
- pid_t waitpid(pid_t p, int status, int opt)
- Options values
- WNOHANG - do not block if status cannot be
obtained - WNOWAIT - keep process in wait status
- WUNTRACED - wait any stopped child process
38 Parent-Child Process Synchronization
- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- include ltsys/wait.hgt
- void main(void)
- pid_t pid int i, status
- for (i1 ilt3i)
- if (fork()0) execv(child,child)
- else
- printf(Parent d\n,getpid())
- while (pid wait(status) pid!-1)
- printf(Child d is done\n,getpid())
- exit(0)
39 Parent-Child Process synchronization (by
priorities)
- include ltstdio.hgt
- include ltsys/types.hgt
- include ltunistd.hgt
- include ltsys/wait.hgt
- void main(void)
- pid_t pid3, p int i, status
- for (i0 ilt3i)
- if ((pidifork())0) execv(child,child
) - else printf(Parent d\n,getpid())
- for(i0(pwaitpid(pidi, status,0)p!-1i)
- if (p!-1) (errnoEINTR)
- printf(Signal Caught s\n,strerrno(errno))
- exit(0)
40 system function
- include ltstdlib.hgt
- int system(const char cmd)
User program system(ps -e) System calls
actions fork() //process creation
service execl(/bin/ps, ps -e) //code
execution