UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.

Slides:



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

Zombie and orphan processes. Zombie process (from wikipedia) When a process ends, all of the memory and resources associated with it are deallocated.
15-213, Fall 06 Outline Shell Lab Processes Signals.
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.
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.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
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.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Processes, Signals, I/O, Shell lab
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Advanced Programming in the UNIX Environment Hop Lee.
Process Description and Control A process is sometimes called a task, it is a program in execution.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP 229, 346, 444, 5201 Revision 1.3.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Processes CS Intoduction to Operating Systems.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
Operating Systems Chapter 2
Creating and Executing Processes
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.
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.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
Zombie and orphan processes. Zombie process (from wikipedia) When a process ends, all of the memory and resources associated with it are deallocated so.
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)
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
2.1 Processes  process = abstraction of a running program  multiprogramming = CPU switches from running program to running program  pseudoparallelism.
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.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
G.Jyostna.
Using Processes.
Unix Process Management
Processes in Unix, Linux, and Windows
UNIX PROCESSES.
System Structure and Process Model
System Structure and Process Model
Processes in Unix, Linux, and Windows
System Structure B. Ramamurthy.
Processes in Unix, Linux, and Windows
2.1 Processes process = abstraction of a running program
System Structure and Process Model
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Lecture 6: Multiprogramming and Context Switching
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
Process Description and Control in Unix
Process Description and Control in Unix
System Programming: Process Management
Presentation transcript:

UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007

Process: A Context for Computation A process is largely defined by: Its CPU state (register values). Its address space (memory contents). Its environment (as reflected in operating system tables).

Process layout

Process Creation Bach 7.1 System call: –Child - 0. –Parent - PID of the child. System call algorithm: –Allocate a slot in process table. –Allocate PID. –Create a logical copy of the parent context. –Return values. #include pid_t fork(void);

Fork Example if ( (pid = fork()) < 0 ) error If (pid == 0) { code for child } else { code for parent }

Before calling fork() void f (int x) { int z; fork(); } void g() { char a; int i; f (42); } int main() { g(); return 0; } RET addr 1 RET addr 2 Before fork() RET addr 1 a i 42 RET addr 2 z SP Unsued Stack Stack for Boot Process

After calling fork() void f (int x) { int z; fork(); } void g() { char a; int i; f (42); } int main () { g(); return 0; } RET addr 1 RET addr 2 Location 3 RET addr 1 a i 42 RET addr 2 z SP Stack for new Process Child SP RET addr 1 a i 42 RET addr 2 z Stack for Boot Process

Example: Race Conditions #include int main() { pid_t pid; if ((pid = fork()) < 0 ) exit(1); //error if(pid != 0) { for(int i = 0; i < 100; i++) cout<<"Parent process "<< i <<endl; } else { for(int i = 100; i < 200; i++) cout <<"Child process "<< i << endl; } return 0; }

Process Termination Bach 7.3 Normal Termination: –Executing a return from the main function. –Calling the exit function. (ANSI C) –Calling the _exit function. (Sys call) Abnormal Termination: –When a process receives certain signals.

Normal Process Termination System call: –Status: IPC. –NEVER returns. System call algorithm: –Mask Signals. –Close open files. –Release memory. –Save the process exit status. –Set the process state to ZOMBIE. #include void exit (int status);

Awaiting Process Termination Bach 7.4 #include pid_t wait(int *status); System call: –Returns the PID of a zombie child -1 when no children exist. –Status is the exit status of the child process. –Wait can block the caller until a child process terminates. System call algorithm: –Search for a zombie child of the process. –Extract PID and status of the zombie child. –Release the process table slot.

Orphans and Zombies When a child exits when its parent is not currently executing a wait(), a zombie emerges. –A zombie is not really a process as it has terminated but the system retains an entry in the process table. –A zombie is put to rest when the parent finally executes a wait(). A child process whose parent has terminated is referred to as orphan. When a parent terminates, orphans and zombies are adopted by the init process ( prosess-id:0 ) of the system.

Invoking other programs Bach 7.5 The exec invokes another program, overlaying the memory space with a copy executable file. The contents of the user-level context is accessible through exec parameters. exec(filename, argv, envp);

notable exec properties an exec call transforms the calling process by loading a new program in its memory space. the exec does not create a new sub- process. unlike the fork there is no return from a successful exec.

System call algorithm Determine the file properties –Determine If the file is an executable. –Determine if the user has permissions. –Determine the file’s layout. Copy exec arguments to system space. Detach old memory regions. Allocate new regions. Copy exec arguments.

Exec example If((pid = fork()) < 0) error; if (pid== 0 ){ exec( arguments ); exit(-1); } // parent continues here

fork/wait/execv Example #include using namespace std; int main() { int x = 42; pid_t pid; if ((pid = fork()) < 0 ) exit(1); if(pid != 0) { int status; cout << "Parent process. x=" << x << endl; wait (&status); } else { cout << "Child process. x=" << x << endl; char* args[] = {"ls", NULL}; execv ("/bin/ls", args); cout << "Never reached" << endl; } return 0; }

The Shell Bach 7.8 The shell read a command line from STDIN and execute. The shell has to main commands: –Internal commands. (cd) –External commands. (cp) External commands may run foreground/background.

Signals Bach 7.2 Signals are notifications sent to a process in order to notify the process of events. –Kernel. –Processes (system call kill) The kernel send a signal by setting a bit in the field of the process table entry. A process can remember different types of signals, but not the number of signals from each type.

Sending Signals Using the keyboard: –Ctrl-C: Causes the system to send an INT signal ( SIGINT ) to the running process. Using shell kill command: –The kill command has the following format: kill [options] pid

Handling Signals The kernel handles signals in the context of the process that receives them so process must run to handle signals. There are three case for handling signals: –The process exits. (default action) –The process ignores. –The process execute particular function. oldfun = signal(signum,newfun);

Non-Catchable Signals Most signals may be caught by the process, but there are a few signals that the process cannot catch, and cause the process to terminate. –For example: KILL and STOP. If you install no signal handlers of your own the runtime environment sets up a set of default signal handlers. –For example: The default signal handler for the TERM signal calls the exit(). The default handler for the ABRT is to dump the process's memory image into a file, and then exit.

Summary 1.Each signal may have a signal handler, which is a function that gets called when the process receives that signal. 2.When the signal is sent to the process, the operating system stops the execution of the process, and "forces" it to call the signal handler function. 3.When that signal handler function returns, the process continues execution from wherever it happened to be before the signal was received, as if this interruption never occurred.

Signal Handlers - Example #include void catch_int(int sig_num) { signal(SIGINT, catch_int); //install again! printf("Don't do that\n"); fflush(stdout); } int main(int argc, char* argv[]) { signal(SIGINT, catch_int); for ( ;; ) pause();//wait till receives a signal. }

Avoiding Signal Races - Masking Signals The occurrence of a second signal while the signal handler function executes. –The second signal can be of different type than the one being handled, or even of the same type. The system also contains some features that will allow us to block signals from being processed. –A global context which affects all signal handlers, or a per-signal type context.