 Process: an instance of a program in execution 
 (User) Thread: an execution flow of the process 
• Pthread (POSIX thread) library 
 Lightweight process (LWP): used to offer better 
support for multithreaded applications 
• LWP may share resources: address space, open files, … 
• To associate a lightweight process with each thread 
• Examples of pthread libraries that use LWP: LinuxThreads, 
IBM’s Next Generation Posix Threading Package (NGPT) 
www.QuontraSolutions.Com
 task_struct data structure 
• state: process state 
• thread_info: low-level information for the process 
• mm: pointers to memory area descriptors 
• tty: tty associated with the process 
• fs: current directory 
• files: pointers to file descriptors 
• signal: signals received 
• … 
www.QuontraSolutions.Com
www.QuontraSolutions.Com
 TASK_RUNNING: executing 
 TASK_INTERRUPTABLE: suspended 
(sleeping) 
 TASK_UNINTERRUPTABLE: (seldom used) 
 TASK_STOPPED 
 TASK_TRACED 
 EXIT_ZOMBIE 
 EXIT_DEAD 
www.QuontraSolutions.Com
 Process descriptor pointers: 32-bit 
 Process ID (PID): 16-bit (~32767 for 
compatibility) 
• Linux associates different PID with each process or 
LWP 
• Programmers expect threads in the same group to 
have a common PID 
• Thread group: a collection of LWPs (kernel 2.4) 
 The PID of the first LWP in the group 
 tgid field in process descriptor: using getpid() system call 
www.QuontraSolutions.Com
 union thread_union { 
struct thread_info thread_info; 
unsigned long stack[2048]; 
}; 
 Two data structures in 8KB (2 pages) 
• thread_info structure (new to 3rd ed.) 
• Kernel mode process stack 
www.QuontraSolutions.Com
www.QuontraSolutions.Com
 Obtain the address of thread_info structure 
from the esp register 
• current_thread_info() 
• movl $0xffffe000, %ecx 
andl %esp, %ecx 
movl %ecx, p 
 The process descriptor pointer of the process 
currently running on a CPU 
• The current macro: equivalent to 
current_thread_info()->task 
• movl $0xffffe000, %ecx 
andl %esp, %ecx 
movl (%ecx), p 
www.QuontraSolutions.Com
www.QuontraSolutions.Com
 LIST_HEAD(list_name) 
 list_add(n,p) 
 list_add_tail(n,p) 
 list_del(p) 
 list_empty(p) 
 list_entry(p,t,m) 
 list_for_each(p,h) 
 list_for_each_entry(p,h,m) 
www.QuontraSolutions.Com
 tasks field in task_struct structure 
• type list_head 
• prev, next fields point to the previous and the next 
task_struct 
 Process 0 (swapper): init_task 
 Useful macros: 
• SET_LINKS, REMOVE_LINKS: insert and remove a 
process descriptor 
• #define for_each_process(p)  
for (p=&init_task; (p=list_entry((p)->tasks.next),  
struct task_struct, tasks)  
) != &init_task; ) 
www.QuontraSolutions.Com
 runqueue 
• run_list field in task_struct structure: type list_head 
 Linux 2.6 implements the runqueue differently 
• To achieve scheduler speedup, Linux 2.6 splits the 
runqueue into 140 lists of each priority! 
• array filed of process descriptor: pointer to the 
prio_array_t data structure 
 nr_active: # of process descriptors in the list 
 bitmap: priority bitmap 
 queue: the 140 list_heads 
• enqueue_task(p, array), dequeue_task(p, array) 
www.QuontraSolutions.Com
 Process 0 and 1: created by the kernel 
• Process 1 (init): the ancestor of all processes 
 Fields in process descriptor for 
parenthood relationships 
• real_parent 
• parent 
• children 
• sibling 
www.QuontraSolutions.Com
www.QuontraSolutions.Com
 To search up the search for the process 
descriptor of a PID 
• Sequential search in the process list is inefficient 
 The pid_hash array contains four hash tables 
and corresponding filed in the process 
descriptor 
• pid: PIDTYPE_PID 
• tgid: PIDTYPE_TGID (thread group leader) 
• pgrp: PIDTYPE_PGID (group leader) 
• session: PIDTYPE_SID (session leader) 
 Chaining is used to handle PID collisions 
www.QuontraSolutions.Com
 Size of each pidhash table: dependent on the 
available memory 
 PID is transformed into table index using 
pid_hashfn macro 
• #define pid_hashfn(x) hash_long((unsigned long)x, 
pidhash_shift) 
• unsigned long hash_long(unsigned long val, 
unsigned int bits) 
{ 
unsigned long hash = val * 0x9e370001UL; 
return hash >> (32-bits); 
} 
www.QuontraSolutions.Com
www.QuontraSolutions.Com
 pids field of the process descriptor: the 
