All the processes are created by the first parent process 'init'.
- pid = 1
- /sbin/init
In unix, the only way to new a child process is fork().
- copy data from parent
- update parent's children list
- update child's PID
- update child's running time
- update child's pointer to parent
- return value for parent = child's PID
- return value for child = 0
Share the same terminal output stream.
Replace the parent process with the other process. PID doesn't change. The executing process never returns to the origin one.
- change PCB
- change user space
execl, execlp, execle, execv, execvp, execvpe
Interface | Attribute | Value |
---|---|---|
execl(), execle(), execv() | Thread safety | MT-Safe |
execlp(), execvp(), execvpe() | Thread safety | MT-Safe env |
- wait for children terminate
- wait for SIGCHLD
- no child
#include <sys/wait.h>
pid_t wait(int *wstatus);
// The wait() system call suspends execution of the calling process until one of its children terminates.
pid_t waitpid(pid_t pid, int *wstatus, int options);
// waitpid(-1, &wstatus, 0);
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void) {
if (fork() == 0){
execl("/bin/ls","/bin/ls",NULL);
} else {
wait(NULL);
printf("I'm the parent.\n");
}
return 0;
}
#include <unistd.h>
int dup(int oldfd);
// It creates a copy of the file descriptor oldfd, using the lowest-numbered unused file descriptor for the new descriptor.
int dup2(int oldfd, int newfd);
// It uses the file descriptor number specified in newfd. If the file descriptor newfd was previously open, it is silently closed before being reused.
int dup3(int oldfd, int newfd, int flags);
// The caller can force the close-on-exec flag to be set for the new file descriptor by specifying O_CLOEXEC in flags.
PCB is a data structure in the operating system kernel containing the information needed to manage the scheduling of a particular process. "The manifestation of a process in an operating system."
- CPU time spent in user-mode code
- user-space memory
- CPU time spent in the kernel
- kernel-space memory
total elapsed time
- real time = user time + sys time
- real time > user time + sys time
- Heavy I/O activity
- Lack of CPU
- real time < user time + sys time
- multithreading
exit()
- clean up kernel-space memory
- clean up uesr-space memory
- kernel send SIGCHLD signal to parent
- child is zombie wait()
- register signal handling function
PID is still in the task list
Should be re-parenting
kill()