System-level programming II: Processes Feb 24, 2000 Topics User-level view of processes Exceptions Context switches Higher level control flow mechanisms.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007.
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes /18-243: Introduction to Computer Systems 13 th Lecture, June 15, 2011 Instructors:
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Fabián E. Bustamante, Spring 2007 Exceptional Control Flow II Today Process Hierarchy Shells Signals Nonlocal jumps Next time Virtual memory.
University of Washington Today Finish up cache-aware programming example Processes  So we can move on to virtual memory 1.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
Exceptional Control Flow Part I September 22, 2008 Topics Exceptions Process context switches Creating and destroying processes class11.ppt ,
Exceptional Control Flow Part II Oct. 22, 2002 Topics Process Hierarchy Shells Signals Nonlocal jumps class17.ppt “The course that gives CMU its.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
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.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes class14.ppt CS 123.
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Process Description and Control A process is sometimes called a task, it is a program in execution.
Exceptional Control Flow Part II Topics Process Hierarchy Signals CS213.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
1 Program and OS interactions: Exceptions and Processes Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Fabián E. Bustamante, Spring 2007 Exceptional Control Flow Part I Today Exceptions Process context switches Creating and destroying processes Next time.
CS 201 Exceptions and System calls Part II. – 2 – Shell Programs A shell is an application program that runs programs on behalf of the user. sh – Original.
Introduction to Processes CS Intoduction to Operating Systems.
Exceptional Control Flow Part I March 4, 2004 Topics Exceptions Process context switches Creating and destroying processes class16.ppt “The course.
Operating Systems Chapter 2
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
CSC 2405 Computer Systems II Exceptions Mini-Lecture Traps & Interrupts.
Program Translation and Execution II: Processes Oct 1, 1998 Topics User-level view of processes Implementation of processes setjmp/longjmp class12.ppt.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes class16.ppt.
Exceptional Control Flow Topics Exceptions except1.ppt CS 105 “Tour of the Black Holes of Computing”
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
University of Washington Exceptional Control Flow The Hardware/Software Interface CSE351 Winter 2013.
Exceptional Control Flow I March 12, 2002
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
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.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Exceptional Control Flow II Oct 23, 2001 Topics Exceptions Process context switches class17.ppt “The course that gives CMU its Zip!”
Overview of today’s lecture Re-view of the previous lecture Process management models and state machines (cont’d from the previous lecture) What is in.
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Exceptional Control Flow
Exceptional Control Flow & Processes
Exceptional Control Flow
Exceptional Control Flow
Exceptional Control Flow
Exceptional Control Flow Oct 24, 2000
Overview of today’s lecture
Exceptional Control Flow Part I
Exceptional Control Flow
Exceptional Control Flow: System Calls, Page Faults etc.
Structure of Processes
More examples How many processes does this piece of code create?
Processes in Unix, Linux, and Windows
Exceptional Control Flow Part I Mar. 11, 2003
Exceptional Control Flow Part I
Exceptions Control Flow
CS 105 “Tour of the Black Holes of Computing!”
Processes Prof. Ikjun Yeom TA – Mugyo
CS201 - Lecture 17 Exceptions
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Lecture 6: Multiprogramming and Context Switching
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
CS510 Operating System Foundations
Exceptional Control Flow & Processes October 2, 2008
Presentation transcript:

System-level programming II: Processes Feb 24, 2000 Topics User-level view of processes Exceptions Context switches Higher level control flow mechanisms class12.ppt “The course that gives CMU its Zip!”

CS 213 S’00– 2 – class12.ppt Control flow inst 1 inst 2 inst 3 … inst n From startup to shutdown, a CPU simply reads and executes a sequence of instructions, one at a time. this sequence is the system’s physical control flow (or flow of control). Physical control flow Time

CS 213 S’00– 3 – class12.ppt Processes Modern operating systems partition the physical control flow into a collection of logical control flows called processes (task or jobs). Each process is an instance of a running program. one process for each program that runs during the system’s lifetime. Time Process AProcess BProcess C

CS 213 S’00– 4 – class12.ppt Concurrent processes Two processes run concurrently (are concurrent) if their flows overlap in time. Otherwise, they are sequential. Examples: Concurrent: A & B, A&C Sequential: B & C Time Process AProcess BProcess C

CS 213 S’00– 5 – class12.ppt User view of concurrent processes Control flows for concurrent processes are physically disjoint in time. However, we can think of concurrent processes are running in parallel with each other. Time Process AProcess BProcess C

CS 213 S’00– 6 – class12.ppt Separate address spaces The OS also provides each process with its own address space. A process must take explicit steps to share part of its address space with other processes. Provides the illusion that the process has its own memory. Process AProcess BProcess C MAMA MBMB MCMC

CS 213 S’00– 7 – class12.ppt Fork(): Creating new processes int fork(void) creates a new process (child process) that is identical to the calling process (parent process) returns 0 to the child process returns child’s pid to the parent process if (fork() == 0) { printf(“hello from child\n”); } else { printf(“hello from parent\n”); }

