operating systems a 240 view processes
play

Operating Systems , a 240 view Processes barely scraping the - PowerPoint PPT Presentation

Operating Systems , a 240 view Processes barely scraping the surface Key abstractions provided by kernel Program = code (static) process Process = a running program instance (dynamic) virtual memory code + state (contents of registers, memory,


  1. Operating Systems , a 240 view Processes barely scraping the surface Key abstractions provided by kernel Program = code (static) process Process = a running program instance (dynamic) virtual memory code + state (contents of registers, memory, other resources) Virtualization mechanisms and hardware support: Key illusions: y a d o T context-switching Logical control flow s k exceptional control flow e Each process seems to have exclusive use of the CPU e W t x e N address translation, paging, TLBs Private address space Each process seems to have exclusive use of full memory Why? How? Context Switching Implementing logical control flow Kernel (shared OS code) switches between processes Abstraction: every process has full control over the CPU Process A Process B Process C Control flow passes between processes via context switch. time Context = Process A Process B Implementation: time-sharing user code Process A Process B Process C context switch kernel code time user code time kernel code context switch user code 7

  2. Creating a new process with fork fork pid_t fork() Process n 1. Clone current parent process to create identical* child process, pid_t pid = fork(); including all state (memory, registers, program counter, …). if (pid == 0) { printf("hello from child\n"); 1 2. Continue executing both copies with one difference: } else { printf("hello from parent\n"); returns 0 to the child process Child Process m • } returns child’s process ID ( pid ) to the parent process • à m à 0 pid_t pid = fork(); pid_t pid = fork(); if (pid == 0) { if (pid == 0) { pid_t pid = fork(); printf("hello from child\n"); printf("hello from child\n"); 2 if (pid == 0) { } else { } else { printf("hello from parent\n"); printf("hello from parent\n"); printf("hello from child\n"); } } } else { pid_t pid = fork(); pid_t pid = fork(); printf("hello from parent\n"); 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"); fork is unique: called in one process, returns in two processes! } } Which prints first? (once in parent, once in child) hello from parent hello from child *almost. See man 3 fork for exceptions. 9 10 fork again fork-exec Parent and child continue from private copies of same state. clone current process fork() replace process code and context (registers, memory) execv() Memory contents ( code , globals, heap , stack , etc.), with a fresh program. Register contents, program counter , file descriptors… See man 3 execv , man 2 execve Only difference: return value from fork() Relative execution order of parent/child after fork() undefined // Example arguments: path="/usr/bin/ls”, // argv[0]="/usr/bin/ls”, argv[1]="-ahl", argv[2]=NULL void fork1() { void fork_exec(char* path, char* argv[]) { int x = 1; pid_t pid = fork(); pid_t pid = fork(); if (pid != 0) { if (pid == 0) { printf("Parent: created a child %d\n”, pid); printf("Child has x = %d\n", ++x); } else { } else { printf("Child: exec-ing new program now\n"); printf("Parent has x = %d\n", --x); execv(path, argv); } } printf("Bye from process %d with x = %d\n", getpid(), x); printf("This line printed by parent only!\n"); } } 11 12

  3. When you run the command ls Exec-ing a new program execv : load/start program in a shell: Stack bottom Null-terminated int execv(char* filename, Stack env var strings 1 char* argv[]) Null-terminated Code/state of shell process. argument strings loads/starts program in current process: unused Heap Executable filename Replaced by code/state of ls. envp[n] == NULL Copy of code/state envp[n-1] With argument list argv Data of shell process. … Code: /usr/bin/bash overwrites code, data, and stack envp[0] fork() : Keeps pid, open files, a few other items argv[argc] == NULL child parent child does not return argv[argc-1] Stack … Stack Stack unless error 2 2 3 argv[0] exec() : Linker vars envp Heap Heap argv argc Data Data Also sets up environment . See also: execve. Data Stack frame for Code: /usr/bin/bash Code: /usr/bin/bash Code: /usr/bin/ls main Code/state of shell process. Stack top 13 14 HCBye waitpid example wait for child processes to terminate CTBye pid_t waitpid(pid_t pid, int* stat, int ops ) void fork_wait() { Suspend current process (i.e. parent) until child with pid ends. int child_status; On success: pid_t child_pid == fork(); Return pid when child terminates. if (child_pid == 0) { Reap child. printf("HC: hello from child\n"); } else { If stat != NULL , waitpid saves termination reason where it points. if (-1 == waitpid(child_pid, &child_status, 0) { See also: man 3 waitpid perror("waitpid"); exit(1); } printf("CT: child %d has terminated\n”, child_pid); } printf("Bye\n"); exit(0); } 16 18

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