processes, signals and shell lab

25
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

Upload: greta

Post on 25-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Processes, Signals and Shell Lab. Siddharth Dhulipalla. Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012. Outline. Midterm Overview Processes Signals Shell Lab Overview Cheating Policy Reminder. Midterm Overview. Average: 54/71 Pick up from ECE Course Hub in HH 1112 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Processes, Signals and Shell Lab

Recitation 9: Section L (1:30pm - 2:20pm)Monday, October 22, 2012

Processes, Signals and Shell LabSiddharth Dhulipalla

Page 2: Processes, Signals and Shell Lab

Outline

✤ Midterm Overview

✤ Processes

✤ Signals

✤ Shell Lab Overview

✤ Cheating Policy Reminder

Page 3: Processes, Signals and Shell Lab

Midterm Overview

✤ Average: 54/71

✤ Pick up from ECE Course Hub in HH 1112

✤ Alternate solution to assembly question found

✤ Regrade requests accepted until Wednesday

✤ Question?

Page 4: Processes, Signals and Shell Lab

Processes

Page 5: Processes, Signals and Shell Lab

Processes

✤ What is a process?

✤ An instance of a program in execution

✤ A Great Idea in Computer Science

✤ Ubiquitous on multitasking systems

✤ Fundamental abstraction provided by the OS

✤ Single thread of execution (control flow)

✤ Full, private memory space and registers

✤ Various other state (file descriptors, etc.)

Page 6: Processes, Signals and Shell Lab

Processes

✤ Four basic process control functions

✤ fork()

✤ exec()

✤ exit()

✤ wait()

✤ Standard on all Unix systems

Page 7: Processes, Signals and Shell Lab

fork()✤ Creates a process (more like mitosis than birth)

✤ Parent and child are exactly alike

✤ Except for return value (%eax)

✤ Equal, but private:

✤ Threads of execution

✤ Registers

✤ Memory

✤ File descriptors (note: files, however, are shared)

Page 8: Processes, Signals and Shell Lab
Page 9: Processes, Signals and Shell Lab

exec()✤ Replaces process

✤ No processes created

✤ New process (mostly) unaware of old state

✤ How programs are run✤ Replace memory image with new program

✤ Set up stack with arguments

✤ Start execution at the entry point (main)

✤ Actually a family of functions✤ man 3 exec

Page 10: Processes, Signals and Shell Lab

exec() Example

Code

int rc = fork(); if (rc == 0) { printf(“child\n”); execl(“/bin/echo”, “/bin/echo”, “sup!”, NULL); exit(EXIT_FAILURE); }printf(“parent\n”);

Output

child sup! parent

Any other possibilities?

Page 11: Processes, Signals and Shell Lab

exit()

✤ Terminates a process

✤ OS frees resources used by the process

✤ Tiny leftover data

✤ Zombie state

✤ Exit status for parent

✤ Must be freed (reaped)

Page 12: Processes, Signals and Shell Lab

wait()

✤ Blocks until child process changes state

✤ Reaps child if it terminated

✤ Frees all remaining resources and gets exit status

✤ Can be used for synchronization

✤ Lots of details

✤ man 2 wait

Page 13: Processes, Signals and Shell Lab

wait() Example

Code

int rc = fork(); if (rc == 0) { printf(“child\n”); execl(“/bin/echo”, “/bin/echo”, “sup!”, NULL); exit(EXIT_FAILURE); }int status; waitpid(rc, &status, 0); printf(“child status %d\n”, WEXITSTATUS(status));

Output

child sup! child status 0

Any other possibilities?

Take a look at: http://csapp.cs.cmu.edu/public/waside/waside-graphs.pdf

Page 14: Processes, Signals and Shell Lab

States

✤ Running

✤ Executing instructions on the CPU

✤ Number bounded by the number of CPU cores

✤ Runnable

✤ Waiting to be running

✤ Blocked

✤ Waiting for an event

✤ Not runnable

✤ Zombie

✤ Terminated but not yet reaped

Page 15: Processes, Signals and Shell Lab

Signals

Page 16: Processes, Signals and Shell Lab

Signals✤ Primitive form of interprocess communication

✤ Notify process of an event

✤ Asynchronous with normal execution

✤ Several types✤ man 7 signal

✤ Sent in various ways✤ ^C, ^Z, ^\

✤ kill command

✤ kill system call

Page 17: Processes, Signals and Shell Lab

Signals✤ What to do when receiving a signal

✤ Ignore

✤ Catch and run signal handler

✤ Terminate

✤ man sigaction

✤ Blocking✤ man sigprocmask

✤ Can’t modify behavior of SIGKILL and SIGSTOP

✤ Signals don’t queue

Page 18: Processes, Signals and Shell Lab

Signals

✤ Signal Handlers

✤ Can be installed to run when a signal is received

✤ Type is “void (*sa_handler)(int)”

✤ Separate control flow in the same process

✤ Resumes control flow on return

✤ Signal handlers can be called anytime

Page 19: Processes, Signals and Shell Lab

Signals

✤ Interesting signals

✤ SIGKILL – force kill a process

✤ SIGINT – kill a process

✤ SIGSTOP and SIGCONT – suspend and resume a process

✤ SIGCHLD – child changed state

✤ SIGSEGV – everyone knows this

Page 20: Processes, Signals and Shell Lab

Shell Lab

Page 21: Processes, Signals and Shell Lab

Shell Lab

✤ Write a not-so-basic shell✤ Fork and execute a new program

✤ Wait for foreground jobs

✤ Support background jobs

✤ React to changes in child state

✤ Input and output redirection

✤ ls > ls_out and wc < filename

✤ Many different designs✤ Some much better than others

Page 22: Processes, Signals and Shell Lab

Shell Lab Tips✤ Read the code we’ve given you.

✤ There is a lot of stuff you don’t need to write for yourself.

✤ It’s a good example of the kind of code we expect from you

✤ If you find yourself using sleep() as a way of avoiding race conditions, you are doing it VERY wrong. We will dock performance points for this.

✤ You should only use it for performance to avoid your code having to execute useless instructions. Your code should still work if we remove calls to sleep.

✤ Read the spec carefully and start early!

Page 23: Processes, Signals and Shell Lab

Shell Lab Hazards

✤ Race Conditions

✤ Hard to debug so start early

✤ Reaping zombies

✤ Again, race conditions

✤ Fiddle with signals

✤ Waiting for foreground job

✤ One of the only places where sleep is acceptable (though you don’t NEED it)

Page 24: Processes, Signals and Shell Lab

Cheating Policy Reminder

✤ Don’t do it.

✤ Two students caught by Moss have received R’s and could potentially be kicked out of their program

✤ Two more cases are currently looked into

✤ Never worth it.

Page 25: Processes, Signals and Shell Lab

Questions?