290 likes | 759 Views
Process. 宋祥葳. Outline. Processes, Lightweight Processes Process and Thread Process Descriptor Process State Process ID Process Data Structure Process relationship Process Resource Limit. What is a Process?. The concept An instance of a program in execution
E N D
Process 宋祥葳
Outline • Processes, Lightweight Processes • Process and Thread • Process Descriptor • Process State • Process ID • Process Data Structure • Process relationship • Process Resource Limit
What is a Process? • The concept • An instance of a program in execution • The basic unit of execution in an operating system • The purpose • Act as an entity to which system resources are allocated • The collection of data structures that describe how far the execution of the program has progressed
Process Concept Command Interpreter User Applications WindowSystem Middleware Operating System Process Process Process Process Instruction Execution & Interrupt Processing I/O Devices Memory
Anatomy of a Process Process State Running, Waiting, Halting, Ready state Process Number Process ID used to identify Process Program Counter Indicate the address of the next instruction to be executed for this processes CPURegister Save the state information when an interrupt occurs Memory Limit Include the page, segment table and the base, limit registers value List of Open Files Process Control Block (PCB)
Linux Process Descriptor structsched_rt_entity { structlist_headrun_list; unsigned long timeout; unsigned inttime_slice; intnr_cpus_allowed; …… }; Include/linux/Sched.h :1074~1378
The Linux Process Descriptor Linux 2.6.28 Process Structure 1074 structtask_struct { 1075 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ 1076 void *stack; 1078 unsigned intflags; /* per process flags */1092 structsched_entityse; 1093 structsched_rt_entityrt; 1126 structlist_headtasks;1128 structmm_struct *mm, *active_mm;1150 structtask_struct*real_parent; /* real parent process (when being debugged) */ 1151 structtask_struct*parent; /* parent process */1214 /* CPU-specific state of this task */ 1215 structthread_structthread; 1216 /* filesystem information */ 1217 structfs_struct*fs; 1218/* open file information */ structfiles_struct*files; …… /include/linux/sched.h1074~1378
Process descriptors handling • Linux packs two different data structures in a single per-process memory area. • The length of this memory is 8192 bytes (2 page frames) Esp is the CPU stack pointer, which is used to address the stack’s top location
Introducing Threads • A thread (or lightweight process) is a basic unit of CPU utilization; it consists of: • Program counter • Register set • Stack space • A thread shares with other threads belong to the same process its: • Code section • Data section • Operating-system resource • A traditional process is equal to a task with one thread Process Process Thread 1 Thread 1 Thread 2 Thread 3 Thread 4
Introducing Threads Process’s address space • Process Control Block (PCB) contains process-specific information • Owner, PID, heap pointer, priority, active thread, and pointers to thread information • Thread Control Block (TCB) contain thread-specific information • Stack pointer, PC, thread state, register TCB for Thread1 …… PC Heap SP state register Stack- Thread2 TCB for Thread2 Stack- Thread1 PC Initialized data SP Code state register
The Thread Structure arch\x86\include\asm\Thread_info.h
Linux Processes State /include/linux/sched.h:173~192
Processes State • TASK_RUNNING • The process is either executing on a CPU or waiting to be executed. • TASK_INTERRUPTIBLE • The Process is suspended until some condition becomes true. • TASK_UNINTERRUPTIBLE • Deliver a signal to the sleeping process leaves its state unchanged. • Execute when a process must wait until a given event occurs without being interrupted.
Processes State • TASK_STOPPED • The process has been stopped, when it received one of the SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU signal. • TASK_TRACED • When a process is being monitored by another, process execution has been stopped by a debugger. • EXIT_DEAD
Wait_event_interruptible /include/linux/Wait.h:245~261
Wait_event /include/linux/Wait.h:174~185
Process ID • What is the Process ID? • The kernel's internal notion of a process identifier • Process ID refers to individual tasks, process groups • The Process ID storage way • The Process ID lives in a hash table • The hash table storage manner refers the process can be found quickly from the numeric pid value.
Identifying a Process • Process ID recycle method • When the kernel reaches the max limit, it must start recycling the lower unused PIDs. • pidmap_array bitmap • Use to manage the condition of PIDs • In 32-bit architectures, the bitmap is stored in a single page. • tgid • All the threads of a multithreaded application share the same identifier (tgid). • tgid is the thread of the PID of the thread group leader. tgid pid pid pid pid pid pid Thread group
Use PID to detect process descriptor get_pid_task(pid) Result=Pid_task(pid) hlist_entry(pid) /kernel/Pid.c
Allocate a pid /kernel/Pid.c
Doubly linked list(1) • The Linux kernel uses hundreds of doubly linked lists that store the various kernel data structures. • For each list, a set of primitive operations must be implemented.
Process list structtask_struct{ …… structlist_headtasks; …… } /include/linux/Sched.h /include/linux/list.h
Pid namespace • The PID namespace allows for creating sets of tasks, with each such set looking like a standalone machine with respect to process IDs. In other words, tasks in different namespaces can have the same IDs. • Provide safety for virtualization
Relationships Among Processes • Processes created by a program have a parent/child relationship. • When a process creates multiple children, these children have sibling relationships. /include/linux/sched.h
Parenthood Relationships P0 P1 P3 P2 Parent P4 Children.next Children.prev Sibling.prev Sibling.next
Process Resource Limits • Each process has an associated set of resource limits. • Process resource limits specify the amount of system resources it can use. • These limit keeps a user from overwhelming the system. • The resource limits for current process are stored in the current->signal->rlim field 43 structrlimit { 44 unsigned long rlim_cur; 45 unsigned long rlim_max; 46 }; /include/ linux/ resource. c
Process Resource Limits • Rlimit Macro /include/asm-generic/resource.h 15 #define RLIMIT_CPU 0 /* CPU time in sec */ 16 #define RLIMIT_FSIZE 1 /* Maximum filesize */ 17 #define RLIMIT_DATA 2 /* max data size */ 18 #define RLIMIT_STACK 3 /* max stack size */ 19 #define RLIMIT_CORE 4 /* max core file size */ • RLIMIT_STACK Example mm/Mmap.c