Notes Linux kernel Learning (ii) process

Source: Internet
Author: User

A process and a thread

A process is an executing program that contains resources such as an independent address space, multiple threads of execution, and so on.

Threads are objects that are active in a process, each with a separate program counter, a process stack, and a set of process registers.

The kernel Dispatches objects that are threads rather than processes. For Linux, threads are a special process.

Two-process descriptor and task structure

The kernel uses the task queue of the bidirectional loop list to hold the process, using the struct task_struct to describe all of the process information.

1 Process Descriptor Task_struct

struct Task_struct {} struct body is quite large, approximately 1.7K bytes. Probably list some look:

    

2 Assigning process descriptors

When a process is transitioning from a user state to a kernel state due to an outage or a system call, the stack used by the process is also switched from the user stack to the kernel stack.

The current process descriptor task_struct can be obtained by getting the stack tail thread_info through the kernel stack.

The THREAD_INFO structure of each process is allocated at the end of his kernel stack. The Task_struct in a task field in a structure is the actual one that points to the job.

    

The kernel processing process is manipulated by the process descriptor task_struct the struct object. So the operation process is going to get the process descriptor that is currently running.

The task_struct address can be found through the Thread_info address, and the offset address of the thread_info is different on different architectures.

/* linux-2.6.38.8/arch/arm/include/asm/current.h */static inline struct task_struct *get_current (void) {     return Current_thread_info ()->task; } #define Current (Get_current ())/* linux-2.6.38.8/arch/arm/include/asm/thread_info.h */  static inline struct Thread_info *current_thread_info (void) {//stack pointer    Register unsigned long SP asm ("SP");     return (struct Thread_info *) (SP & ~ (thread_size-1)); }

3 Status of the process

Each process in the system is bound to be one of five process states or to switch. The value of this field must also be one of the following five status flags:

Task_running (run)-the process is executable; it is either executing or waiting for execution in the run queue (the run queue will be discussed in chapter 4th).

This is the only possible state that the process executes in user space, which can also be applied to processes that are executing in kernel space.

Task_interruptible (interruptible)-The process is sleeping (that is, it is blocked), waiting for certain conditions to be reached. Once these conditions are reached,

The kernel will set the process state to run. Processes in this state are also woken up early and ready to run because of a signal being received.

Task_uninterruptible (non-disruptive)-this state is the same as the interruptible state, except that the received signal will not be awakened or ready to be put into operation.

This state usually occurs when the process must wait without interference or wait for the event to occur soon. Because the task in this state does not respond to the signal,

Therefore, it is less used than the interruptible state.

__task_traced-processes that are tracked by other processes, such as tracing the debugger through Ptrace.

__task_stopped (stop)-the process stops executing and the process is not operational or operational. Typically this state occurs when a sigstop is received,

SIGTSTP, Sigttin, Sigttou and other signals. In addition, any signal received during debugging will cause the process to enter this state.

Three-process creation

Fork:copy the current process to create a new process;

EXEC: Reads the executable file and loads it into the address space to start running.

1 Fork Process

The creation process is done by calling the Do_fork function, which provides a number of parameter flags to indicate how the process was created.

Long do_fork (unsigned long clone_flags,             unsigned long stack_start,             struct pt_regs *regs,             unsigned long stack_size,             int __user *parent_tidptr,             int __user *child_tidptr) {    struct task_struct *p;    ... Create process    p = copy_process (Clone_flags, Stack_start, Regs, Stack_size,                   child_tidptr, NULL, trace);    ... Add the process to the run queue    wake_up_new_task (p);}

copy_process The child process is created through the parent process and is not executed:

Task_struct *copy_process (unsigned long clone_flags,                                   unsigned long stack_start,                                   struct Pt_regs *regs,                                   unsigned long stack_size,                                   int __user *child_tidptr,                                   struct PID *pid,                                   int trace) {       struct task_struct *p;       //Create process kernel stack and process descriptor       P = dup_task_struct (current);       The resulting process is exactly the same as the content of the parent process, initializing the new creation process ...       return p;}

dup_task_struct Creates a child process kernel stack and process descriptor based on the parent process:

static struct task_struct *dup_task_struct (struct task_struct *orig) {       struct task_struct *tsk;       struct Thread_info *ti;       int node = Tsk_fork_get_node (orig);       Create a Process Descriptor object       tsk = alloc_task_struct_node (node);
Create process kernel stack thread_info ti = Alloc_thread_info_node (tsk, node); Make child process descriptors consistent with parent process err = arch_dup_task_struct (tsk, orig); The process descriptor stack points to thread_info tsk->stack = ti; Make the child process Thread_info content consistent with the parent process, but the task points to the child process task_struct setup_thread_stack (tsk, orig); return tsk;}

After the process copy_process is created, it is returned to Do_fork and the new creation process is added to the run queue for execution.

Four implementations of threads in Linux with kernel threads

The threading mechanism provides a set of threads that share memory address space, files, and other resources in the same program. All threads are implemented as processes in the Linux kernel.

The kernel does not provide scheduling algorithms, or data structures to characterize threads, but rather as a process of sharing resources with other processes, unlike other systems.

The difference between a kernel thread and a normal process is:

Kernel threads do not have a separate address space

Runs only in kernel space and does not switch to user space

The end of the five processes

At the end of the process, the kernel frees the resources it occupies and tells the parent process to update parent-child relationships. The exit finalization process is called, and the process is usually terminated with the last call to Do_exit.

void Do_exit (Long code) {       //Gets the currently running process       struct task_struct *tsk = current;        ... Sets pf_exiting       exit_signals (tsk);       Release the task_struct mm_struct memory       exit_mm (tsk);       Exit receive IPC signal queue       Exit_sem (tsk);       Process namespace       Exit_shm (tsk);       File descriptor       exit_files (tsk);       File System       Exit_fs (TSK);       Resource release       exit_thread ();       Sends a signal to the parent process       exit_notify (tsk, group_dead);       ... Switch to another process       tsk->state = task_dead;       Tsk->flags |= Pf_nofreeze;       Schedule ();       ......}

Notes Linux kernel Learning (ii) process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.