CSC 660 Advanced OS Processes CSC 660 Advanced

  • Slides: 30
Download presentation
CSC 660: Advanced OS Processes CSC 660: Advanced Operating Systems 1

CSC 660: Advanced OS Processes CSC 660: Advanced Operating Systems 1

Topics 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. What is

Topics 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. What is a Process? Task Descriptor Process Lifecycle Process Address Space Context Switch Linked lists Creation fork() and clone() Process Relationships Termination Zombies CSC 660: Advanced Operating Systems 2

What is a process? A process is a program in execution. Program code +

What is a process? A process is a program in execution. Program code + dynamic execution context. Virtualization Processes provide virtual CPU + virtual memory. Kernel refers to processes as tasks. CSC 660: Advanced Operating Systems 3

What is in a process? A process consists of. Program code. Address space. Data.

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 4

Process Control Block: task_struct CSC 660: Advanced Operating Systems 5

Process Control Block: task_struct CSC 660: Advanced Operating Systems 5

Process Identity • Kernel: Address of task_struct – current macro • User space: PID

Process Identity • Kernel: Address of task_struct – current macro • User space: PID – current->pid – 32768 possible PIDs – pidhash CSC 660: Advanced Operating Systems 6

Process Lifecycle CSC 660: Advanced Operating Systems 7

Process Lifecycle CSC 660: Advanced Operating Systems 7

Process State TASK_RUNNING Process is ready or running. TASK_INTERRUPTIBLE Waiting, able to receive interrupts.

Process State TASK_RUNNING Process is ready or running. TASK_INTERRUPTIBLE Waiting, able to receive interrupts. TASK_UNINTERRUPTIBLE Waiting, unable to receive interrupts. TASK_STOPPED Execution stopped, can resume with SIGCONT. TASK_TRACED Execution stopped by the ptrace() debugging mechanism. EXIT_ZOMBIE Execution terminated, but parent hasn’t wait()ed. EXIT_DEAD Being removed from system as parent has wait()ed. CSC 660: Advanced Operating Systems 8

Process Address Space Process appears to have flat memory space. Discontinguous areas of physical

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 space 3 GB virtual memory on 32 -bit architectures. High 1 GB reserved for mapping kernel memory. CSC 660: Advanced Operating Systems 9

32 -bit Process Address Space 0 x. BFFFFFFF stack ESP heap bss data 0

32 -bit Process Address Space 0 x. BFFFFFFF stack ESP heap bss data 0 x 08000000 text CSC 660: Advanced Operating Systems EIP 10

Context Switch When does scheduler switch out process? Blocked on I/O. Time slice expires.

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 11

Context Switch Example CSC 660: Advanced Operating Systems 12

Context Switch Example CSC 660: Advanced Operating Systems 12

Your own task_struct How to find your task_struct? 1. stack pointer points to top

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 CSC 660: Advanced Operating Systems 13

Your own task_struct CSC 660: Advanced Operating Systems 14

Your own task_struct CSC 660: Advanced Operating Systems 14

Kernel linked lists Circular doubly-linked list. No special first or last node. Defined in

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 15

Kernel linked lists CSC 660: Advanced Operating Systems 16

Kernel linked lists CSC 660: Advanced Operating Systems 16

Kernel Linked Lists struct my_struct *p; struct list_head *new, *x, *my; INIT_LIST_HEAD(&p->list) Create a

Kernel Linked Lists struct 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 17

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

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 18

Where do processes come from? Kernel creates some processes. Kernel threads. init Processes create

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 19

Process Creation and Termination CSC 660: Advanced Operating Systems 20

Process Creation and Termination CSC 660: Advanced Operating Systems 20

fork() System call creates a new process Creates a new task_struct. Initializes with copy

fork() System call creates a new process Creates 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 21

clone() Threads are just a type of process. Share address space, files, signal handlers.

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 22

Process Relationships real_parent Process that created p or process 1 (init). parent Process to

Process Relationships real_parent Process that created p or process 1 (init). parent Process to be signaled on termination. If ptrace(), is debugging process, else real_parent. children List of all children created by p. sibling Pointers to next and previous elements of sibling list. CSC 660: Advanced Operating Systems 23

Process Relationships CSC 660: Advanced Operating Systems 24

Process Relationships CSC 660: Advanced Operating Systems 24

Process Tree (Solaris) CSC 660: Advanced Operating Systems 25

Process Tree (Solaris) CSC 660: Advanced Operating Systems 25

Process Termination Voluntary exit() system call. C compiler automatically adds to binaries. Involuntary signal

Process Termination Voluntary exit() system call. C compiler automatically adds to binaries. Involuntary signal exception CSC 660: Advanced Operating Systems 26

do_exit() 1. Sets PF_EXITING flag. 2. Releases resources: 1. Address space. 2. File handles.

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. 5. 6. 7. Notifies parent of termination (SIGCHLD) Reparents any children of process. Set process state to EXIT_ZOMBIE. Calls schedule() to switch to new process. CSC 660: Advanced Operating Systems 27

The Undead Why do we need zombies? Must keep task_struct so parents can get

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 28

Key Points • Process = program + execution context • Execution context stored in

Key Points • Process = program + execution context • Execution context stored in PCB (task_struct. ) – Tasks organized in a linked list. – Referenced by PID from user space. • Process provides virtual CPU + memory. • Scheduler manages context switches. • Processes and threads created by clone(). – Parent/child relationship. – Kernel reclaims resources on exit(). CSC 660: Advanced Operating Systems 29

References 1. 2. 3. 4. 5. 6. Daniel P. Bovet and Marco Cesati, Understanding

References 1. 2. 3. 4. 5. 6. Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3 rd edition, O’Reilly, 2005. Robert Love, Linux Kernel Development, 2 nd edition, Prentice-Hall, 2005. Claudia Rodriguez et al, The Linux Kernel Primer, Prentice-Hall, 2005. Peter Salzman et. al. , Linux Kernel Module Programming Guide, version 2. 6. 1, 2005. Avi Silberchatz et. al. , Operating System Concepts, 7 th edition, 2004. Andrew S. Tanenbaum, Modern Operating Systems, 3 nd edition, Prentice-Hall, 2005. CSC 660: Advanced Operating Systems 30