Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes



Similar documents
CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

Lecture 25 Systems Programming Process Control

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

Process definition Concurrency Process status Process attributes PROCESES 1.3

ARUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS. If you have two

Outline. Review. Inter process communication Signals Fork Pipes FIFO. Spotlights

Libmonitor: A Tool for First-Party Monitoring

Interlude: Process API

5. Processes and Memory Management

Networks. Inter-process Communication. Pipes. Inter-process Communication

7.2 Examining Processes on the Command Line

Laboratory Report. An Appendix to SELinux & grsecurity: A Side-by-Side Comparison of Mandatory Access Control & Access Control List Implementations

Linux Kernel Architecture

Operating System Structure

ELEC 377. Operating Systems. Week 1 Class 3

Grundlagen der Betriebssystemprogrammierung

Jorix kernel: real-time scheduling

System Calls and Standard I/O

Program 5 - Processes and Signals (100 points)

Virtuozzo Virtualization SDK

File System Overview and Interview Questions About Unix

SQLITE C/C++ TUTORIAL

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

REAL TIME OPERATING SYSTEM PROGRAMMING-II: II: Windows CE, OSEK and Real time Linux. Lesson-12: Real Time Linux

Intro to GPU computing. Spring 2015 Mark Silberstein, , Technion 1

Programmation Systèmes Cours 4 Runtime user management

Hands-On UNIX Exercise:

Manage cloud infrastructures using Zend Framework

Shared Memory Segments and POSIX Semaphores 1

Debugging with TotalView

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Minix Mini Unix (Minix) basically, a UNIX - compatible operating system. Minix is small in size, with microkernel-based design. Minix has been kept

Parallelization: Binary Tree Traversal

An Implementation Of Multiprocessor Linux

LSN 10 Linux Overview

Set-UID Privileged Programs

The Linux Kernel: Process Management. CS591 (Spring 2001)

CSC230 Getting Starting in C. Tyler Bletsch

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Introduction. What is an Operating System?

1 Posix API vs Windows API

Linux Syslog Messages in IBM Director

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Informatica e Sistemi in Tempo Reale

Assignment 5: Adding and testing a new system call to Linux kernel

Computer Systems and Networks. ECPE 170 Jeff Shafer University of the Pacific. Linux Basics

IPC. Semaphores were chosen for synchronisation (out of several options).

Components of a Computer System

How ToWrite a UNIX Daemon

II. Systems Programming using C (Process Control Subsystem)

Chapter 6, The Operating System Machine Level

Operating Systems. Design and Implementation. Andrew S. Tanenbaum Melanie Rieback Arno Bakker. Vrije Universiteit Amsterdam

Outline. Operating Systems Design and Implementation. Chap 1 - Overview. What is an OS? 28/10/2014. Introduction

CSE 120 Principles of Operating Systems. Modules, Interfaces, Structure

Shared Address Space Computing: Programming

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Secure Shell Demon setup under Windows XP / Windows Server 2003

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

Advanced Bash Scripting. Joshua Malone

Unix Scripts and Job Scheduling

Command Line - Part 1

Sources: On the Web: Slides will be available on:

Betriebssysteme KU Security

Operating Systems for Parallel Processing Assistent Lecturer Alecu Felician Economic Informatics Department Academy of Economic Studies Bucharest

PetaLinux SDK User Guide. Application Development Guide

CS420: Operating Systems OS Services & System Calls

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

CS161: Operating Systems

CS 241 Data Organization Coding Standards

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

1. Computer System Structure and Components

Example of Standard API

HP-UX Essentials and Shell Programming Course Summary

Systems Programming & Scripting

Linux command line. An introduction to the Linux command line for genomics. Susan Fairley

CIS 551 / TCOM 401 Computer and Network Security

Kernel comparison of OpenSolaris, Windows Vista and. Linux 2.6

The C Programming Language course syllabus associate level

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

Using SNMP with Content Gateway (not V-Series)

OS: IPC I. Cooperating Processes. CIT 595 Spring Message Passing vs. Shared Memory. Message Passing: Unix Pipes

An Incomplete C++ Primer. University of Wyoming MA 5310

