1 CSCI 350 Ch. 3 Programmer's Interface Mark Redekopp
2 Getting on the Same Page • What is a thread? • What is a process? • What is user mode vs. kernel mode?
3 Getting on the Same Page • What is a thread? – Independently scheduled task – PC + state (i.e. registers, stack, etc.) • What is a process? – Instance of an executable program – Threads + independent address space • What is user mode vs. kernel mode? – User mode = non-priviledged execution environment – Kernel mode = privileged execution environment where the OS executes
4 Chapter 3 of "Operating Systems…", Anderson and Dahlin PROGRAMMING INTERFACE
5 Overview • OS must provide services to a user application – Process and thread management – Input/output (file systems, network, etc.) – Memory management – Authentication and security – Graphics and window management
6 Where Services Might Execute • Many services must be provided Login to user level processes by the OS Service OS code • Options exist for where to locate User running as Process separate those services user OS GUI Widgets process Library – Directly in the user process by linking in a user-level OS library Kernel Scheduling Code OS – Separate user level processes Kernel Kernel Task – Part of the kernel executed in the Portions of control flow of the calling process Device Drivers – Part of the kernel running as a separate kernel task
7 Considerations • Evaluate where services should be OS code User running as located based on: Process separate user – Safety, Flexibility, Performance OS process Library • Pros of locating services in a user-level Kernel process Code OS Kernel – Safety/Isolation from other kernel structures Kernel Task (i.e. a bug cannot bring down the rest of the kernel) – Flexibility: Easier to update and provide new versions of functionality (no recompilation of kernel needed) • Cons of location services in a user-level process – Performance: Overhead of moving data and switching contexts
8 Common OS Layers/Architecture • System calls are the "interface" between user applications and the User mode User Applications kernel – OS may provide libraries that provide Portable OS Library some services and abstraction above OS System Calls the system call level Portable OS Kernel Kernel mode – Changing the interface of system calls Hardware Abstraction Layer has a major impact on software Hardware/Target Specific Code (aka Board Support • Likely requires recompile/update of Package) software applications + Device Drivers
9 Meet the syscalls • Process management Some definitions • Address space = Protected – fork() : new_pid (hardware checked) memory ranges accessible only to a single process – exec(char* exec, char** args) (and possibly the kernel) – wait(pid) • pid = Process ID (unique identifier of a process) – exit() • fd = File descriptor (identifier to lookup data structure holding the • I/O state of I/O access to a file, network socket, "pipe", etc.) – open(name) : fd • Often just an integer (index) – read(fd, buffer, size) : int into some table of descriptors in the kernel – write(fd, buffer, size) : int – close(fd) – pipe(fd[2]) – select(fd_array[], fd_array_size) : fd – dup2(fromFd, toFd)
10 fork, exec, wait, exit SYSCALLS FOR PROCESS MANAGEMENT
11 Process Management • Most OSs allow one user process to create another through various system calls – Rather than the kernel initiating all process creations • Windows approach – Perform process creation, environment setup, and program startup through a single system call • Single createProcess (…) system call • Unix approach – Separate process creation, environment setup, and program startup with separate system calls • Create process with fork() system call • Setup environment with possible calls to open(), close(), dup2() • Load program image and start execution with exec(…) system call
12 Unix Process Syscalls • Suppose a process (pid=1) is running and wants to start another 0xffffffff Kernel mem. Memory PCB (pid 1) Stack 0x7ffffc80 Heap User Process (./prog1) 0x5fffe180 pid = fork(); if(pid==0) exec("./prog2"); else wait(pid); Code User mem. pid = fork(); 0x080a4 if(pid==0) exec("./prog2"); else wait(pid); task_list_ptr PCB (pid=1, PC=0x080a4, stack=0x7ffffc80, fds :{…} ) OS Kernel 0x0 Abstract View Process 1 AS
13 Fork • A call to fork copies the state (exact replica) of the current ( parent ) process to a new child process 0xffffffff 0xffffffff Kernel mem. Memory Memory – Returns new pid (e.g. 2) to the parent – Returns 0 to the child process (it can retrieve its PCB (pid 1) PCB (pid 1) PCB (pid 2) PCB (pid 2) own pid with another system call if it needs that info) Stack Stack 0x7ffffc80 0x7ffffc80 Heap Heap User Process Child Process (./prog1) (PID=2) 0x5fffe180 0x6fffe180 pid=2 pid = fork(); pid = fork(); pid=0 if(pid==0) if(pid==0) exec("./prog2"); exec("./prog2"); else else wait(pid); Code Code wait(pid); User mem. pid = fork(); pid = fork(); 0x080a4 0x080a4 if(pid==0) if(pid==0) exec("./prog2"); exec("./prog2"); else else wait(pid); wait(pid); Notice the child process is task_list_ptr executing the same code PCB PCB as the parent but since (pid=1, (pid=2, 'pid' is different it will PC=0x080a4, PC=0x080a4, execute a separate branch of the 'if' statement stack=0x7ffffc80, stack=0x6fffe180, fds : {…} ) fds :{…} ) OS Kernel 0x0 0x0 Abstract View Process 2 AS Process 1 AS
14 Exec 1 • A call to exec now loads a new program (e.g. ./prog2) with its own code, sets up its global data, stack, heap, etc. 0xffffffff 0xffffffff Kernel mem. Memory Memory – exec returns only if an error occurs • Parent can continue execution or block until the PCB (pid 1) PCB (pid 1) PCB (pid 2) PCB (pid 2) child process finishes by calling wait Stack Stack 0x7ffffc80 0x7ffffc80 Heap Heap User Process Child Process (./prog1) (PID=2) pid=2 pid = fork(); pid = fork(); pid=0 if(pid==0) if(pid==0) exec("./prog2"); exec("./prog2"); else else wait(pid); Code Code wait(pid); User mem. pid = fork(); pid = fork(); 0x080a4 0x080a4 if(pid==0) if(pid==0) exec("./prog2"); exec("./prog2"); else else wait(pid); wait(pid); Child process starts to task_list_ptr load and execute a new PCB PCB program (pid=1, (pid=2, PC=0x080a4, PC=0x080a4, Parent process can do stack=0x7ffffc80, stack=0x6fffe180, other work or block (wait) until child process is done fds : {…} ) fds :{…} ) OS Kernel 0x0 0x0 Abstract View Process 2 AS Process 1 AS
15 Exec, Wait, and Exit • The effect of a call to exec is a separate program image is loaded and begins execution • If the parent calls wait then it will block until 0xffffffff 0xffffffff Kernel mem. Memory Memory the child process calls exit – PCB (pid 1) PCB (pid 1) exit is generally called when the program finishes PCB (pid 2) PCB (pid 2) main() Stack 0x7ffffc80 Heap User Process Child Process (./prog1) (./prog2) Stack 0x5fffe180 pid=2 pid = fork(); int main() Heap if(pid==0) { exec("./prog2"); printf("Hi\n"); else return 0; wait(pid); } Code User mem. pid = fork(); Child process is now 0x080a4 if(pid==0) independently executing a exec("./prog2"); else separate program wait(pid); Code task_list_ptr PCB PCB int main() 0x04010 { (pid=1, (pid=2, printf("Hi\n"); return 0; PC=0x080a4, PC=0x04010, } stack=0x7ffffc80, stack=0x6fffe180, fds : {…} ) fds :{…} ) OS Kernel 0x0 0x0 Abstract View Process 2 AS Process 1 AS
16 Exit • exit deletes the current process and its resources (i.e. address space, etc.) • PCB is not deallocated until parent or other 0xffffffff 0xffffffff Kernel mem. Memory Memory entity specifically release it – PCB (pid 1) PCB (pid 1) This allows the parent or other task to examine the PCB (pid 2) PCB (pid 2) child process' exit status, etc. Stack 0x7ffffc80 Heap User Process Child Process (./prog1) (./prog2) Stack 0x5fffe180 pid=2 pid = fork(); int main() Heap if(pid==0) { exec("./prog2"); printf("Hi\n"); else return 0; wait(pid); } // exit() is called Code User mem. pid = fork(); Child exits and its 0x080a4 if(pid==0) resources are returned exec("./prog2"); else wait(pid); Code task_list_ptr PCB PCB int main() 0x04010 { (pid=1, (pid=2, printf("Hi\n"); return 0; PC=0x080a4, PC=0x04010, PCB remains until } stack=0x7ffffc80, stack=0x6fffe180, explicitly deallocated by fds : {…} ) fds :{…} ) another entity OS Kernel 0x0 0x0 Abstract View Process 2 AS Process 1 AS
Recommend
More recommend