pid data structures 
• nr: PID number 
• pid_chain: links to the previous and the next 
elements in the hash chain list 
• pid_list: head of the per-PID list (in thread 
group) 
www.QuontraSolutions.Com
www.QuontraSolutions.Com
 do_each_trask_pid(nr, type, task) 
 while_each_trask_pid(nr, type, task) 
 find_trask_by_pid_type(type, nr) 
 find_trask_by_pid(nr) 
 attach_pid(task, type, nr) 
 detach_pid(task, type) 
 next_thread(task) 
www.QuontraSolutions.Com
 Processes in TASK_STOPPED, 
EXIT_ZOMBIE, EXIT_DEAD: not linked in 
lists 
 Processes in TASK_INTERRUPTABLE, 
TASK_UNINTERRUPTABLE: wait queues 
 Two kinds of sleeping processes 
• Exclusive process 
• Nonexclusive process: always woken up by the 
kernel when the event occurs 
www.QuontraSolutions.Com
 struct _ _wait_queue_head { 
spinlock_t lock; 
struct list_head task_list; 
}; 
typedef struct _ _wait_queue_head wait_queue_head_t; 
 struct _ _wait_queue { 
unsigned int flags; 
struct task_struct * task; 
wait_queue_func_t func; 
struct list_head task_list; 
}; 
typedef struct _ _wait_queue wait_queue_t; 
www.QuontraSolutions.Com
 Wait queue handling functions: 
• add_wait_queue() 
• add_wait_queue_exclusive() 
• remove_wait_queue() 
• wait_queue_active() 
• DECLARE_WAIT_QUEUE_HEAD(name) 
• init_waitqueue_head() 
 To wait: 
• sleep_on() 
• interruptible_sleep_on() 
• sleep_on_timeout(), interruptible_sleep_on_timeout() 
• Prepare_to_wait(), prepare_to_wait_exclusive(), finish_wait() 
• Macros: wait_event, wait_event_interruptible 
www.QuontraSolutions.Com
 To be woken up: 
• Wake_up, wake_up_nr, wake_up_all, 
wake_up_sync, wake_up_sync_nr, 
wake_up_interruptible, 
wake_up_interruptible_nr, 
wake_up_interruptible_all, 
wake_up_interruptible_sync, 
wake_up_interruptible_sync_nr 
www.QuontraSolutions.Com
 RLIMIT_AS 
 RLIMIT_CORE 
 RLIMIT_CPU 
 RLIMIT_DATA 
 RLIMIT_FSIZE 
 RLIMIT_LOCKS 
 RLIMIT_MEMLOCK 
 RLIMIT_MSGQUEUE 
 RLIMIT_NOFILE 
 RLIMIT_NPROC 
 RLIMIT_RSS 
 RLIMIT_SIGPENDING 
 RLIMIT_STACK 
www.QuontraSolutions.Com
 Process switch, task switch, context switch 
• Hardware context switch: a far jmp (in older Linux) 
• Software context switch: a sequence of mov 
instructions 
 It allows better control over the validity of data being loaded 
 The amount of time required is about the same 
 Performing the Process Switch 
• Switching the Page Global Directory 
• Switching the Kernel Mode stack and the hardware 
context 
www.QuontraSolutions.Com
 TSS: a specific segment type in x86 
architecture to store hardware contexts 
www.QuontraSolutions.Com
 In traditional UNIX, resources owned by parent 
process are duplicated 
• Very slow and inefficient 
 Mechanisms to solve this problem 
• Copy on Write: parent and child read the same 
physical pages 
• Lightweight process: parent and child share per-process 
kernel data structures 
• vfork() system call: parent and child share the 
memory address space 
www.QuontraSolutions.Com
 clone(fn, arg, flags, child_stack, tls, ptid, 
ctid): creating lightweight process 
• A wrapper function in C library 
• Uses clone() system call 
 fork() and vfork() system calls: 
implemented by clone() with different 
parameters 
 Each invokes do_fork() function 
www.QuontraSolutions.Com
 Kernel threads run only in kernel mode 
 They use only linear addresses greater 
than PAGE_OFFSET 
www.QuontraSolutions.Com
 kernel_thread(): to create a kernel thread 
 Example kernel threads 
• Process 0 (swapper process), the ancestor of all 
processes 
• Process 1 (init process) 
• Others: keventd, kapm, kswapd, kflushd (also 
bdflush), kupdated, ksoftirqd, … 
• 
www.QuontraSolutions.Com
 exit() library function 
