cs 251 fall 2019 cs 240 spring 2020 principles of
play

CS 251 Fall 2019 CS 240 Spring 2020 Principles of Programming - PowerPoint PPT Presentation

CS 251 Fall 2019 CS 240 Spring 2020 Principles of Programming Languages Foundations of Computer Systems Ben Wood Ben Wood Operating Systems, Process Model Process model Process management (Unix/Linux/macOS)


  1. λ CS 251 Fall 2019 CS 240 Spring 2020 Principles of Programming Languages Foundations of Computer Systems Ben Wood Ben Wood Operating Systems, Process Model Process model Process management (Unix/Linux/macOS) https://cs.wellesley.edu/~cs240/s20/ OS Process Model 1

  2. Program, Application Software Programming Language Compiler/Interpreter Operating System Instruction Set Architecture Microarchitecture Hardware Digital Logic Devices (transistors, etc.) Solid-State Physics OS Process Model 2

  3. Operating Systems Problem: unwieldy hardware resources complex and varied limited Solution: operating system Manage, abstract, and virtualize hardware resources Simpler, common interface to varied hardware Share limited resources among Protect OS Process Model 3

  4. Operating Systems , a 240 view barely scraping the surface Key abstractions provided by kernel process virtual memory Virtualization mechanisms and hardware support: context-switching exceptional control flow address translation, paging, TLBs OS Process Model 4

  5. Processes Program = code (static) Process = a running program instance (dynamic) code + state (contents of registers, memory, other resources) Key illusions: y a d o T Logical control flow s k e Each process seems to have exclusive use of the CPU e W t x e N Private address space Each process seems to have exclusive use of full memory Why? How? OS Process Model 5

  6. Implementing logical control flow Abstraction: every process has full control over the CPU Process A Process B Process C time Implementation: time-sharing Process A Process B Process C time OS Process Model 6

  7. Context Switching Kernel (shared OS code) switches between processes Control flow passes between processes via context switch. Context = Process A Process B user code context switch kernel code time user code kernel code context switch user code OS Process Model 7

  8. fork pid_t fork() 1. Clone current parent process to create identical* child process, including all state (memory, registers, program counter, …). 2. Continue executing both copies with one difference: returns 0 to the child process • returns child’s process ID ( pid ) to the parent process • pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } fork is unique: called in one process, returns in two processes! (once in parent, once in child) *almost. See man 3 fork for exceptions. OS Process Model 8

  9. Creating a new process with fork Process n pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); 1 } else { printf("hello from parent\n"); Child Process m } à m à 0 pid_t pid = fork(); pid_t pid = fork(); if (pid == 0) { if (pid == 0) { printf("hello from child\n"); printf("hello from child\n"); 2 } else { } else { printf("hello from parent\n"); printf("hello from parent\n"); } } pid_t pid = fork(); pid_t pid = fork(); 3 if (pid == 0) { if (pid == 0) { printf("hello from child\n"); printf("hello from child\n"); } else { } else { printf("hello from parent\n"); printf("hello from parent\n"); } } hello from parent Which prints first? hello from child OS Process Model 9

  10. fork and private copes Parent and child continue from private copies of same state. Memory contents ( code , globals, heap , stack , etc.), Register contents, program counter , file descriptors… Only difference: return value from fork() Relative execution order of parent/child after fork() undefined void fork1() { int x = 1; pid_t pid = fork(); if (pid == 0) { printf("Child has x = %d\n", ++x); } else { printf("Parent has x = %d\n", --x); } printf("Bye from process %d with x = %d\n", getpid(), x); } OS Process Model 10

  11. fork-exec clone current process fork() execv() replace process code and context (registers, memory) with a fresh program. See man 3 execv , man 2 execve // Example arguments: path="/usr/bin/ls”, // argv[0]="/usr/bin/ls”, argv[1]="-ahl", argv[2]=NULL void fork_exec(char* path, char* argv[]) { pid_t pid = fork(); if (pid != 0) { printf("Parent: created a child %d\n”, pid); } else { printf("Child: exec-ing new program now\n"); execv(path, argv); } printf("This line printed by parent only!\n"); } OS Process Model 11

  12. Running the command ls in a shell: Executing a new program Stack 1 Code/state of shell process. Heap Replaced by code/state of ls. Copy of code/state Data of shell process. Code: /usr/bin/bash fork() : child parent child Stack Stack Stack 2 2 3 exec() : Heap Heap Data Data Data Code: /usr/bin/bash Code: /usr/bin/bash Code: /usr/bin/ls Code/state of shell process. OS Process Model 12

  13. execv : load/start a program Stack bottom Null-terminated int execv(char* filename, env var strings char* argv[]) Null-terminated argument strings loads/starts program in current process: unused Executable filename envp[n] == NULL With argument list argv envp[n-1] … overwrites code, data, and stack envp[0] Keeps pid, open files, a few other items argv[argc] == NULL does not return argv[argc-1] … unless error argv[0] Linker vars envp argv Also sets up environment . See also: execve. argc Stack frame for main Stack top OS Process Model 13

  14. exit : end a process void exit(int status) End process with status: 0 = normal, nonzero = error. atexit() registers functions to be executed upon exit OS Process Model 14

  15. wait for child processes to terminate pid_t waitpid(pid_t pid, int* stat, int ops) Suspend current process (i.e. parent) until child with pid ends. On success: Return pid when child terminates. Reap child. If stat != NULL , waitpid saves termination reason where it points. See also: man 3 waitpid OS Process Model 15

  16. HCBye waitpid example CTBye void fork_wait() { int child_status; pid_t child_pid = fork(); if (child_pid == 0) { printf("HC: hello from child\n"); } else { if (-1 == waitpid(child_pid, &child_status, 0) { perror("waitpid"); exit(1); } printf("CT: child %d has terminated\n”, child_pid); } printf("Bye\n"); exit(0); } OS Process Model 16

  17. Zombies! Terminated process still consumes system resources Reaping with wait/waitpid What if parent doesn’t reap? If any parent terminates without reaping a child, then child will be reaped by init process (pid == 1) What if parent runs a long time? e.g. , shells and servers OS Process Model 17

  18. Error-checking Check return results of system calls for errors! (No exceptions.) Read documentation for return values. Use perror to report error, then exit. void perror(char* message) Print " <message> : <reason that last system call failed.> " OS Process Model 18

  19. Examining processes on Linux (demo) ps pstree top /proc OS Process Model 19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend