lecture 17 Flashcards

Process API: fork, exec, wait, and exit

1
Q

parent process

A

existing process managed by the system
either in running, ready or blocked state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

child process

A

process created by parent and managed by system
either in running, ready or blocked state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

fork()

A

creates child process
system call –> trap exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

when a parent process calls fork(), what happens?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

fork() API

A

void fork()
- returns 0 if child is created normally
- returns -1 if error
- returns child PID to parent

**called once but returns twice

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

do parent processes and child processes share the same physical frame in DRAM?

A

no, they map to other addresses
- same concept as any other
process (1 process for specified
region of virtual memory and
physical memory)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

include <sys/types.h>

#include <unistd.h></unistd.h>

  1. int main() {
  2. printf(“parent\n”);
  3. pid_t pid = fork();
  4. printf(“parent and child\n”);
  5. printf(“PID = %d\n”, pid );
  6. return 0;
  7. }

what lines of code does the parent process execute?

A

1-7 all of them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

include <sys/types.h>

#include <unistd.h></unistd.h>

  1. int main() {
  2. printf(“parent\n”);
  3. pid_t pid = fork();
  4. printf(“parent and child\n”);
  5. printf(“PID = %d\n”, pid );
  6. return 0;
  7. }

what lines of code does the child process execute?

A

4-7
fork() –> line 3, creates child then it begins executing the following lines after that

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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?

A

pid = fork();

fork() –> creates child process from parent process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

exit()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

exit(int status)

A

deallocates memory segments of process –> garbage collector

trap exception (system call) that terminates process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

exit() steps

A
  1. context switching (user to kernel)
  2. kernel executes exit instructions
  3. deallocates memory (stack, heap, etc) minus slot that holds exit status and PID (PCB)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

process table

A

table that stores PID for every process and pointer to address location of PCB block

running or terminated processes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

does the process table entry still exist for a zombie child?

A

yes, until the parent reads the exit status of the child it still exists

18
Q

what happens once the parent reads the exit status of the child?

A

the entry in the process table gets terminated –> process is fully deleted from memory

19
Q

how does the parent process read the child status?

A

wait() –> system call trap exception

20
Q

when a parent calls wait(), what happens?

A

suspends the parent process from running on the cpu until the child is terminated

21
Q

wait(int *childstatus) API

A

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

22
Q

wait() calling steps

A
  1. CPU switches to kernel mode (context switching)
  2. parent process yields CPU
  3. dispatcher places parent into blocked queue (not ready to run bc async exception)
  4. child terminates (normally or abnormally)
  5. async interrupter signal (sick child signal) sent to system to notify parent child has terminated
  6. signal handler moves parent to ready to run queue
  7. parent gets ready to run on CPU and execute instructions
23
Q

does the parent have to call wait for every child process?

A

yes, 1:1 ratio –> 1 wait() call for every child process

24
Q

parent calls wait() allows for what 2 steps

A
  1. parent to read child exit status
  2. system removes zombie child entry from process table and unallocates zombie child PCB memory segment (pid and exit status)
25
Q

when the parent calls wait() and reads the exit status of child/system deallocates all memory of child, what is this called?

A

parent reaps zombie child

26
Q

once parent reads child’s exit status what happens

A

PCB allocation (pid and exit status) gets deallocated and child process is fully terminated

27
Q

what happens if parent terminates before reaping child?

A

child process –> orphaned
system process called init adopts orphaned child and reaps it

28
Q

3 reasons why process terminates

A
  1. exception whose default is to terminate (abnormal)
  2. running and exiting normally (done executing instructions)
  3. calling exit() function –> immediately terminates
29
Q

why does the parent have to enter blocked when yielding the CPU instead of ready to run?

A

inefficient for the parent process to be running on CPU suspended by the wait() call

30
Q

how is suspended parent process moved from blocked to ready to run queue?

A

child = terminates –> async interrupt signal (sick child signal) gets sent to system to notify parent that child has terminated
signal handler moves parent process out of blocked queue
parent process enters ready to run

31
Q

why does the parent call wait()?

A

terminated child occupies resources
(entry in process table and memory allocated in PCB –> exit status)

parent calls wait –> learns the zombie child’s exit status and unallocates these resources

32
Q

int execv(char* filename, char* argv[])

A

replaces aka clobbers the current process in memory with different process

33
Q

int execv(const char* path, char *const argv[])

explain the arguments

A

system call (sync exception)

const char* path: path to the executable file you want to execute

char *const argv[]: array of strings that represent arguments to be passed in the new program

34
Q

what does execv() do after it is called?

A

clobbers read/write, stack, heap, PCB (CPU context), etc memory segments for new program
child PID and other management information in the PCB is retained

**the operating system loads the new program into the current process’ memory space (replaces current program)
- if no errors: transfer control to
new program
- errors: returns -1

35
Q

int main() {
char *args[] = {“ls”, “-l”, NULL};
execv(“/bin/ls”, args);
return 1;
}

explain what is happening here

A

execv –> tries to execute the ls command with the -l option

if successful: replaces current process with the ls command
if unsuccessful: returns -1

36
Q

zombie

A

terminated child not yet reaped by parent

37
Q

orphan

A

parent is terminated before child is

38
Q

int main() {
pid_t pid = fork();
if ( pid == 0) {
printf(“child pid);
exit(0):
} else {
printf(“parent pid);
while(1) {
sleep(1);
}
}
return 0;
}
what is the outcome of this program and why?

A

zombie child (terminated process not yet reaped by parent process)

no wait() call by parent –> wait = reaping by parent to child
since it is not called, the parent never gets suspended and reads the exit status of child while the system deallocates memory

39
Q

int main() {
pid_t pid = fork();
if ( pid == 0) {
printf(“child pid);
while(1) {
sleep(1);
}

} else { 
	printf(“parent pid); 
	exit(0); 
}
return 0;  }  what is the outcome of this program and why?
A

orphan child process (parent process gets terminated before child does)

no wait() for parent to read exit status and deallocate memory used by child –> prevents zombie

no exit() for child: loop where child is left stopping and starting infinitely
- parent has exit() call –> gets
terminated while child is stuck
in infinite loop

40
Q

What is the difference between a child that is a zombie or an orphan, and can a child be both?

A

zombie:
- child process that has terminated but is waiting for parent to read exit status
- parent has to call wait() so entry in process table is deleted
- once that is done, zombie gets “reaped” and resources released by OS
- **not actively executing code, simply waiting for parent to reap

orphan:
- child process whose parent process terminated before it
- adopted by init process (PID 1) : new parent
- not zombies –> actively running and executing code

no, mutually exclusive states
- zombie: child process whose waiting for exit status to be read by parent to fully terminate (still alive slightly)
- orphan: child process whose parent terminated before them but is still alive executing

41
Q

How does a parent reap a child?

A

wait() function –> suspends parent process until child terminates and exit status of child process is read
- wait is async exception –> parent gets sent to blocked queue
- child terminates –> async interrupt signal (sick child signal) gets sent to system to let parent know child has terminated
- parent reads exit status, etc “reaps” it
- memory = deallocated, process table entry = deleted, child = full terminated
- parent moves from blocked to ready to run by signal handler does