Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 1
Lecture 15
Log into Linux. Copy files from
/home/hwang/cs375/lecture15/*.*
Reminder: Project 4 due today. Project 5 posted to course webpage. Questions?
Lecture 15 Log into Linux. Copy files from - - PowerPoint PPT Presentation
Lecture 15 Log into Linux. Copy files from /home/hwang/cs375/lecture15/*.* Reminder: Project 4 due today. Project 5 posted to course webpage. Questions? Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 1 Outline
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 1
Log into Linux. Copy files from
Reminder: Project 4 due today. Project 5 posted to course webpage. Questions?
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 2
Introduction to processes Creating new processes Note: this material is covered in both BLP and
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 3
Suppose we are writing an application that
We could do one of the following to solve this
write the required routines from scratch find and use library routines that do the work use existing programs such as gnuplot or the PNM
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 4
The third approach can be very powerful. It is
UNIX allows us run other programs from our
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 5
UNIX allows us to do this in a manner that is
In BLP Chapter 11 and AUP Chapter 5, we
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 6
A program is a collection of instructions and
A process is a running program. It consists of
The instruction and user data segments are
System data includes the current directory,
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 7
A new child process is created by the kernel on
A child inherits most of the system data from
Each process has a unique process ID (PID - a
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 8
Each process is a member of a unique process
One process is the process group leader. The
Only one process group is the foreground
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 9
At the command line use “ps ajx” to list the
A parent may wait for a child to terminate by
If a child ends before the parent calls wait( ),
If a parent ends, a child's parent process ID is
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 10
The system( ) function (defined in <cstdlib>)
The parent process waits until the program
The program is run just as if the following had
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 11
The system( ) function is built upon the fork( ),
fork( ) creates a child process that is a clone of
The exec( ) routine reinitializes a process from
fork( ) and exec( ) are usually used together.
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 12
There are six exec( ) calls. First execl( ) -
Here is an example:
execl(“/bin/ls”, “ls”, “-al”, “/tmp”, (char *)0);
Recall that a new process is not created by
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 13
exec( ) is the only way to execute a program
Other exec( ) calls are execlp( ), execle( ),
execv*( ) routines expect the arguments to be
exec*p( ) routines look for the program in the PATH
exec*e( ) routines pass a pointer to a new
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 14
If we only had exec( ) there would be only one
The fork( ) system call is the only way to create
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 15
The child gets copies of the parent's open file
When fork( ) returns, both processes (parent
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 16
For example, the following code:
might display (exact order depends on which
See frk_xmpl1.cpp
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 17
Examine frk_xmpl2.cpp How many processes will be created? Can
See vssh_xmpl.cpp for a very simple shell
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 18
wait( ) causes the parent to sleep until any
It is not necessary for a parent to wait. See wait_xmpl.cpp for example code.
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 19
wait( ) returns the child's PID and passes back
A stopped process is different than a
Tuesday, October 19 CS 375 UNIX System Programming - Lecture 15 20
waitpid( ) allows you to: (1) wait on a specific
If pid == -1, waitpid( ) waits for any child.