II. Systems Programming using C (Process Control Subsystem)



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

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

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

ELEC 377. Operating Systems. Week 1 Class 3

Libmonitor: A Tool for First-Party Monitoring

Chapter 6, The Operating System Machine Level

1 Posix API vs Windows API

System Calls and Standard I/O

Operating Systems. 05. Threads. Paul Krzyzanowski. Rutgers University. Spring 2015

CS420: Operating Systems OS Services & System Calls

CSC 2405: Computer Systems II

Lecture 25 Systems Programming Process Control

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

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

Process definition Concurrency Process status Process attributes PROCESES 1.3

5. Processes and Memory Management

Operating Systems 4 th Class

CS3600 SYSTEMS AND NETWORKS

Operating Systems and Networks

Linux/UNIX System Programming. POSIX Shared Memory. Michael Kerrisk, man7.org c February 2015

Leak Check Version 2.1 for Linux TM

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

CSC230 Getting Starting in C. Tyler Bletsch

Operating System Overview. Otto J. Anshus

Debugging with TotalView

Operating System Structure

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

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

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

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

7.2 Examining Processes on the Command Line

SQLITE C/C++ TUTORIAL

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

umps software development

Linux Kernel Architecture

Embedded Software development Process and Tools: Lesson-4 Linking and Locating Software

Operating System Structures

Shared Address Space Computing: Programming

Informatica e Sistemi in Tempo Reale

Implementing Rexx Handlers in NetRexx/Java/Rexx

Introduction. What is an Operating System?

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

Example of Standard API

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

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

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

Simple C Programs. Goals for this Lecture. Help you learn about:

CHAPTER 15: Operating Systems: An Overview

C++ Programming Language

W4118 Operating Systems. Junfeng Yang

Operating Systems. Privileged Instructions

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

CS5460: Operating Systems

CIS 551 / TCOM 401 Computer and Network Security. Spring 2005 Lecture 4

Object Oriented Software Design II

Multi-core Programming System Overview

Intel P6 Systemprogrammering 2007 Föreläsning 5 P6/Linux Memory System

Chapter 3 Operating-System Structures

Writing a C-based Client/Server

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Parallelization: Binary Tree Traversal

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

An Implementation Of Multiprocessor Linux

Lecture 25 Symbian OS

OPERATING SYSTEM SERVICES

Grundlagen der Betriebssystemprogrammierung