CS 213 S’00– 8 – class12.ppt Exit(): Destroying processes void exit(int status) exits a process atexit() registers functions to be executed upon exit void cleanup(void) { printf(“cleaning up\n”); } main() { atexit(cleanup); if (fork() == 0) { printf(“hello from child\n”); } else { printf(“hello from parent\n”); } exit(); }

CS 213 S’00– 9 – class12.ppt Wait(): Synchronizing with children int wait(int *child_status) suspends current process until one of its children terminates return value = the pid of the child process that terminated if child_status != NULL, then the object it points to will be set to a status indicating why the child process terminated main() { int child_status; if (fork() == 0) { printf(“hello from child\n”); } else { printf(“hello from parent\n”); wait(&child_status); printf(“child has terminated\n”); } exit(); }

CS 213 S’00– 10 – class12.ppt Exec(): Running new programs int execl(char *path, char *arg0, char *arg1, …, 0) loads and runs executable at path with args arg0, arg1, … –path is the complete path of an executable –arg0 becomes the name of the process »typically arg0 is either identical to path, or else it contains only the executable filename from path –“real” arguments to the executable start with arg1, etc. –list of args is terminated by a (char *)0 argument returns -1 if error, otherwise doesn’t return! main() { if (fork() == 0) { execl(“/usr/bin/cp”,“cp”,“foo”,“bar”,0); } wait(NULL); printf(“copy completed\n”); exit(); }

CS 213 S’00– 11 – class12.ppt Linux process hierarchy shell child grandchild init [1] [0] Daemon e.g. snmp

CS 213 S’00– 12 – class12.ppt Unix Startup: Step 1 init [1] [0] process 0: handcrafted kernel process process 1: user mode process fork() and exec(/sbin/init) 1. Pushing reset button loads the PC with the address of a small bootstrap program. 2. Bootstrap program loads the boot block (disk block 0). 3. Boot block program loads kernel (e.g., /vmunix ) 4. Boot block program passes control to kernel. 5. Kernel handcrafts the data structures for process 0.

CS 213 S’00– 13 – class12.ppt Unix Startup: Step 2 init [1] [0] forks a getty (get tty or get terminal) for the console getty Daemons e.g. ftpd, httpd /etc/inittab init forks new processes as per the /etc/inittab file

CS 213 S’00– 14 – class12.ppt Unix Startup: Step 3 init [1] [0] getty execs a login program login

CS 213 S’00– 15 – class12.ppt Unix Startup: Step 4 init [1] [0] login gets user’s login and password if OK, it execs a shell if not OK, it execs another getty tcsh

CS 213 S’00– 16 – class12.ppt Example: Loading and running programs from a shell /* read command line until EOF */ while (read(stdin, buffer, numchars)) { if ( ) background_process = TRUE; else background_process = FALSE; /* for commands not in the shell command language */ if (fork() == 0) { execl(cmd, cmd, 0) } if (!background_process) retpid = wait(&status); }

