Title: Ch3201'ppt
1Unix Processes
- Agenda
- Unix state diagram
- Unix Primitive for task creation fork
- Parent / child example
- Exec, system calls
- Shell example
- Task Termination
- Inter Process Communication
2Fork command
- A low-level primitive for process creation
- The process created had an identical image of the
process that created it. - It refers to the same code in the memory, but
creates different data segments in memory - When initiated the registers have the same values
as the parent process
3Fork Process Mapping
Code
4Parent/Child Example
- include ltstdio.hgt
- define DIM 8
- Void main()
- int pidint i, answerint arrDIM
1,2,3,4,5,6,7,8printf(Im the main process
\n) - if ((pid fork()) 0) / this is
the child process / pid getpid() - answer 0 for (i 0 i lt DIM
i) answer arri
printf(Child (d) sum d\n, answer) - exit (0)
- / Of child process /
/ parent code continue here/ If (pid lt
0) fprintf(stderr, fork failed\n)
exit (1) answer 1 for (i 0 i
lt DIM i) answer arri
printf(Parent product d\n, answer)
5Possible Output
Im the main process Child (1945) sum 1 Child
(1945) sum 3 Parent product 1 Child (1945)
sum 6 Child (1945) sum 10 Parent product
1 Parent product 6 Parent product 24 Child
(1945) sum 15 Parent product 120 Child
(1945) sum 21 Parent product 720 Child
(1945) sum 28 Child (1945) sum 36 Parent
product 5040 Parent product 40320
6Unix Boot Procedure
- When the system boot it creates a process with
pid 1 - This process creates all the other system
processes using fork command
7Exac family
- There is a way to produce several processes by
executing different files - execl, execv, execle, execlp, execvp execute a
file - SYNOPSIS
- int execl (path, arg0 ,arg1, ...argn, (char
)0) - char path, arg0, arg1, ...,argn
- int execv(path, argv)
- char path, argv
- int execle (path, arg0 ,arg1, ...argn, (char
)0, envp) - char path, arg0, arg1, ...,argn, envp
- int execlp (path, arg0 ,arg1, ...argn, (char
)0) - char path, arg0, arg1, ...,argn
- int execvp (file, argv)
- char file, argv
- extern char envronment
- Each process has its own code area
8Exac family
- There is a way to produce several processes by
executing different files. - Exec are a family of system calls for creation
new process in the system - execl, execv, execle, execlp, execvp execute a
file - SYNOPSIS
- int execl (path, arg0 ,arg1, ...argn, (char
)0) - char path, arg0, arg1, ...,argn
- int execv(path, argv)
- char path, argv
- int execle (path, arg0 ,arg1, ...argn, (char
)0, envp) - char path, arg0, arg1, ...,argn, envp
- int execlp (path, arg0 ,arg1, ...argn, (char
)0) - char path, arg0, arg1, ...,argn
- int execvp (file, argv)
- char file, argv
- extern char envronment
- Each process has its own code area
9Exec state attributes
- The following remain the same after an exec call
- Process ID
- Parent Process ID
- Process Group ID
- Access groups
- Working directory
- Root directory
- Control terminal
- Resource usage
- Interval timers
- Resource limits
- File mode mask
- Signal mask
- Open descriptors
10Exec Process Mapping
Code x
Code y
Code z
11Shell example
- The shell is a process that handle terminal
commands - For each command, a new shell is created that
take care of the command.
void shell () char Command80, Params100
int pid, status while (1)
typePrompt() readParams(Command,
Params) if ((pid fork()) lt 0)
printf("Unable to execute command\n")
continue if (pid ! 0)
wait (pid, status) else / this is the
new child process / execve (Command,
Params, 0) / Of while /
12Process creation by shell command
- Processes can be created using shell command
- int system (char cmd)
- Execute the command in sh.
- Waits for the command to terminate, then return
- Sh performs all normal processing on cmd before
executing - char cmd80
- sprintf(cmd, "cp s s\n", file1, file2)
- system (cmd)
- Input / output control is done via popen()
- FILE popen(char cmd, char mode)
- void pclose (FILE stream)
- Mode r, w, rw.
13Shell Example
- include ltstdio.hgt
- void main (int argc, char argv)
- int i, count, total 0
- char cmd 80
- FILE in
- for (i1 i lt argc i)
- sprintf(cmd, "wc -w s\n", argvi)
- in popen(cmd, "r")
- fscanf(in, "d", count)
- total count
- pclose(in)
-
- printf("Total count is d words\n", total)
- return (0)
14Process Termination
- Process termination occurs in 3 ways
- Process runs to completion, main return
- Process call exit
- Process encounters execution error or receives an
interrupt signal - void exit(int status)
- Has the following consequences
- All the open I/O descriptors in the process are
closed. - If the patent process of the terminating process
is executing a wait or is interested in the
SIGCHLD signal, then it is notified of the
termination, and the low-order byte of the
statues is made available to it. - If the terminating process has at least one
executing Childs, the parent process ID of the
children is set 1 (init process)
15Inter Process Communication PIPES
- In memory communication channel between
processes. - Fifo mechanism
- Has two descriptors read write
- Usually use a buffer of 4096 bytes
- When buffer is full the writer blocks
- Reading more then there are available bytes
results in one of the following - Return EOF(0) if the buffer is empty and the
write end of the pipe has been closed - Return whats left on the pipe if the buffer is
not empty and the write end of the pipe has been
closed - Blocks the reading process if at least one
descriptor to the write of the pipe remains open
16Pipe Example
include ltstdio.hgtinclude ltstring.hgtinclude
ltsys/wait.hgtdefine READ 0define WRITE
1void main (int argc, char argv) int
p2 int i, pid1, status char buffer
20 pipe(p) if ((pid fork()) 0)
pclose(pWRITE) while ((i
read(pREAD,buffer, 6)) ! 0)
bufferi '\0' printf("d chars
's' received by child\n", i, buffer)
exit(0)
/in parent / close (pREAD) write
(pWRITE, "Hello there", sizeof("Hello
there")) write (pWRITE, "from me", sizeof
(from me")) pclose(pWRITE) while
(wait(status) ! pid ) if (status )
printf("Child failed \n") else
printf("Child finished\n") return (0)
17Pipe between 2 UNIX processes
pipe(p) if ((pid1 fork()) 0) / child
1 / pclose(pWRITE) dup (pREAD,
0) / stdin becomes duplication of pREAD/
pclose(pREAD) / not used /
execvp(argvi1, argvi1) / continue
after execvp ony if problem / sprintf(msg,
"Couldn't exec s", argvi1)
perror(msg) exit(1) if ((pid2
fork()) 0) / child 2 /
pclose(pREAD) dup (pWRITE, 1) /
stdin becomes duplication of pREAD/
pclose(pWRITE) / not used /
execvp(argv1, argv1) / continue
after execvp ony if problem / sprintf(msg,
"Couldn't exec s", argv1) perror(msg)
exit(1)
- include ltstdio.hgt
- include ltstring.hgt
- define READ 0
- define WRITE 1
- void main (int argc, char argv)
- int p2
- int i, pid1, pid2, status
- char msg200
- argv / ignore argv0/
- for (i 1 i lt argc i)
- if (strcmp(argvi, "") 0)
- / break into two commands/
- argvi '\0'
- break
-
-
18Pipe between 2 UNIX processes (cont)
- / in parent /
- close (pREAD)
- pclose(pWRITE)
- while (wait(status) ! pid2 )
- if (status )
- printf("Child two failed \n")
- else
- printf("Child two finished\n")
- return (0)
-