1 / 91

CS 3214 Computer Systems

CS 3214 Computer Systems. Godmar Back. Part 1. Processes. Processes. Def: An instance of a program in execution OS provides each process with key abstractions Logical control flow 1 flow – single-threaded process Multiple flows – multi-threaded process Private address space

evansj
Download Presentation

CS 3214 Computer Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 3214Computer Systems Godmar Back

  2. Part 1 Processes CS 3214 Spring 2018

  3. Processes • Def: An instance of a program in execution • OS provides each process with key abstractions • Logical control flow • 1 flow – single-threaded process • Multiple flows – multi-threaded process • Private address space • Abstracted resources: e.g., stdout/stdin file descriptors • These abstractions create the illusion that each process has access to its own • CPU (or CPUs for multi-threaded processes) • Memory • Devices: e.g., terminal CS 3214 Spring 2018

  4. Context Switching • Historical motivation for processes was introduction of multi-programming: • Load multiple processes into memory, and switch to another process if current process is (momentarily) blocked • This required protection and isolation between these processes, implemented by a privileged kernel: dual-mode operation. • Time-sharing: switch to another process periodically to make sure all processes make equal progress • Switch between processes is called a context switch CS 3214 Spring 2018

  5. Dual-Mode Operation • Two fundamental modes: • “kernel mode” – privileged • aka system, supervisor or monitor mode • Intel calls its PL0, Privilege Level 0 on x86 • “user mode” – non-privileged • PL3 on x86 • Bit in CPU – controls operation of CPU • Privileged operations can only be performed in kernel mode. Example: hlt • Must carefully control transition between user & kernel mode int main() { asm(“hlt”); } CS 3214 Spring 2018

  6. Mode Switching • User  Kernel mode • For reasons external or internal to CPU • External (aka hardware) interrupt: • timer/clock chip, I/O device, network card, keyboard, mouse • asynchronous (with respect to the executing program) • Internal interrupt (aka software interrupt, trap, or exception) • are synchronous • can be intended (“trap”): for system call (process wants to enter kernel to obtain services) • or unintended (usually): (“fault/exception”) (division by zero, attempt to execute privileged instruction in user mode, memory access violation, invalid instruction, alignment error, etc.) • Kernel  User mode switch on iret instruction CS 3214 Spring 2018

  7. Timer interrupt: P1 is preempted, context switch to P2 I/O device interrupt:P2’s I/O completeswitch back to P2 user mode kernel mode System call: (trap): P2 starts I/O operation, blocks context switch to process 1 Timer interrupt: P2 still has time left, no context switch A Context Switch Scenario Process 1 Process 2 Kernel CS 3214 Spring 2018

  8. user mode kernel mode Context Switching, Details intr_entry: (saves entire CPU state) (switches to kernel stack) intr_exit: (restore entire CPU state) (switch back to user stack) iret Process 1 Process 2 Kernel switch_threads: (in) (saves caller’s state) switch_threads: (out) (restores caller’s state) (kernel stack switch) CS 3214 Spring 2018

  9. user mode kernel mode User processes access kernel services by trapping into the kernel, executing kernel code to perform the service, then returning – very much like a library call. Unless the system call cannot complete immediately, this does not involve a context switch. System Calls Process 1 Kernel Kernel’s System Call Implementation CS 3214 Spring 2018

  10. Syscall example: write(2) • 32-bit Linux /* gcc -static -O -g -Wall write.c -o write */ #include <unistd.h> int main() { const char msg[] = "Hello, World\n"; return write(1, msg, sizeofmsg); } /usr/include/asm/unistd.h: …. #define __NR_write 4 …. 0805005a <__write_nocancel>: 805005a: 53 push %ebx 805005b: 8b 54 24 10 mov 0x10(%esp),%edx#arg2 805005f: 8b 4c 24 0c mov 0xc(%esp),%ecx# arg1 8050063: 8b 5c 24 08 mov 0x8(%esp),%ebx# arg0 8050067: b8 04 00 00 00 mov $0x4,%eax # syscall no 805006c: cd 80 int $0x80 805006e: 5b pop %ebx 805006f: 3d 01 f0 ff ffcmp $0xfffff001,%eax 8050074: 0f 83 56 1e 00 00 jae 8051ed0 <__syscall_error> 805007a: c3 ret CS 3214 Spring 2018

  11. user mode kernel mode KernelThreads Most OS support kernel threads that never run in user mode – these threads typically perform book keeping or other supporting tasks. They do not service system calls or faults. Process 1 Process 2 Kernel Kernel Thread Careful: “kernel thread” not the same as kernel-level thread (KLT) – more on KLT later CS 3214 Spring 2018

  12. Context vs Mode Switching • Mode switch guarantees kernel gains control when needed • To react to external events • To handle error situations • Entry into kernel is controlled • Not all mode switches lead to context switches • Kernel decides when – subject of scheduling policies • Mode switch does not change the identity of current process/thread • See blue/yellow colors in slide on ctxt switch details • Hardware knows about modes, does not (typically) know about contexts CS 3214 Spring 2018

  13. Bottom Up View: Exceptions • An exception is a transfer of control to the OS in response to some event (i.e., change in processor state) User Process OS exception current event next exception processing by exception handler exception return (optional) CS 3214 Spring 2018

  14. RUNNING Scheduler picks process Process must wait for event Process preempted BLOCKED READY Event arrived Reasoning about Processes:Process States • Only 1 process (per CPU) can be in RUNNING state CS 3214 Spring 2018

  15. Process States • RUNNING: • Process is on CPU, its instructions are executed • READY: • Process could make progress if a CPU were available • BLOCKED: • Process cannot make progress even if a CPU were available because it’s waiting for something (e.g., a resource, a signal, a point in time, a child to terminate, I/O, …) • Model is simplified • OS have between 5 and 10 states typically • Terminology not consistent across OS: • E.g., Linux calls BLOCKED “SLEEPING” and both READY and RUNNING processes are called “RUNNING”; a “RUNNING” process is also called the ‘current’ process on its CPU. CS 3214 Spring 2018

  16. User View • If process’s lifetimes overlap, they are said to execute concurrently • Else they are sequential • Default assumption is concurrent execution • Exact execution order is unpredictable • Programmer should never make any assumptions about it • Any interaction between processes must be carefully synchronized CS 3214 Spring 2018

  17. Process Creation • Two common paradigms: • Cloning vs. spawning • Cloning: (Unix) • “fork()” clones current process • child process then loads new program • Spawning: (Windows) • “exec()” spawns a new process with new program • Difference is whether creation of new process also involves a change in program CS 3214 Spring 2018

  18. #include <unistd.h> #include <stdio.h> int main() { int x = 1; if (fork() == 0) { // only child executes this printf("Child, x = %d\n", ++x); } else { // only parent executes this printf("Parent, x = %d\n", --x); } // parent and child execute this printf("Exiting with x = %d\n", x); return 0; } fork() Child, x = 2 Exiting with x = 2 Parent, x = 0 Exiting with x = 0 CS 3214 Spring 2018

  19. fork() #include <sys/types.h> #include <unistd.h> #include <stdio.h> int main(int ac, char *av[]) { pid_t child = fork(); if (child < 0) perror(“fork”), exit(-1); if (child != 0) { printf ("I'm the parent %d, my child is %d\n", getpid(), child); wait(NULL); /* wait for child (“join”) */ } else { printf ("I'm the child %d, my parent is %d\n", getpid(), getppid()); execl("/bin/echo", "echo", "Hello, World", NULL); } } CS 3214 Spring 2018

  20. fork/exec/exit/wait parent wait() fork() child exit() exec() CS 3214 Spring 2018

  21. fork() vs. exec() • fork(): • Clone most state of parent, including memory • Inherit some state, e.g. file descriptors • Keeps program, changes process • Called once, returns twice • exec(): • Overlays current process with new executable • Keeps process, changes program • Called once, does not return (if successful) CS 3214 Spring 2018

  22. exit(3) vs. _exit(2) • exit(3) destroys current processes • OS will free resources associated with it • E.g., closes file descriptors, etc. etc. • Can have atexit() handlers • _exit(2) skips them • Exit status is stored and can be retrieved by parent • Single integer • Convention: exit(EXIT_SUCCESS) signals successful execution, where EXIT_SUCCESS is 0 CS 3214 Spring 2018

  23. wait() vs waitpid() • int wait(int *status) • Blocks until any child exits • If status != NULL, will contain value child passed to exit() • Return value is the child pid • Can also tell if child was abnormally terminated • intwaitpid(pid_tpid, int *status, int options) • Can say which child to wait for CS 3214 Spring 2018

  24. Wait Example void fork10() { pid_t pid[N]; int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminate abnormally\n", wpid); } } If multiple children completed, will take in arbitrary order • Can use macros WIFEXITED and WEXITSTATUS to get information about exit status CS 3214 Spring 2018

  25. Observations on fork/exit/wait • Process can have many children at any point in time • Establishes a parent/child relationship • Resulting in a process tree • Zombies: processes that have exited, but their parent hasn’t waited for them • “Reaping a child process” – call wait() so that zombie’s resources can be destroyed • Orphans: processes that are still alive, but whose parent has already exited (without waiting for them) • Become the child of a dedicated process (“init”) who will reap them when they exit • “Run Away” processes: processes that (unintentionally) execute an infinite loop and thus don’t call exit() or wait() CS 3214 Spring 2018

  26. The fork()/join() paradigm • After fork(), parent & child execute in parallel • Unlike a fork in the road, here we take both roads • Used in many contexts • In Unix, ‘join()’ is called wait() • Purpose: • Launch activity that can be done in parallel & wait for its completion • Or simply: launch another program and wait for its completion (shell does that) Parent: fork() Parent process executes Child process executes Child process exits Parent:join() OS notifies CS 3214 Spring 2018

  27. What do these command lines do? • unix> one • unix> one first second third • unix> one & • unix> one < a • unix> one > b • unix> one | two • unix> one < a | two > b • unix> one | two | three | four & • unix> one & two & three CS 3214 Spring 2018

  28. File DESCRIPTORS CS 3214 Spring 2018

  29. Unix File Descriptors • Unix provides a file descriptor abstraction • File descriptors are • Small integers that have a local meaning within one process • Can be obtained from kernel • Several functions create them, e.g. open() • Can refer to various kernel objects (not just files) • Can be passed to a standard set of functions: • read, write, close, lseek, (and more) • Can be inherited when a process forks a child CS 3214 Spring 2018

  30. Examples • 0-2 are initially assigned • 0 – stdin • 1 – stdout • 2 – stderr • But this assignment is not fixed – process can change it via syscalls • int fd = open(“file”, O_RDONLY); • int fd = creat(“file”, 0600); CS 3214 Spring 2018

  31. Implementing I/O Redirection • dup and dup2() system call • pipes: pipe(2) CS 3214 Spring 2018

  32. dup2 #include <stdio.h> #include <stdlib.h> // redirect stdout to a file int main(int ac, char *av[]) { int c; intfd = creat(av[1], 0600); if (fd == -1) perror("creat"), exit(-1); if (dup2(fd, 1) == -1) perror("dup2"), exit(-1); while ((c = fgetc(stdin)) != EOF) fputc(c, stdout); } CS 3214 Spring 2018

  33. user view kernel view The Big Picture Process 1 0 1 2 Terminal Device 3 open(“x”) File Descriptor Open File x Process 2 0 1 Final steps (not included inanimation): Parent doesclose(3), leaves the child’sstdin to be the only referenceto the file descriptor.Once the child is done and closes its stdin (or exits!)OS closes file and removesentry from Open File table. 2 3 dup2(3,0) close (3) CS 3214 Spring 2018

  34. user view kernel view The Big Picture Process 1 0 1 2 Terminal Device 3 open(“x”) 4 File Descriptor Open File x open(“x”) close(4) File Descriptor • Opening the same file within one process yields • separate read/write offsets for each descriptor • Compare to dup/dup2/fork which create asecond reference to the same file descriptorwith a shared offset/file pointer. CS 3214 Spring 2018

  35. Reference Counting • Multiple file descriptors may refer to same open file • Within the same process: • fd = open(“file”); fd2 = dup(fd); • Across related processes: • fd = open(“file”); fork(); • But one process can also open a file multiple times: • fd = open(“file”); fd2 = open(“file”); • In this case, fd and fd2 have different read/write offsets • In both cases, closing fd does not affect fd2 • Reference Counting at 2 Levels: • Kernel keeps track of how many processes refer to a file descriptor – fork() and dup()/dup2() may add refs • And keeps track of how many file descriptors refer to an open file across all processes. Ditto for other kernel objects such as pipes. • close(fd) removes reference in current process only! CS 3214 Spring 2018

  36. Practical Implications • Number of simultaneously open file descriptors per process is limited • Soft limit of 1024 on current Linux, for instance; can be increased to up to 64K (hard limit) on rlogin machines • Must make sure fd’s are closed when done • Else ‘open()’ may fail • Number space is reused • “double-close” error may inadvertently close a new file descriptor assigned the same number CS 3214 Spring 2018

  37. IPC via “pipes” • A bounded buffer providing a stream of bytes flowing through • Properties • Writer() can put data in pipe as long as there is space • If pipe() is full, writer blocks until reader reads() • Reader() drains pipe() • If pipe() is empty, readers blocks until writer writes • Classic abstraction • Decouples reader & writer • Safe – no race conditions • Automatically controls relative progress – if writer produces data faster than reader can read it, it blocks – and OS will likely make CPU time available to reader() to catch up. And vice versa. write() read() Fixed Capacity Buffer CS 3214 Spring 2018

  38. int main() { intpipe_ends[2]; if (pipe(pipe_ends) == -1) perror("pipe"), exit(-1); int child = fork(); if (child == -1) perror("fork"), exit(-1); if (child == 0) { char msg[] = { "Hi" }; close(pipe_ends[0]); write(pipe_ends[1], msg, sizeofmsg); } else { char bread, pipe_buf[128]; close(pipe_ends[1]); printf("Child said "); fflush(stdout); while ((bread = read(pipe_ends[0], pipe_buf, sizeofpipe_buf)) > 0) write(1, pipe_buf, bread); } } pipe Note: there is no race condition in this code. No matter what the scheduling order is, the message sent by the child will reach the parent. CS 3214 Spring 2018

  39. esh – extensible shell • Open-ended assignment • Encourage collaborative learning • Run each other’s plug-ins • Does not mean collaboration on your implementation • Secondary goals: • Exposure to yacc/lex and exposure to OO-style programming in C CS 3214 Spring 2018

  40. Big Picture Issues • State maintenance • How to maintain an accurate depiction of the state of external entities subject to change outside of the program’s control, when… • external entities can change state asynchronously, and … • tools for monitoring state changes (e.g., signals) are imperfect • Concurrency control • How to ensure the correctness of data when … • the control flow is subject to asynchronous interruption (e.g., by signal handling), and… • there are complex control flows in shell CS 3214 Spring 2018

  41. Big Questions … and what you need to know • In what state is every child process of the shell? How can the shell affect the state of its child processes? • How to create processes executing specified code • Signals and their effects on the receiver • How to send/receive signals • Terminal control and I/O • What functions in the shell code must not be interrupted by asynchronous events? • System calls for receiver to manage incoming signals • How to handle incoming signals CS 3214 Spring 2018

  42. Using the list implementation list_entry(e, structesh_command, elem) structesh_pipeline: …. struct list commands structesh_command: …. structlist_elemelem; …. • Key features: “list cell” – here call ‘list_elem’ is embedded in each object being kept in list • Means you need 1 list_elem per list you want to keep an object in structesh_command: …. structlist_elemelem; …. structlist_elem *next; structlist_elem *prev; structlist_elem head structlist_elem tail structlist_elem *next; structlist_elem *prev; structlist_elem *next; structlist_elem *prev; structlist_elem *next; structlist_elem *prev; CS 3214 Spring 2018

  43. Unix Startup: Step 1 1. Pushing reset button loads the PC with the address of a small bootstrap program. 2. Bootstrap program loads the boot block (disk block 0). 3. Boot block program loads kernel binary (e.g., /boot/vmlinux) 4. Boot block program passes control to kernel. 5. Kernel handcrafts the data structures for process 0. [0] Process 0: handcrafted kernel process Process 0 forks child process 1 init [1] Child process 1 execs /sbin/init CS 3214 Spring 2018

  44. Unix Startup: Step 2 [0] init [1] /etc/inittab init forks and execs daemons per /etc/inittab, and forks and execs a getty program for the console Daemons e.g. sshd, httpd getty CS 3214 Spring 2018

  45. Unix Startup: Step 3 [0] init [1] The getty process execs a login program login CS 3214 Spring 2018

  46. Unix Startup: Step 4 [0] init [1] login reads login and passwd. if OK, it execs a shell. if not OK, it execs another getty tcsh CS 3214 Spring 2018

  47. Shell Programs • A shell is an application program that runs programs on behalf of the user. • sh – Original Unix Bourne Shell • csh – BSD Unix C Shell, tcsh– Enhanced C Shell • bash –Bourne-Again Shell int main() { char cmdline[MAXLINE]; while (1) { /* read */ printf("> "); fgets(cmdline, MAXLINE, stdin); if (feof(stdin)) exit(0); /* evaluate */ eval(cmdline); } } Execution is a sequence of read/evaluate steps CS 3214 Spring 2018

  48. void eval(char *cmdline) { char *argv[MAXARGS]; /* argv for execve() */ intbg; /* should the job run in bg or fg? */ pid_tpid; /* process id */ bg = parseline(cmdline, argv); if (!builtin_command(argv)) { if ((pid = fork()) == 0) { /* child runs user job */ if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found.\n", argv[0]); exit(0); } } if (!bg) { /* parent waits for fg job to terminate */ int status; if (waitpid(pid, &status, 0) < 0) unix_error("waitfg: waitpid error"); } else /* otherwise, don’t wait for bg job */ printf("%d %s", pid, cmdline); } } Simple Shell eval Function CS 3214 Spring 2018

  49. Problem with Simple Shell Example • Shell correctly waits for and reaps foreground jobs. • But what about background jobs? • Will become zombies when they terminate. • Will never be reaped because shell (typically) will not terminate. • Creates a memory leak and prevent pids from being reused • Solution: Reaping background jobs requires a mechanism called a signal. • Asynchronous – can arrive at any time. OS will interrupt process as soon as it does CS 3214 Spring 2018

  50. CS 3214 Spring 2018

More Related