Section 8: Processes What is a process Creating processes Fork-Exec

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes /18-243: Introduction to Computer Systems 13 th Lecture, June 15, 2011 Instructors:
University of Washington Today Midterm grades posted  76. HW 3 due Lab 4 fun! 1.
University of Washington Today Finish up cache-aware programming example Processes  So we can move on to virtual memory 1.
University of Washington Today Move on to … 1. University of Washington Hmm… Lets look at this again Disk Main Memory L2 unified cache L1 I-cache L1 D-cache.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
Exceptional Control Flow Part I September 22, 2008 Topics Exceptions Process context switches Creating and destroying processes class11.ppt ,
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes class14.ppt CS 123.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP 229, 346, 444, 5201 Revision 1.3.
1 Program and OS interactions: Exceptions and Processes Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Carnegie Mellon Exceptions and Processes Slides adapted from: Gregory Kesden and Markus Püschel of Carnegie Mellon University.
Fabián E. Bustamante, Spring 2007 Exceptional Control Flow Part I Today Exceptions Process context switches Creating and destroying processes Next time.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Creating and Executing Processes
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes : Introduction to Computer Systems 13 th Lecture, Oct. 7, 2014 Instructors:
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
System calls for Process management
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes class16.ppt.
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Exceptional Control Flow I March 12, 2002
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
Carnegie Mellon Exceptions and Processes Slides adapted from: Gregory Kesden and Markus Püschel of Carnegie Mellon University.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Carnegie Mellon Exceptions and Processes Slides adapted from: Gregory Kesden and Markus Püschel of Carnegie Mellon University.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Overview of today’s lecture Re-view of the previous lecture Process management models and state machines (cont’d from the previous lecture) What is in.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Exceptional Control Flow & Processes
Unix Process Management
Processes & Virtual Memory I CSE 351 Winter 2017
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Exceptional Control Flow Part I
Instructors: Anthony Rowe, Seth Goldstein and Gregory Kesden
CS703 – Advanced Operating Systems
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Processes CSE 351 Autumn 2016 Instructor: Justin Hsia
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
CS 105 “Tour of the Black Holes of Computing!”
Exceptional Control Flow Part I Mar. 11, 2003
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Exceptional Control Flow Part I
Process Programming Interface
CS 105 “Tour of the Black Holes of Computing!”
Processes Prof. Ikjun Yeom TA – Mugyo
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Lecture 6: Multiprogramming and Context Switching
System Control Flow and Processes CSE 351 Winter 2018
CS 105 “Tour of the Black Holes of Computing!”
Processes CSE 351 Autumn 2018 Guest Instructor: Teaching Assistants:
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Processes & Virtual Memory I CSE 351 Winter 2019
Exceptional Control Flow & Processes October 2, 2008
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
Chapter 3 Process Description and Control
Presentation transcript:

Section 8: Processes What is a process Creating processes Fork-Exec

Fork-Exec fork-exec model: fork() creates a copy of the current process execve() replaces the current process’ code & address space with the code for a different program There is a whole family of exec calls – see exec(3) and execve(2) // 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"); man 3 exec man 2 execve - v is for “vector” (of arguments), e is for “environment” Note: the return values of fork() and execv() should be checked for errors! Fork-Exec

Exec-ing a new program Stack Code: /usr/bin/bash Data Heap Very high-level diagram of what happens when you run the command ”ls” in a Linux shell: fork(): parent child child Stack Code: /usr/bin/bash Data Heap Stack Code: /usr/bin/bash Data Heap Stack Code: /usr/bin/ls Data exec(): Fork-Exec

execve: Loading and Running Programs Null-terminated env var strings unused cmd line arg strings envp[n] == NULL envp[n-1] envp[0] … Linker vars argv[argc] == NULL argv[argc-1] argv[0] envp argc argv Stack bottom Stack frame for main Stack top int execve( char *filename, char *argv[], char *envp[] ) Loads and runs in current process: Executable filename With argument list argv And environment variable list envp Env. vars: “name=value” strings (e.g. “PWD=/homes/iws/pjh”) execve does not return (unless error) Overwrites code, data, and stack Keeps pid, open files, a few other items Run the printenv command in a linux shell to see your own environment variables. Fork-Exec

exit: Ending a process void exit(int status) Exits a process Status code: 0 is used for a normal exit, nonzero for abnormal exit atexit() registers functions to be executed upon exit void cleanup(void) { printf("cleaning up\n"); } void fork6() { atexit(cleanup); fork(); exit(0); exit(3) and atexit(3) have more details. Fork-Exec

Zombies Idea Reaping What if parent doesn’t reap? When process terminates, it still consumes system resources Various tables maintained by OS Called a “zombie” A living corpse, half alive and half dead Reaping Performed by parent on terminated child Parent is given exit status information Kernel discards process What if parent doesn’t reap? If any parent terminates without reaping a child, then child will be reaped by init process (pid == 1) But in long-running processes we need explicit reaping e.g., shells and servers On more recent Linux systems, init has been renamed as “systemd”. Fork-Exec

wait: Synchronizing with Children int wait(int *child_status) Suspends current process (i.e. the parent) until one of its children terminates Return value is the pid of the child process that terminated On successful return, the child process is reaped If child_status != NULL, then the int that it points to will be set to a status indicating why the child process terminated There are special macros for interpreting this status – see wait(2) If parent process has multiple children, wait() will return when any of the children terminates waitpid() can be used to wait on a specific child process child_status shouldn’t be examined directly – it should be examined using the macros WIFEXITED(), WEXITSTATUS(), etc. (see wait(2)). Fork-Exec

wait Example void fork_wait() { int child_status; pid_t child_pid; if (fork() == 0) { printf("HC: hello from child\n"); } else { child_pid = wait(&child_status); printf("CT: child %d has terminated\n”, child_pid); } printf("Bye\n"); exit(0); HC Bye CT Fork-Exec

Process management summary fork gets us two copies of the same process (but fork() returns different values to the two processes) execve has a new process substitute itself for the one that called it Two-process program: First fork() if (pid == 0) { //child code } else { //parent code } Two different programs: if (pid == 0) { execve() } else { //parent code } Now running two completely different programs wait / waitpid used to synchronize parent/child execution and to reap child process Fork-Exec

Summary Processes Process management At any given time, system has multiple active processes Only one can execute at a time, but each process appears to have total control of the processor OS periodically “context switches” between active processes Implemented using exceptional control flow Process management fork-exec model Give ps / pstree demo if you haven’t yet!! Fork-Exec