Recitation 9: Processes, Signals, TSHLab

Slides:



Advertisements
Similar presentations
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
15-213, Fall 06 Outline Shell Lab Processes Signals.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
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)
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.
CSE 451 Section 4 Project 2 Design Considerations.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
CS162B: Forking Jacob T. Chan. Fork  Forks are:  Implement with two or more prongs that is used for taking up or digging  Division into branches or.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
Creating and Executing Processes
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Computer Studies (AL) Operating System Process Management - Process.
System calls for Process management
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
Operating Systems Process Creation
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
OPERATING SYSTEMS 3 - PROCESSES PIETER HARTEL 1. Principle of concurrency - giving a process the illusion that it owns the whole machine  A process has:
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
CSCI 4061 Recitation 2 1.
Error handling I/O Man pages
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Section 8: Processes What is a process Creating processes Fork-Exec
Unix Process Management
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
CGS 3763 Operating Systems Concepts Spring 2013
Recitation 9: Tshlab + VM
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
Recitation 14: Proxy Lab Part 2
EECE.4810/EECE.5730 Operating Systems
Processes in Unix, Linux, and Windows
Recitation 8 Processes, Signals, Tshlab
Chapter 3: Processes.
Recitation 9: Processes, Signals, TSHLab
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Unix System Calls and Posix Threads
Operation System Program 1
Process Programming Interface
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Section 2 Processes January 27rd, 2017 Taught by Joshua Don.
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

Recitation 9: Processes, Signals, TSHLab Instructor: TAs

Outline Cachelab Style Process Lifecycle Signal Handling

Style Grading Cachelab grades are available on Autolab Common mistakes Click ‘view source’ on your latest submission to see our feedback Common mistakes Descriptions at the top of your file and functions. NULL checking for malloc/calloc and fopen. ERROR CHECKING IS KEY IN TSHLAB! Writing everything in main function without helpers. Lack of comments in general. The labs are hard, don’t lose points after your hard work.

Shell Lab Due next week, Tuesday, March 27. Simulate a Linux-like shell with I/O redirection. PLEASE review the lecture slides and read the writeup before starting, and also while mid way through the assignment. A lot of important points mentioned in the writeup. Lot of style points can be saved solely by reading the writeup and following the instructions. Pay attention to race conditions!

Process “Lifecycle” We will review each of these phases today Fork() – Create a duplicate, a “child”, of the process Execve() – Replace the running program Exit() – End the running program Waitpid() – Wait for a child

Notes on Examples Full source code of all programs is available TAs may demo specific programs In the following examples, exit() is called We do this to be explicit about the program’s behavior Exit should generally be reserved for terminating on error Unless otherwise noted, all syscalls succeed Error checking code is omitted.

Processes are separate How many lines are printed? If pid is at address 0x7fff2bcc264c, what is printed? int main(int argc, char** argv) { pid_t pid; pid = fork(); printf(“%p - %d\n”, &pid, pid); exit(0); } Two lines printed, same address, different PID values, one pid is 0

Processes Change What does this program print? int main(int argc, char** argv) { char* args[3]; args[0] = “/bin/echo”; args[1] = “Hi 18213!”; args[2] = NULL; execv(args[0], args); printf(“Hi 15213!\n”); exit(0); }

On Error How should we handle malloc failing? const size_t HUGE = 1 * 1024 * 1024 * 1024; int main(int argc, char** argv) { char* buf = malloc(HUGE * HUGE); if (buf == NULL) fprintf(stderr, "Failure at %u\n", __LINE__); exit(1); } printf("Buf at %p\n", buf); free(buf); exit(0);

Exit values can convey information Two values are printed, describe their relation. int main(int argc, char** argv) { pid_t pid = fork(); if (pid == 0) { exit(getpid());} else int status = 0; waitpid(pid, &status, 0); printf(“0x%x exited with 0x%x\n”, pid, WEXITSTATUS(status)); } exit(0); Note, exit status is just the LSB.

Processes have ancestry Find the errors in this code, assume fork() and exit() are successful int main(int argc, char** argv) { int status = 0, ret = 0; pid_t pid = fork(); if (pid == 0) pid = fork(); exit(getpid()); } ret = waitpid(-1, &status, 0); printf(“Process %d exited with %d\n”, ret, status); exit(0); The parent process only has 1 child, but calls waitpid twice. The second call will return an error

Process Graphs How many different sequences can be printed? int main(int argc, char** argv) { int status; pid_t pid; if (fork() == 0) pid = fork(); printf(“Child: %d\n”, getpid()); if (pid == 0) {exit(0);} } pid = wait(&status); printf(“Parent: %d\n”, pid); exit(0);

Process Diagram ---print---exit --fork---wait---------------------------------print---exit ---fork---print---wait---print---exit ---print---exit

Process Graphs How many different sequences can be printed? int main(int argc, char** argv) { pid_t pid; char* tgt = “child”; pid = fork(); if (pid == 0) { pid = getppid(); // Get parent pid tgt = “parent”; } kill(pid, SIGKILL); printf(“Sent SIGKILL to %s:%d\n”, tgt, pid); exit(0);

Signals and Handling Signals can happen at any time Control when through blocking signals Signals also communicate that events have occurred What event(s) correspond to each signal? Write separate routines for receiving (i.e., signals)

Blocking Signals What value(s) does this code print? int counter = 0; void handler(int sig) {counter++;} int main(int argc, char** argv) { sigset_t mask, prev; int i; sigfillset(&mask); sigprocmask(SIG_BLOCK, &mask, &prev); signal(SIGCHLD, handler); for (i = 0; i < 10; i++) if (fork() == 0) {exit(0);} } sigprocmask(SIG_SETMASK, &prev, NULL); printf(“%d\n”, counter); return 0;

Proper signal handling For the previous code, how to handle the signals? We want to count child exits. We don’t want to count exits until all 10 children are created. Discuss Answer on Lecture 15, slide 56

Proper signal handling For the previous code, how to handle the signals? We want to count child exits. We don’t want to count exits until all 10 children are created. Print how many children have exited ahead of the parent

If you get stuck Read the writeup! Do manual unit testing before runtrace and sdriver! Post private questions on piazza! Read the man pages on the syscalls. Especially the error conditions What errors should terminate the shell? What errors should be reported?