/* File: blkcopy.c. size_t n

Chapter 10 Case Study 1: LINUX

Operating System Components

Chapter 2 System Structures

Chapter 3: Operating-System Structures. Common System Components

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

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

Audit Trail Administration

Carnegie Mellon Virtual Memory of a Linux Process Process-specific data structs (ptables, Different for

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

How To Run A Test File Extension On A Rexx (Unix) On A Microsoft Linux (Amd64) (Orchestra) (For Windows) (

CSE331: Introduction to Networks and Security. Lecture 34 Fall 2006

Chapter 12 File Management

Migration of Process Credentials

Chapter 12 File Management. Roadmap

Module 20: The Linux System

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

Real Time Programming: Concepts

Q N X S O F T W A R E D E V E L O P M E N T P L A T F O R M v Steps to Developing a QNX Program Quickstart Guide

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

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

How to Add a New System Call for Minix 3

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Process Description and Control william stallings, maurizio pizzonia - sistemi operativi

CS 103 Lab Linux and Virtual Machines

Get the Better of Memory Leaks with Valgrind Whitepaper

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

EECS 354 Network Security. Introduction

POSIX. RTOSes Part I. POSIX Versions. POSIX Versions (2)

Transcription:

II. Systems Programming using C (Process Control Subsystem) 129

Intended Schedule Date Lecture Hand out Submission 0 20.04. Introduction to Operating Systems Course registration 1 27.04. Systems Programming using C (File Subsystem) 1. Assignment 2 04.05. Systems Programming using C (Process Control) 2. Assignment 1. Assignment 3 11.05. Processes Scheduling 3. Assignment 2. Assignment 4 18.05. Process Synchronization 4. Assignment 3. Assignment 5 25.05. Inter Process Communication 5. Assignment 4. Assignment 6 01.06. Pfingstmontag 6. Assignment 5. Assignment 7 08.06. Input / Output 7. Assignment 6. Assignment 8 15.06. Memory Management 8. Assignment 7. Assignment 9 22.06. 9. Assignment 8. Assignment Filesystems 10 29.06. 10. Assignment 9. Assignment 11 06.07. Special subject: Transactional Memory 10. Assignment 12 13.07. Special subject: XQuery your Filesystem 13 20.07. Wrap up session 27.07. First examination date Second examination date 130

Process Control Subsystem 131

!"#$%"& *+%,-.('#")-/ UNIX system architecture (System V)!"+5#<"$=$;6$1))" -."#&'"("%!"#$"%&'"("% :$;6$1))&%&A%;#?"D.('#")15E$5E4.>?+%##'#"AA"-8.3.4"5&8*%%&0$4"#/*8"9 71#"%4.5&'('#")-8/0%"&.12.3.4"59 B+#"$4:$;<"''4 C;))5+%D1#%;+ 8;<=9 Files :5EE"$5+6 821//"#&8*89"9 :$;<"'''#"5"$5+6'4.5&'('#")-86#78".. 87$4#7%&.12.3.4"59 :$;<"'';$4 <5#"%A5+6 8.89"+1%0$:9 Processes F"%>?"+-889*#*84"#9!A;>D-82%78>9 G"$H#"#$"%&"$-82"@%>"-2$%@"$9.="%>?"$4 @"$31A#5+6 85"57#3 5*$*:"5"$49 01$231$"4.#"5"$5+6!"#$"%&'"("% )*#+,*#"&'"("% 01$231$"!"#$%&'()*&+,*(-.)/ 132

Program and Processes A program is an executable file residing on disk in a directory read into memory executed by the kernel as a result of one of the six exec(3) functions. An executing instance of a program is called a process. Every process has a unique numeric identifier called process ID. The process ID is always a non-negative integer. Although unique, process ID are reused (kernel delays re-usage). 133

Running process printing pid 134

Status information of processes man 1 ps describes the user command program /bin/ps 135

Obtaining process state with ps(1) 136

Getting process identifiers 137

Virtual Memory Each process has its own address space in virtual memory. Processes: do not know about each other (from their perspective) run alone on the machine their address spaces are independent from each other For inter-process communication special kernel mechanisms have to be invoked ( lecture 5) 138

Virtual Memory on Linux Memory locations are addressed through pointers. As such the natural word size of a processor sets an upper bound for the largest address to reference. On 32-bit systems: 2 32 Bytes = 4294967296 Bytes = 4 GiB Often this exceeds the physical memory available and gives name to the term virtual memory. Linux divides virtual memory in kernel and user space. Each user process has its own address space from 0 to TASK_SIZE. TASK_SIZE depends on architecture. kernel space user space 2 32 / 2 64 TASK_SIZE 0 139

Building programs 140

From source to executable code From Source Code To Executable Code Translation Steps (multi-phase compilation) Compilation HLL source code to assembler source code Assembly Assembler source code to object code Linking Object code to executable code Compilers and assemblers create object files containing the generated binary code and data for a source file. Linkers combine multiple object files into one, loaders take object files and load them into memory. Goal: An executable binary file (a.out) From high-level language (HLL) source code to executable code, i.e., concrete processor instructions in combination with data. 122 141

Translation step using gcc(1) Translation Steps Using gcc(1) Quellcode C/C++ Assembler-Quellcode Objektdatei, Bibliotheksdatei Eingabedateien *.c/*.cc/*.cpp *.s *.o/*.a Präprozessor Compiler Assembler Binder Ausgabedateien *.i/*.ii *.s *.o a.out Vorverarbeiteter C/C++-Quellcode Assembler-Quellcode Objektdatei Ausführbare Datei (ungebunden) (= Objektdatei, ladbar) 123 142

Creation of an executable file Creation Of An Executable File (Filename).c Kompilieren gcc (Filename).s = Operation = Kommando = Eingang oder Ausgang Assemblieren gas Object/Library Files (Filename).o ld Binden a.out 125 143

Linking an executable binary Linking An Executable Binary OBJ1 OBJ2 OBJ3.text1.data1.bss1.text2.bss2.text3.data3.bss3 Eingabedaten: ungebundene Objektdateien Bindung (linking).text: Code.data: initialisierte Variablen.bss: nicht initialisierte Variablen OBJtotal.text1.text2.text3.data1.data3.bss1.bss2.bss3 Verarbeitungsresultat: ausführbare Datei (gebunden, reloziert) Each object code (compiled seperately) starts at address 0 Linking them together involves centralization of sections relocation of adresses 138 144

Program file organization Typical Program Organisation A typical program divides naturally in sections Code machine instructions, should be unmodifiable, size is known after compilation, does not change (.text) Data static data initialized (.data) /uninitialized (.bss) constant address in memory permanent life time dynamic data stack or heap storage space not known volatile life time 110 145

Memory Layout 146

(Virtual) Memory Layout Virtual Memory And Segments Virtual Memory Whenever a process is created, the kernel provides a chunk of physical memory which can be located anywhere Through the magic of virtual memory (VM), the process believes it has all the memory on the computer ( lecture 8) Typically the VM space is laid out in a similar manner: Text Segment (.text) Initialized Data Segment (.data) Uninitialized Data Segment (.bss) The Stack The Heap 112 147

{ Memory Segments (TASK_SIZE) 148

Memory segments Memory Segments Text Segment. The text segment contains the actual code (including constants) to be executed. It s usually sharable, so multiple instances of a program can share the text segment to lower memory requirements. This segment is usually marked read-only so a program can t modify its own instructions. Initialized Data Segment. This segment contains global variables which are initialized by the programmer. Uninitialized Data Segment. Also named.bss (block started by symbol) which was an operator used by an old assembler. This segment contains uninitialized global variables. All variables in this segment are initialized to 0 or NULL pointers before the program begins to execute. 115 149

Memory segments Memory Segments (cont.) The Stack The stack is a collection of stack frames which we will discuss later. When a new frame needs to be added (as a result of a newly called function), the stack grows downward. The Heap Dynamic memory, where storage can be (de-)allocated via C s free(3)/malloc(3). The C library also gets dynamic memory for its own personal workspace from the heap as well. As more memory is requested on the fly, the heap grows upward. 116 150

Loading into memory segments Variable Placement Variables (outside a function) Globally declared variables go to the Uninitialized Data Segment if they are not initialized, to Initialized Data Segment otherwise. Necessary for the OS to decide if storage has to be loaded with initialization data from the executable binary. Variables (inside a function) Implicit assumption of auto, go to The Stack. Declared as static, see above. Constants (const) Text Segment Function Parameters Are pushed on The Stack or stored in registers. If pointers are passed, data is elsewhere. 120 151

Variable placement, life time, visibility Variable Placement And Life Time (Code) int a; /* Permanent life time */ static int b; /* dito, but reduced scope */ void func ( void) { char c; /* only for the life time of func() */ /* but 2x; visible only in func() */ static int d; /* i m unique, exist once at a stable */ /* address, visible only in func() */ } int main ( void) { int e; /* life time of main() */ } int *pi = ( int*) malloc ( sizeof( int )); /* newborn */ func (); func (); free (pi ); /* RIP, pi points to an invalid address */ return (0); 118 152