CS 213 S’00– 17 – class12.ppt Example: Concurrent network server void main() { master_sockfd = sl_passivesock(port); /* create master socket */ while (TRUE) { worker_sockfd = sl_acceptsock(master_sockfd); /* await request */ switch (fork()) { case 0: /* child closes its master and manipulates worker */ close(master_sockfd); /* code to read and write to/from worker socket goes here */ exit(0); default: /* parent closes its copy of worker and repeats */ close(worker_sockfd); case -1: /* error */ fprintf("fork error\n"); exit(0); }

CS 213 S’00– 18 – class12.ppt Implementing processes Processes are managed by a shared piece of OS code called the kernel the kernel is not a separate process, but rather runs as part of user process Question: How does the control flow change from one process to another? Process AProcess B user code kernel code user code kernel code user code Time context switch

CS 213 S’00– 19 – class12.ppt Altering the control flow So far in class, we’ve discussed two mechanisms for changing the control flow: jumps call and return using the stack discipline. These are insufficient for a useful system only instructions can change control flow. difficult for the CPU to react to internal or external events. –data arriving from a disk or a network adapter. –divide by zero –user hitting ctl-c no way for the OS to preempt one process for another. –processes must explicitly pass control to the OS. »cooperative multitasking a la Windows 3.1.

CS 213 S’00– 20 – class12.ppt Advanced mechanisms for altering control flow Low level mechanism: exceptions –change in control flow in response to an internal or external event implemented as a combination of both hardware and OS software Higher level mechanisms: process context switch process receiving a signal nonlocal jumps (setjmp/longjmp) these higher level mechanisms are implemented by either: –OS software (context switch and signals). –Language runtime library: nonlocal jumps.

CS 213 S’00– 21 – class12.ppt System context for exceptions Local/IO Bus Memory Network adapter Network adapter IDE disk controller IDE disk controller Video adapter Video adapter Display Network Processor Interrupt controller Interrupt controller SCSI controller SCSI controller SCSI bus Serial port controller Serial port controller Parallel port controller Parallel port controller Keyboard controller Keyboard controller Keyboard Mouse Printer Modem disk cdrom

CS 213 S’00– 22 – class12.ppt Exceptions An exception is a transfer of control to the OS in response to some event (i.e. change in processor state) User ProcessOS exception exception processing by exception handler (also called an interrupt handler) exception return (optional) event

CS 213 S’00– 23 – class12.ppt Internal (CPU) exceptions Internal exceptions occur as a result of events generated by executing instructions. Execution of a system call instruction. –allows a program to ask for OS services (e.g., timer updates) Execution of a break instruction –used by debuggers Errors during instruction execution –arithmetic overflow, address error, parity error, undefined instruction Events that require OS intervention –virtual memory page fault

CS 213 S’00– 24 – class12.ppt External exceptions (interrupts) External exceptions occur as a result of events generated by devices external to the processor (managed by interrupt controller). I/O interrupts –hitting ctl-c at the keyboard –arrival of a packet from a network –arrival of a data sector from a disk Hard reset interrupt –hitting the reset button Soft reset interrupt –hitting ctl-alt-delete on a PC

CS 213 S’00– 25 – class12.ppt System calls (internal exceptions) System calls (traps) are expected program events e.g., fork(), exec(), wait(), getpid() User code: calls a user-level library function which executes a special syscall instruction –e.g., syscall(id) results in a switch from user mode to kernel mode transfers control to a kernel system call interface System call interface: find entry in syscall table corresponding to id determine number of parameters copy parameters from user memory to kernel memory save current process context (in case of abortive return) invoke appropriate function in kernel

CS 213 S’00– 26 – class12.ppt Context of a process Context of a process: state that is necessary to restart the process if it is interrupted includes the following: –user-level context –register context –system-level context User-level context: text, data, and bss segments, plus user stack includes arguments and environment variables Register context: PC, general purpose regs and floating point regs, IEEE rounding mode, kernel stack pointer, process table address, etc. System-level context: various OS tables, process and memory tables, kernel stack, etc.

CS 213 S’00– 27 – class12.ppt Context switches The kernel may decide to pass control to another process when the current process does one of the following: puts itself to sleep –e.g., after initiating an I/O request to a slow device exits returns from a system call returns after being interrupted by an I/O device A context switch passes control to a new process via the following steps: save the current process context select a new process to run (scheduling) restore the (previously saved) context of the new process pass control to the new process

CS 213 S’00– 28 – class12.ppt Higher level mechanisms for altering the control flow Signals signals are software events generated by OS and processes –an OS abstraction for exceptions and interrupts signals are sent from the kernel or processes to other processes. different signals are identified by small integer ID’s –e.g., SIGINT: sent to foreground process when user hits ctl-c –e.g., SIGALRM: sent to process when a software timer goes off the only information in a signal is its ID and the fact that it arrived. Signal handlers programs can install signal handlers for different types of signals –handlers are asynchronously invoked when their signals arrive. See text for more details

CS 213 S’00– 29 – class12.ppt A program that reacts to externally generated events (ctrl-c) #include static void handler(int sig) { printf(”You think hitting ctrl-c will stop the bomb?\n"); sleep(2); printf("Well..."); fflush(stdout); sleep(1); printf("OK\n"); exit(0); } main() { signal(SIGINT, handler); /* installs ctl-c handler */ while(1) { }

CS 213 S’00– 30 – class12.ppt A program that reacts to internally generated events #include int beeps = 0; /* SIGALRM handler */ void handler(int sig) { printf("BEEP\n"); fflush(stdout); if (++beeps < 5) alarm(1); else { printf("BOOM!\n"); exit(0); } main() { signal(SIGALRM, handler); alarm(1); /* send SIGALRM in 1 second */ while (1) { /* handler returns here */ } bass> a.out BEEP BOOM! bass>

CS 213 S’00– 31 – class12.ppt Nonlocal jumps: setjmp()/longjmp() Powerful (but dangerous) user-level mechanism for transferring control to an arbitrary location. controlled to way to break the procedure stack discipline useful for error recovery int setjmp(jmp_buf j) must be called before longjmp identifies a return site for a subsequent longjmp. setjmp implementation: remember where you are by storing the current register context, stack pointer, and PC value in jmp_buf. return 0

CS 213 S’00– 32 – class12.ppt setjmp/longjmp (cont) void longjmp(jmp_buf j, int i) meaning: –return from the setjmp remembered by jump buffer j again... –…this time returning i called after setjmp a function that never returns! longjmp Implementation: restore register context from jump buffer j set %eax (the return value) to i jump to the location indicated by the PC stored in jump buf j.

CS 213 S’00– 33 – class12.ppt setjmp/longjmp example #include jmp_buf buf; main() { if (setjmp(buf) != 0) { printf(“back in main due to an error\n”); else printf(“first time through\n”); p1(); /* p1 calls p2, which calls p3 */ }... p3() { if (error) longjmp(buf, 1) }

CS 213 S’00– 34 – class12.ppt Putting it all together: A program that restarts itself when ctrl-c’d #include sigjmp_buf buf; void handler(int sig) { siglongjmp(buf, 1); } main() { signal(SIGINT, handler); if (!sigsetjmp(buf, 1)) printf("starting\n"); else printf("restarting\n"); while(1) { sleep(1); printf("processing...\n"); } bass> a.out starting processing... restarting processing... restarting processing... restarting processing... Ctrl-c