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