Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.

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.
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Carnegie Mellon 1 Exceptional Control Flow : Introduction to Computer Systems Recitation 9: Monday, October 21 st, 2013 Ian Hartwig Section E.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 5 th Instructors: Gregory Kesden and Markus Püschel.
15-213, Fall 06 Outline Shell Lab Processes Signals.
System Programming Project 2 Due : 4/19...?. Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s.
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.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Recitation 8 (Nov. 1) Outline Lab 5 hints Virtual Memory Reminder Shell Lab: Due THIS Thursday TA: Kun Gao Modified from Minglong Shao’s Recitation, Fall.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
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
Processes, Signals, I/O, Shell lab
Exceptional Control Flow Part II Topics Process Hierarchy Signals CS213.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
CS 201 Exceptions and System calls Part II. – 2 – Shell Programs A shell is an application program that runs programs on behalf of the user. sh – Original.
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.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
Operating Systems Chapter 2
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Creating and Executing Processes
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
Program Translation and Execution II: Processes Oct 1, 1998 Topics User-level view of processes Implementation of processes setjmp/longjmp class12.ppt.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
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.
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.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
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 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
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.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Exceptional Control Flow II Oct 23, 2001 Topics Exceptions Process context switches class17.ppt “The course that gives CMU its Zip!”
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
Recitation 7 – 3/18/02 Outline fork and scheduling Signals
G.Jyostna.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Exceptional Control Flow Oct 24, 2000
Unix Process Management
Exceptional Control Flow
Exceptional Control Flow
Exceptional Control Flow Part II
Lecture 5: Process Creation
Exceptional Control Flow
CSC Advanced Unix Programming, Fall 2015
Exceptional Control Flow
Exceptional Control Flow
Lecture 6: Multiprogramming and Context Switching
Exceptional Control Flow
Signals.
Exceptional Control Flow
Recitation 9: Processes, Signals, TSHLab
Exceptional Control Flow
Presentation transcript:

Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong Shao’s Recitation, Fall 2004

Exceptional control flow (ECF) Abrupt changes in the control flow React to changes in system state that are not captured by internal program variables and are not necessarily related to the execution of the program Happens at all levels of a computer system Exceptions Concurrent processes Signals Non-local jumps

Exceptions Interrupt (asynchronous exceptions) I/O interrupt, hardware reset, software reset, etc. Traps System calls, breakpoint traps, etc. Faults Page fault, protection fault, etc. Aborts Parity error, machine check, etc.

Process concept An instance of running program Multiple processes run “concurrently” by time slicing Context switching Control flow passes from one process to another Preemptive scheduler of OS Process vs Threads (a favorite interview question) Threads are logical flows that run in the context of a single process

Process IDs & process groups A process has its own, unique process ID pid_t getpid(); A process belongs to exactly one process group pid_t getpgrp(); A new process belongs to which process group? Its parent’s process group A process can make a process group for itself and its children setpgid(0, 0);

Create a new process int fork(void) Create a new process that is identical to the parent process Return 0 to child process Return child’s pid to the parent process Call once, return twice Test your understanding… Problem 1

#include int cnt = 0; int main(void) { if (fork() == 0){ cnt ++; fork(); cnt++; } cnt ++; printf("%d", cnt); return 0; } Possible output:

Reaping child process Child process becomes zombie when terminates Still consume system resources Parent performs reaping on terminated child pid_t wait(int *status) pid_t waitpid(pid_t pid, int *status, int options) Straightforward for reaping a single child Tricky for Shell implementation! Multiple child processes Both foreground and background

Signals Section 8.5 in text Read at least twice … really! A signal tells our program that some event has occurred

Important signals (Fig 8.23) SIGINT Interrupt signal from terminal (ctrl-c) SIGTSTP Stop signal from terminal (ctrl-z) SIGCHLD A child process has stopped or terminated

