W4118 Operating Systems Instructor: Junfeng YangOutline Linux process overview Linux process data structures Linux process operationsFinding process information ps top For each process, there is a corresponding directory /proc/<pid> to store this process information in the /proc pseudo file systemHeader filesinclude/linux/sched.h – declarations for most process data structuresinclude/linux/wait.h – declarations for wait queuesinclude/asm-i386/system.h – architecture-dependent declarationsSource files kernel/sched.c – process scheduling routines kernel/signal.c – signal handling routines kernel/fork.c – process/thread creation routines kernel/exit.c – process exit routines fs/exec.c – executing program arch/i386/kernel/entry.S – kernel entry points arch/i386/kernel/process.c – architecture-dependent process routinesProcess-related filesKernel address space Kernel needs work space as wellStore kernel code, data, heap, and stack• E.g., process control blocksMust be protected from user processes Can give kernel its own address space Problem: switching address space is costly Solution: map kernel address space into process address spaceLinux process address spaceUser spaceKernel spaceUser-mode stack-areaProcess code and datakernel modeuser modeKernel space is also mapped into user space from user mode to kernel mode, no need to switch address spacesProtection?Kernel space is only accessible when mode bit = 0Shared runtime-libraries03G4Gprocess descriptorandkernel-mode stackLinux: processes or threads? Linux uses a neutral term: tasksTasks represent both processes and threadsWhen processes trap into the kernel, they share the Linux kernel’s address space kernel threadsOutline Linux process overview Linux process data structures Linux process operationsLinux task data structures task_struct: process control block thread_info: low level task data, directly accessed from entry.S kernel stack: work space for systems calls (the kernel executes on the user process’s behalf) or interrupt handlers Task queues: queues that chain tasks togetherProcess Control Block in Linux task_struct (process descriptor in ULK)include/linux/sched.hEach task has a unique task_structProcess statesstate: what state a process is inTASK_RUNNING – the thread is running on the CPU or is waiting to runTASK_INTERRUPTIBLE – the thread is sleeping and can be awoken by a signal (EINTR)TASK_UNINTE RRUPTIBLE – the thread is sleeping and cannot be awakened by a signalTASK_STOPPED – the process has been stopped by a signal or by a debuggerTASK_TRACED – the process is being traced via the ptracesystem callexit_state: how a process exitedEXIT_ZOMBI E – the process is exiting but has not yet been waited for by its parentEXIT_DEAD – the process has exited and has been waited forHardware state Thread: thread_struct – hardware state, e.g., registers x86 hardware state is defined in include/asm-i386/processor.hProcess scheduling prio: priority of the process Static_prio, run_list, array, sleep_avg, timestamp, last_ran, time_slice, …More on Linux scheduling laterProcess IDs process ID: pid thread group ID: tgidpid of first thread in processgetpid() returns this ID, so all threads in a process share the same process ID many system calls identify a process by its PIDLinux kernel uses pidhash to efficiently find processes by pidssee include/linux/pid.h, kernel/pid.cProcess Relationships Processes are related: children, siblingParent/child (fork()), siblingsPossible to "re-parent"• Parent vs. original parentParent can "wait" for child to terminate Process groups: signal_struct->pgrpPossible to send signals to all members Sessions: signal_struct->sessionProcesses related to loginOther PCB data structures user: user_struct – per-user information (for example, number of current processes) mm, active_mm: mm_struct – memory areas for the process (address space) fs: fs_struct – current and root directories associated with the process files: files_struct – file descriptors for the process signal: signal_struct – signal structures associated with the processthread_info include/asm-i386/thread_info.h low level task data, directly accessed from entry.S current_thread_info: get current thread_infostruct from Ckernel stack Each process in Linux has two stacks, a user stack and a kernel stack (8KB by default)Kernel stack can only be accessed in kernel modeKernel code runs on kernel stack Why not reuse user-space stack?homeworkFinding kernel stack (on x86)CPU0trGlobal Descriptor Table8-KBTask’s kernel-stackCPU0kern stack topespUpon intr, h/w retrieves kernel stack top and load it into %esp, also pushes previous %esp &%eip on kernel stackChanges on each context switch (__switch_to inarch/i386/kernel/process.c)initialized in startup_32 in arch/i386/boot/compressed/head.SConnections between task_struct and kernel stack Linux uses part of a task’s kernel-stack to store a structure thread_infothread_info and task_struct contain pointers to each otherTask’s kernel-stackTask’s thread-infoTask’sprocess-descriptorstruct task_struct8-KB8KB aligned0xe800e0000xe8010000How to find thread_info from kernel stack?movl $0xFFFFE000, %eaxandl %esp, %eax (mask out last 13 bits)Task’s kernel-stackTask’s thread-infoTask’sprocess-descriptorstruct task_struct8-KB8KB aligned0xe800e000esp0xe8010000How Linux manages processes Linux uses multiple queues to manage processesQueue for all tasksQueue for “running” tasks Queues for tasks that temporarily are “blocked”while waiting for a particular event to occur These queues are implemented using doubly-linked list (struct list_head in include/linux/list.h)Some tasks are ‘ready-to-run’Those tasks that are ready-to-run comprise a sub-list of all the tasks, and they are arranged on a queue known as the ‘run-queue’(struct runqueue in kernel/sched.c)Those tasks that are blocked while awaiting a specific event to occurare put on alternative sub-lists, called ‘wait queues’, associated withthe particular event(s) that will allow a blocked task to be unblocked (wait_queue_t in include/linux/wait.h and kernel/wait.c)run_queueinit_task listKernel Wait
View Full Document