Variable placement in address space Variable Placement And Life Time (Diagram) Adresse 0 PC(t=0) PC(t=x) pi 1. Instruktion 2. Instruktion 3. Instruktion 4. Instruktion... a b d int Code Daten Halde (Heap) SP(t=x) SP(t=0) max. c pi e Stapel (Stack) t=0: Programmausführung wird gestartet, d.h., Ausführungsumgebung ist bereits initialisiert t=x: beliebiger Zeitpunkt während der Programmausführung 119 153

Multitasking 154

Multitasking Systems In terms of hardware parallelism as much processes may be executed concurrently as processors are available. Multitasking systems allow for (apparently) simultaneous execution of multiple processes on a single processor. The kernel switches between processes in short intervals to simulate the parallel execution (task switch). The kernel decides in what way the computing time is assigned to different processes (scheduling lecture 3). 155

*+$%+,#",-."$-/01#2+$"34+$+55"5%#6#!"#$%&&%3562002738232192723:;0"<-<.92;<79273-=)3()*9)"..23>")??@3A2>253()*9)"..362106351BC3:>)255)"<.37<)3.163D26)12;55E562.??@3()*9)"..23517>3F*7217"7>2)315*012)63G52C273217"7>2)371BC6H Processes and Threads '("%)*&3562002732192723:;0"<-56)I792317392.2175".2)3:;0"<-<.92;<793>")J??@3K<39021BC2.3()*+255392CL)1923MC)2">53621027351BC3>273:>)255)"<.3<762)217"7>2) 33333<7>3.163D26)12;55E562.??@3N21723O5*0"61*7 ()*+25523<7>3MC)2">53!L77273<762)5BC12>01BC3!*.;1712)63P2)>27??@3A237"BC3D26)12;55E562.32172Q3.2C)2)23*>2)3"0023N*.;17"61*7273<762)56=6+6 Threads: Parallel running activities inside a process. Each thread belongs to dedicated process. 789 7!9 7:9 Single Process 7<)32173()*+255 7<)32173()*+255.2C)2)23()*+2552 Multiple Processes 7;9.2C)2)23()*+2552 Single Control Flow 7<)3%3MC)2">.2C)2)23MC)2">5 Three Control Flows (Threads) Single Control Flow 8237<)32173MC)2"> Multiple Control Flows (Threads) 823.2C)2)23MC)2">5!"#$%$&'(")()*+,-. /*01234

