Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Operating Systems Lecture 7.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
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.
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.
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 Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Processes, Signals, I/O, Shell lab
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
System Structuring with Threads. Example: A Transcoding Web Proxy Appliance “Proxy” Interposed between Web (HTTP) clients and servers. Masquerade as (represent)
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
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.
The Classical OS Model in Unix. Nachos Exec/Exit/Join Example Exec parentExec child Join Exit SpaceID pid = Exec(“myprogram”, 0); Create a new process.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
Operating Systems Chapter 2
Outline for Today Announcements –Keep those group s coming… –What I learned from Who’s who forms Objective of the lecture –Abstractions provided by.
Creating and Executing Processes
1 Outline Objective: –Get specific with UNIX system calls –Continue with message-passing style interprocess communication (IPC) – including UNIX example.
Lecture Thread: Unix process-oriented system calls
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
Chapter 11 Process Management
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
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 *
Signals. Introduction r A signal is a mechanism for notifying a process that an event has occurred. m When a signal is sent to a process is normal execution.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
© 숙대 창병모 1 제 10 장 신호 (Signal). © 숙대 창병모 2 Contents 1. Signal Concepts 2. signal() 3. Interrupted System Calls 4. kill() /raise() 5. alarm() pause() 6.
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.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
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.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
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.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
1 Outline for Today Objective: –Continue with message-passing style interprocess communication (IPC) –Get specific with UNIX system calls Administrative.
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
Protection of System Resources
Unix Process Management
Processes in Unix, Linux, and Windows
UNIX PROCESSES.
Exceptional Control Flow Part II
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Process Creation Process Termination
Tutorial 3 Tutorial 3.
제11장 프로세스 관리.
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Processes in Unix, Linux, and Windows
Process Description and Control in Unix
Process Description and Control in Unix
System Programming: Process Management
Presentation transcript:

Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone of the parent (Note: this is NOT the same as Nachos fork which is internal thread creation). parent program runs in child process to set it up for exec (again, not a Nachos exec which is part of its API, but has different semantics) child can exit, parent can wait for child to do so. Rich facilities for controlling processes by asynchronous signals. notification of internal and/or external events to processes or groups the look, feel, and power of interrupts and exceptions default actions: stop process, kill process, dump core, no effect user-level handlers

Unix Process Control The fork syscall returns a zero to the child and the child process ID to the parent. Fork creates an exact copy of the parent process. int pid; int status = 0; if (pid = fork()) { /* parent */ ….. pid = wait(&status); } else { /* child */ exit(status); } Parent uses wait to sleep until the child exits; wait returns child pid and status. Wait variants allow wait on a specific child, or notification of stops and other signals. Child process passes status back to parent on exit, to report success/failure.

Child Discipline After a fork, the parent program (not process) has complete control over the behavior of its child process. The child inherits its execution environment from the parent...but the parent program can change it. sets bindings of file descriptors with open, close, dup pipe sets up data channels between processes Parent program may cause the child to execute a different program, by calling exec* in the child context.

Fork/Exit/Wait Example fork parent fork child Child process starts as clone of parent: increment refcounts on shared resources. OS resources Parent and child execute independently: memory states and resources may diverge. wait exit On exit, release memory and decrement refcounts on shared resources. Consider what happens if the child exits first, or if the parent exits first, to understand why the reference counts are necessary. Parent sleeps in wait until child stops or exits. “join” Child enters zombie state: process is dead and most resources are released, but process descriptor remains until parent reaps exit status via wait.

Exec, Execve, etc. Children should have lives of their own. Exec* “boots” the child with a different executable image. parent program makes exec* syscall (in forked child context) to run a program in a new child process exec* overlays child process with a new executable image restarts in user mode at predetermined entry point (e.g., crt0) no return to parent program (it’s gone) arguments and environment variables passed in memory file descriptors etc. are unchanged The exec variants are library calls that use the sys call execve

Fork/Exec/Exit/Wait Example int pid = fork(); Create a new process that is a clone of its parent. exec*(“program” [, argvp, envp]); Overlay the calling process virtual memory with a new program, and transfer control to it. exit(status); Exit with status, destroying the process. int pid = wait*(&status); Wait for exit (or other status change) of a child. fork parent fork child initialize child context exec Consider what happens if the child exits first, or if the parent exits first, to understand why the reference counts are necessary. wait exit

