A program in execution or the unit of work in a modern time-sharing system
Process
Needs resources such as CPU time, memory, files and I/O devices to accomplish its task
Resources are allocated to the process either when it is created or while it is executing
Process structure
Text section (program code), Data section (global variables), Heap (dynamically allocated memory), Stack (temporary data)
A program becomes a process when an executable file is loaded into memory
Two processes may be associated with the same program but are considered separate execution sequences
Process states
New
Ready
Running
Waiting
Terminated
Process Control Block (PCB)
Repository for information that may vary from process to process
Information in PCB
Process state
Program counter
CPU registers
CPU-scheduling information
Memory-management information
Accounting information
I/O status information
Thread
A single thread of execution within a process
In a single processor system, a process has a single thread of execution
In a multicore/multiprocessor system, a process can have multiple threads of execution running in parallel
Process Scheduling
Selecting an available process for program execution on the CPU
Scheduling Queues
Job Queue
Ready Queue
Device Queue
Long Term Scheduler
Selects processes from the job queue and loads them into main memory for execution
Short Term Scheduler
Selects from among the processes that are ready to execute and allocates the CPU to one of them
Medium Term Scheduler
Performs swapping (removing a process from main memory and storing it into secondary storage) and moves processes between CPU, I/O waiting queue and ready queue
Process types
I/O bound (spends more time doing I/O)
CPU bound (spends more time doing computations)
Context Switch
Switching the CPU from one process to another by saving the context of the old process and loading the context of the new process
Process Creation
1. Parent process creates child process
2. Child process inherits resources from parent
3. Parent can wait for child to terminate or continue concurrently
Process Identifier (PID)
Unique integer number identifying each process
Linux process tree
init (pid 1)
kthreadd
sshd
Process System Calls in Unix/Linux
1. fork()
2. exec()
3. wait()
4. exit()
The fork() system call creates a new child process as a copy of the parent process
The exec() system call replaces the process's memory space with a new program
The wait() system call allows the parent to wait for the child process to terminate
C program showing UNIX system calls fork, exec, wait
1. Fork child process
2. If fork fails, print error and return 1
3. If child process, execute /bin/ls
4. If parent process, wait for child to complete
5. Print "Child Complete"
6. Return 0
Two different processes are running copies of the same program
Child process
Value of pid is zero
Parent process
Value of pid is an integer value greater than zero (the actual pid of the child process)
Child process inherits privileges, scheduling attributes, and certain resources such as open files from the parent
Child process
Overlays its address space with the UNIX command /bin/ls using the execlp() system call
Parent process
Waits for the child process to complete with the wait() system call
When the child process completes, the parent process resumes from the call to wait() and completes using the exit() system call
Process termination
A process terminates when it finishes executing its final statement and asks the operating system to delete it by using the exit() system call
The process may return a status value to its parent process via the wait() system call
All the resources of the process including physical and virtual memory, open files and I/O buffers are deallocated by the operating system
Reasons a parent may terminate the execution of one of its children
The child has exceeded its usage of some of the resources that it has been allocated
The task assigned to the child is no longer required
The parent is exiting and the operating system does not allow a child to continue if its parent terminates
Cascading Termination
If a parent process terminates either normally or abnormally then all its children must also be terminated
Terminating a process in Linux/UNIX
Use the exit() system call providing an exit status as a parameter
Under normal termination, exit() may be called either directly (i.e. exit(1)) or indirectly (i.e. by a return statement in main())