Skip to content

Process

the process is the OS abstraction for a running program

controlled by Process Control Block(PCB), has all info about the process, heavyweight

Process components

  • every state the process needs
    • memory address space
    • code and data for the program
    • stack
    • PC
    • registers
    • OS resources
    • nmaed by its process ID (PID)

Process state

  • new: process is being created
  • ready: process is waiting to be assigned to a processor
  • running: instructions are being executed
  • waiting (blocked): process is waiting for some event to occur (eg: I/O completion)

Process Life Cycle

Creation

  • every process is created by another process, the creating process is called parent and the created process is called child.
  • view by pstree, create by BOOL CreateProcess(char *programName, char *args[]) on Windows, int fork(), int exec(char *prog, char *argv[]) on Unix
    • CreateProcess: create/init PCB, load program by programName, copy and give args[] to the program, and then add it to the ready queue
    • fork: copy the PCB of the parent, copy entire contents of address space of parent, and initiailize kernal resources to point to resource used by parent, and then initialize hardware context to be copy of the parents.
    • exec: stops the current process, load and initialize the new program, then set ready (DOES NOT CREATE NEW PROCESS)

alt text

fork vs CreateProcess

  • fork is copy of the parent, CreateProcess is load a new program
  • fork return 0 to the child process, and return the child PID to the parent process, therefore, fork() == 0 indicate this is a child process

alt text

Termination

  • ExitProcess(int status) on Windows, exit(int status) on Unix
  • close open files, connections, release memory BY OS, not the process itself

waiting

  • wait() on Unix, which suspend current process until any child process terminates
  • waitpid(int pid) on Unix, which suspend current process until the child process with pid terminates

Questions

how many processes are created when program runs?

int main(){
    int pid = fork();
    if (pid == 0) {
        // child process
        printf("child process\n");
        fork()
    } else {
        // parent process
        printf("parent process\n");
        wait(5);

        waitpid(pid, NULL, 0);
    }
    return 0;
}
  • 3 processes are created, 1 parent and 2 children
  • however, only 1 or 2 active processes. we dont know if the second process is exited or not.