Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla."— Presentation transcript:

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

2 Outline ✤ Midterm Overview ✤ Processes ✤ Signals ✤ Shell Lab Overview ✤ Cheating Policy Reminder

3 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?

4 Processes

5 ✤ 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.)

6 Processes ✤ Four basic process control functions ✤ fork() ✤ exec() ✤ exit() ✤ wait() ✤ Standard on all Unix systems

7 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)

8

9 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

10 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?

11 exit() ✤ Terminates a process ✤ OS frees resources used by the process ✤ Tiny leftover data ✤ Zombie state ✤ Exit status for parent ✤ Must be freed (reaped)

12 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

13 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 http://csapp.cs.cmu.edu/public/waside/waside-graphs.pdf

14 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

15 Signals

16 ✤ 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

17 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

18 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

19 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

20 Shell Lab

21 ✤ 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

22 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!

23 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)

24 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.

25 Questions?


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

Similar presentations


Ads by Google