Processes

16 Jun 2019

What is a Process?

A software program consists of a sequence of code instructions and data stored on disk; it’s a passive entity.

A process is a program actively executing from main memory within its own address space.

Loading Executable Object Files

When a program is loaded into RAM, it becomes an actively executing application. The OS allocates a stack and heap to the app in addition to code and global data.

  • The stack contains local variables.
    • As main() calls function f1, we allocate f1’s local variables on the stack.
    • If f1 calls f2, we allocate f2’s variables on the stack below f1’s, therby growing the stack, etc…
    • when f2 is done, we deallocate f2’s local variables, popping them off the stack, and return to f1.
  • The stack dynamically expands and contracts as the program runs, and different levels of nested functions are called.
  • The heap contains run-time variables/buffers.
    • Obtained from malloc().
    • The program should free() the malloc’ed memory.
  • The heap can also expand and contract during program execution.

Applications and Processes

An application may consist of multiple processes, each execution in its own address space. e.g. a server could be split into multiple processes, each one dedicated to a specific task (UI, computation, communication, etc.). The application’s various processes talk to each other using Inter-Process Communication (IPC).

Process Manager Functionalities

  • Creation/deletion of processes (and threads).
  • Synchronization of processes (and threads).
  • Managing process state.
    • Processes state like PC, stack pointer, etc.
    • Resources like open files, etc.
    • Memory limits to enforce an address space.
  • Scheduling processes.
  • Monitoring processes.
    • Deadlock, protection.

What defines the state of a process?

  • Memory image: code, data, heap, stack.
  • Process state, e.g. ready, running, or waiting.
  • Accounting info, e.g. process ID, privilige.
  • Program counter (PC) value.
  • CPU registers’ values.
  • CPU-scheduling info, e.g. priority.
  • Memory management info, e.g. base and limit registers, page tables.
  • I/O status info, e.g. list of open files.

Process Control Block

Each process is represented in the OS by a process control block (PCB), which contains complete information on a process. The OS maintains a PCB table containing one entry for every process in the system. The PCB table is typically of fixed size and this size determines the maximum number of processes an OS can have. (The actual maximum may be less due to other resource constraints, e.g. memory.)

Process Descriptor

  • Process - dynamic, program in motion.
    • Kernel data structures maintain “state”.
    • Descriptor, task_struct
    • Complex struct with pointers to others.
  • Type of info in the task_struct
    • state
    • id
    • priorities
    • locks
    • files
    • signals
    • memory maps
    • queues, list pointers, etc.

Creating Processes in Windows

  • CreateProcess()
    • Pass an argument to CreateProcess() indicating which program to start running.
    • This invokes a system call to OS that then invokes the process manager to:
      • Allocate space in memory for the process.
      • Set up PCB state for the process; assigns PID, etc.
      • Copy code of the program name from hard disk to main memory, sets PC to entry point in main().
      • Schedule the process for execution.
    • It combines fork() and exec() system calls in UNIX/Linux and achieves the same effect.

Creating Processes in UNIX/Linux

  • Use fork() command to create/spawn new processes from within a process.
    • When a process (called a parent process) calls fork(), a new process (called a child process) is created.
    • The child process is an exact copy of the parent process.
      • All code and data are exactly the same.
      • All addresses are appropriately mapped.
    • The child starts executing at the same point as the parent, namely just after returning from the fork() call.

fork()

The fork() call returns an int value. In the parent process, the returned value is the child’s PID. In the child, the returned value is 0. Since both parent and child execute the same code starting from the same place, i.e. just after the fork(), you could add the following code to differentiate the child’s behavior from the parent’s:

PID = fork();
if (PID==0) { /* child */
codeforthechild();
exit(0);
}
/* parent’s code here */

Loading Processes

The exec() system call loads new program code into the calling process’s memory.

Wating on Processes

The wait() system call is used by a parent process to be informed of when a child has completed, i.e. called exit(). Once the parent has called wait(), the child’s PCB and address space can be freed. There is also waitpid() to wait on a particular child process to finish.

Process Hierarchy

  • The OS creates a single process at the start up.
  • An existing process can spawn one or more new processes during execution.
    • Parent-child relationship
    • A parent process may have some control over its child process(es):
      • suspend/activate execution
      • wait for termination, etc.
  • The hierarchy of processes is tree-structured.
Top