• Two system calls in Linux 2.6 
 _exit() system call 
 Handled by do_exit() function 
 exit_group() system call 
 By do_group_exit() function 
 Process removal 
• Releasing the process descriptor of a zombie 
process by release_task() 
www.QuontraSolutions.Com
TThhaannkkss ffoorr YYoouurr 
AAtttteennttiioonn!! 
www.QuontraSolutions.Com

Process and Threads in Linux - PPT

  • 2.
     Process: aninstance of a program in execution  (User) Thread: an execution flow of the process • Pthread (POSIX thread) library  Lightweight process (LWP): used to offer better support for multithreaded applications • LWP may share resources: address space, open files, … • To associate a lightweight process with each thread • Examples of pthread libraries that use LWP: LinuxThreads, IBM’s Next Generation Posix Threading Package (NGPT) www.QuontraSolutions.Com
  • 3.
     task_struct datastructure • state: process state • thread_info: low-level information for the process • mm: pointers to memory area descriptors • tty: tty associated with the process • fs: current directory • files: pointers to file descriptors • signal: signals received • … www.QuontraSolutions.Com
  • 4.
  • 5.
     TASK_RUNNING: executing  TASK_INTERRUPTABLE: suspended (sleeping)  TASK_UNINTERRUPTABLE: (seldom used)  TASK_STOPPED  TASK_TRACED  EXIT_ZOMBIE  EXIT_DEAD www.QuontraSolutions.Com
  • 6.
     Process descriptorpointers: 32-bit  Process ID (PID): 16-bit (~32767 for compatibility) • Linux associates different PID with each process or LWP • Programmers expect threads in the same group to have a common PID • Thread group: a collection of LWPs (kernel 2.4)  The PID of the first LWP in the group  tgid field in process descriptor: using getpid() system call www.QuontraSolutions.Com
  • 7.
     union thread_union{ struct thread_info thread_info; unsigned long stack[2048]; };  Two data structures in 8KB (2 pages) • thread_info structure (new to 3rd ed.) • Kernel mode process stack www.QuontraSolutions.Com
  • 8.
  • 9.
     Obtain theaddress of thread_info structure from the esp register • current_thread_info() • movl $0xffffe000, %ecx andl %esp, %ecx movl %ecx, p  The process descriptor pointer of the process currently running on a CPU • The current macro: equivalent to current_thread_info()->task • movl $0xffffe000, %ecx andl %esp, %ecx movl (%ecx), p www.QuontraSolutions.Com
  • 10.
  • 11.
     LIST_HEAD(list_name) list_add(n,p)  list_add_tail(n,p)  list_del(p)  list_empty(p)  list_entry(p,t,m)  list_for_each(p,h)  list_for_each_entry(p,h,m) www.QuontraSolutions.Com
  • 12.
     tasks fieldin task_struct structure • type list_head • prev, next fields point to the previous and the next task_struct  Process 0 (swapper): init_task  Useful macros: • SET_LINKS, REMOVE_LINKS: insert and remove a process descriptor • #define for_each_process(p) for (p=&init_task; (p=list_entry((p)->tasks.next), struct task_struct, tasks) ) != &init_task; ) www.QuontraSolutions.Com
  • 13.
     runqueue •run_list field in task_struct structure: type list_head  Linux 2.6 implements the runqueue differently • To achieve scheduler speedup, Linux 2.6 splits the runqueue into 140 lists of each priority! • array filed of process descriptor: pointer to the prio_array_t data structure  nr_active: # of process descriptors in the list  bitmap: priority bitmap  queue: the 140 list_heads • enqueue_task(p, array), dequeue_task(p, array) www.QuontraSolutions.Com
  • 14.
     Process 0and 1: created by the kernel • Process 1 (init): the ancestor of all processes  Fields in process descriptor for parenthood relationships • real_parent • parent • children • sibling www.QuontraSolutions.Com
  • 15.
  • 16.
     To searchup the search for the process descriptor of a PID • Sequential search in the process list is inefficient  The pid_hash array contains four hash tables and corresponding filed in the process descriptor • pid: PIDTYPE_PID • tgid: PIDTYPE_TGID (thread group leader) • pgrp: PIDTYPE_PGID (group leader) • session: PIDTYPE_SID (session leader)  Chaining is used to handle PID collisions www.QuontraSolutions.Com
  • 17.
     Size ofeach pidhash table: dependent on the available memory  PID is transformed into table index using pid_hashfn macro • #define pid_hashfn(x) hash_long((unsigned long)x, pidhash_shift) • unsigned long hash_long(unsigned long val, unsigned int bits) { unsigned long hash = val * 0x9e370001UL; return hash >> (32-bits); } www.QuontraSolutions.Com
  • 18.
  • 19.
     pids fieldof the process descriptor: the pid data structures • nr: PID number • pid_chain: links to the previous and the next elements in the hash chain list • pid_list: head of the per-PID list (in thread group) www.QuontraSolutions.Com
  • 20.
  • 21.
     do_each_trask_pid(nr, type,task)  while_each_trask_pid(nr, type, task)  find_trask_by_pid_type(type, nr)  find_trask_by_pid(nr)  attach_pid(task, type, nr)  detach_pid(task, type)  next_thread(task) www.QuontraSolutions.Com
  • 22.
     Processes inTASK_STOPPED, EXIT_ZOMBIE, EXIT_DEAD: not linked in lists  Processes in TASK_INTERRUPTABLE, TASK_UNINTERRUPTABLE: wait queues  Two kinds of sleeping processes • Exclusive process • Nonexclusive process: always woken up by the kernel when the event occurs www.QuontraSolutions.Com
  • 23.
     struct __wait_queue_head { spinlock_t lock; struct list_head task_list; }; typedef struct _ _wait_queue_head wait_queue_head_t;  struct _ _wait_queue { unsigned int flags; struct task_struct * task; wait_queue_func_t func; struct list_head task_list; }; typedef struct _ _wait_queue wait_queue_t; www.QuontraSolutions.Com
  • 24.
     Wait queuehandling functions: • add_wait_queue() • add_wait_queue_exclusive() • remove_wait_queue() • wait_queue_active() • DECLARE_WAIT_QUEUE_HEAD(name) • init_waitqueue_head()  To wait: • sleep_on() • interruptible_sleep_on() • sleep_on_timeout(), interruptible_sleep_on_timeout() • Prepare_to_wait(), prepare_to_wait_exclusive(), finish_wait() • Macros: wait_event, wait_event_interruptible www.QuontraSolutions.Com
  • 25.
     To bewoken up: • Wake_up, wake_up_nr, wake_up_all, wake_up_sync, wake_up_sync_nr, wake_up_interruptible, wake_up_interruptible_nr, wake_up_interruptible_all, wake_up_interruptible_sync, wake_up_interruptible_sync_nr www.QuontraSolutions.Com
  • 26.
     RLIMIT_AS RLIMIT_CORE  RLIMIT_CPU  RLIMIT_DATA  RLIMIT_FSIZE  RLIMIT_LOCKS  RLIMIT_MEMLOCK  RLIMIT_MSGQUEUE  RLIMIT_NOFILE  RLIMIT_NPROC  RLIMIT_RSS  RLIMIT_SIGPENDING  RLIMIT_STACK www.QuontraSolutions.Com
  • 27.
     Process switch,task switch, context switch • Hardware context switch: a far jmp (in older Linux) • Software context switch: a sequence of mov instructions  It allows better control over the validity of data being loaded  The amount of time required is about the same  Performing the Process Switch • Switching the Page Global Directory • Switching the Kernel Mode stack and the hardware context www.QuontraSolutions.Com
  • 28.
     TSS: aspecific segment type in x86 architecture to store hardware contexts www.QuontraSolutions.Com
  • 29.
     In traditionalUNIX, resources owned by parent process are duplicated • Very slow and inefficient  Mechanisms to solve this problem • Copy on Write: parent and child read the same physical pages • Lightweight process: parent and child share per-process kernel data structures • vfork() system call: parent and child share the memory address space www.QuontraSolutions.Com
  • 30.
     clone(fn, arg,flags, child_stack, tls, ptid, ctid): creating lightweight process • A wrapper function in C library • Uses clone() system call  fork() and vfork() system calls: implemented by clone() with different parameters  Each invokes do_fork() function www.QuontraSolutions.Com
  • 31.
     Kernel threadsrun only in kernel mode  They use only linear addresses greater than PAGE_OFFSET www.QuontraSolutions.Com
  • 32.
     kernel_thread(): tocreate a kernel thread  Example kernel threads • Process 0 (swapper process), the ancestor of all processes • Process 1 (init process) • Others: keventd, kapm, kswapd, kflushd (also bdflush), kupdated, ksoftirqd, … • www.QuontraSolutions.Com
  • 33.
     exit() libraryfunction • Two system calls in Linux 2.6  _exit() system call  Handled by do_exit() function  exit_group() system call  By do_group_exit() function  Process removal • Releasing the process descriptor of a zombie process by release_task() www.QuontraSolutions.Com
  • 34.
    TThhaannkkss ffoorr YYoouurr AAtttteennttiioonn!! www.QuontraSolutions.Com