csc 660: advanced operating systemsslide #1 csc 660: advanced os processes
TRANSCRIPT
CSC 660: Advanced Operating Systems Slide #1
CSC 660: Advanced OS
Processes
CSC 660: Advanced Operating Systems Slide #2
Topics
1. What is a Process?2. Task Descriptor3. Process Lifecycle4. Process Address Space5. Context Switch6. Linked lists7. Creation8. fork() and clone()9. Process Relationships10. Termination11. Zombies
CSC 660: Advanced Operating Systems Slide #3
What is a process?
A process is a program in execution.Program code + dynamic execution context.
VirtualizationProcesses provide virtual CPU + virtual memory.
Kernel refers to processes as tasks.
CSC 660: Advanced Operating Systems Slide #4
What is in a process?
A process consists of.Program code.
Address space.
Data.
Resources: open files, signals.
At least one thread of execution.
Threads contain:Program counter.
Stack.
Register set.
CSC 660: Advanced Operating Systems Slide #5
Process Control Block: task_struct
CSC 660: Advanced Operating Systems Slide #6
Process Identity
• Kernel: Address of task_struct– current macro
• User space: PID– current->pid– 32768 possible PIDs– pidhash
CSC 660: Advanced Operating Systems Slide #7
Process Lifecycle
CSC 660: Advanced Operating Systems Slide #8
Process StateTASK_RUNNING
Process is ready or running.
TASK_INTERRUPTIBLEWaiting, able to receive interrupts.
TASK_UNINTERRUPTIBLEWaiting, unable to receive interrupts.
TASK_STOPPEDExecution stopped, can resume with SIGCONT.
TASK_TRACEDExecution stopped by the ptrace() debugging mechanism.
EXIT_ZOMBIEExecution terminated, but parent hasn’t wait()ed.
EXIT_DEADBeing removed from system as parent has wait()ed.
CSC 660: Advanced Operating Systems Slide #9
Process Address Space
Process appears to have flat memory space.Discontinguous areas of physical memory mapped
via page table to produce virtual memory.
Described by mm_struct
Virtual memory space3GB virtual memory on 32-bit architectures.
High 1GB reserved for mapping kernel memory.
CSC 660: Advanced Operating Systems Slide #10
32-bit Process Address Space
0x08000000
0xBFFFFFFF
text
data
heap
stack
EIP
ESP
bss
CSC 660: Advanced Operating Systems Slide #11
Context Switch
When does scheduler switch out process?Blocked on I/O.
Time slice expires.
Where is switching done?In schedule() function.
How does it switch?Switch to new page table.
Save hardware context to stack.
Switch stack pointers.
Load hardware context of next process from stack.
CSC 660: Advanced Operating Systems Slide #12
Context Switch Example
CSC 660: Advanced Operating Systems Slide #13
Your own task_struct
How to find your task_struct?1. stack pointer points to top of kernel stack
2. thread_info stored at bottom of kernel stack
3. mask 13 lsb’s of stack pointer to get address
4. thread_info has pointer to task_struct
Use current macro to do this normally.
CSC 660: Advanced Operating Systems Slide #14
Your own task_struct
CSC 660: Advanced Operating Systems Slide #15
Kernel linked lists
Circular doubly-linked list.No special first or last node.
Defined in <linux/list.h> struct list_head {
struct list_head *next, *prev;
}
Use by embedding in structures: struct my_struct {
struct list_head list;
void *data;
}
CSC 660: Advanced Operating Systems Slide #16
Kernel linked lists
CSC 660: Advanced Operating Systems Slide #17
Kernel Linked Listsstruct my_struct *p;struct list_head *new, *x, *my;INIT_LIST_HEAD(&p->list)
Create a new linked list of my_struct’s.
list_add(new, p)Add new list_head to list after p.
list_del(struct list_head *x)Remove structure containing list_head pointer x.
list_for_each(p, &my->list)Iterate over list starting with my, with p as pointer to current element.
list_entry(p, struct my_struct, list)Return pointer to my_struct whose list_head is p.
CSC 660: Advanced Operating Systems Slide #18
Process Lists
#define next_task(p) list_entry((p)->tasks.next, struct task_struct, tasks)
#define prev_task(p) list_entry((p)->tasks.prev, struct task_struct, tasks)
#define for_each_process(p) \
for (p = &init_task ; (p = next_task(p)) != &init_task ; )
CSC 660: Advanced Operating Systems Slide #19
Where do processes come from?
Kernel creates some processes.Kernel threads.
init
Processes create all other processes.Process copies itself with fork().
Original is parent, new process is child.
Child can load its own program with exec().
CSC 660: Advanced Operating Systems Slide #20
Process Creation and Termination
CSC 660: Advanced Operating Systems Slide #21
fork()
System call creates a new processCreates a new task_struct.
Initializes with copy of parent resources.
Creates a new address space.
Page table is copy-on-write pointer to parent.
Places child process on ready queue.
Returns in both parent and child.Parent return value is PID of child process.
Error return value of -1 indicates no child.
Child return value is 0.
CSC 660: Advanced Operating Systems Slide #22
clone()
Threads are just a type of process.Share address space, files, signal handlers.
All processes/threads created by clone()Process: clone(SIGCHLD, 0)
Thread: clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
CSC 660: Advanced Operating Systems Slide #23
Process Relationships
real_parentProcess that created p or process 1 (init).
parentProcess to be signaled on termination.
If ptrace(), is debugging process, else real_parent.
childrenList of all children created by p.
siblingPointers to next and previous elements of sibling list.
CSC 660: Advanced Operating Systems Slide #24
Process Relationships
CSC 660: Advanced Operating Systems Slide #25
Process Tree (Solaris)
CSC 660: Advanced Operating Systems Slide #26
Process Termination
Voluntaryexit() system call.
C compiler automatically adds to binaries.
Involuntarysignal
exception
CSC 660: Advanced Operating Systems Slide #27
Signals
• Software interrupts used for asynchronous communiction between processes.– SIGALRM: alarm() timer has gone off.– SIGINT: interrupt signal generated by Ctrl-c.– SIGSEGV: segmentation violation sent by kernel when
process accesses invalid address.– SIGKILL: terminates a process.
• Process can either– Ignore the signal: SIGKILL/STOP cannot be ignored.– Catch the signal: Create a signal handler function and
register it with the kernel to be called on signal.
CSC 660: Advanced Operating Systems Slide #28
do_exit()
1. Sets PF_EXITING flag.2. Releases resources:
1. Address space.2. File handles.3. Signal handlers and timers.
3. Sets exit_code member of task_struct.4. Notifies parent of termination (SIGCHLD)5. Reparents any children of process.6. Set process state to EXIT_ZOMBIE.7. Calls schedule() to switch to new process.
CSC 660: Advanced Operating Systems Slide #29
The Undead
Why do we need zombies?Must keep task_struct so parents can get exit_code member.
Parent issues wait() system call to get exit.If parent predeceases child, init will be parent.
Calls release_task() to free resources.Removes process from lists, hashes, caches.
Frees kernel stack and thread_info struct.
Frees task_struct.
CSC 660: Advanced Operating Systems Slide #30
Kernel Threads
• System processes that run only in kernel mode– Use kernel page tables (mem > PAGE_OFFSET)– Created by kernel_thread() function which calls do_fork(flags|CLONE_VM|CLONE_UNTRACED,…)
• Process 0: swapper– The idle process, runs cpu_idle() when no other
processes in TASK_RUNNING state.– Created statically when kernel boots.
• Process 1: init– Created by process 0 via kernel_thread().– Uses execve() system call to load init program.
CSC 660: Advanced Operating Systems Slide #31
Logging In
login program1. Checks password.
2. chdir to homedir.
3. chown terminal to user.
4. sets group IDs
5. initializes env variables
6. changes UID to user
7. execs login shell
CSC 660: Advanced Operating Systems Slide #32
Key Points
• Process = program + execution context• Execution context stored in PCB (task_struct.)
– Tasks organized in a doubly linked list.
– Referenced by PID from user space.
– Process states: RUNNING, (UN)INTERRUPTIBLE
• Process provides virtual CPU + memory.• Scheduler manages context switches.• Processes and threads created by clone().
– Parent/child and sibling relationships.
– Kernel reclaims resources on exit().
CSC 660: Advanced Operating Systems Slide #33
References1. Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel,
3rd edition, O’Reilly, 2005.2. Robert Love, Linux Kernel Development, 2nd edition, Prentice-Hall,
2005.3. Claudia Rodriguez et al, The Linux Kernel Primer, Prentice-Hall,
2005.4. Peter Salzman et. al., Linux Kernel Module Programming Guide,
version 2.6.1, 2005.5. Avi Silberchatz et. al., Operating System Concepts, 7th edition, 2004.6. W. Richard Stevens and Stephen Rago, Advanced Programming in
the UNIX Environment, 2nd edition, 2005.7. Andrew S. Tanenbaum, Modern Operating Systems, 3nd edition,
Prentice-Hall, 2005.