Process management CA 644 Overview of processe Process

  • Slides: 26
Download presentation
Process management CA 644

Process management CA 644

Overview of processe �Process ◦ ◦ ◦ ◦ often include: Open files Pending signals

Overview of processe �Process ◦ ◦ ◦ ◦ often include: Open files Pending signals Kernel data Memory state Memory address space with mapping Threads Data section contain global variables

Overview of processe �A program can has 2 or more processes �More processes can

Overview of processe �A program can has 2 or more processes �More processes can share same resources such as open files, memory �Process starts when : ◦ Fork() = duplicate process child process ◦ Exec() = create new address space �Process ends by : ◦ Exit() = terminate the process and free its

Process Descriptor and the Task Structure �Kernel store list of process in task list

Process Descriptor and the Task Structure �Kernel store list of process in task list (task array) �Each element in task list is a process description type struct task_struct (define in linux/sched. h) ◦ ◦ Open files Process’s address space Process’s state …. .

Allocating the Process Descriptor �With old kernel, struct task_struct was stored at the end

Allocating the Process Descriptor �With old kernel, struct task_struct was stored at the end of kernel stak �New kernel using struct thread_info as process descriptor and is stored in the bottom of stack

Allocating the Process Descriptor struct thread_info { struct task_struct *task; struct exec_domain *exec_domain; __u

Allocating the Process Descriptor struct thread_info { struct task_struct *task; struct exec_domain *exec_domain; __u 32 flags; __u 32 status; __u 32 cpu; int preempt_count; mm_segment_t addr_limit; struct restart_block; void *sysenter_return; int uaccess_err; };

Storing the Process Descriptor �PID is used to identify proccess �Type is pid_t which

Storing the Process Descriptor �PID is used to identify proccess �Type is pid_t which is an int (max = 32768) �Look up the process descriptor by current macro = take 13 leastsignificant bits of stack which done by current_thread_info() ex : current_thread_info()->task;

Process state �The state field holds informations : ◦ TASK_RUNNING : running or in

Process state �The state field holds informations : ◦ TASK_RUNNING : running or in run queue ◦ TASK_INTERRUPTIBLE : sleeping + waiting signals ◦ TASK_UNINTERRUPTIBLE : doesn’t wake up by signals but by particular events ◦ __TASK_TRACED : being traced by others ◦ __TASK_STOPPED: has stopped

Process state

Process state

Manipulating the Current Process State �In order to change a process’s state : set_task_state(task,

Manipulating the Current Process State �In order to change a process’s state : set_task_state(task, state); Or task->state = state Or set_current_state(state)

Process context �Process often operates in user-space, but when it call a system calls

Process context �Process often operates in user-space, but when it call a system calls or error handle it will procss in kernel-space. �When process exit from kernel, it’ll resume priority in userspace unless a higher process has been running in the kernel.

The Process Family Tree �A process has child and parent processes �To get parrent

The Process Family Tree �A process has child and parent processes �To get parrent and list child : struct task_struct *task; struct list_head *list; struct task_struct *my_parent; list_for_each(list, &current->children) { *my_parent = current->parent; task = list_entry(list, struct task_struct, sibling); /* task now points to one of current’s children */ }

The Process Family Tree �There is an init process after booting have no parents

The Process Family Tree �There is an init process after booting have no parents �To get this init : struct task_struct *task; for (task = current; task != &init_task; task = task->parent) ; /* task now points to init */

The Process Family Tree �To get next tasks in task list : list_entry(task->tasks. next,

The Process Family Tree �To get next tasks in task list : list_entry(task->tasks. next, struct task_struct, tasks) Or next_task(task) �To get previous taks in task list : list_entry(task->tasks. prev, struct task_struct, tasks) Or prev_task(task)

The Process Family Tree �To print all tasks in task list struct task_struct *task;

The Process Family Tree �To print all tasks in task list struct task_struct *task; for_each_process(task) { /* this pointlessly prints the name and PID of each task */ printk(“%s[%d]n”, task->comm, task->pid); }

Process creation �Unix uses two methods : ◦ Fork() �Duplicate current tasks �Doesn’t inherited

Process creation �Unix uses two methods : ◦ Fork() �Duplicate current tasks �Doesn’t inherited from parrent tasks(resources …) ◦ Exec() �Load new execution into the current address space

Process creation – copy-on-write �Copy-on-write(COW) is technique to prevent copying of the data ->

Process creation – copy-on-write �Copy-on-write(COW) is technique to prevent copying of the data -> parent and child can share a single copy �Until memory need be written, it is read-only -> delay the copying

Process creation – fork () Fork is implement via clone () � [fork()|vfork()|__clone()] ->

Process creation – fork () Fork is implement via clone () � [fork()|vfork()|__clone()] -> clone() -> do_fork() � � Do_fork() -> copy_process() 1. 2. 3. 4. Call dup_task_struct() : create new kernel stack, thread_info, task_struct Check new child will not exceed the resource limit Initialize values to differentiate itself from parent(task_struck unchange *) Child’s state is set to TASK_UNINTERRUPTIBLE to ensure it does not yet run 5. Call copy_flags() to update flags in task_struct 1. 2. If PF_SUPERPRIV flag set == super privileges PF_FORKNOEXEC flag = process didn’t call exec() 6. Call alloc_pid() to assign an available PID to new task 7. Determine shared resource or copy it 8. Cleans up and return pointer to new child (mayby return address) In common case, after fork, it call exec immediately. � Vfork is same as fork, expect page table entries of parent aren’t copied, parrent is block for child to execute until it exits or calls exec() �

Process creation – vfork () �Vfork is same as fork, expect page table entries

Process creation – vfork () �Vfork is same as fork, expect page table entries of parent aren’t copied �Parrent is block for child to execute until it exits or calls exec()

The Linux Implementation of Threads �A thread is merely a process that share resources.

The Linux Implementation of Threads �A thread is merely a process that share resources. �Each threat has unique task_struct

Creating Threads �Threads are called similar with normal tasks, except some flags in clone()

Creating Threads �Threads are called similar with normal tasks, except some flags in clone() clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0); �This code behavior like normal fork() but share VM, File system, File descriptions, Signal handle. �If we want to call normal fork() : clone(SIGCHLD, 0);

Creating Threads

Creating Threads

Kernel Threads �Kernel threads operate in kernelspace but have no an address space (mm

Kernel Threads �Kernel threads operate in kernelspace but have no an address space (mm pointer is null) �Kernel thread is created only by another kernel thread and on system boot �Discuss more on other chapters

Process Termination �A process is terminated by exit() or by receiving signal of exception

Process Termination �A process is terminated by exit() or by receiving signal of exception it cannot handle �The bulk of work is handle by do_exit()

Process Termination �Do_exit() 1. 2. 3. 4. 5. 6. 7. 8. 9. will do

Process Termination �Do_exit() 1. 2. 3. 4. 5. 6. 7. 8. 9. will do : Set PF_EXITING flag in task_struct Call del_timer_sync() to remove any kernel timers (ensure no timers is queue or running) In BSD, call acct_update_integrals() Call exit_mm() to release mm_struct, if address space is no longer shared, kernel will destroy it exit_sem() to dequeued any semaphore (race condition) exit_files() and exit_fs() to free resource if no longer share Set exit code in task_struct exit_notify() send signal to parent, set exit_state = EXIT_ZOMBIME Call schedule() to switch into new process (no return)

Process Termination �After exit, there are some resources which are still store in kernel

Process Termination �After exit, there are some resources which are still store in kernel stack: ◦ thread_info ◦ task_struct ◦ process descriptor �After parent receive information from them, these resources will be freed up