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

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

Embed Size (px)

Citation preview

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

Recitation 9: Section L (1:30pm - 2:20pm)

Monday, October 22, 2012

Processes, Signals and Shell LabSiddharth Dhulipalla

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

Outline

✤ Midterm Overview

✤ Processes

✤ Signals

✤ Shell Lab Overview

✤ Cheating Policy Reminder

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

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

Processes

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

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

Processes

✤ Four basic process control functions

✤ fork()

✤ exec()

✤ exit()

✤ wait()

✤ Standard on all Unix systems

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

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla
Page 9: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

Signals

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

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

Shell Lab

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

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

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: Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla

Questions?