User space vs. kernel space
A process is executing either in user space, or in kernel space. Depending on which priviledges, address space and address (EIP) a process is executing in, we say that it is either in user space, or kernel space.
When executing in user space, a process has normal priviledges and can and can’t do certain things. When
executing in kernel space, a process has every priviledge, and can do anything.
Processes switch between user space and kernel space using system calls.
Memory Allocation
malloc is a library function, implemented in the standard C library. It is not a system call.
malloc is implemented by two means in Linux. The first is the brk() system call, which grows a process’s data
segment. The second is mapping /dev/zero to get anonymous memory. In both cases, the end result is
the same.
The kernel allocates a virtual memory area for the application, but does not allocate physical memory for
it. Only when the area is accessed physical memory is allocated. This is known as “memory overcommit”, and there are ways to disable it.
Processes vs. threads
In Linux, processes and threads are almost the same. The major difference is that threads share the same
virtual memory address space.
The low level interface to create threads is the clone() system call. The higher level interface is
pthread_create().
Context switches between processes in Linux are fast. Context switches between threads are even faster.
Linux threading is “1-1”, not “1-N” or “M-N” ????
What is a process ?
Process is an instance of a program in execution
Process scheduling
• Allocates processes to CPU using a scheduling algorithm
• Every process is given a time slice for execution
• Context switching when it changes execution context from one process to another
• Processes run till their time slices elapse and are then swapped out of CPU
• Alternately, they voluntarily relinquish CPU while awaiting a resource
Process synchronization
• Between parent-child processes through wait and exit
Inter-process communication
• Asynchronous signaling of events
• Synchronous transmission of messages between processes
Memory management
• Manages allocation of memory for an executing process
• Protection of private address space of a process
• Memory management by writing process memory temporarily to secondary
memory
– Swapping: Entire process written to swap device
– Paging: Pages of memory written to swap device
Creation of processes
Every process except process 0 is created by the fork() system call
• Process 0 ( i.e swapper ) is created by system at boot time
• Process 1 init is created by swapper, is the parent-of-all processes
With a fork, child process is created with the same text and data segment as parent
Using an exec in the child process overlays current process with a new program
When a command is executed from the shell, it internally does a fork and exec
Parent process needs to “wait” on the child to receive the exit status & cleanup process table entry for child
A parent process may use wait() or waitpid()
to wait for its children’s termination status
.
Orphaned process
• Parent dies before child exits
• Child process is adopted by init , i.e its parent becomes init i.r
process id 1
• Init does a wait on the orphaned child process , handles cleanup
from process table on exit
If the parent process dies before handling the child’s termination status, the child
becomes an orphan process. Init process takes over as the parent and handles the
termination status
Defunct or zombie process
If the child terminates before the parent has handled its termination status, the
child process becomes a zombie
• child process exits, but still has an entry in the process table
• parent is notified with a SIGCHLD
• The child is in “defunct or zombie” state till the parent does a “wait” and reads the exit
status of the child
• Child process does not consume CPU resources, only sits in the
process table
Process has exit ( i.e no swappable image), but
occupying a process structure in memory for its parent
process
Z ( Zombie )
T ( Stopped) Process has been stopped by user
Process is in sleep state, may be waiting for a
resource, completion of I/O, terminal input, memory etc
S ( Sleep )
Process is running or scheduled to run and is on the
run queue
R ( Running )
Process State Description
• Process priority: Lower the priority number, higher the priority
• Priority can be modified using the nice, renice commands
§ /proc
• Is a pseudo or virtual filesystem; does not occupy disk space
• Interfaces with internal kernel data structures to obtain info on
systems/processes and change settings
Proc File System
Is a pseudo or virtual filesystem; does not occupy disk space
• Interfaces with internal kernel data structures to obtain info on systems/processes and change settings
/proc contains process specific sub-directories, named on the process ID
• Has subdirectories such as ( this is a sample from the Linux OS, may vary of different systems )
• cmdline – command line arguments of process
• cwd – link to current working directory
• environ – values of environment variables
• mem – memory held by the process
• status – process status
• statm – process memory status information
• fd – link to files opened in process
• Provides info about running kernel – meminfo, cpuinfo, devices, interrupts, kmsg, modules, net etc.
“top” uses the /proc to display periodically updated process status
information in a user-friendly manner
Displays system summary and details, status of individual tasks
(memory, cpu, pid and more)
• The memory line displays the
• Total physical RAM available on the system,
• Amount of usage, free, shared, along with the amount of ram in buffers
• Most importantly it displays per process the
• Amount of physical memory consumed
• percentage of the available processor time a process is taking
• process state
• total amount of processor time the process has had
• top information can be analyzed to debug system behavior and
performance problems
Signals and processes
Signals inform processes of occurrence of events
• Could be sent from a process or sent by the kernel
• Signals inform processes of occurrence of events
• Could be sent from a process or sent by the kernel
• kill command/system call used to send signals to processes
• kill <pid> sends SIGTERM signal ( default signal)
– Process can handle signal and terminate gracefully
• kill -9 <pid > sends SIGKILL signal
– For forceful termination; process cannot handle/ignore this signal
Control commands from the shell send signals to processes
• Ctrl-C on a foreground process
– Sends signal ( SIGINT) to terminate process
• Ctrl-Z on a foreground process
– Sends signal ( SIGTSTP ) to suspend/stop a process
On shell exit, sends a hangup signal ( SIGHUP ) to its children, causes them to be killed
• “nohup” utility causes commands to ignore SIGHUP( /bin/csh does this by default)
Upon child process termination, wait returns with the exit status of the child process
• wait() blocks till any one of the children terminate
• waitpid() has a non-blocking option, can be used to wait on a particular process id
Parent can register for SIGCHLD and call wait() in the context of the signal handler
Exec a new program
Using exec() after a fork() allows a new program( executable) to be executed
• Process ID of child does not change after a fork
Child runs with the data, heap and stack segments of the new executable
• Different exec variants available – execl, execv, execle,execve, execlp, execvp
Differ in manner program arguments and environment variables are passed to the child