lecture 17 Flashcards
Process API: fork, exec, wait, and exit
parent process
existing process managed by the system
either in running, ready or blocked state
child process
process created by parent and managed by system
either in running, ready or blocked state
fork()
creates child process
system call –> trap exception
when a parent process calls fork(), what happens?
child process is created
- duplicate of parent process
- only difference –> PID in PCB
process management section is
different than parent
- identical user memory segments,
CPU context
fork() API
void fork()
- returns 0 if child is created normally
- returns -1 if error
- returns child PID to parent
**called once but returns twice
do parent processes and child processes share the same physical frame in DRAM?
no, they map to other addresses
- same concept as any other
process (1 process for specified
region of virtual memory and
physical memory)
include <sys/types.h>
#include <unistd.h></unistd.h>
- int main() {
- printf(“parent\n”);
- pid_t pid = fork();
- printf(“parent and child\n”);
- printf(“PID = %d\n”, pid );
- return 0;
- }
what lines of code does the parent process execute?
1-7 all of them
include <sys/types.h>
#include <unistd.h></unistd.h>
- int main() {
- printf(“parent\n”);
- pid_t pid = fork();
- printf(“parent and child\n”);
- printf(“PID = %d\n”, pid );
- return 0;
- }
what lines of code does the child process execute?
4-7
fork() –> line 3, creates child then it begins executing the following lines after that
include <sys/types.h>
#include <unistd.h></unistd.h>
int main() {
pid_t pid;
int x = 1;
pid = fork(); if (pid == 0) { printf("child: x = %d\n", ++x); exit(0); } printf("parent: x = %d\n", --x); exit(0); }
what line of code creates the child process?
pid = fork();
fork() –> creates child process from parent process
include <sys/types.h>
#include <unistd.h></unistd.h>
int main() {
pid_t pid;
int x = 1;
pid = fork(); if (pid == 0) { printf("child: x = %d\n", ++x); exit(0); } printf("parent: x = %d\n", --x); exit(0); }
what lines of code does the child process execute?
if (pid == 0) {
printf(“child…”);
exit(0);
}
executes these if pid == 0 –> means fork() created child process normally
- executes the body of if only
when that happens
- exclusive code to child, parent
can’t execute it since pid != 0 for
parent
include <sys/types.h>
#include <unistd.h></unistd.h>
int main() {
pid_t pid;
int x = 1;
pid = fork(); if (pid == 0) { printf("child: x = %d\n", ++x); exit(0); } printf("parent: x = %d\n", --x); exit(0); }
what lines of code does the parent process execute?
everything before the if and after it
** can’t execute the if bc it only pertains to the child process if that gets created correctly
include <sys/types.h>
#include <unistd.h></unistd.h>
int main() {
pid_t pid;
int x = 1;
pid = fork(); if (pid == 0) { printf("child: x = %d\n", ++x); exit(0); } printf("parent: x = %d\n", --x); exit(0); }
what does the exit(0) do?
exit (int status)
- function that deallocates user
memory
(stack, heap, read/write, read only)
- leaves one slot allocated that
holds the child’s PID and exit
status (in PCB process
management) until parent process
reads it
exit()
deallocates memory (stack, heap, read and write, read-only)
for child process –> leaves the PID and exit status in one slot in PCB process management section until parent reads it
- makes child process zombie
exit(int status)
deallocates memory segments of process –> garbage collector
trap exception (system call) that terminates process
exit() steps
- context switching (user to kernel)
- kernel executes exit instructions
- deallocates memory (stack, heap, etc) minus slot that holds exit status and PID (PCB)
process table
table that stores PID for every process and pointer to address location of PCB block
running or terminated processes
does the process table entry still exist for a zombie child?
yes, until the parent reads the exit status of the child it still exists
what happens once the parent reads the exit status of the child?
the entry in the process table gets terminated –> process is fully deleted from memory
how does the parent process read the child status?
wait() –> system call trap exception
when a parent calls wait(), what happens?
suspends the parent process from running on the cpu until the child is terminated
wait(int *childstatus) API
return value is pid of child process that got terminated
if parent has no child –> -1 returned
childstatus –> integer value that indicates the reason child terminated and exit status
wait() calling steps
- CPU switches to kernel mode (context switching)
- parent process yields CPU
- dispatcher places parent into blocked queue (not ready to run bc async exception)
- child terminates (normally or abnormally)
- async interrupter signal (sick child signal) sent to system to notify parent child has terminated
- signal handler moves parent to ready to run queue
- parent gets ready to run on CPU and execute instructions
does the parent have to call wait for every child process?
yes, 1:1 ratio –> 1 wait() call for every child process
parent calls wait() allows for what 2 steps
- parent to read child exit status
- system removes zombie child entry from process table and unallocates zombie child PCB memory segment (pid and exit status)