Thread

advertisement
제37강 : Process (Data Structures)
Ch 3 Process
- Data Structures -
1
Data Structure change (1)
don’t divide PCB for swapping
• ‘Process’  ‘Task’ & ‘Thread’ (Linux)
• Lions:
Segment based swapping
PCB  (proc & user)
• Linux:
Paging based swapping
LRU (Least_Recently_Used) page is replaced.
[Lions PCB]
[Linux PCB]
proc
user
a.out
task_struct
a.out
2
Data Structure change (2)
Static
• Lions:
• Linux:
 Dynamic
Allocation
space -- static allocation (kernel coding time)
space -- dynamically allocated on-demand
kernel manages space pool
proc
proc[N]
file[M]
file
inode
inode[I]
storage
pool
- static allocation
- arrays
- not flexible (space management)
[Lions]
- Dynamic allocation (kernel)
- Central pool, linked list
- flexible
[Linux]
3
Array  Linked list
static allocation
proc[]
dynamic allocation
proc
proc[1]
proc[2]
task_struct
proc[3]
proc[4]
task_struct
proc[5]
task_struct
[Lions]
[Linux]
4
Data Structure change (3)
Multiple Structs for fork overhead
files
Lions’
PCB
struct
files
fs
per
thread
per
thread
tty
basic
info
signals
mm
Linux
PCB
struct
signals
child copies all values
(always)
[Lions]
tty
basic
mm
info
fs
files: open files
fs:
file system
mm: main memory
child may copy
selectively
[Linux]
5
Process Creation Overhead
Light-weight & Heavy-weight
files
per
files
per
(heavy-weight creation)
copy everything
fs
thread
basic
tty
info
mm
thread
basic
info
fs
Child’s
Private
copy
signals
tty
mm
signals
(light-weight creation)
copy selectively
per
thread
basic
info
No copy
for child
Child share
parent’s
data
6
clone() system call
- five flags files
per
fs
thread
basic
info
tty
no copy
per
eg clone(10101)
mm
copy
files
(1)
fs
(0)
thread
tty
(1)
mm
(0)
signals
(1)
basic
info
signals
UNIX process
clone(11111) = UNIX fork()
Linux thread = LWP(Light-Weight Process)
clone(00000)
MIN data copy
7
struct task_struct {
volatile long state;
struct thread_info *thread_info;
unsigned long flags;
int prio, static_prio;
struct list_head tasks;
struct mm_struct
*mm;
struct task_struct *parent;
struct list_head children;
struct list_head sibling;
struct tty_struct
*tty;
/* ipc stuff */
struct sysv_sem sysvsem;
/* CPU-specific state of this task */
struct thread_struct thread;
/* file system information */
struct fs_struct
*fs;
/* open file information */
struct files_struct
*files;
/* namespace */
struct namespace *namespace;
/* signal handlers */
struct signal_struct
*signal;
struct sighand_struct *sighand;
};
/* -1 unrunnable, 0 runnable, >0 stopped */
/* per process flags, defined below */
/* parent process */
/* list of my children */
/* linkage in my parent's children list */
files
per
fs
thread
basic
info
tty
mm
signals
8
struct task_struct {
volatile long state;
struct thread_info *thread_info;
unsigned long flags;
int prio, static_prio;
struct list_head tasks;
struct mm_struct
*mm;
struct task_struct *parent;
struct list_head children;
struct list_head sibling;
struct tty_struct
*tty;
/* ipc stuff */
struct sysv_sem sysvsem;
/* CPU-specific state of this task */
struct thread_struct thread;
/* file system information */
struct fs_struct
*fs;
/* open file information */
struct files_struct
*files;
/* namespace */
struct namespace *namespace;
/* signal handlers */
struct signal_struct
*signal;
struct sighand_struct *sighand;
};
/* -1 unrunnable, 0 runnable, >0 stopped */
/* per process flags, defined below */
/* parent process */
/* list of my children */
/* linkage in my parent's children list */
files
per
fs
thread
basic
info
tty
mm
signals
9
files
Linux “thread”
per
fs
thread
tty
basic
mm
info
•
•
•
•
•
•
•
signals
Linux has no “true” thread
Allows clone() with “minimum data copy” flag
Child shares data with parent through pointers
Low overhead for process creation
clone(“minimum data copy”) = “LWP” = “thread”
clone() can specify where new thread belongs to
man clone
10
CLONE(2)
Linux Programmer's Manual
CLONE(2)
NAME
clone - create a child process
SYNOPSIS
#include <sched.h>
int clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
_syscall2(int, clone, int, flags, void *, child_stack)
DESCRIPTION
clone creates a new process, just like fork(2). Unlike fork(2), these calls allow the child
process to share parts of its execution context with the calling process, such as the
memory space, the table of file descriptors, and the table of signal handlers. (Note that on
this manual page, "calling process" normally corresponds to "parent process".)
Main use of clone is to implement threads: multiple threads of control in a program that
run concurrently in a shared memory space.
When the child process is created with clone, it executes the function application fn(arg).
(This differs from fork(2), where execution continues in the child from the point of the
fork(2) call.) The fn argument is a pointer to a function that is called by the child process
at the beginning of its execution. The arg argument is passed to the fn function. When
the fn(arg) function application returns, the child process terminates. The integer returned
by fn is the exit code for the child pro cess. The child process may also terminate
explicitly by calling exit(2) or after receiving a fatal signal.
11
Data Structure change (4)
PCB separates from kernel stack
• struct user was small (Lions)  task_struct is big (Linux)
struct user
• Linux
thread_info
struct task_struct
{
{
}
}
kernel
stack
kernel
stack
– task_struct does not share space with kernel_stack
* instead, thread_info resides in kernel_stack page
* thread_info is much smaller than task_struct
* thread_info has pointer to
task_struct
* thread_info is for thread;
task_struct for task
12
/* this struct shares the supervisor stack pages */
struct thread_info {
struct task_struct *task;
/* main task structure */
struct exec_domain *exec_domain;
/* execution domain */
unsigned long
flags;
/* low level flags */
unsigned long
status;
/* thread-sync flags */
u32
cpu;
/* current CPU */
s32
preempt_count;
struct restart_block
/*restart_block;
u8 supervisor_stack[0];
};
struct thread_info
{ task_struct *task;}
task_struct
files
per
fs
thread
basic
info
High Address
kernel
stack
tty
mm
signals
13
Download