CITS2002 Systems Programming. Creating a new process using fork() 1 next CITS2002 CITS2002 schedule

Similar documents
CSC 1600 Unix Processes. Goals of This Lecture

518 Lecture Notes Week 3

Operating systems and concurrency - B03

Fall 2015 COMP Operating Systems. Lab #3

Operating Systems 2230

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

CS240: Programming in C

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

System Programming. Process Control III

Process Management 1

System Programming. Process Control II

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

System Programming. Signals II

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

Computer Science & Engineering Department I. I. T. Kharagpur

Operating System Structure

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

Unix Processes 1 / 31

Lesson 2. process id = 1000 text data i = 5 pid = 1200

Operating Systems. Lecture 05

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

CS240: Programming in C

CSC209H Lecture 5. Dan Zingaro. February 4, 2015

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

fork System-Level Function

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

PROCESS PROGRAMMING INTERFACE

CS Operating Systems Lab 3: UNIX Processes

Introduction to Processes

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

Announcement (1) sys.skku.edu is now available

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

Process Management! Goals of this Lecture!

Computer Systems Assignment 2: Fork and Threads Package

Project 2: Shell with History1

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

Processes. CS3026 Operating Systems Lecture 05

Creating a Shell or Command Interperter Program CSCI411 Lab

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Programming Assignment #1: A Simple Shell

UNIX Processes. by Armin R. Mikler. 1: Introduction

API Interlude: Process Creation (DRAFT)

Processes: Introduction. CS 241 February 13, 2012

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

Operating Systemss and Multicore Programming (1DT089)

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Lecture 3 Process API in UNIX systems

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Process Management! Goals of this Lecture!

CS 550 Operating Systems Spring Process III

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Part II Processes and Threads Process Basics

University of Washington What is a process?

Process Management 1

TCSS 422: OPERATING SYSTEMS

Operating Systems, laboratory exercises. List 2.

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

Operating systems fundamentals - B06

CSC209 Fall Karen Reid 1

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX.

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Operating Systems Lab

coe628 Final Study Guide (2017)

4.8 Summary. Practice Exercises

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

CS 201. Processes. Gerson Robboy Portland State University

Recitation 8 Processes, Signals, Tshlab

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

INF1060: Introduction to Operating Systems and Data Communication. Pål Halvorsen. Wednesday, September 29, 2010

CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

CS 261 Fall Mike Lam, Professor. Processes

Exceptional Control Flow Part I

System Programming. Pipes I

Final Precept: Ish. Slides Originally Prepared by: Wonho Kim

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

Process management 1

Altering the Control Flow

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Exceptional Control Flow Part II Nov. 2, 2009"

This tutorial covers a foundational understanding of IPC. Each of the chapters contain related topics with simple and useful examples.

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Concurrency. Stefan D. Bruda. Winter 2018

Transcription:

1 next CITS2002 CITS2002 schedule Creating a new process using fork() fork() is very unusual because it returns different values in the (existing) parent process, and the (new) child process: the value returned by fork() in the parent process will be the process-indentifier, of process-id, of the child process; the value returned by fork() in the child process will be 0, indicating that it is the child, because 0 is not a valid process-id. Each successful invocation of fork() returns a new monotonically increasing process-id (the kernel 'wraps' the value back to the first unused positive value when it reaches 100,000). #include <stdio.h> #include <unistd.h> void function(void) { int pid; switch (pid = fork()) { case -1 : printf("fork() failed\n"); exit(exit_failure); // some systems define a pid_t // process creation failed case 0: // new child process printf("c: value of pid=%i\n", pid); printf("c: child's pid=%i\n", getpid()); printf("c: child's parent pid=%i\n", getppid()); default: // original parent process sleep(1); printf("p: value of pid=%i\n", pid); printf("p: parent's pid=%i\n", getpid()); printf("p: parent's parent pid=%i\n", getppid()); fflush(stdout); produces: c: child's value of pid=0 c: child's pid=5642 c: child's parent pid=5641 p: parent's value of pid=5642 p: parent's pid=5641 p: parent's parent pid=3244 Of note, calling sleep(1) may help to separate the outputs, and we fflush() in each process to force its output to appear. CITS2002 Systems Programming, Lecture 10, p1, 28th August 2018.

prev 2 next CITS2002 CITS2002 schedule Where does the first process come from? The last internal action of booting a Unix-based operating system results in the first single 'true' process, named init. init has the process-id of 1. It is the ancestor process of all subsequent processes. In addition, because the operating system strives to maintain a hierarchical relationship amongst all processes, a process whose parent terminates is 'adopted' by the init process. CITS2002 Systems Programming, Lecture 10, p2, 28th August 2018.

prev 3 next CITS2002 CITS2002 schedule The general calling sequence of system calls If a single program has two distinct execution paths/sequences, then the parent and child may run different parts of the same program. Typically the parent will want to know when the child terminates. The typical sequence of events is: the parent process fork()s a new child process. the parent waits for the child's termination, calling the blocking function wait( &status ). [optionally] the child process replaces details of its program (code) and data (variables) by calling the execve() function. the child calls exit(value), with an integer value to represent its success or failure. By convention, zero (= EXIT_SUCCESS) indicates successful execution, non-zero otherwise. the child's value given to exit() is written by the operating system to the parent's status. CITS2002 Systems Programming, Lecture 10, p3, 28th August 2018.

