UNIX. Session 2. UNIX processes and forking fork system call exec system call death of process kill fork and I/O using it UNIX.

Similar documents
Operating System Structure

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

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

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

Process Management 1

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

Processes COMPSCI 386

SOFTWARE ARCHITECTURE 3. SHELL

Operating systems and concurrency - B03

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

Unix Processes. What is a Process?

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS]

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.

Process Management 1

fork System-Level Function

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

CS 550 Operating Systems Spring Process III

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Processes. Dr. Yingwu Zhu

CSC209 Fall Karen Reid 1

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

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

Process Management! Goals of this Lecture!

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

CSC 1600 Unix Processes. Goals of This Lecture

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

CS240: Programming in C

CSCI 4061: Making Processes

Processes. Processes (cont d)

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

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Parents and Children

System Programming. Process Control II

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Process Creation in UNIX

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

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

Assignment 1. Teaching Assistant: Michalis Pachilakis (

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Processes, Signals, I/O, Shell Lab : Introduc>on to Computer Systems Recita>on 9: Monday, Oct. 21, 2013 Marjorie Carlson Sec>on A

Process Management! Goals of this Lecture!

Chap 4, 5: Process. Dongkun Shin, SKKU

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

ESE 333 Real-Time Operating Systems 2 What is an operating system? Two views: 1. Top-down view: Extended machine ffl Covers the details of the hardwar

Lecture 5. Systems Programming: Unix Processes Creation: fork & exec Process Communication: Pipes

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

CSC209 Review. Yeah! We made it!

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CS Operating Systems Lab 3: UNIX Processes

Processes: Introduction. CS 241 February 13, 2012

Operating System Labs. Yuanbin Wu

Process Control. Philipp Koehn. 23 April 2018

Recitation 8: Tshlab + VM

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

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Exceptional Control Flow Part I September 22, 2008

Operating Systems. Lecture 05

CS 201. Processes. Gerson Robboy Portland State University

Killing Zombies, Working, Sleeping, and Spawning Children

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Discussion of Assignments 2. Line buffered vs. full buffered I/O. Some often encountered issues in the submissions.

Processes. CS3026 Operating Systems Lecture 05

PROCESS PROGRAMMING INTERFACE

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

Part II Processes and Threads Process Basics

Exceptional Control Flow Part I

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Lecture 4: Process Management

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

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

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

OS Lab Tutorial 1. Spawning processes Shared memory

Exceptional Control Flow Part I

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

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

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

SE350: Operating Systems

CSC 252: Computer Organization Spring 2018: Lecture 19

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

Creating a Shell or Command Interperter Program CSCI411 Lab

Giving credit where credit is due

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

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

Overview: Concurrent Architectures - Unix: Forks and Pipes

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?

Exceptional Control Flow Part I Oct. 17, 2002

Control Flow. Systemprogrammering 2007 Föreläsning 2 Exceptional Control Flow Part I. Exceptional Control Flow. Altering the Control Flow

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

PROCESS CONTROL: PROCESS CREATION: UNIT-VI PROCESS CONTROL III-II R

Introduction to Processes

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

Transcription:

ProgrammingII Session 2 process handling processes and forking fork system call exec system call death of process kill fork and I/O using it ProgrammingII Short Course Notes Alan Dix 1996 II/20

process: A process identified by process id (pid) process includes: program application system including file descriptors pid = 597 system e.g. file descriptors ProgrammingII Short Course Notes Alan Dix 1996 II/21

Forking 'fork' duplicates process: copies complete process state: program + system including file descriptors immutable Ð shared 597 632 system system $ echo $$ 597 $ (echo $$) 632 $ ProgrammingII Short Course Notes Alan Dix 1996 II/22

Forking 2 old process called the parent new process called the child process ids allocated sequentially so effectively unique (but do wrap after a very long time) finding process ids at the shell prompt: use 'ps' in a C program: use 'int p = getpid();' in a shell script: use '$$' N.B. useful for naming temporary files: tmpfile = "/tmp/myfile$$" ProgrammingII Short Course Notes Alan Dix 1996 II/23

Fork system call pid_t p = fork(); ( pid_t int ) if successful process successful fork returns: 0 Ð to child process child pid Ð to parent process parent and child are different! negative result on failure ProgrammingII Short Course Notes Alan Dix 1996 II/24

parent forks 597 int i = 3, c_pid = -1; c_pid = fork(); if ( c_pid == 0 ) printf("child\n"); else if ( c_pid > 0 ) printf("parent\n"); else printf("failed\n"); DATA i = 3 c_pid = -1 Execution 1 after fork parent and child identical 597 632 int i = 3, c_pid = -1; c_pid = fork(); if ( c_pid == 0 ) printf("child\n"); else if ( c_pid > 0 ) printf("parent\n"); else printf("failed\n"); DATA i = 3 DATA i = 3 c_pid = 632 c_pid = 0 int i = 3, c_pid = -1; c_pid = fork(); if ( c_pid == 0 ) printf("child\n"); else if ( c_pid > 0 ) printf("parent\n"); else printf("failed\n"); except for the return value of fork ProgrammingII Short Course Notes Alan Dix 1996 II/25

Execution 2 because are different 597 632 int i = 3, c_pid = -1; c_pid = fork(); if ( c_pid == 0 ) printf("child\n"); else if ( c_pid > 0 ) printf("parent\n"); else printf("failed\n"); DATA i = 3 DATA i = 3 c_pid = 632 c_pid = 0 int i = 3, c_pid = -1; c_pid = fork(); if ( c_pid == 0 ) printf("child\n"); else if ( c_pid > 0 ) printf("parent\n"); else printf("failed\n"); program execution differs 597 632 int i = 3, c_pid = -1; c_pid = fork(); if ( c_pid == 0 ) printf("child\n"); else if ( c_pid > 0 ) printf("parent\n"); else printf("failed\n"); DATA i = 3 DATA i = 3 c_pid = 632 c_pid = 0 int i = 3, c_pid = -1; c_pid = fork(); if ( c_pid == 0 ) printf("child\n"); else if ( c_pid > 0 ) printf("parent\n"); else printf("failed\n"); so parent and child behaviour diverge ProgrammingII Short Course Notes Alan Dix 1996 II/26

exec system call execv(char *prog, char **argv); replaces the current process with prog never returns except on failure argv is passed to the 'main' of prog N.B. needs at least argv[0] set to program name new process: Ð replaced by prog Ð reinitialised system Ð partly retained file descriptors still open several variants (execl, execvp,... ) often used after fork to spawn a fresh program ProgrammingII Short Course Notes Alan Dix 1996 II/27

exec vs. fork fork duplicates process exec replaces process 597 system 493 system fork exec 597 system 632 system 493 system fork child shares open file descriptors exec-ed process retains open fds ProgrammingII Short Course Notes Alan Dix 1996 II/28

death of a forked process when parent dies children become orphans! system init process 'adopts' them when child dies parent (or init) informed by signal ( SIGCHLD ) child process partly destroyed rump retained until parent 'reaps' Ð using wait or wait3 system call until then child is 'zombie' Ð ps says <exiting> or <defunct> N.B. zombie state necessary so parent can discover which child died ProgrammingII Short Course Notes Alan Dix 1996 II/29

wait if parent does not reap children... they stay zombies forever... system resources may run out reap using variants of wait union wait status; int pid = wait(status); block until a child has died return pid of deceased child int sysv(char *prog,char **argv) { union wait status; int pid = fork(); if ( pid < 0 ) return -1; if ( pid == 0 ) execvp(prog,argv); if ( wait(&status)!= pid ) return -1; return 0; } wait3 similar, but with more options ProgrammingII Short Course Notes Alan Dix 1996 II/30

SIGCHLD & wait3 wait blocks no good for concurrent execution SIGCHILD says when child has died 1 first catch your signal signal(sigchld,my_reaper); function 'my_reaper' called when signal arrives 2 then reap a child int my_reaper() { union wait status; while( wait3(&status,wnohang,null) >= 0 ); } use WNOHANG so that wait3 doesn't block loop to reap multiple children ProgrammingII Short Course Notes Alan Dix 1996 II/31

kill system call int kill(int pid, int sig); sends the signal sig to process pid target process Ð pid must have same effective user id (usually launched by same user) super user programs can kill anyone! special case: pid == -1 kill? broadcast (kill nearly everyone) donõt worry Ð super user only only kills the process if it is not caught (using signal system call) but some signals can never be caught self-destruct a process can send signals to itself! ProgrammingII Short Course Notes Alan Dix 1996 II/32

fork and I/O low-level I/O open file descriptors shared so: output is merged input goes to first read Ð accept similar close down may be delayed until all processes close fd close all unwanted fds or use ioctl to set close-on-exec high-level I/O C stdio is buffered: duplicated at fork may get flushed after fork duplicate writes stderr OK Ð unbuffered careful with stdio use stderr or setbuff(fd,null) ProgrammingII Short Course Notes Alan Dix 1996 II/33

³ Hands on ³ look at the program fork-test.c in prog2 check you understand how it works copy it into your directory and compile and run it write a test program for the sysv function on the wait slide get it to run /bin/cat on a test file the argv structure you pass to sysv will look something like this: static char *ex_argv[3] = { "cat", "tom", NULL }; try redirecting the output by opening a file and then using dup2: int fd = open( "dick", O_WRONLY O_CREAT ); dup2(fd,1); ProgrammingII Short Course Notes Alan Dix 1996 II/34