Processes. Processes (cont d)

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

CS240: Programming in C

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

Process Management 1

Signal Example 1. Signal Example 2

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Process Management! Goals of this Lecture!

CS240: Programming in C

Killing Zombies, Working, Sleeping, and Spawning Children

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

CSC 1600 Unix Processes. Goals of This Lecture

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

COP4342 UNIX Tools Assignment #3: A Simple Unix Shell. Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018

HW 1: Shell. Contents CS 162. Due: September 18, Getting started 2. 2 Add support for cd and pwd 2. 3 Program execution 2. 4 Path resolution 3

The Shell, System Calls, Processes, and Basic Inter-Process Communication

Fall 2015 COMP Operating Systems. Lab #3

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

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

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

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Computer Systems Assignment 2: Fork and Threads Package

File Descriptors and Piping

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

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

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

CS213. Exceptional Control Flow Part II. Topics Process Hierarchy Signals

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

Network Socket Programming - 1 BUPT/QMUL

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

Operating Systemss and Multicore Programming (1DT089)

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

Workshop on Inter Process Communication Solutions

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

Operating System Structure

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.

CSE 421: Introduction to Operating Systems

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

Signals. POSIX defines a variety of signal types, each for a particular

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.

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

SOFTWARE ARCHITECTURE 3. SHELL

UNIX System Calls. Sys Calls versus Library Func

Design Overview of the FreeBSD Kernel CIS 657

System Programming. Signals I

Processes. CS3026 Operating Systems Lecture 05

PVPSIDDHARTHA INSTITUTE OF TECHNOLOGY

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

OS Lab Tutorial 1. Spawning processes Shared memory

Exceptional Control Flow Exists at All Levels of a System. Systemprogrammering 2007 Föreläsning 3 Exceptional Control Flow Part II

Operating Systems, laboratory exercises. List 2.

Overview. Administrative. * HW 1 grades. * HW 2 Due. Topics. * 5.1 What is a Signal? * Dealing with Signals - masks, handlers

ECF Exists at All Levels of a System Exceptional Control Flow Part II Oct. 22, 2002 Topics! Process Hierarchy! Shells! Signals!

Processes COMPSCI 386

Process Management! Goals of this Lecture!

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

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

System Calls & Signals. CS449 Spring 2016

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

Signals. Goals of this Lecture. Help you learn about: Sending signals Handling signals

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

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

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

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

Pipelines, Forks, and Shell

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

F28HS Hardware-Software Interface: Systems Programming

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

Process Creation in UNIX

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

Inter-process communication (IPC)

CS 470 Operating Systems Spring 2013 Shell Project

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

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

Exceptional Control Flow Part II

Implementation of a simple shell, xssh

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Operating Systemss and Multicore Programming (1DT089)

Recitation 8: Tshlab + VM

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

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

Cross-platform daemonization tools.

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

Inter-Process Communication

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Giving credit where credit is due

Altering the Control Flow

Basic OS Progamming Abstrac7ons

Signals are a kernel-supported mechanism for reporting events to user code and forcing a response to them. There are actually two sorts of such

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Basic OS Progamming Abstrac2ons

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

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Processes. Dr. Yingwu Zhu

Signals! Goals of this Lecture! Help you learn about:" Sending signals" Handling signals"

Transcription:

Processes UNIX process creation image-file arg1 arg2 Shell command line example ls -l Equivalent to /bin/ls -l Why? How do you find out where the image file is? Background processes ls -l & Execute a process by a system call int execlp ( const char *file, const char *arg, ) image-file name Arguments as in main() and end with NULL Replace the current process image with the specified image file Return only if it fails Copyright 1997 Computer Science 217: Processes Page 202 Processes (cont d) Example execlp( ls, ls, -l, NULL ); fprintf( stderr, Couldn t execute ls \n ); exit(1); There are many other alternatives to execlp: int execl( const char *path, const char *arg, ); int execv( const char *path, char * const argv[] ); int execle( const char *path, const char *arg,, char * const envp[] ); int execve( const char *path, char * const argv[], char * const envp[] ); int execvp( const char *file, char * const argv[] ); What are the differences from execlp? See man page for details Copyright 1997 Computer Science 217: Processes Page 203

Fork Sometimes you don t want the new process to replace the current one fork() splits the current process into two copies: pid = fork(); /* pid!= 0 */ /* pid == 0 */ Parent Child Return -1 on failure (see man page for more details) A child process has a unique process id (pid), different from its parent s A child process inherits environment variables from its parents A child process share file descriptors with its parent Are there any problems with sharing file descriptors? Copyright 1997 Computer Science 217: Processes Page 204 Fork (cont d) Duplicate a file descriptor and pick an unused descriptor for it int dup( int fd ); Duplicate fd Use the lowest numbered, unallocated file descriptor Thus, one can close file descriptors to let the system pick the right one Connect the standard input (stdin) of a program to a file: int fd; fd = open( foo, O_RDONLY, 0 ); close( 0 ); dup( fd ); close( fd ); This is not so convenient, so there is an extension call: int dup2( int fd1, int fd2 ); Use fd2 to duplicate fd1 If fd2 is in use, perform close( fd2 ) fd = open( foo, O_RDONLY, 0 ); dup( fd, 0 ); close( fd ); Copyright 1997 Computer Science 217: Processes Page 205

