OS – Process Creation and Destruction

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

CSCC69: Operating Systems
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
CSE 451 Section 4 Project 2 Design Considerations.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
Creating and Executing Processes
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
More on UART Interrupts; System Calls Reference on Interrupt Identification Register(IIR) slide 17 of
Operating Systems CSE 411 CPU Management Sept Lecture 9 Instructor: Bhuvan Urgaonkar.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
System calls for Process management
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Operating Systems Recitation 4, April th, 2002 Signals.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
Process Management Azzam Mourad COEN 346.
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
ACCESS CONTROL. Components of a Process  Address space  Set of data structures within the kernel - process’s address space map - current status - execution.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
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.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Error handling I/O Man pages
UNIX signals.
Exceptional Control Flow
G.Jyostna.
User-Written Functions
Precept 14 : Ish dup() & Signal Handling
Functions and the Stack
Unix Process Management
Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command Some by invocation from other running.
Processes A process is a running program.
UNIX PROCESSES.
Exceptional Control Flow: System Calls, Page Faults etc.
Lecture 5: Process Creation
CSC Advanced Unix Programming, Fall 2015
Operating Systems Lecture 12.
Unix System Calls and Posix Threads
OS – Process Creation and Destruction
CS510 Operating System Foundations
Processes Creation and Threads
Process Description and Control in Unix
Signals.
Process Description and Control in Unix
Presentation transcript:

OS – Process Creation and Destruction CS/COE 0449 (term 2174) Jarrett Billingsley

Class announcements We can talk about project 2 now :D If that’s even on your mind, still. About project 3… gdb is still your friend ;D Stop avoiding using thoth. You have to use it. Don’t copy and paste. Don’t do it. Really. Stop. Use functions and variables. Oh yeah, exam on Thursday I guesssssssss…………… 3/14/2017 CS/COE 0449 term 2174

POSIX Process Creation 3/14/2017 CS/COE 0449 term 2174

Mitosis Code Memory Code Memory fork() In the beginning… When a UNIX/Linux system first starts, the only process running is init. If you do pstree, it’s the root! The way every process starts in POSIX is by splitting off from another process. The POSIX API function is called fork(). The original process is the parent, and the newly-forked process is the child. Just like when cells split, both processes are completely identical! Same code, same data, same everything. Even where it’s executing is the same… Parent Child Code Memory Code Memory fork() 3/14/2017 CS/COE 0449 term 2174

fork() is weird. Parent Child int cpid = fork(); if(cpid == 0) { Let’s look at an example of fork(). We can use strace -f to watch the syscalls from both processes. When you do a fork()… Parent Child int cpid = fork(); if(cpid == 0) { // child } else { // parent } int cpid = fork(); if(cpid == 0) { // child } else { // parent } 3/14/2017 CS/COE 0449 term 2174

Process identifiers (pids) Every process has a unique numeric identifier, called its pid. getpid() gets the pid of the current process. getppid() gets the pid of the current process’s parent. fork() returns the child’s pid in the parent. You use pids as arguments to process-management syscalls. For example, the waitpid() function waits for a child process to terminate (exit). We’ll see that being used shortly! 3/14/2017 CS/COE 0449 term 2174

WHAT IF YOU CALLED FORK() IN A LOOP DON’T, lol This is a forkbomb – a process which endlessly spawns new processes, bringing the system to a halt. It’s usually accidental, but can be used as a form of DoS (denial-of-service) attack. thoth has protection against this :^) but still don’t do it :^) You could get your account into a nasty state where Dr. Misurda has to reset it. So DON’T. 3/14/2017 CS/COE 0449 term 2174

But… what if execvp() fails? Changing identities Making endless clones of the same process isn’t that useful. We transform a child process with one of the exec*() functions. Parent Child But… what if execvp() fails? bash Memory bash Memory ls Memory execvp() fork() 3/14/2017 CS/COE 0449 term 2174

Error Handling in C time for a tangent

