Recitation 7 – 3/18/02 Outline fork and scheduling Signals

Slides:



Advertisements
Similar presentations
L7: Exceptions Problem Description Error Handling –terminate the program –return a value representing an error –return a legal value and leave the program.
Advertisements

Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
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.
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Exceptional Control Flow Part II Topics Process Hierarchy Signals CS213.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Introduction to Processes CS Intoduction to Operating Systems.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
Operating Systems Chapter 2
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
Exception Compiler Baojian Hua
Program Translation and Execution II: Processes Oct 1, 1998 Topics User-level view of processes Implementation of processes setjmp/longjmp class12.ppt.
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Exception Compiler Baojian Hua
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.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
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.
Signals. Introduction r A signal is a mechanism for notifying a process that an event has occurred. m When a signal is sent to a process is normal execution.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
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)
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Setjmp, Longjmp Useful functions for dealing with errors and interrupts setjmp saves its environment (i.e. registers) in env for later use by longjmp After.
Exceptional Control Flow II Oct 23, 2001 Topics Exceptions Process context switches class17.ppt “The course that gives CMU its Zip!”
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
UNIX signals.
G.Jyostna.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Precept 14 : Ish dup() & Signal Handling
Task Control: Signals and Alarms Chapter 7 and 8
Protection of System Resources
CENG334 Introduction to Operating Systems
Chapter 4: Threads.
Unix Process Management
Tarek Abdelzaher Vikram Adve Marco Caccamo
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Exceptional Control Flow Part II
Exceptional Control Flow
Chapter 14 - Advanced C Topics
Chapter 4: Threads.
CSC Advanced Unix Programming, Fall 2015
Processes in Unix, Linux, and Windows
COP4020 Programming Languages
Exceptional Control Flow
The Environment of Unix Process
Processes in Unix, Linux, and Windows
CS510 Operating System Foundations
Low-Level Thread Dispatching on the x86
Signals.
CMSC 202 Exceptions.
Presentation transcript:

15-213 Recitation 7 – 3/18/02 Outline fork and scheduling Signals setjmp/longjmp Emulating C++ exceptions in C James Wilson e-mail: wilson2@andrew.cmu.edu Office Hours: Friday 1:30 – 3:00 Wean Cluster 520x

