System Programming Project 2 Due : 4/19...?. Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s.

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.
June 1, 1999Foreground/Background Processing1 Introduction to UNIX H. Foreground/Background Processing.
Last Lab: Writing a shell
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.
Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.
Process Relationships Terminal and Network Logins Process Groups and Sessions Job Control Relationships.
Lesson 10-Controlling User Processes. Overview Managing and processing processes. Managing jobs. Exiting/quitting when jobs have been stopped.
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.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
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.
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
1 Signals COS Goals of Today’s Lecture Overview of signals  Notifications sent to a process  UNIX signal names and numbers  Ways to generate.
CSSE Operating Systems
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
Exceptional Control Flow Part II Topics Process Hierarchy Signals CS213.
1 Signals and Session Management Chapter 4. 2 Contents u Signal Generation and Handling u Unreliable Signals u Reliable Signals u Signals in SVR4 u Signals.
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.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
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.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
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).
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
The kernel considers each program running on your system to be a process A process lives as it executes, with a lifetime that may be short or long A process.
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Project1: Unix Shell with History Feature Goals Descriptions Methodology Submission.
Signals and Signal Handling. Signals A predefined message sent between two processes or from the kernel to a process, or by a user to a process A software.
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 *
Exceptional Control Flow Part II Topics Process Hierarchy Shells Signals.
KUKUM Real Time System Module #3 POSIX programming Lecture 2.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
1 Exceptional Control Flow III. 2 Outline Loading and Running Programs Multitasking, shells Suggested reading 8.4.
CS 241 Section Week #10 (04/01/10) ‏. Topics This Section  MP5 Overview  Signals.
Agenda Managing Processes (Jobs) Command Grouping Running jobs in background (bg) Bringing jobs to foreground (fg), Background job status (jobs) Suspending.
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.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 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.
Operating Systems Practical Session 3, Signals 1.
Process Relationships Chien-Chung Shen CIS/UD
Operating Systems Summer Semester 2011 Practical Session 2, Signals 1.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
G.Jyostna.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Unix Process Management
Exceptional Control Flow
Exceptional Control Flow
Exceptional Control Flow Part II
Exceptional Control Flow
The Linux Command Line Chapter 10
Exceptional Control Flow
CHAPTER 13 Processes.
Exceptional Control Flow
Signals.
Exceptional Control Flow
Exceptional Control Flow
Presentation transcript:

System Programming Project 2 Due : 4/19...?

Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s focus Having user’s focus Background job Background job Many jobs running in background Many jobs running in background They are totally inpendent They are totally inpendent No focus No focus FG BG BG BG

FG & BG & Stopped in Unix BG FG Stopped Ctrl+Z fgbg fg cmd& cmd

FG & BG & Stopped Example >parrot hi 01-hi02-hi>jobs [1] Stopped parrot hi >bg 1 >03-hi 04-hi>ls hello.c abc.txt shell >05-hi 06-hi Ctrl+Z >parrot hi & >01-hi 02-hi>jobs [1] Running parrot hi & >03-hi 04-hi >fg 1 05-hi...10-hi> Ex.1Ex.2

Signal Handling in shell Ex. Terminating FG job user push Ctrl+C Shell receives ‘SIGINT’ signal Shell sends ‘SIGINT’ to FG job FG job receives ‘SIGINT’ and terminates

Important Unix Signals SIGINT (Ctrl+C by user) SIGINT (Ctrl+C by user) terminate process terminate process SIGTSTP (Ctrl+Z by user) SIGTSTP (Ctrl+Z by user) stop process stop process SIGCONT (from shell to child) SIGCONT (from shell to child) continue stopped process continue stopped process SIGCHLD (from child to shell) SIGCHLD (from child to shell) notify state change of child process notify state change of child process Run -> Complete Run -> Complete Run -> Terminate or Stop Run -> Terminate or Stop Stop -> Run Stop -> Run > kill –l : show all signals

signal(), sigaction() #include #include int flag; void myHandler (int sig) { flag=1; // this can stop while loop } main() { flag=0; while (!flag); printf(“1”); flag=0; while (!flag); printf(“2”); flag=0; while (!flag); printf(“3”); } // After myHandler is executed // SIGINT handler changes to default signal(SIGINT, myHandler); // SIGINT handler is myHandler forever struct sigaction action, old_action; action.sa_handler = myHandler; sigemptyset(&action.sa_mask); action.sa_flags = SA_RESTART; sigaction(SIGINT, &action, &old_action; This is example of busy-waiting