Pseudo parallel execution!"#$%"&''('#")"!"%'*%"+,-.$,/"0$*$12$3))&"#$%"& Conceptually: Each process has its own machine -."+),//'012'.*'1234 45,4$16"'',7,84579 45,4$16"'',:,845:9 45,4$16"'',;,845;9 45,4$16"'',<,845<9 4$16"''?)> task 'C03+#?@2"@ switch 45D,4$12$3))> 6E0+"$,8!"#$"%&'(#)*+,"9 45!D,4$1C"'',51@#$1+,!+1CF 8GH"$A3+#?@2'I3#"@,I"',4$16"''"'9 PC: program counter PCB: Process Control Block ",%/'012'.*'2154 45,4$16"'',7 45,4$16"'',: 45,4$16"'',; 45,4$16"'',< 45,4$16"'',7 45,4$16"'',:?'AB 45>!"+"2?@2 5*6+2#71*620083900:;1*63;2#")"72)3<2=>62)3#)*3()*+2;;?3@,>3;2#")"723()*A)"..+B>02) CCD3E2F:267120023()*+2;;23G!"#$"%&'()*+,-."!!"!H I:3J2@2.3I217#:6!736:)32162;3@2)3K12)3()*A)"..23".30":-263G1.3L16#)*+2;;*);M;72.H CCD3<2"02)3(N3O1)@3"PO2=>;206@3K*63@263K12)3()*+2;;263P26:7+7 E=>0:;;-*0A2):6A26Q 137 157!"#$%$&'(")()*+,-. /*01234 ="%# time time ="%# 4$16"'',7 4$16"'',: 4$16"'',; 4$16"'',< Time-Multiplexing

!"#$%"&''('#")" Task Switch and PCB *$+,"''-)'./01#-23"24-254*6! *$+,"''47 *$+,"''48 *64%24:*67;4'%./"$24 :*68;4%24*64<+=%"$"24 *$+,"''41>-?#40-?46*@ *$+,"''41>-?#42%./# :*67;4%24*64<+=%"$"24 *64%24:*68;4'%./"$24 *6A4$"01"$4*645"$46*@ :*67;B4:*68;A4C%$#-"11"4*6 5"$4*$+,"''"474D48 9"%# *674D4*684'%254%24*6!74D *6!84"2#/01#"2 *6!A4*$+."''46+2#$+14!1+.< :*$+,"''C"$E01#-23'50#"2;!"#$%$&'(")()*+,-. Glatz 2005 /*01234 158

Process Life Cycle!"#$%"&''('#")" *&+,-./0,'"12"%1"'23$45"''"' 56"713892:2;7#<"72;=321;273()*+27727 >;?2)7@<21A:")23(<"72;B 9"A2;3A273()*C)"..73DE39"A2#<"72F G?")?2;3A273()*C)"..73DE3G?")?#<"72F ()*C)"..":0"6-3DE3H2?)12:7#<"72F ()*C)"..:22;A1C6;C3DE3I2).1;12)6;C7#<"72F 6&7"8#9,#"%.4$),# 9"A:")23J"?212;3.K772;321;2.3L*)C2C2:2;2;3/*)."?32;?7#)2@<2; MMN3O2)21;:")6;C3+P17@<2;3H2?)12:77Q7?2.36;A3R##01!"?1*;S3P20@<23T;<"0?23P*31;3A2) 33333"67-K<):")2;3J"?213":C202C?371;A MMN3U1;3:27?1..?273H2?)12:77Q7?2.3!21;321;3*A2)3.2<)2)23/*)."?236;?2)7?K?+2; kann!"#$%$&'(")()*+,-. /*0123%4 159

Process Termination 160

Process Termination!"#$%"&''('#")" *$+,"''#"$)%-%"$.-/ /*).25362)3()*+2778225619:59; <%=3>*)."023?225619:593<-)21@10019= <&=3A*)+21B1923?225619:59382136:)CD3()*+277372087B32)!"55B2.3/2D02)3<-)21@10019= <$=3A*)+21B1923?225619:5938213!"B"7B)*#D"02.3/2D02)32)!"55B36:)CD3 333333EF7B2.3<:5-)21@10019= <G=3H2).1512):5936:)CD3"562)253()*+2773<:5-)21@10019= >?;3IJ:-197B2)3/"00;3<%=3>*)."023?225619:59 K156#)*+277230":-253@21B2)L3@2553M0B2)5#)*+2773B2).1512)B <910B3-N)3O51P3:563Q156*@7L35:)3826159B3-N)3"562)23?2B)12877F7B2.2= 161

Eight way for a process to terminate Normal termination Returning from main Calling exit(3) Calling _exit(2) (POSIX.1) or _Exit(3) (ISO C) Return of the last thread from its start routine Calling pthread_exit(3) from the last thread Abnormal termination Calling abort(3) Receipt of a signal ( lecture 5) Response of the last thread to a cancellation request 162

Normal termination details Normal termination Returning from main Equivalent to calling exit(3) Calling exit(3) closes/flushes all standard I/O streams calls all exit handlers registered with atexit(3) ISO C does not deal with file descriptors, multiple processes (parents and children), and job control, as such its definition is incomplete for UNIX systems. 163

Normal termination details Calling _exit(2) (POSIX.1) or _Exit(3) (ISO C) ISO C defines _Exit(3) to provide a way for a process to terminate without exit or signal handlers. Whether standard I/O streams are flushed is system dependent. On UNIX systems _exit(2) and _Exit(3) are synonymous and do not flush standard I/O streams. The _exit(2) function is called by exit(2) and handles the UNIX system-specific details Return of the last thread from its start routine The return value of the thread is not used as the return value of the process. When the last thread returns, the process exits with 0. Calling pthread_exit(3) from the last thread Same as above 164

Starting and terminating a C program 165

Process Creation 166

Process Creation *+,+,-.$/0"''"$0"12132-134-5"$)%3%"$132!"#$%"&''('#")" 425)3216-"7523489:2.23;<10:3-=)3>12023!"#!$$!$%&'&(!"&?@ A0023()*+299232B19:12)263>*63A6-"6<3"63C6D30"C-2632E1<F3D,5,39*0"6<23489:2.30GC-: A00<2.2162)23489:2.23;)!*!+,-./0+/1&!%&'&(!"&?@ ()*+29923E2)D263H213I2D")-32)+2C<:3C6D3<202<26:01753E12D2)3:2).1612): AC90J926D23K)21<619923-=)3()*+2992)+2C<C6<@ ;%?3489:2.9:"):3;L61:1"01912)C6<? ;&?3489:2."C-)C-3+C)3()*+2992)+2C<C6<3DC)7531)<26D2162630"C-26D263()*+299 ;$?3I26C:+2)"6-*)D2)C6<3+C.34:"):26321629362C263()*+299293;A##01!":1*699:"):? ;M?3AC90J9C6<32162934:"#20"C-:)"<93;#,(23%41#?!"#$%$&'(")()*+,-. 167 /*0123%&

What is a process?!"#$%"&''('#")" *+',%'#,"%-,.$/0"''1 4156738293()*:)"..2;7<15!02)93=1;3>?@ >A()*:)"..B38,6,3main()A/C;!71*; 4156738293D27)12E99F972.93=G8)299)"C.E202:C;:?@ H"72;E2)21563=1;!0,347"5!3C;83I2"#? ()*:)"..5*82E2)2156 H"72;97)C!7C)3+C)3()*+2992J2)<"07C;:38C)5638"93 D27)12E99F972.3=!"#$%!&'()**%"'+,&'-%#-'(./$%01#1 2%!34%5!&'0)**.)++6+7/ 2%8.,6)--)&%!&'0)**06*,9+: 2%!&'0)**.'+,);, kernel space 232 / 2 64 PCB user space argv, env ()*+299A 5*82 ()*+299A 8"72; (>D G8)299)"C. TASK_SIZE linux/sched.h: struct task_struct 0 168

Process Hierarchy 169

!"#$%"&''('#")" *+,+-./$01"''2%"$3$42%".5!"%'6%"78.9:%;< UNIX process hierarchy /=>?A %:%# /=>?@ B"##( B"##( B"##( '2 B"##( C%:D '2!"#$%$&'(")()*+,-. /*0123$$ 170

UNIX system start (simplified) Bootstrap loads system code to memory. Initialization of internal kernel data structures. Mount root filesystem Environment for the first process is created. System code becomes process with PID=0 Process 0 forks and creates process 1 (kernel space) Process 1 creates environment in user space and turns over PID=0 (kernel space), PID=1 (user space) 171

UNIX system start (simplified) Process 1 uses exec on, e.g., /sbin/init & becomes init init is responsible for bringing up a UNIX system after the kernel has been bootstrapped. It usually reads system-dependent initialization files (such as /etc/rc* or /etc/inittab, /etc/init.d) and brings the system to a certain state, such as multiuser. For instance, the terminals for users to login (getty) are started. It is a normal user process running with superuser privileges. init never dies. Meanwhile, process 0 starts kernel services in kernel space and finally becomes the swapper, i.e., the scheduler process. 172

Process Hierarchy on Linux Linux has a hierarchical process schema, i.e., each process is connected to its parent process. The kernel starts init as first process. The process hierarchy is a result of the process creation used on UNIX systems: fork(2) exec(3) family 173

Process creation and joining *$+,"'''#-$#.+$)"/ 3456*$+,"''0"$7"##2/1 3!56*$+,"''0"$1-&"82/1 3956*$+,"''"$,"212/1!"#$%"&''('#")" 45"16 -*)! 4)2"72 *$+,"''0"$"%/%12/1'.+$)"/ 3:56*$+,"''0"$"%/%12/1 3;56*$+,"''#$".."/ 8*16 2917 :"17 2917 Operating!"#$%$&'(")()*+,-. Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term 2009 /*0123%$ 174

Related system calls!"#$%"&''('#")" *('#")+,-$,-".-/$.0$12"''3456$"+73*#+$# ;9%<30$12"''" 0=*>?356$"+7' @%971A'30$12"''" @%971A'356$"+7' B6+%9 exec() 5 5 5-1$C fork() 5 5 5 B$"+#" 5 pthread_create() CreateProcess() CreateThread() *('#")+,-$,-".-/$.0$12"''3456$"+738"$"%9%:,9: ;9%<30$12"''" 0=*>?356$"+7' @%971A'30$12"''" @%971A'356$"+7' D1%9 wait()63waitpid() pthread_join() 5 5 A+%# 5 5 WaitForSingleObject() WaitForSingleObject()!"#$%$&'(")()*+,-. /*0123%4 175

Initialization of processes/threads!"#$%"&''('#")" *"%#"$+,&"-./0-1,#"0-,0-0"2"-3$/4"''"567$",8' 5670189!21:2;< =2)2)>?;7 @21:2)7">23A>2)3B":2;#?--2) C;1:1"0#")".2:2)3-A)3;2?2;3()*+2DD @21:2)7">23A>2)3:2.#*)E)23B":21F2;G H21D#12023!*;!)2:2)3C.#02.2;:12)?;72;31;3H2:)12>DDID:2.2;< 90%:;3$/4"''" 3<=>?;67$",8' *%08/@'; 3$/4"''" A"$"$&20+ fork() J CreateProcess() J >0#"$B$/4C/)D popen() J J J *%08/@'; 67$",8' >0%#%,EB,$,)D J pthread_create() J CreateThread() 176

Inheritance with fork / exec *+,+-./"$"$&012.01#"$.3$45"''"1!"#$%"&''('#")" /"$"$&012.&"%.61%783$45"''"1 421.3/*)!1567 8*923:593;*#123"002)3<"=253>?3!"#$%"&%#'()*+%!"()%,-'.)@ <"=2192A!)1#=*)25B39123C*53D0=2)5#)*+2AA3E2+*625 F21=2)23D1625AGH"-=25B3+,4,3:A2)I6)*:#319B3J.62E:56AC")1"E025B3K)E21=AC2)+21GH51AB ;*5=)*00=2).15"03:AL, 421.38H"151567 (M<3:593((M< <"=2192A!)1#=*)25B39123!/"0)1"*1)2)!351GH=362A2=+=3H"E25 >N473!/"0)1"*1)2)!3L1)93A="59")9.OAA163E21.3<"=21P--525351GH=362A2=+=@3 F21=2)23D1625AGH"-=25B3+,4,3:A2)I6)*:#319B3J.62E:56AC")1"E025B3K)E21=AC2)+21GH51AB ;*5=)*00=2).15"03:AL, Operating!"#$%$&'(")()*+,-. Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term 2009 /*0123$% 177

Forking processes 178

*+,+*-.$/0"''"-12#"$-32%45-.$/0"''6"$78&"9127-:;/$<%27=!"#$%"&''('#")" ()*+25562)7"8209:73;3()*+25537"820<351=>3"9-31:3+?213@*:<)*00-0A5523.1<37021=>2.3B*C2 ()1:+1#8215#120D int main() { int k, status; pid_t pid; k=fork(); if (k == 0) {... // Anweisungen, die nur Kindprozess } Forking processes on UNIX... // ausführen soll exit(0); // Kindprozess terminiert } else {... // Anweisungen, die nur Elternprozess... // ausführen soll pid = wait(&status); // Auf Ende des Kindprozesses warten exit(0); }!"#$%$&'(")()*+,-. /*0123%4 179

Forking processes on UNIX!"#$%"&''('#")" *$+,"''-"$./&"012.343*$+,"''-"$5+66"012.37/&38+$9:;18$18< =0#"$26$+,"'' >%256$+,"'' *$%2,%6/&0/18 main() { int k, status; pid_t pid; main() { int k, status; pid_t pid; k=fork(); if (k == 0) {...... exit(0); } else {...... pid = wait(&status); } } k=fork(); if (k == 0) {...... exit(0); } else {...... pid = wait(&status); } } -*)! #")286 9:10; 5"16 2716!"#$%$&'(")()*+,-. /*0123%4 180

Process system calls in a nutshell *+%,-.('#")/01$01"213$24$56"''"$6"070+720+829"$)%+%"$0+7!"#$%"&''('#")" system call pid = fork () pid = wait (&statloc) pid = waitpid (pid, &statloc, options) s = execve (name, argv, environp) exit (status) 42567)2189:;3<!"#$%&'()*+,$+)&-&$./)0$1(/2(*$3(4&+= >)+29;?321:2:3@1:A#)*+255B3A2)31A2:?15673.1?3A2.3>0?2):#)*+255315?3<;18?3 C31.3@1:A#)*+25539:A335%6$7#89&++$):&/')!)9(')8/31.3>0?2):#)*+2553 +9)D6!E3-"0053/2702)B3A"::3FD6!;"82G2)?3H%= I")?2?3"9-3A123J2).1:12)9:;31);2:A21:253@1:A#)*+25525 <;18?3(KL3A253@1:A#)*+2552538+G,3C31.3/2702)-"003+9)D6!E3 +95M?+01673FD6!;"823A253422:A1;9:;55?"?9539:A3FD6!;"82G2)?53A253 @1:A#)*+255253.1??2053statloc= I")?2?3"9-3A123J2).1:12)9:;321:253825?1..?2:3@1:A#)*+25525,3N#?1*:53 51:A3+,4,3!21:3I")?2:B3-"0053@1:#)*+2553:*673:167?3822:A2?, <;18?3(KL3A253@1:A#)*+2552538+G,3C31.3/2702)-"003+9)D6!E3 +95M?+01673FD6!;"823A253422:A1;9:;55?"?9539:A3FD6!;"82G2)?53A253 @1:A#)*+255253.1??2053statloc= >)52?+?30"9-2:A2:3()*+2553A9)67321:2:3:292:3()*+2553<:292)3()*+2553 2)8?3O*:3"0?2.3()*+255=,3<P18?3H%38213/2702)3+9)D6!B35*:5?3!21:23FD6!H ;"82= Q67?9:;R3253;18?3S302167?3O2)56712A2:23T")1":?2:3A125253UV5?2."9-)9-5W J2).1:12)?3"9-)9-2:A2:3()*+25539:A35?200?3FD6!;"82G2)?3-D)342?)12855VH 5?2.382)21?3<G1)A3D82)3wait() /3waitpid()3A2.3>0?2):#)*+2553 +9;M:;01673;2."67?=!"#$%$&'(")()*+,-. /*0123&& 181

The fork(2) system call PID is returned to parent, because a process can have more than one child, and there is no function that allows a process to obtain the PID of its children. Returning 0 to the child is apt, since PID=0 is reserved by the kernel, so it s not possible for 0 to be the PID of a child. The child can always obtain the PID of its parent, calling getppid(2). 182

!""*+%,-*,'./-*+/0-'123$-*,''#4#-'!"#$%"&''('#")" 41)352-1612)263512723829)1--23:123-*09;<!""#$%&'#&(()*)'(<3=*)."03;2).1612);3>+:,3-2?02)?"-;3@+,8,3A*)+21;192)3()*+277">>)BC?D +,-.&*/"0"1)<3EB-)B-#")".2;2)352)3exit()F/B6!;1*63@5"73:"73G165#)*+2773+B)HC!F 92>263:100D 41232)?"0;263:1)3512723I6-*).";1*6263A*.3JK7;2.3+B927;200;L FFM382152342);23!*.>1612);3A1"3N7;";0*COF(")".2;2)35273wait()Pwaitpid()FEB-)B-7 FFM3N7;";0*CO3+219;3"B-321623720>7;352!0")12);23Q")1">023N7;";B7O3@E5)277)2-2)26+D FFM3N7;";B7O3!"663.1;;2073A*)52-1612);2)3R"!)*73"B792:2);2;3:2)526< 33333WIFEXITED(status)<3STUV3-H)36*)."0W3/EXJV3-H)3A*)+21;193">92>)*C?26 33333WEXITSTATUS(status)<3X12-2);35263THC!9">2:2);3@-"00736*)."023S2).1612)B69D 8217#120< Obtaining termination status pid_t pid; int status; pid = wait(&status); if (WIFEXITED(status)) printf( Rückgabewert ist %d\n, WEXITSTATUS(status);!"#$%$&'(")()*+,-. /*0123&$ 183

Zombie Processes!"#$%"&''('#")" *$+,"'''#-$#.)%#./"$,0"%1"2.34+)&%"5*$+&6")7 =216 8-66.397:35062)7#)*+28839")6263 8-66.3!7:3:17;#)*+288362).1712)6<3 "E-357;23;283:17;#)*+28828 >2F*)35062)7#)*+28839")626 ( C ( C ( C ( C fork() ( C ( D ( C fork() ( ( C ( D D ( C ( D wait() @*;2A exit() exit() B"627A wait() =*.>12? #)*+288 184!"#$%$&'(")()*+,-. /*0123%4

Zombie prevention *+)&%","$-%./"$0.12)%##"3'24.5"36$+7"'' 526)1--27!"#$%&38391:;#)*+2<<=3;2)31.3>?<@2.3A21@2)02B@=3*BA*C032)3@2).1:12)@3C"@ '(%)&*+",-.(/038391:;#)*+2<<=3;2<<2:3D0@2):#)*+2<<3:1EC@3.2C)32F1<@12)@!"#$%"&''('#")" G%H G&H G$H GIH GKH D0@2) D0@2) D0@2) D0@2) D0@2) fork() 91:;% 91:;% 91:;% wait()j exit() L"EC3>EC)1@@3G$H7 91:;&31<@391:;3M*:391:;% 91:;%31<@391:;3M*:3D0@2) 91:;&31<@3D:!203M*:3D0@2) fork() 91:;& 91:;& 91:;& L"EC3>EC)1@@3GIH7 D0@2)3C"@3!21:2391:;2)3.2C) 91:;&31<@391:;3M*:31:1@N ()*+2<<3G";*#@12)@23O"1<2H!"#$%$&'(")()*+,-. /*0123&4 185

Process chaining 186

Process chaining!"#$%"&''('#")" *$+,"''#-$#.)%#./"$0"##123.456-%2%237 8+9": <"%# /"$0"##123 * = * = ;-#"2: execve() *! *! 45365173.1839253:3272;<=>?@-)@-253)2"01A12)8 6582)A;B1292392)3:3272;<=>C")1"5825D3?)83@593?5+"B0392)3?@-)@-#")".282) <15392)3/@5!81*53A1593"0023E021;BF39,B,3()*+2AAG2)!288@5E=!"#$%$&'(")()*+,-. 187 /*0123&%

exec(3) family ok = execl(pathname, arg0, arg1,..., NULL); Zweck: Startet ausführbare Datei als neuen Prozess, ersetzt (terminiert) damit laufenden Prozess Parameter: const char * pathname: Pfadname der Datei const char * arg0, * arg1,..: 1./2./.. Kommandozeilenargumente (Liste mit NULL abschließen) Rückgabewert int: keine Rückkehr falls okay; im Fehlerfall Rückgabe von -1 ok = execle(pathname, arg0, arg1,..., NULL, envp[]); Zweck: Startet ausführbare Datei als neuen Prozess, ersetzt (terminiert) damit laufenden Prozess Parameter: const char * pathname: Pfadname der Datei const char * arg0, * arg1,..: 1./2./.. Kommandozeilenargumente (Liste mit NULL abschließen) const char * envp[]: Adresse einer neuen Umgebungsvariablenliste Rückgabewert int: keine Rückkehr falls okay; im Fehlerfall Rückgabe von -1 ok = execlp(filename, arg0, arg1,..., NULL) Zweck: Startet ausführbare Datei als neuen Prozess, ersetzt (terminiert) damit laufenden Prozess Parameter: const char * filename: Dateiname (Suchpfad entsprechend Umgebungsvariable PATH) const char * arg0, * arg1,..: 1./2./.. Kommandozeilenargumente (Liste mit NULL abschließen) Rückgabewert int: keine Rückkehr falls okay; im Fehlerfall Rückgabe von -1 ok = execv(pathname, argv[]); Zweck: Startet ausführbare Datei als neuen Prozess, ersetzt (terminiert) damit laufenden Prozess 188

ok = execlp(filename, arg0, arg1,..., NULL) Zweck: Startet ausführbare Datei als neuen Prozess, ersetzt (terminiert) damit laufenden Prozess Parameter: const char * filename: Dateiname (Suchpfad entsprechend Umgebungsvariable PATH) const char * arg0, * arg1,..: 1./2./.. Kommandozeilenargumente (Liste mit NULL abschließen) exec(3) family Rückgabewert int: keine Rückkehr falls okay; im Fehlerfall Rückgabe von -1 ok = execv(pathname, argv[]); Zweck: Startet ausführbare Datei als neuen Prozess, ersetzt (terminiert) damit laufenden Prozess Parameter: const char * pathname: Pfadname der Datei const char * argv[]: Adresse einer Liste mit allen Kommandozeilenargumenten Rückgabewert int: keine Rückkehr falls okay; im Fehlerfall Rückgabe von -1 ok = execve(pathname, argv[], envp[]); Tab. 3 5 Unix-Systemaufrufe execl(), execle(), execlp() und execv() Zweck: Startet ausführbare Datei als neuen Prozess, ersetzt (terminiert) damit laufenden Prozess Parameter: const char * pathname: Pfadname der Datei const char * argv[]: Adresse einer Liste mit allen Kommandozeilenargumenten const char * envp[]: Adresse einer neuen Umgebungsvariablenliste Rückgabewert int: keine Rückkehr falls okay; im Fehlerfall Rückgabe von -1 ok = execvp(filename, argv[]); Zweck: Startet ausführbare Datei als neuen Prozess, ersetzt (terminiert) damit laufenden Prozess Parameter: const char * filename: Dateiname (Suchpfad entsprechend Umgebungsvariable PATH) const char * argv[]: Adresse einer Liste mit allen Kommandozeilenargumenten Rückgabewert int: keine Rückkehr falls okay; im Fehlerfall Rückgabe von -1 Tab. 3 6 Unix-Systemaufrufe execve() und execvp() 189

Differences among the exec functions 190

main function 191

Program Startup The main function Prototype for the main function The function called at program startup is named main It shall be defined with a return type of int And either with no parameters: int main ( void); Or with two parameters: int main ( int argc, char * argv []); 192

Constraints For main Function Constraints of main function Terminology (though any names may be used) argc stands for argument count argv stands for argument vector If argc and argv are declared The value of argc shall be nonnegative argv[0] represents the program name or argv[0][0] shall be the null character if the program name is not available argv[1] to argv[argc-1] represent program parameters argv[argc] shall be a NULL pointer 193

Command-line Arguments main: Argument vector When a program is executed, the process that does the exec can pass command-line arguments to the new program Normal operation for UNIX system shells argv: NULL echo\0 hello,\0 world\0 194

Environment Environment Each program is also passed an environment list Like the argument list, it is an array of character pointers Each pointing to a null-terminated C string The address of the array is contained in a global variable: extern char ** environ ; environ: NULL HOME=/home/holu\0 SHELL=/bin/ksh\0 PS1=\w \$\0 195 287

Environment (cont.) Environment (cont.) Terminology environ is called environment pointer The array of pointers is the environment list The strings they point to are the envoronment strings By convention, name=value string are used Historical third argument to main (not ISO C) int main ( int argc, char * argv [], char * envp ); Most UNIX systems have provided a third argument to main ISO C specifies main with two arguments Posix.1 specifies environ to be used instead of 3rd arg 288 196

Echo Command-line Arguments Echo command-line arguments # include <stdio.h> int main ( int argc, char * argv []) { int i; /* for (i = 0; argv[i]!= NULL; i ++) */ for ( i = 0; i < argc ; i ++) printf (" argv [%d]: %s\n", i, argv [i ]); } return (0); $./a. out -a 1 -arg2 -- arg3 argv [0]:./a. out argv [1]: -a argv [2]: 1 argv [3]: -arg2 argv [4]: -- arg3 289197

Building a shell 198

(Very) basic sketch of a shell *+,+-./012#%31'4"%'".5"$.61%789:";; int main () { char kdo[100]; pid_t pid; int status;!"#$%"&''('#")" } while(1) { printf("$>"); gets(kdo); pid = fork(); if (pid==0) execl(kdo,null); else wait(&status); } 56738"93:*;2-)"<.2=>3+21<>3=?)3;12362-2@091=>2)#)2>">1*=3A;,@,3!21=23B2)")C21>?=< 333333321=<2C"?>2)3D@200E62-2@023?=;3!21=23D!)1#>1=>2)#)2>">1*=F!"#$%$&'(")()*+,-. /*0123&4 199

How a shell (basically) works... *+,-#%.,'/"%'"01"$023"44 562003738*.."9:*19;2)#)2;2)373()*+2<<31.3=29>;+2).*:><!"#$%"&''('#" 8*.."9:*?2)")@21;>9A3BC9:0*<<D6021-2EF B%E3C1902<2938*.."9:*+21023"@35;"9:"):219A"@23BC19A"@232:1;12)@")E B&E38*.."9:*19;2)#)2;";1*93B9"D63G21029"@<D60><<3.1;3H)2;>)9IE B$E3C19A2@">;2)3=2-2603B!"#$%&#'()*++,'-EJ 33333KKI3L"F3M><-N6)>9A3:>)D63562003<20@<; BOE3/"00<391D6;3B$EF35!)1#;:";21J 33333KKI3L"F3M@")@21;293:2)35!)1#;@2-2602 B4E3/"00<391D6;3BOEF35;");293"0<32P;2)92<38*.."9:*3B"><-N6)@")23Q";21E 200

*+",,-./0'12+$&3$".43#"%.'#3$#"5 56")637*)82)9):;8#)*+2<<= Start up of an executable!"#$%"&''('#")" 8:9 8=9 8?9 '+!"#$%& '+!"#$%& '+!"#$%& 16$789 '+ '()*+ '()*+ ;)< ">";.;)< 56")63>1;62)9):;8#)*+2<<=??@35A2003B")6263;1CA63":-3D;82382<3E1;8#)*+2<<2<3F-GA)63-*)631;3E*..";8H2)")I216:;9J!"#$%$&'(")()*+,-. /*0123&4 201

Next lecture: Next lecture/tutorial slots Process Scheduling Mo, 04.05., 12:00-14:00, C 252 (new room!) Next tutorial: Introduction to the C Programming Language Part II Tomorrow, Tue, 28.04., 16:00-18:00, C 252 Slides: http://www.informatik.uni-konstanz.de/ arbeitsgruppen/dbis/lehre/operating-systems/ Have fun and see you next week! 202