What output does the following have? int main(int argc, char *argv[]) { int i = 0; printf("%d\n", i); if(fork()) { ++i; } else { } else { printf("%d\n", i); } return 0;

Scheduling Dependent Output Parents always first produces: 0, 1, 2, 0, 1 Lowest level child always first: 0, 0, 1, 1, 2 A good number more versions…

What’s wrong with the code? int main(int argc, char *argv[]) { int i = 0; printf("%d\n", i); if(fork()) { ++i; } else { } else { printf("%d\n", i); } return 0;

Need to add wait() int main(int argc, char *argv[]) { int i = 0; printf("%d\n", i); if(fork()) { ++i; wait(); } else { } else { printf("%d\n", i); } return 0;

Signals Software events generated by OS and processes an OS abstraction for exceptions and interrupts Sent from the kernel or a process to other processes. Different signals are identified by small integer ID’s Only information in a signal is its ID and the fact that it arrived. Num. Name Default Description 2 SIGINT Terminate Interrupt from keyboard (cntl-c) 9 SIGKILL Kill program (cannot override or ignore) 11 SIGSEGV Terminate & Dump Segmentation violation 14 SIGALRM Timer signal 17 SIGCHLD Ignore Child stopped or terminated

Signals #include <stdio.h> #include <malloc.h> #include <signal.h> // Signal handler // Called when process receives signal // Just exits static void sig_handler ( int sig ) { printf( "Segmentation fault caught, exiting gracefully.\n" ); exit ( 16 ); }

Signals int main() { float *f, t1, t2; signal ( SIGSEGV, sig_handler ); // set function to handle signals f = (float*) malloc ( 3 * sizeof ( float ) ); f[0] = 1; f[1] = 8; f[2] = 7; t1 = *(f+0) + *(f+1); printf ( "%f\n", t1 ); free ( f ); f = NULL; // set f to NULL t2 = *(f+1) + *(f+2); // accessing it generates SIGSEGV printf ( "%f", t2 ); // which is caught by process and return 0; // and calls handling function }

Signals Output without signal handling: Output with signal handling: 9.000000 Segmentation fault Output with signal handling: Segmentation fault caught, exiting gracefully. Provides programmer with method to clean up program (free all used blocks of memory, unlock mutex, etc..) if a kill signal is sent

Setjmp, Longjmp int setjmp(jmp_buf env); void longjmp(jmp_buf env, int val); Useful functions for dealing with errors and interrupts setjmp saves its environment (i.e. registers) in env for later use by longjmp After longjmp completes, program starts after call to setjmp, as if setjmp had returned val.

Setjmp, Longjmp: Example What is the output of the following program? #include <setjmp.h> #include <stdio.h> int a(char *s, jmp_buf env) { int i; i = setjmp(env); printf("Setjmp returned -- %d\n", i); printf("s = %s\n", s); return i; } int b(int i, jmp_buf env) printf("In b: i = %d, Calling longjmp\n", i); longjmp(env, i); int main() { jmp_buf env; if(a("Bob", env) != 0) exit(0); b(3,env); return 0; }

Setjmp, Longjmp: Example con’t UNIX> sj Setjmp returned -- 0 s = Bob In b: I = 3, Calling longjmp Setjmp returned -- 3 UNIX> sj Setjmp returned -- 0 s = Bob In b: I = 3, Calling longjmp Setjmp returned -- 3 Segmentation Fault

Setjmp, Longjmp: Example con’t Let’s take a look at the stack to see why we’re segfaulting. %ebp old %ebp First, main( ) looks like this. %eip --> in main env[8] ... env[0] %esp

Setjmp, Longjmp: Example con’t old %ebp env[8] Then main( ) calls a( ). %eip --> in a ... env[0] s = “Bob” Rtn Addr %ebp old %ebp i %esp

Setjmp, Longjmp: Example con’t old %ebp Then main( ) calls a( ). %eip --> in a Then a( ) calls setjmp( ). This saves the current state of the registers. env[8] …. env[0] s = “Bob” Rtn Addr %ebp old %ebp i %esp

Setjmp, Longjmp: Example con’t old %ebp env[8] …. Returned to main( ), and main( ) calls b( ). %eip --> in b env[0] i = 3 Rtn Addr %ebp old %ebp i %esp

Setjmp, Longjmp: Example con’t old %ebp env[8] longjmp( ) is called, and the regs are restored to their values of when a( ) was called. %eip --> in a …. env[0] s =?? 3 Rtn Addr %ebp old %ebp i %esp Why the segfault?? --> the stack is in a bad state. a( ) expects a (char *) instead of the int value 3. This a common bug with setjmp/longjmp ---> You CANNOT return from a function that calls setjmp!

C++ exception handling introduction A more general and powerful error handling mechanism than return values Simplified syntax: class A { }; try { do_something(); } catch (const A&) { // handle exception A The function do_something can throw/raise exceptions with: throw A();

C++ exception handling introduction Exceptions can be arbitrary types in C++, including objects with constructors C++ allows for nested try blocks; exceptions that are uncaught at one level percolate up to the next Uncaught exceptions terminate the program, but this behavior can be changed The full syntax and semantics, like most of C++, is complicated Read Stroustrup, chapter 14

Emulating exception handling in C setjmp/longjmp mechanism allows for some limited emulation of C++ exceptions Use setjmp to set up a try block and the exception handlers Exceptions are thrown using longjmp with an appropriate exception code

C++ exception handling example #include <stdio.h> #include <math.h> #include <unistd.h> class Range { }; class Close { }; void sqrt_loop(void) { double d; printf("Enter a number between 0 and 100: "); if (scanf("%lf",&d) == EOF) throw Close(); if (d<0 || d>100) throw Range(); printf("Square root of %f is %f\n",d,sqrt(d)); } int main(void) { try { while(1) sqrt_loop(); } catch (const Range &) { fprintf(stderr,"Number out of range\n"); exit(0); catch (...) { fprintf(stderr,"Uncaught exception\n"); exit(1); return 0;

Emulating exception handling in C example #include <setjmp.h> #include <stdio.h> #include <math.h> #include <unistd.h> jmp_buf ex_buf; #define EX_RANGE 1 #define EX_CLOSE 2 void sqrt_loop(void) { double d; printf("Enter a number between 0 and 100: "); if (scanf("%lf",&d) == EOF) longjmp(ex_buf,EX_CLOSE); if (d<0 || d>100) longjmp(ex_buf,EX_RANGE); printf("Square root of %f is %f\n",d,sqrt(d)); } int main(void) { int ex_no; switch (ex_no = setjmp(ex_buf)) { case 0: while(1) sqrt_loop(); break; case EX_RANGE: fprintf(stderr,"Number out of range\n"); exit(0); default: fprintf(stderr,"Uncaught exception %d\n",ex_no); exit(ex_no); } return 0;

Limitations We can only throw an integer error code to the exception catcher. C++ allows arbitrary objects to be thrown You can create a pointer for an “exception object,” but this is not type-safe and allocation/disposal is not automatic Need a stack of jmp_buf structures to emulate an exception stack Otherwise, storing and restoring the exception jmp_buf gets messy Uncaught exceptions are not automatically rethrown Even with a stack, the above implementation is not thread-safe Each thread needs a separate exception stack