Wait A parent may want to wait until its child dies (terminates), and then do something pid_t wait( int *status_location ); Returned pid is the pid of the terminated child process Return -1 if no childen alive The basic way to fork and wait is: if ( fork() == 0 ) execlp( ls, ls, -l, NULL ); /* child */ fd = dup( fd1 ); /* if needed */ pid = wait( &status ); /* parent */ Copyright 1997 Computer Science 217: Processes Page 206 Pipes and Filters A pipe -- An interprocess communication channel Process A Process B output input A filter takes input from stdin and puts output to stdout Filter Many Unix tools are filters and csh supports pipes ls -l more Copyright 1997 Computer Science 217: Processes Page 207

Pipe Creation System call to create a pipe int pipe( int fd[ 2 ] ); Return 0 on success and -1 on failure fd[ 0 ] is opened for reading fd[ 1 ] is opened for writing Two coordinated processes created by fork() can pass data using read() and write() Simple example: a parent writes to a pipe and a child reads from a pipe int pid, p[2]; pipe( p ); if ( ( pid = fork() ) == 0 ) close( p[1] ); read using p[0] as file descriptor close( p[0] ); write using p[1] as file descriptor close( p[1] ); /* send EOF to the read port */ wait( &status ); Copyright 1997 Computer Science 217: Processes Page 208 Standard I/O Pipe Connect a pipe to parent s stdout and child s stdin int pid, p[2]; pipe( p ); if ( ( pid = fork() ) == 0 ) close( p[1] ); dup2( p[0], 0 ); /* duplicate p[0] using stdin */ close( p[0] ); read from stdin exit( 0 ); close( p[0] ); dup2( p[1], 1 ); /* duplicate p[1] using stdout */ close( p[1] ); write to stdout wait( &status ); Copyright 1997 Computer Science 217: Processes Page 209

Signals Outside world signals are interrupts CTRL-C is typed phone is hung up... Processes may need to respond to the signals A process a signal handler() Copyright 1997 Computer Science 217: Processes Page 210 UNIX Signal Facility UNIX provides a mechanism for catching a signal (like an exception) void (* signal( int sig, void (* handler)( int ) ) ( int ); The function handler() will be invoked on a signal sig handler( sig ); Return the old signal handler on success and -1 on an error Examples #include <signal.h> signal( SIGINT, SIG_IGN ); /* ignore interrupts */ signal( SIGINT, SIG_DFL ); /* restore the default handler */ There are more than 30 predefined signals in <signal.h> Copyright 1997 Computer Science 217: Processes Page 211

Signal Handling Example Remove tempfile on an interrupt: #include <sys/signal.h> char *tempfile = temp.xxx ; void cleanup( int sig ) unlink( tempfile ); exit( 1 ); ; void main( void ) int fd; signal( SIGINT, cleanup ); /* set up cleanup() for interrupt */ fd = open( tempfile, O_CREAT, 0666 ); /* processing using tempfile */ close( fd ); What happens to background processes (started with & for example)? Copyright 1997 Computer Science 217: Processes Page 212 Signal Handling Safely #include <sys/signal.h> char *tempfile = temp.xxx ; void cleanup( int sig ) unlink( tempfile ); exit( 1 ); ; void main( void ) int fd; /* protect background processes */ if ( signal( SIGINT, SIG_IGN )!= SIG_IGN ) signal( SIGINT, cleanup ); fd = open( tempfile, O_CREAT, 0666 ); /* processing using tempfile */ close( fd ); Copyright 1997 Computer Science 217: Processes Page 213

Interpret Signals Interrupt a long printout and go to main processing loop #include <sys/signal.h> #include <setjmp.h> jmp_buf jmpbuf; void handler( int sig ) signal( SIGINT, handler ); /* reset handler */ fprintf( stderr, Interrupted\n ); longjmp( jmpbuf, 0 ); /* go to main loop */ void main( void ) if ( signal( SIGINT, SIG_IGN )!= SIG_IGN ) signal( SIGINT, handler); setjmp( jmpbuf ); for ( ; ; ) /* main processing loop */ Copyright 1997 Computer Science 217: Processes Page 214 Signals with Fork Signals are sent to all your processes You often want to disable signals and enable them later fork() may cause two processes to read your terminal (/dev/tty) at the same time An example: #include <signal.h> if ( fork() == 0 ) execlp( ); h = signal( SIGINT, SIG_IGN ); /* parent ignores interrupts */ wait( &status ); /* until child is done */ signal( SIGINT, h ); /* restore interrupts */ Copyright 1997 Computer Science 217: Processes Page 215

Alarm Create a child process and kill it in 20 seconds #include <signal.h> int pid; void OnAlarm( int sig ) kill( pid, SIGKILL ); if ( ( pid = fork() ) == 0 ) execlp(... ); signal( SIGALARM, OnAlarm ); /* setup the handler */ alarm( 20 ); /* fire off the alarm */ What if you want to have an alarm in less than a second? unsigned ualarm( unsigned value, unsigned interval ) int setitimer( int which, struct itimerval *value, struct itimerval *ovalue ) Copyright 1997 Computer Science 217: Processes Page 216 More Alarm Examples Implement time out in communication protocols Retransmission if no acknowledgment Decide NFS is not responding. Implement imestamps for performance measurements The typical way to get a timestamp is: #include <sys/time.h> gettimeofday( struct timeval *tp; struct timezone *tzp ); But, this call takes x0 to x00 microseconds So, use ualarm to implement something less expensive. Other examples? Copyright 1997 Computer Science 217: Processes Page 217