csci 330 unix and network programming unit xii: process & pipe part 1
TRANSCRIPT
![Page 1: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/1.jpg)
CSCI 330UNIX and Network Programming
Unit XII: Process & Pipe
Part 1
![Page 2: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/2.jpg)
2
Unit Overview• Process Management
• create new process• change what process is doing
• Pipe concept• enables inter-process communication
CSCI 330 - UNIX and Network Programming
![Page 3: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/3.jpg)
Process Management System Calls• fork
• create a new process
• wait• wait for a process to terminate
• exec• execute a program
3CSCI 330 - UNIX and Network Programming
![Page 4: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/4.jpg)
System Call: fork• creates new process that is duplicate of current process• new process is almost the same as current process
• new process is child of current process• old process is parent of new process
• after call to fork, both processes run concurrently
4CSCI 330 - UNIX and Network Programming
![Page 5: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/5.jpg)
System Call: fork timeline
5
before
after
CSCI 330 - UNIX and Network Programming
![Page 6: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/6.jpg)
System Call: fork example
6CSCI 330 - UNIX and Network Programming
![Page 7: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/7.jpg)
System Call: fork• new process is almost the same as current process
pid_t fork(void)
• return value of fork is different:
parent: fork returns process id of child process
child: fork returns 0• fork returns -1 on failure
7CSCI 330 - UNIX and Network Programming
![Page 8: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/8.jpg)
System Call: fork
8CSCI 330 - UNIX and Network Programming
![Page 9: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/9.jpg)
System Call: fork example
9CSCI 330 - UNIX and Network Programming
![Page 10: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/10.jpg)
System Call: waitpid_t wait(int *status)
• lets parent process wait until a child process terminates• parent is restarted once a child process terminates
• returns process id of terminated child• return -1 if there is no child to wait for
• status holds exit status of child• can be examined with WEXITSTATUS(status)
10CSCI 330 - UNIX and Network Programming
![Page 11: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/11.jpg)
System Call: wait example
11CSCI 330 - UNIX and Network Programming
![Page 12: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/12.jpg)
System Call: exec• family of functions that replace current process image with
a new process image
• actual system call: execve• library functions
•execl, execlp, execle•execv, execvp
• arguments specify new executable to run and its arguments and environment
12CSCI 330 - UNIX and Network Programming
![Page 13: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/13.jpg)
C Library Functions: exec
13CSCI 330 - UNIX and Network Programming
![Page 14: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/14.jpg)
C Library Function: execlint execl(const char *path, const char *arg, ...)
• starts executable for command specified in path• new executable runs in current process
• path is specified as absolute path• arguments are specified as list, starting at argv[0],
terminated with (char *NULL)• new executable keeps same environment• returns -1 on error
14CSCI 330 - UNIX and Network Programming
![Page 15: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/15.jpg)
System Call: getpid
15CSCI 330 - UNIX and Network Programming
![Page 16: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/16.jpg)
C Library Function: execl
16CSCI 330 - UNIX and Network Programming
![Page 17: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/17.jpg)
C Library Functions: exec• execl, execlp, execle
• specify arguments and environment as list
• execv, execvp• specifiy arguments and environment as array of string values
• execlp, execvp• look for new executable via PATH
17CSCI 330 - UNIX and Network Programming
![Page 18: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/18.jpg)
C Library Function: execv
18CSCI 330 - UNIX and Network Programming
![Page 19: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/19.jpg)
Together: fork and exec• UNIX does not have a system call to spawn a new
additional
process with a new executable
• instead:• fork to duplicate current process• exec to morph child process into new executable
19CSCI 330 - UNIX and Network Programming
![Page 20: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/20.jpg)
Together: fork and exec
20CSCI 330 - UNIX and Network Programming
![Page 21: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/21.jpg)
UNIX Pipe• can create a software pipeline:
set of processes chained by their standard streams• output of one process becomes input of second process
command line example:
ls | wc
implemented via pipe system call
21CSCI 330 - UNIX and Network Programming
![Page 22: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/22.jpg)
System Call: pipe
22CSCI 330 - UNIX and Network Programming
![Page 23: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/23.jpg)
System Call: pipeint pipe(int pipefd[2])
• creates a channel to transport data• has direction: one side to write, one side to read
• available via 2 file descriptors pipefd[2]• read side pipefd[0]• write side pipefd[1]
• can be used synchronize producer and consumer of data
23CSCI 330 - UNIX and Network Programming
![Page 24: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/24.jpg)
System Call: pipe example
24CSCI 330 - UNIX and Network Programming
![Page 25: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/25.jpg)
System Calls: pipe and fork• Idea: read and write end of pipe in different processes
• fork creates two processes
• parent process: • close read end of pipe• write to write end of pipe
• child process:• close write end of pipe• read from read end of pipe
25CSCI 330 - UNIX and Network Programming
![Page 26: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/26.jpg)
System Call: pipe & fork example
26CSCI 330 - UNIX and Network Programming
![Page 27: CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1](https://reader033.vdocuments.net/reader033/viewer/2022061602/5697bfd01a28abf838caa79c/html5/thumbnails/27.jpg)
27
Summary• Process Management
• create new process (fork)• wait for child process (wait)• change what a process is doing (exec)
• Pipe concept• Pipe for inter-process communication
CSCI 330 - UNIX and Network Programming