prev 4 next CITS2002 CITS2002 schedule Waiting for a Process to Terminate The parent process typically lets the child process execute, but wants to know when the child has terminated, and whether the child terminated successfully or otherwise. A parent process calls the wait() system call to suspend its own execution, and to wait for any of its child processes to terminate. The (new?) syntax &status permits the wait() system call (in the operating system kernel) to modify the calling function's variable. In this way, the parent process is able to receive information about how the child process terminated. #include <stdio.h> #include <unistd.h> #include <sys/wait.h> void function(void) { switch ( fork() ) { case -1 : printf("fork() failed\n"); // process creation failed exit(exit_failure); case 0: // new child process printf("child is pid=%i\n", getpid() ); for(int t=0 ; t<3 ; ++t) { printf(" tick\n"); sleep(1); exit(exit_success); default: { int child, status; printf("parent waiting\n"); child = wait( &status ); // original parent process printf("process pid=%i terminated with exit status=%i\n", child, WEXITSTATUS(status) ); exit(exit_success); CITS2002 Systems Programming, Lecture 10, p4, 28th August 2018.

prev 5 next CITS2002 CITS2002 schedule Memory in Parent and Child Processes The (existing) parent process and the (new) child process continue their own execution. Importantly, both the parent and child have their own copy of their program's memory (variables, stack, heap). The parent naturally uses the memory that it had before it called fork(); the child receives its own copy of the same memory. The copy is made at the time of the fork(). As execution proceeds, each process may update its own memory without affecting the other process. [ OK, I lied - on contemporary operating systems, the child process does not receive a full copy of its parent's memory at the time of the fork(): ] the child can share any read-only memory with its parent, as neither process can modify it. the child's memory is only copied from the parent's memory if either the parent modies its (original) copy, or if the child attempts to write to its copy (that it hasn't yet received!) this sequence is termed copy-on-write. CITS2002 Systems Programming, Lecture 10, p5, 28th August 2018.

prev 6 next CITS2002 CITS2002 schedule Running a New Program Of course, we do not expect a single program to meet all our computing requirements, or for both parent and child to conveniently execute different paths through the same code, and so we need the ability to commence the execution of new programs after a fork(). Under Unix/Linux, a new program may replace the currently running program. The new program runs as the same process (it has the same pid, confusing!), by overwriting the current process's memory (instructions and data) with the instructions and data of the new program. The single system call execv() requests the execution of a new program as the current process: #include <stdio.h> #include <stdlib.h> #include <unistd.h> char *program_arguments[] = { "ls", "-l", "-F", NULL ;... execv( "/bin/ls", program_arguments ); // A SUCCESSFUL CALL TO exec() DOES NOT RETURN exit(exit_failure); // IF WE GET HERE, THEN exec() HAS FAILED On success, execv() does not return (to where would it return?) On error, -1 is returned, and errno is set appropriately (EACCES, ENOENT, ENOEXEC, ENOMEM,...). The single system call is supported by a number of library functions (see man execl) which simplify the calling sequence. Typically, the call to execv() (via one of its library interfaces) will be made in a child process, while the parent process continues its execution, and eventually waits for the child to terminate. CITS2002 Systems Programming, Lecture 10, p6, 28th August 2018.

prev 7 next CITS2002 CITS2002 schedule Why the exit status of a program is important To date, we've always used exit(exit_failure) when a problem has been detected, or exit(exit_success) when all has gone well. Why? The operating system is able to use the exit status of a program to determine if it was successful. Consider the following program which exits with the integer status provided as a command-line argument: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int status = EXIT_SUCCESS; // DEFAULT STATUS IS SUCCESS (=0) if(argc > 1) { status = atoi(argv[1]); printf("exiting(%i)\n", status); exit(status); CITS2002 Systems Programming, Lecture 10, p7, 28th August 2018.

prev 8 CITS2002 CITS2002 schedule Why the exit status of a program is important, continued Most operating system shells are, themselves, programming languages, and they may use a program's exit status to direct control-flow within the shells - thus, the programming language that is the shell, is treating your programs as if they are external functions. Shells are typically programmed using files of commands named shellscripts or command files and these will often have conditional constructs, such as if and while, just like C. It's thus important for our programs to work with the shells that invoke them. We now compile our program, and invoke it with combinations of zero, and non-zero arguments: prompt> mycc -o status status.c prompt>./status 0 &&./status 1 exiting(0) exiting(1) prompt>./status 1 &&./status 0 exiting(1) prompt>./status 0./status 1 exiting(0) prompt>./status 1./status 0 exiting(1) exiting(0) Example1 - consider the sequence prompt> cd mydirectory && rm -f * Example2 - consider the actions in a Makefile (discussed in a later lecture). If a target has more than one action, then the make program executes each until one of them fails (or until all succeed). CITS2002 Systems Programming, Lecture 10, p8, 28th August 2018.