Some aspects
Process is one of the fundamental abstractions in the Linux
process: a process is a program in the midst of execution,not only including executing program code but also includes a set of related resources such as open files ,pending signals,internal kernal data,processor state,memory mappings and so on.Processes are the living result of running program code.
Thread:is the object of activity within the process.Each thread includes a unique program counter,process stack and set of processor registers.Be known that * the kernel schedules the individual threads but not the processes*. And to Linux,a thread is just a special kind of process,it does not differentiate between them.
A program itself is not a process.a process is an active program and related resources.
The virtualization
the processes include two kinds of virtualizations:virtualized processor and virtualized memory
virtualized processor:makes the process the illusion that it alone monopolizes .
virtualized processor:makes the process the illusion that it alone monopolizes memory.
Process Descriptor and the task structure
kernel stores the list of processes in a circular doubly linked list call task list.The element/node in the list is called process descriptor and it contains all the information of a specific process.
Often the struct task_struct is stored at the end of the kernel stack if each process.
The process state
TASK_RUNNING:the process is runnable.It is running right now or it is in the run-queue waiting to run
TASK_INTERRUPTIBLE:the process is sleeping(blocked) and waiting for some condition to exist to return to the state TASK_RUNNING
TASK_UNINTERRUPTIBLE:is same to the state TASK_INTERRUPTIBLE except it doesn’t wake up and become runnable if it receives a signal.It is used less often
__TASK_TRACED:this process is being traced by another process such as debug
__TASK_STOPPED:Process is stopped
Process Context
the most important job for a process is to execute the program code.The program code is read in from an executable file and executed within the program’s address space.Noraml program execution is in user-space and when this program calls a system call or triggers an exception,it enters kernel-space,now the kernel is said to be “executing on behalf of the process” and is in process context,when it is over the process resumes execution in user-space.
The process Family tree
All processes are sescendants of the init process,whose PID is 1.The kernel starts the init process in the last step of the boot process and the init process will read the system initscripts and execute more programs and finally complete the boot process.
Every process has exactly one parent and 0 or more children.the processes which have the same parent is called siblings and the relationship is stored in the process descriptor
Process Creation
with two distinct functions:fork() and exec().
fork():create a child process that is a copy of the current task and diffs from the parent only in its PID and PPID and share the same certain resources and statistics.
exec():loads a new executable into the address space and begins executing it.
In Linux,fork() is using the method called ‘copy-on-write pages(COW)’.COW is a technique to delay or prevent copying of the data and the parent and child process just share a single copy
and if the data need to be written,a duplicate is made and each process receives a unique copy.the duplication of resources occurs only when they are written and before they are just share read-only
Some more detailed info about the fork()
Linux implements fork() via clone() system call,the clone() will call do_fork() which is defined in the kernel/fork.c.do_fork() will call copy_process() and then starts the process running.And here is what the copy_process() will do:
1.calls dup_task_struct(),which creats a new kernel stack,thread_info structure for the new process.
2.Then checks that the new child will not exceed the resource limits for the current user.
3.the child should be different from its parent so various numbers of the process descriptor are cleaned or set to the defaule value
4.make the child’s state to TASK_UNINTERRUPTIBLE to ensure that it won’t run.
5.calls copy_flags() to update the flags number of the task_struct.
6.calls alloc_pid() to assign an new PID for the child process.
7.make the desicion whether to do COW or not.
8.cleans up and returns to the caller a pointer which points to the new child.
Then back to the function do_fork().If the copy_process(0 reuturns OK and the child process will be woken_up and run.* Usually the kernel runs the child process first*
Somethings about the Threads
Linux has no concept of a thread.Linux implements all threads as standard processes.
a thread is merely a process that shares certain resources with other processes,such as an address space.
Kernel Threads
kernel will do some operation in the background sometimes and via the ** kernel threads(the standard process that exist solely in kernel-space).The difference between kernel thread and normal process is **kernel thread doesn’t have an address space(the mm pointer is NULL) and it just operate in kernel-space and doesn’t context switch into user-space.The examples are flush and ksoftirqd task.
Kernel threads are created on system boot by other kernel threads. Indeed, a kernel thread can be created only by another kernel thread
Process Termination
processes will die at last.The kernel will release all of the related resources and notifies the parent of its demise
The Dilemma of the parentless task
If the parent process is released,how about its children?The solution is to reparent a task’s children to another process in the current thread group or be the child of the init process.