operating system project / lecture 1 tasks and...
TRANSCRIPT
![Page 1: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/1.jpg)
Operating System Project / Lecture 1
Tasks and scheduling
Bon Keun Seo
![Page 2: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/2.jpg)
Program and process
• Program: executable code
• Process: a running instance of a program
– Runtime resources (in user’s view) • CPU: a set of registers, including IP (instruction pointer) register
• Memory: code, data, heap, stack, …
• File: open files, sockets, device handle, …
/bin/bash Program
(bash) Process 1
(bash) Process 2
(bash) Process 3
![Page 3: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/3.jpg)
Process and threads
• An execution sequence inside a process
– Shares resources: code, memory, files
Resource allocation unit
Scheduling unit (registers, stack)
![Page 4: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/4.jpg)
Modes of tasks
• A process / thread can run either in user or kernel mode
– System call or interrupt
3 1 1
0 Kernel
1
2
3 Application
write(…)
User mode Kernel mode
sys_write(…)
sys_enter
sys_exit
![Page 5: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/5.jpg)
Thread implementation model
• User-level thread vs. kernel-level thread
Process User-thread
Kernel-thread
Ring 3
Ring 0
User-level thread library Linux Solaris
![Page 6: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/6.jpg)
Tasks in Linux kernel
• Both process and thread
– struct task_struct <include/linux/sched.h>
– Everything the kernel has to know
– 391 lines, about 2KB
• Task list
– The list of tasks in kernel
– Doubly linked list
mm
state
parent
tty
thread
fs
files
signal
![Page 7: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/7.jpg)
Key resources in task_struct
Variable Meaning
pid Process identifier
state Task’s state (running, stopped, interruptible, …)
mm Virtual memory mapping
parent Parent task
tty I/O console
thread CPU-specific state
fs Current directory
files Open file descriptors
signal / sighand Signal handler
pending Pending signals
![Page 8: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/8.jpg)
Kernel stack and task_struct
• Kernel stack
– An execution stack for kernel use • After sysenter instruction,
– CPU jumps to a specific system call handler
– Sets stack frame pointer to a kernel stack
– Provides protection to kernel data structures
– Per task, 8KB by default
• task_struct was at the top of kernel stack < v2.6
– Too large to fit in kernel stack anymore (>2KB)
• struct thread_info
– Light-weight process descriptor in the kernel stack
![Page 9: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/9.jpg)
Acquiring process descriptor
• current_thread_info() = %ESP & 0xffffe000
– Calculated from the kernel stack frame address
• current macro
– Current thread’s task_struct
– current_thread_info()->task
Kernel Stack
thread_info
task_struct
0x015fa000
0x015fb000
0x015fbfff
current
current_thread_info()
![Page 10: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/10.jpg)
States of tasks
State Description
TASK_RUNNING Currently running or on a runqueue waiting to run.
TASK_INTERRUPTIBLE Sleeping, waiting for some condition to exist.
TASK_UNINTERRUPTIBLE Identical to TASK_INTERRUPTIBLE, except that it doesn’t wake up and become runnable if it receives a signal.
TASK_ZOMBIE Terminated, but its parent has not yet issued a wait().
TASK_STOPPED Process execution has stopped. (by signal)
![Page 11: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/11.jpg)
Lifetime of a task
![Page 12: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/12.jpg)
Kernel mode contexts
• Process context
– System call
– Kernel is ‘executing on behalf of the process’
– The current macro is valid
• Interrupt context
– Interrupt handler
– No ‘valid’ task for the interrupt
– The current macro is invalid
![Page 13: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/13.jpg)
Process creation
• fork() In POSIX API
– Implemented via clone() system call
– Copy on write method
– do_fork() <kernel/fork.c>
• Allocate a new PID
• copy_process()
– dup_task_struct() to create a new kernel stack, thread_info, task_struct
– Copy resources: copy_files(), copy_fs(), copy_mm(), …
– copy_thread() to initialize the kernel stack
– sched_fork() to ready for the new task to schedule
• wake_up_new_task()
– Schedule the child task (run the child first)
![Page 14: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/14.jpg)
Thread management
• No explicit thread in Linux kernel
– Implemented also via clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
• POSIX compatibility
– NPTL (Native POSIX Thread Library) from RedHat
– User-level library + kernel support
– Thread group • PID-related system calls should be handled correctly
– getpid(), kill(), _exit()
• Thread group ID as a PID of a process: task_struct.tgid
– The PID of the thread group leader
![Page 15: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/15.jpg)
Kernel thread
• Standard process that exist solely in kernel space
– No address space
– Schedulable entity: can sleep and wake up periodically
• Creating a kernel thread
– int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
– struct task_struct * kthread_run(int (*fn)(void *), void *arg, char *name);
• Managing status
– kthread_stop() a thread forces the other thread to stop
– kthread_should_stop() check whether to stop or not
![Page 16: Operating System Project / Lecture 1 Tasks and schedulingcsl.skku.edu/uploads/SWE3015S14/swe3015s14task.pdf–Doubly linked list mm state parent tty thread fs files signal . Key resources](https://reader035.vdocument.in/reader035/viewer/2022071215/60443208a29b0c1b977a98ee/html5/thumbnails/16.jpg)
Process management
• PID hash table
– To find a task struct from PID – struct pid *find_vpid_ns(int nr)
– struct task_struct pid_task(struct pid *pid, enum pid_type)
• Process family tree
– Root: /sbin/init (PID 1)
P0
P1 P2 P3
P4 sibling.prev/next
parent
children.prev/next