04 Processes Slides
04 Processes Slides
04. Processes
Paul Krzyzanowski
Rutgers University
Spring 2015
stack
data+bss
text
Running
Preemption I/O
Scheduler
Ready Blocked
I/O complete
Disk 2
Blocked
Sys call
or Interrupt
Return Zombie
exit
Kernel Linger until a
Created Running parent process
returns from wait()
[allows a parent to
Preempt get the exit code]
Sleep
Reschedule
I/O complete
Ready Blocked
Wake up
#include <stdio.h>
switch (pid=fork()) {
case 0: printf("I'm the child\n");
break;
default:
printf("I'm the parent of %d\n", pid);
break;
case -1:
perror("fork");
}
}
#include <unistd.h>
execvp("ls", av);
perror("ls failed to run!");
exit(1);
}
The perror and exit functions
run ONLY if execvp failed –
otherwise the new program
overlays the current process
• Windows approach
– CreateProcess system call to create a new child process
– Specify the executable file and parameters
– Identify startup properties (windows size, input/output handles)
– Specify directory, environment, and whether open files are inherited
#include <stdlib.h>
switch (my_pid=fork()) {
case 0: /* do child stuff */ break;
case -1: /* do error stuff */ break;
• Sending a signal:
– kill (int pid, int signal_number)
• Detecting a signal:
– signal (signal_number, function)