Title: System Programming Project 2
1System ProgrammingProject 2
2Foreground Background
- Foreground job
- Only one at moment
- Having users focus
- Background job
- Many jobs runningin background
- They are totallyinpendent
- No focus
BG
BG
FG
BG
3FG BG Stopped in Unix
BG
FG
fg
cmd
cmd
fg
bg
CtrlZ
Stopped
4FG BG Stopped Example
- gtparrot hi
- 01-hi
- 02-hi
- gtjobs
- 1 Stopped parrot hi
- gtbg 1
- gt03-hi
- 04-hi
- gtls
- hello.c abc.txt shell
- gt05-hi
- 06-hi
gtparrot hi gt01-hi 02-hi gtjobs 1 Running
parrot hi gt03-hi 04-hi gtfg 1 05-hi ... 10-hi gt
CtrlZ
Ex.1
Ex.2
5Signal Handling in shell
user push CtrlC
Shell receives SIGINT signal
Shell sends SIGINT to FG job
FG job receives SIGINTand terminates
6Important Unix Signals
- SIGINT (CtrlC by user)
- terminate process
- SIGTSTP (CtrlZ by user)
- stop process
- SIGCONT (from shell to child)
- continue stopped process
- SIGCHLD (from child to shell)
- notify state change of child process
- Run -gt Complete
- Run -gt Terminate or Stop
- Stop -gt Run
- gt kill l show all signals
7signal(), sigaction()
- include ltsignal.hgt
- int flag
- void myHandler (int sig)
- flag1 // this can stop while loop
-
- main()
- flag0 while (!flag) printf(1)
- flag0 while (!flag) printf(2)
- flag0 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 exampleof busy-waiting
8Specification 1/2
- CtrlC terminates FG job
- CtrlC shouldnt terminate shell
- Do nothing if there is no FG job
- CtrlZ stops FG job
- CtrlZ shouldnt stop shell
- Do nothing if there is no FG job
- Running in background
- Putting in the end of command
- Terminate all jobs when shell exit
9Specification 2/2
- jobs command
- Shows list of job state
- job_id state command
- State is BG running or Stopped
- fg job_id command
- Change job_id job to FG job
- Stopped or BG ? FG
- bg job_id command change a job to BG
- Change job_id job to BG job
- Stopped ? BG
- - Dont use exec().
- - If job_id is not specified, its up to you.
Make it work like bash
10SIGINT, 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 jobs state to Stopped
- send SIGTSTP to FG job to stop
-
Use kill (pid, sig) to send signal!!
11SIGCHLD 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!!
12Hints
- waitpid(pid, status, option)
- If pid-1, wait for any child to change state
- If option WUNTRACEDWNOHANG
- Return value is pid of process when process is
terminated or stopped. 0 or -1 for other state
change - WNOHANG means doesnt wait
- Status can be checked with macros. - If
WIFEXITED(status) gt 0, completed - If
WIFSIGNALED(status) gt 0, terminated - If
WIFSTOPPED(status) gt 0, stopped - from http//database.sarang.net/study/glibc/24.htm
- and my try and error...T.T
13Hints
- Signal from group A goes to all process in group
A - Shells group and child group is same as default.
- But only shell should receive users signal.
- setpgid(pid, pgid)
- Set process group id
- If pid0, it is same as getpid()
- If pgid0, it is same as pid.(pid becomes a
group leader) - getpgrp(), getpgid(pid)
- get process group id
14Hints
- sigprocmask(mode, set, oldset)
- Block signals
- Example code
- sigset_t sigset, oldsetsigemptyset(sigset)
sigaddset(sigset, SIGCHLD) sigprocmask(SIG_BLOC
K, 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.