processes and threads in linux (chap. 3, understanding the linux kernel)
DESCRIPTION
Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel). J. H. Wang Sep. 22, 2008. Processes, Lightweight Processes, and Threads. Process : an instance of a program in execution (User) Thread : an execution flow of the process Pthread (POSIX thread) library - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/1.jpg)
Processes and Threads in Linux
(Chap. 3, Understanding the Linux Kernel)
J. H. WangSep. 22, 2008
![Page 2: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/2.jpg)
Processes, Lightweight Processes, and Threads
• 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– To associate a lightweight process with each thread– Examples of pthread libraries: LinuxThreads, IBM’s
Next Generation Posix Threading Package (NGPT)
![Page 3: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/3.jpg)
Process Descriptor
• task_struct data structure– state: process state– tty: tty associated with the process– fs: current directory– files: pointers to file descriptors– mm: pointers to memory area
descriptors– sig: signals received– …
![Page 4: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/4.jpg)
Linux Process Descriptor
![Page 5: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/5.jpg)
Process State
• TASK_RUNNING: executing• TASK_INTERRUPTABLE: suspended
(sleeping)• TASK_UNINTERRUPTABLE: (seldom
used)• TASK_STOPPED• TASK_ZOMBIE
![Page 6: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/6.jpg)
Identifying a Process
• Process descriptor pointers: 32-bit• Process ID (PID): 16-bit (~32767)
![Page 7: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/7.jpg)
Process descriptor handling
• union task_union { struct task_struct task; unsigned long stack[2048];};
• 8KB (2 pages)– Process descriptor– Kernel mode process stack
![Page 8: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/8.jpg)
Storing the process descriptor and the process kernel stack
![Page 9: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/9.jpg)
The current Macro
• The process descriptor pointer of the process currently running on a CPU– movl $0xffffe000, %ecx
andl %esp, %ecxmovl %ecx, p
![Page 10: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/10.jpg)
The Process List
• prev_task, next_task• Process 0 (swapper): init_task• Useful macros:
– SET_LINKS, REMOVE_LINKS: insert and remove a process descriptor
– #define for_each_task(p) \ for (p=&init_task; (p=p->next_task) != &init_task; )
![Page 11: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/11.jpg)
Doubly Linked Lists
![Page 12: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/12.jpg)
List Handling Functions and Macros
• LIST_HEAD(list_name)• List_add(n,p)• List_add_tail(n,h)• List_del(p)• List_empty(p)• List_entry(p,t,f)• List_for_each(p,h)
![Page 13: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/13.jpg)
List of TASK_RUNNING processes
• runqueue– run_list– nr_running
• Runqueue management functions:– add_to_runqueue()– del_from_runqueue()– move_first_runqueue()– move_last_runqueue()– task_on_runqueue()– wake_up_process()
![Page 14: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/14.jpg)
Pidhash table
• Size: PIDHASH_SZ 1024• #define pid_hashfn(x)
((((x)>>8)^(x)) & (PIDHASH_SZ-1))• Functions:
– hash_pid(), unhash_pid(): insert and remove a process in the pidhash table
– find_task_by_pid()
![Page 15: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/15.jpg)
The pidhash table and chained lists
![Page 16: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/16.jpg)
Parenthood Relationships among Processes
• Process 0 and 1: created by the kernel• Process 1 (init): the ancestor of all
processes– p_opptr (original parent)– p_pptr (parent)– p_cptr (child)– p_ysptr (younger sibling)– p_osptr (older sibling)
![Page 17: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/17.jpg)
Parenthood Relationships among five processes
![Page 18: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/18.jpg)
How Processes are Organized
• Processes in TASK_STOPPED, TASK_ZOMBIE: 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
![Page 19: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/19.jpg)
Wait Queues
• 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;
![Page 20: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/20.jpg)
Handling Wait Queues• 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()– wait_event()– wait_event_interruptible()
![Page 21: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/21.jpg)
• 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
![Page 22: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/22.jpg)
Process Resource Limits
• RLIMIT_AS• RLIMIT_CORE• RLIMIT_CPU• RLIMIT_DATA• RLIMIT_FSIZE• RLIMIT_LOCKS• RLIMIT_MEMLOCK• RLIMIT_NOFILE• RLIMIT_NPROC• RLIMIT_RSS• RLIMIT_STACK
![Page 23: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/23.jpg)
Process Switch
• Process switch, task switch, context switch– Hardware context switch: a far jmp– 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
![Page 24: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/24.jpg)
Task State Segment
• A specific segment type in x86: TSS
![Page 25: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/25.jpg)
Creating Processes
• 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
![Page 26: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/26.jpg)
clone(), fork(), and vfork() System Calls
• clone(fn, arg, flags, child_stack): creating lightweight process– A wrapper function in C library– Uses clone() system call
• fork() system call: implemented by clone()
• vfork() system call: implemented by clone()
• Each invokes do_fork() function
![Page 27: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/27.jpg)
Kernel Threads
• Each kernel thread executes a single specific kernel C function
• Kernel threads run only in kernel mode
• They use only linear addresses greater than PAGE_OFFSET
![Page 28: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/28.jpg)
Kernel Threads
• 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, …–
![Page 29: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/29.jpg)
Destroying Processes
• exit() library function– _exit() system call– Handled by do_exit() function
• Process removal– Releasing the process descriptor of a
zombie process by release_task()
![Page 30: Processes and Threads in Linux (Chap. 3, Understanding the Linux Kernel)](https://reader035.vdocuments.us/reader035/viewer/2022081511/568145e7550346895db2e6df/html5/thumbnails/30.jpg)
Thanks for Your Attention!