Specification 1/2 Ctrl+C terminates FG job Ctrl+C terminates FG job Ctrl+C shouldn’t terminate shell Ctrl+C shouldn’t terminate shell Do nothing if there is no FG job Do nothing if there is no FG job Ctrl+Z stops FG job Ctrl+Z stops FG job Ctrl+Z shouldn’t stop shell Ctrl+Z shouldn’t stop shell Do nothing if there is no FG job Do nothing if there is no FG job Running in background Running in background Putting ‘&’ in the end of command Putting ‘&’ in the end of command Terminate all jobs when shell ‘exit’ Terminate all jobs when shell ‘exit’

Specification 2/2 ‘jobs’ command ‘jobs’ command Shows list of job state Shows list of job state [job_id] state command [job_id] state command State is ‘BG running’ or ‘Stopped’ State is ‘BG running’ or ‘Stopped’ ‘fg job_id’ command ‘fg job_id’ command Change ‘job_id’ job to FG job Change ‘job_id’ job to FG job Stopped or BG  FG Stopped or BG  FG ‘bg job_id’ command change a job to BG ‘bg job_id’ command change a job to BG Change ‘job_id’ job to BG job Change ‘job_id’ job to BG job Stopped  BG Stopped  BG - Don’t use exec(). - If ‘job_id’ is not specified, it’s up to you. Make it work like bash

SIGINT, SIGTSTP Handler sigint_handler { search FG job if there is FG job send SIGINT to FG job to terminate } sigtstp_handler { search FG job if there is FG job change the job’s state to Stopped send SIGTSTP to FG job to stop } Use ‘kill (pid, sig)’ to send signal!!

SIGCHLD Handler sigchld_handler { get pid whose state changed to completed / terminated / stopped / continue... if it is terminated(SIGINT) or completed delete the job from job list shell comes back else if it is stopped(SIGTSTP) shell comes back } See Hints page!!

Hints waitpid(pid, &status, option) waitpid(pid, &status, option) If pid=-1, wait for any child to change state If pid=-1, wait for any child to change state If option = WUNTRACED|WNOHANG If option = WUNTRACED|WNOHANG Return value is pid of process when process is terminated or stopped. 0 or -1 for other state change Return value is pid of process when process is terminated or stopped. 0 or -1 for other state change WNOHANG means doesn’t wait WNOHANG means doesn’t wait Status can be checked with macros. - If WIFEXITED(status) > 0, completed - If WIFSIGNALED(status) > 0, terminated - If WIFSTOPPED(status) > 0, stopped Status can be checked with macros. - If WIFEXITED(status) > 0, completed - If WIFSIGNALED(status) > 0, terminated - If WIFSTOPPED(status) > 0, stopped from and my try and error...T.T and my try and error...T.T

Hints Signal from group A goes to all process in group A Signal from group A goes to all process in group A Shell’s group and child group is same as default. Shell’s group and child group is same as default. But only shell should receive user’s signal. But only shell should receive user’s signal. setpgid(pid, pgid) setpgid(pid, pgid) Set process group id Set process group id If pid=0, it is same as getpid() If pid=0, it is same as getpid() If pgid=0, it is same as pid. (pid becomes a group leader) If pgid=0, it is same as pid. (pid becomes a group leader) getpgrp(), getpgid(pid) getpgrp(), getpgid(pid) get process group id get process group id

Hints sigprocmask(mode, set, oldset) sigprocmask(mode, set, oldset) Block signals Block signals Example code Example code sigset_t sigset, oldset; sigemptyset(&sigset); sigaddset(&sigset, SIGCHLD); sigprocmask(SIG_BLOCK, &sigset, &oldset); sigset_t sigset, oldset; sigemptyset(&sigset); sigaddset(&sigset, SIGCHLD); sigprocmask(SIG_BLOCK, &sigset, &oldset); /* run the job */ /* add job to job list */ sigprocmask(SIG_UNBLOCK, &sigset, &oldset); /* run the job */ /* add job to job list */ sigprocmask(SIG_UNBLOCK, &sigset, &oldset);  Short jobs are terminated(sending SIGCHLD) before shell adds them to job list. Mask prevents this by blocking SIGCHLD.