S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CS Lecture 17 Outline Named pipes Signals Lecture 17
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
CSE 451 Section 4 Project 2 Design Considerations.
Advanced Programming in the UNIX Environment Hop Lee.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
LINUX System Programming with Processes. Overview 1. What is a Process? 2. fork() 3. exec() 4. wait() 5. Process Data 6. File Descriptors across Processes.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Operating Systems Chapter 2
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
System calls for Process management
Operating Systems Processes 1.
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Operating Systems Recitation 4, April th, 2002 Signals.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
Process Management Azzam Mourad COEN 346.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
UNIX signals.
Unix Process Management
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
CSC Advanced Unix Programming, Fall 2015
Processes in Unix, Linux, and Windows
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Operation System Program 1
Inter-Process Communication ENCE 360
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Process Description and Control in Unix
Signals.
Process Description and Control in Unix
System Programming: Process Management
Presentation transcript:

S -1 Processes

S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c) return with error If there is more than one child, wait() returns on termination of any children waitpid can be used to wait for a specific child pid waitpid also has an option to block or not to block pid_t waitpid( pid, &status, option ); pid == -1 waits for any child option == NOHANG non-blocking option == 0 blocking waitpid(-1, &status, 0) equivalent to wait(&status)

S -3 example: wait.c #include void main( void ) { int status; if( fork() == 0 ) exit( 7 ); /* normal exit */ wait( &status ); prExit( status ); if( fork() == 0 ) abort(); /* generates SIGABRT */ wait( &status ); prExit( status ); if( fork() == 0 ) status /= 0; /* generates SIGFPE */ wait( &status ); prExit( status ); }

S -4 prExit.c #include void prExit( int status ) { if( WIFEXITED( status ) ) printf( "normal termination, exit status = %d\n", WEXITSTATUS( status )); else if( WIFSIGNALED( status ) ) printf( "abnormal termination, signal number = %d\n", WTERMSIG( status )); else if( WIFSTOPPED( status ) ) printf( "child stopped, signal number = %d\n", WSTOPSIG( status )); }

S -5 exec Six versions of exec: execl( char *pathname, char *arg0,..., (char*) 0 ); execv( char *pathname, char *argv[] ); execle( char *pathname, char *arg0,..., (char*) 0, char *envp[] ); execve( char *pathname, char *argv[], char *envp[] ); execlp( char *filename, char *arg0,..., (char*) 0 ); execvp( char *filename, char *argv[] );

S -6 Memory Layout of a C program text heap stack initialized data uninitialized data read from program file by exec initialized to zero by exec command-line arguments and environment variables low address high address dynamically allocated memory appears in the heap function invocations and local variables appear in the stack grow & shrink as needed

S -7 Miscellaneous: permissions Read permissions for a directory and execute permissions for it are not the same: –Read: read directory, obtain a list of filenames –Execute: lets users pass through the directory when it is a component of a pathname being accessed Cannot create a new file in a directory unless user has write permissions and execute permission in that directory To delete an existing file, the user needs write and execute permissions in the directory containing the file, but does not need read or write permission for file itself (!!!)

S -8 Miscellaneous: buffering control int setbuffer(FILE *fp, char *buf, int size) –specifies that “ buf ” should be used instead of the default system- allocated buffer, and sets the buffer size to “ size ” –if “ buf ” is NULL, i/o will be unbuffered –used after stream is opened, but before it is read or written int setlinebuf( FILE *fp ) –used to change stdout or stderr to line buffered –can be called anytime A stream can be changed from unbuffered or line buffered to block buffered by using freopen(). A stream can be changed from block buffered or line buffered to unbuffered by using freopen() followed by setbuf() with a buffer argument of NULL.

S -9 Signals

S -10 Motivation for Signals (11.15) When a program forks into 2 or more processes, rarely do they execute independently of each other The processes usually require some form of synchronization, and this is typically handled using signals Data usually needs to be passed between processes also, and this is typically handled using pipes and sockets, which we’ll discuss in detail in a week or two Signals are usually generated by –machine interrupts –the program itself, other programs, or the user (e.g. from the keyboard)

S -11 Introduction lists the signal types on cdf. Table 11.5 and signal(5) give a list of some signal types and their default actions When a C program receives a signal, control is immediately passed to a function called a signal handler The signal handler function can execute some C statements and exit in three different ways: –return control to the place in the program which was executing when the signal occurred –return control to some other point in the program –terminate the program by calling the exit (or _exit) function

S -12 sigset() A default action is provided for each kind of signal, such as terminate, stop, or ignore For nearly all signal types, the default action can be changed using the signal() function. The exceptions are SIGKILL and SIGSTOP Usage: signal(int sig, void (*disp)(int)) For each process, UNIX maintains a table of actions that should be performed for each kind of signal. The signal() function changes the table entry for the signal named as the first argument to the value provided as the second argument The second argument can be SIG_IGN (ignore the signal), SIG_DFL (perform default action), or a pointer to a signal handler function

S -13 sigset() example #include int i = 0; void quit( int code ) { fprintf( stderr, "\nInterrupt (code=%d, i=%d)\n", code, i ); exit( 123 ); } void main( void ) { if (signal( SIGINT, quit ) == SIG_IGN) exit( 1 ); if (signal( SIGTERM, quit ) == SIG_IGN) exit( 2 ); if (signal( SIGQUIT, quit ) == SIG_IGN) exit( 3 ); for(;;) if( i++ % == 0 ) putc( '.', stderr ); }

S -14 Checking the return value The data type that signal() returns is: pointer to function with int argument returning void So, the variable used to hold the result of a call to signal should be declared as follows: void (*signal_result)(int); It is possible for a child process to accept signals that are being ignored by the parent, which more than likely is undesirable Thus, the normal method of installing a new signal handler is: oldhandler = sigset( SIGHUP, SIG_IGN ); if( oldhandler != SIG_IGN ) sigset( SIGHUP, newhandler );

S -15 Signalling between processes One process can send a signal to another process using the misleadingly named function call kill( int pid, int sig ) This call sends the signal “ sig ” to the process “ pid ” Signalling between processes can be used for many purposes: –kill errant processes –temporarily suspend execution of a process –make processes aware of the passage of time –synchronize the actions of processes

S -16 Timer signals Three interval timers are maintained for each process: –SIGALRM (real-time alarm, like a stopwatch) –SIGVTALRM (virtual-time alarm, measuring CPU time) –SIGPROF (used for profilers, which we’ll cover later) Useful functions to set and get timer info are: –setitimer(), getitimer() –alarm() (simpler version: only sets SIGALRM ) –pause() (suspend until next signal arrives) –sleep() (caused calling process to suspend) –usleep() (like sleep(), but with finer granularity) Note: sleep() and usleep() are interruptible by other signals