L7: Processes Flashcards
Process Creation:
Key Process Routines
- fork
- exec
- wait
Process Communication:
Key Routines
These facilitate read/write between processes:
- pipe
- dup
- sockets
Process Controlling:
Key Routines
Cause and Handle exeptions and interrupts:
- signal
- alarm
3 Major Categories of
Process Operations
- Process Creation
- Process Communication
- Process Control
fork() function:
Basic Idea
Clone an existing process,
then have it run an alternate set of code from
the parent.
The child “forks” from the parent.
Which library contains the “fork()” function?
unistd.h
Forking:
Sequence of steps
- execute “fork()”
- The complete parent state is cloned to create the child process.
- Child is created.
- The “fork()” call returns
- pid of child, in parent process
- 0, in child process
- Return value is used to determine next step:
- parent executes parent code
- child executes child code
Code:
Have Parent Process
wait for
Child Process to finish
int status;
if( fork() ) {
//parent process stuff
wait(&status); //wait for child to update status
exit(0); //parent exits
}
exec() function:
Basic Use
- Use fork() to get a child process
- In the child process, use “execlp()” or “execvp()”
- This will “overlay” the image of the new program onto the child image
- The existing context(such as the file descriptors) is preserved
- The Child process now runs as the second program, and exits with a status code
- The parent process waits for the child to complete
exec:
Two Formats
- execlp( filename, argv0, argv1, …, (char *)0);
- The last parameter must be null
- execvp(filename, argv);
- filename can be a program or a script
- image of the current process is replaced, can only return if that fails
- Most Context is preserved
- stdio buffers are NOT FLUSHED
- must close or flush them manually using fflush or fclose before exec
What is a
Process?
- An instance of an executing program
- A fundamental concept in Computer Science and a powerful coding technique
- Performs independent computations
- Processes interact via messages
- Identified using a process id (pid)
Possible Process States
- running
- actually executing on machine
- sleeping
- ready to run, waiting for its turn
- idle
- sleeping for more than 20 seconds
- waiting
- waiting for an event, such as disk I/O
- zombie
- processes ended, but the parent has not yet recieved notification from the system