What does it meeeeeean If you look at the manpages for execvp, it says this: If any of the exec() functions returns, an error occurred. The return value is -1, and errno will be set to indicate the error. You’ll see similar descriptions for fork, open, close, read, write… A return value of -1 Something called “errno” In Java, if you try to open a file that doesn’t exist, what happens? FileNotFoundException. But C has no exceptions. 3/14/2017 CS/COE 0449 term 2174

In-band signaling The C Way™ to report errors is to return some impossible value. For many POSIX functions, this is a negative number. In addition, syscalls (and some C library calls!) can put a value in a global variable (!) called errno. It’s short for “error number.” The Proper Way to Handle Errors™ therefore looks like: int fd = open(“myfile.txt”, O_RDONLY); if(fd < 0) { int error = errno; // save errno if(error == EEXIST) // file already exists... else if(error == ... // etc } 3/14/2017 CS/COE 0449 term 2174

“You mean we have to do that on every syscall?” YEP. I mean, unless you want a crashy, insecure program ¯\_(ツ)_/¯ Be sure to read the errors that a syscall can produce, then: handle the ones you care about; and fail gracefully for the errors you don’t handle. The simplest method is “cry and exit.” Use strerror or perror: int fd = open(“myfile.txt”, O_RDONLY); if(fd < 0) { perror(“couldn’t open myfile.txt”); exit(1); } 3/14/2017 CS/COE 0449 term 2174

POSIX Process Destruction back on track

Apoptosis, necrosis, and lysis After a child process finishes exec*(), a few things could happen: Both processes could live long, happy lives, and run forever. The child could exit; or the parent could exit; or both could. When I say “exit,” I mean one of the following possibilities: The process exited normally (apoptosis) The process crashed (necrosis) The process was terminated by another process (lysis) The parent can find out how the child exited through the status parameter of waitpid. The manpages are your frieeeeeeends Let’s see an example of a child process exiting two ways… What are these mentions of “signals”? 3/14/2017 CS/COE 0449 term 2174

POSIX Signals

Polling vs. Asynchronous Polling means “asking over and over if something happened.” Asynchronous means “being notified when something happens.” Polling Asynchronous notification 3/14/2017 CS/COE 0449 term 2174

AAAA SIGINT!!! What’s a signal? normal_code(); nothing_unusual(); A signal is an asynchronous way the OS notifies your program of certain special events. This happens outside the normal flow of execution. In this way, signals are kind of like exceptions in Java. When a signal is sent, the program runs a signal handler. AAAA SIGINT!!! normal_code(); nothing_unusual(); whatever(); if(blah == blah) printf(“banana”); void handler(int sig) { printf(“O NO”); } 3/14/2017 CS/COE 0449 term 2174

Good signals to know All the POSIX signals have names that start with SIG. Many of the important ones have to do with process termination. SIGSEGV is your friend, the segvfault! SIGBUS is another kind of illegal memory access. SIGILL is when an illegal instruction is executed. SIGFPE is when a floating-point (or integer…?) error happens. SIGABRT is caused by the abort() function. SIGINT is the ctrl+C interrupt. Usually, it stops the program. SIGTERM: “hey, time to quit, just letting you know” SIGKILL: “nothin personnell… kid…” This is serious business. The process is being terminated, NOW. Signals aren’t all bad, though… 3/14/2017 CS/COE 0449 term 2174

Handling signals You can catch signals by setting up a signal handler of your own. You do that with the signal() function. The first argument is the signal to handle. The second is a function pointer to a handler, or: SIG_IGN to ignore the signal; SIG_DFL to perform the default action. It works kinda weirdly. When your handler is called, it usually unsets the handler, so you have to re-set it in the handler to get the signal again. But not for SIGALRM??? idk 3/14/2017 CS/COE 0449 term 2174

Sending and receiving signals You can send signals from your program with the kill() function. You give it a pid and a signal. You can send signals from the shell with the kill command. A common useful invocation is: kill -9 pid This sends the SIGKILL signal to a process. 3/14/2017 CS/COE 0449 term 2174