How to Send Signals Process int kill(pid_t pid, int sig) Groups int kill(pid_t gid, int sig), where gid is negative Process can also send a signal to itself int alarm(unsigned int secs) to send a SIGALRM signal Can we use signals to count events? No Why? Signals not queued!!

Signals: sending OS Kernel blocked pending 1 Process 1 Process 2 other events OS procedure kill(pid, SIGINT) divide by zero: SIGFPE ctrl-c: SIGINT child process exit: SIGCHLD

Signals: receiving OS Kernel blocked pending 1 Process 2 OS procedure 0 Check when schedule the process to run

Receiving a signal Default action The process terminates [and dumps core] The process stops until restarted by a SIGCONT signal (ctrl-z) The process ignore the signal Can modify (additional action) “Handle the signal” -- install signal handler void sigint_handler(int sig); signal(SIGINT, sigint_handler); An example: problem 3

Problem 3 void handler(int sig){ static int beeps = 0; printf("YO\n"); if (++beeps < 2) alarm(1); /* next SIGALRM will be delivered in 1s */ else{ printf("MA\n"); kill(getpid(), SIGKILL); } int main(){ signal(SIGALRM, handler); alarm(1); /* next SIGALRM will be delivered in 1s */ while (1) ; printf(" is Great!\n"); return 0; } 1. Output: YO MA 2. The program will terminate

Signals not queued int counter = 0; void handler(int sig) { counter++; sleep(1); return; } int main() { int i; signal(SIGUSER2, handler); if (fork() == 0){ for (i = 0; i < 5; i++){ kill(getppid(), SIGUSR2); printf(“sent SIGUSR2 to parent\n”); } exit(0); } wait(NULL); printf(“counter = %d\n”, counter); exit(0); } Output: sent SIGUSR2 to parent counter = 1

Race hazard A data structure is shared by two pieces of code that can run concurrently Different behaviors of program depending upon how the schedule interleaves the execution of code.

An example of race hazard sigchld_handler() { pid = waitpid(…); deletejob(pid); } eval() { pid = fork(); if(pid == 0) { /* child */ execve(…); } /* parent */ /* signal handler may run BEFORE addjob()*/ addjob(…); }

An okay schedule Shell Signal HandlerChild fork() addjob() execve() exit() sigchld_handler() deletejobs() time

A problematic schedule Shell Signal HandlerChild fork() execve() exit() sigchld_handler() deletejobs() time addjob() Job added to job list after the signal handler tried to delete it!

Solution: blocking signals sigchld_handler() { pid = waitpid(…); deletejob(pid); } eval() { sigprocmask(SIG_BLOCK, …) pid = fork(); if(pid == 0) { /* child */ sigprocmask(SIG_UNBLOCK, …) /* child inherits parents block set */ execve(…); } /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…); sigprocmask(SIG_UNBLOCK, …) } More details (page 633)

Non-local jump int setjmp(jmp_buf env) Must called before longjmp Stores current register context, stack pointer, and PC in the jump buffer env First called, return 0 if no error void longjmp(jmp_buf env, int i) Restores register context from jump buffer env Jumps back to where previous setjmp is called, behaves like setjmp just completes. But this time it returns i, not 0 Can only jump to an active context A function that has been called but not yet completed

Non-local jump (cont) int sigsetjmp(jmp_buf env) Also saves blocked signals void siglongjmp(jmp_buf env, int i) Restores blocked signals besides others Let’s see an example: problem 4

Problem 4 jmp_buf stuff; int foo(){ char c = getc(stdin); if (c == 'x') longjmp(stuff, 42); else return 3; return -1; } int main() { int n; n = setjmp(stuff); while ((n+=foo())<0) sleep(1); printf("%d\n", n); } Answer:

Summary Process fork(), waitpid(), execve() Reaping child processes Signals signal(), install handler, signals not queued Non-local jumps Check man page to understand the system calls better man waitpid (fork, signal, …) Read text book!