Network Programming with Sockets. Process Management in UNIX

System Calls Related to File Manipulation

CSC 2405: Computer Systems II

An Introduction to the Linux Command Shell For Beginners

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

CS10110 Introduction to personal computer equipment

/* File: blkcopy.c. size_t n

Have both hardware and software. Want to hide the details from the programmer (user).

Operating Systems 4 th Class

SLURM Resources isolation through cgroups. Yiannis Georgiou Matthieu Hautreux

A Crash Course on UNIX

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

System Administration

Introduction. dnotify

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Keil C51 Cross Compiler

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

This presentation explains how to monitor memory consumption of DataStage processes during run time.

Transcription:

Computer Systems II Creating and Executing Processes 1 Unix system calls fork( ) wait( ) exit( ) 2 How To Create New Processes? Underlying mechanism - A process runs fork to create a child process - Parent and children execute concurrently - Child process is a duplicate of the parent process parent fork() child 3 1

Process Creation After a fork, both parent and child keep running, and each can fork off other processes. A process tree results. The root of the tree is a special process created by the OS during startup. A process can choose to wait itfor children to terminate. For example, if C issued a wait() system call, it would block until G finished. 4 Bootstrapping When a computer is switched on or reset, there must be an initial program that gets the system running This is the bootstrap program - Initialize CPU registers, device controllers, memory - Load the OS into memory - Start the OS running OS starts the first process (such as init ) OS waits for some event to occur - Hardware interrupts or software interrupts (traps) 5 Fork System Call Current process split into 2 processes: parent, child Returns -1 if unsuccessful fork() Returns 0 in the child Returns the child s identifier in the parent Sac ret = 0 Sac ret = xxx 6 2

Fork System Call The child process inherits from parent - identical copy of memory - CPU registers - all files that have been opened by the parent Execution proceeds concurrently with the instruction ti following the fork system call The execution context () for the child process is a copy of the parent s context at the time of the call 7 How fork Works (1) UNIX 8 How fork Works (2) pid = 26 ret = 26 UNIX ret = 0 9 3

How fork Works (3) pid = 26 ret = 26 UNIX ret = 0 10 How fork Works (4) pid = 26 ret = 26 UNIX ret = 0 11 How fork Works (5) pid = 26 ret = 26 UNIX ret = 0 12 4

How fork Works (6) Process Status ret = 26 < > UNIX 13 Orderly Termination: exit() To finish execution, a child may call exit(number) This system call: - Saves result = argument of exit - Closes all open files, connections - Deallocates memory - Checks if parent is alive - If parent is alive, holds the result value until the parent requests it (with wait); in this case, the child process does not really die, but it enters a zombie/defunct state - If parent is not alive, the child terminates (dies) 14 Waiting for the Child to Finish Parent may want to wait for children to finish - Example: a shell waiting for operations to complete Waiting for any some child to terminate: wait() - Blocks until some child terminates - Returns the process ID of the child process - Or returns -1 if no children exist (i.e., already exited) Waiting for a specific child to terminate: waitpid() - Blocks till a child with particular process ID terminates #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); 15 5

State Transition on wait and exit Calls 16 Other useful system calls: getpid, getppid getpid returns the identifier of the calling process. Example call (pid is an integer): pid = getpid(); getppid returns the identifier of the parent. Note: - Zombies can be noticed by running the 'ps' command (shows the process list); you will see the string "<defunct>" as their command name: ps -ef ps ef grep mdamian 17 Fork Example 1: What Output? int main() pid_t pid; int x = 1; pid = if (pid!= 0) printf( parent: x = %d\n, --x); else printf( child: x = %d\n, ++x); 18 6

Fork Example 2 Key Points - Both parent and child can continue forking void fork2() printf("l0\n"); printf("l1\n"); printf("\n"); L0 L1 L1 19 Fork Example 3 void fork3() L2 printf("l0\n"); printf("l1\n"); printf("l2\n"); L2 printf("\n"); L0 L1 L2 L1 L2 20 Fork Example 4 void fork4() printf("l0\n"); if (fork()!= 0) printf("l1\n"); if (fork()!= 0) printf("l2\n"); printf("\n"); 21 7

Fork Example 5 void fork5() printf("l0\n"); if (fork() == 0) printf("l1\n"); if (fork() == 0) printf("l2\n"); printf("\n"); 22 Summary Fork - Creates a duplicate of the calling process - The result is two processes: parent and child - Both continue executing from the same point on Exit - Orderly program termination - Unblocks waiting parent Wait - Used by parent - Waits for child to finish execution 23 Unix system calls execv execl 24 8

Unix s execv The system call execv executes a file, transforming the calling process into a new process. After a successful execv, there is no return to the calling process. execv(const char * path, char * const argv[]) path is the full path for the file to be executed argv is the array of arguments for the program to execute each argument is a null-terminated string the first argument is the name of the program the last entry in argv is NULL 25 How execv Works (1) pid = 26 char * argv[ ] = /bin/ls, 0; int cpid = fork( ); if (cpid = = 0) <parent code> wait(&cpid); cpid = 26 cpid = 0 char * argv[ ] = /bin/ls, 0; int cpid = fork( ); if (cpid = = 0) <parent code> wait(&cpid); /bin/ls UNIX kernel 26 How execv Works (2) pid = 26 char * argv[ ] = /bin/ls, 0; int cpid = fork( ); if (cpid = = 0) <parent code> wait(&cpid); cpid = 26 Exec destroys the process image of the calling process. A new process image is constructed from the executable file (ls). /bin/ls UNIX kernel 27 9

How execv Works (3) pid = 26 char * argv[ ] = /bin/ls, 0; cpid = 26 int cpid = fork( ); if (cpid = = 0) <parent code> wait(&cpid); <first line of ls> /bin/ls UNIX kernel 28 Example: A Simple Shell Shell is the parent process - E.g., bash Parses command line - E.g., ls l Invokes child process - Fork, execvp Waits for child - Wait execvp bash fork wait ls 29 execv Example #include <stdio.h> #include <unistd.h> char * argv[] = /bin/ls, -l, 0; int main() int pid, status; if ( (pid = fork() ) < 0 ) printf( Fork error \n ); if(pid == 0) /* Child executes here */ printf( Exec error \n ); else /* Parent executes here */ wait(&status); printf("hello there! \n ); return 0; Note the NULL string at the end 30 10

execv Example Sample Output Sample output: total 282 drwxr-xr-x 2 mdamian faculty 512 Jan 29 14:02 assignments -rw-r--r-- 1 mdamian faculty 3404 Jan 29 14:05 index.html drwxr-xr-x 2 mdamian faculty 512 Jan 28 15:02 notes Hello there! 31 execl Same as execv, but takes the arguments of the new program as a list, not a vector: Example: execl( /bin/ls, /bin/ls, -l, 0); Is equivalent to Note the NULL string at the end char * argv[] = /bin/ls, -l, 0; execl is mainly used when the number of arguments is known in advance 32 General purpose process creation In the parent process: int childpid; char * const argv[ ] = ; main childpid = if(childpid == 0) // I am child... // Do some cleaning, close files else // I am parent... <code for parent process> 33 11

Combined fork/exec/wait Common combination of operations - Fork to create a new child process - Exec to invoke new program in child process - Wait in the parent process for the child to complete Single call that combines all three - int system(const tchar *cmd); Example int main() system( echo Hello world ); 34 Properties of fork / exec sequence In 99% of the time, we call execv( ) after fork() - the memory copying during fork() is useless - the child process will likely close open files and connections - overhead is therefore high - might as well combine both in one call (OS/2) vfork() - a system call that creates a process without creating an identical memory image - sometimes called lightweight fork - child process is understood to call execv() almost immediately 35 Variations of execv execv - Program arguments passed as an array of strings execvp - Extension of execv - Searches for the program name in the PATH environment execl - Program arguments passed directly as a list execlp - Extension of execv - Searches for the program name in the PATH environment 36 12

Summary exec(v,vp,l,lp) - Does NOT create a new process - Loads a new program in the image of the calling process - The first argument is the program name (or full path) - Program arguments are passed as a vector (v,vp) or list (l,lp) - Commonly called by a forked child system: - combines fork, wait, and exec all in one 37 13