Join Scenarios Several cases must be considered for join (e.g., exit/wait). What if the child exits before the parent does the wait? “Zombie” process object holds child status and stats. What if the parent continues to run but never joins? Danger of filling up memory with zombie processes? Parent might have specified it was not going to wait or that it would ignore its child’s exit. Child status can be discarded. What if the parent exits before the child? Orphans become children of init (process 1). What if the parent can’t afford to get “stuck” on a join? Asynchronous notification (we’ll see an example later).

Immediate Notification: Upcalls

Unix Signals Signals notify processes of internal or external events. the Unix software equivalent of interrupts/exceptions only way to do something to a process “from the outside” Unix systems define a small set of signal types Examples of signal generation: keyboard ctrl-c and ctrl-z signal the foreground process synchronous fault notifications, syscall errors asynchronous notifications from other processes via kill IPC events (SIGPIPE, SIGCHLD) alarm notifications With the exception of sigchld, signals are not stacked up and only one will be processed

Process Handling of Signals 1. Each signal type has a system-defined default action. abort and dump core (SIGSEGV, SIGBUS, etc.) ignore, stop, exit, continue 2. A process may choose to block (inhibit) or ignore some signal types. 3. The process may choose to catch some signal types by specifying a (user mode) handler procedure. specify alternate signal stack for handler to run on system passes interrupted context to handler handler may munge and/or return to interrupted context

Predefined Signals (a Sampler) Name Default action Description SIGINT Quit Interrupt SIGILL Dump Illegal instruction SIGKILL Kill (can not be caught, blocked, or ignored SIGSEGV Out of range addr SIGALRM Alarm clock SIGCHLD Ignore Child status change SIGTERM Sw termination sent by kill

User’s View of Signals Instructs kernel to send SIGALRM in int alarmflag=0; alarmHandler () { printf(“An alarm clock signal was received\n”); alarmflag = 1; } main() { signal (SIGALRM, alarmHandler); alarm(3); printf(“Alarm has been set\n”); while (!alarmflag) pause (); printf(“Back from alarm signal handler\n”); Instructs kernel to send SIGALRM in 3 seconds Sets up signal handler Pause returns upon signal Suspends caller until signal

User’s View of Signals II main() { int (*oldHandler) (); printf (“I can be control-c’ed\n”); sleep (3); oldHandler = signal (SIGINT, SIG_IGN); printf(“I’m protected from control-c\n”); sleep(3); signal (SIGINT, oldHandler); printf(“Back to normal\n”); sleep(3); printf(“bye\n”); }

Yet Another User’s View main(argc, argv) int argc; char* argv[]; { int pid; signal (SIGCHLD,childhandler); pid = fork (); if (pid == 0) /*child*/ { execvp (argv[2], &argv[2]); } else {sleep (5); printf(“child too slow\n”); kill (pid, SIGINT); } childhandler() { int childPid, childStatus; childPid = wait (&childStatus); printf(“child done in time\n”); exit; } Use this one to print out lecture notes pre-class. Kill explicitly send a signal to another process. Owner of the processes matters, if pid is 0, send to entire process group. What does this do?

Yet Another User’s View main(argc, argv) int argc; char* argv[]; { int pid; signal (SIGCHLD,childhandler); pid = fork (); if (pid == 0) /*child*/ { execvp (argv[2], &argv[2]); } else {sleep (5); printf(“child too slow\n”); kill (pid, SIGINT); } childhandler() { int childPid, childStatus; childPid = wait (&childStatus); printf(“child done in time\n”); exit; } Collects status SIGCHLD sent by child on termination; if SIG_IGN, dezombie Kill explicitly send a signal to another process. Owner of the processes matters, if pid is 0, send to entire process group. What does this do?

Naming Destinations for Messages: Ports replyport-> replyport->receive(response);

Advantages of Ports

Port Issues

Examples of Ports in Real Systems

Sockets for Client-Server Message Passing 1. Create a named socket syscalls: sfd = socket(…) bind (sfd, ptr,…) 2. Listen for clients listen(sfd, …) 4. Connection made and continue listening cfd=accept(sfd, …) 5. Exchange data write(cfd, …) 6. Done: close(cfd); close(sfd); Client 3. Create unnamed socket & ask for connection syscalls: cfd=socket(…) err=connect(cfd, ptr, …) 5. Exchange data read(cfd, …) 6. Done: close(cfd); name P 450 Glass (at work) Box would probably be forked off into separate thread. name

Notification of Pending Messages

Polling: Select