Program Translation and Execution II: Processes Oct 1, 1998 Topics User-level view of processes Implementation of processes setjmp/longjmp class12.ppt.

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

Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
1 Processes Professor Jennifer Rexford
Processes CSCI 444/544 Operating Systems Fall 2008.
Exceptional Control Flow Part I September 22, 2008 Topics Exceptions Process context switches Creating and destroying processes class11.ppt ,
Introduction to Kernel
OS Spring’03 Introduction Operating Systems Spring 2003.
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 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.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows 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.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
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 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
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.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Creating and Executing Processes
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
System-level programming II: Processes Feb 24, 2000 Topics User-level view of processes Exceptions Context switches Higher level control flow mechanisms.
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 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.
1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Exceptional Control Flow I March 12, 2002
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
1 Structure of Processes Chapter 6 Process State and Transition Data Structure for Process Layout of System Memory THE DESIGN OF THE UNIX OPERATING SYSTEM.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
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.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
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.
Introduction to Kernel
Process concept.
Exceptional Control Flow & Processes
Protection of System Resources
Exceptional Control Flow Oct 24, 2000
Overview of today’s lecture
Exceptional Control Flow Part I
Processes in Unix, Linux, and Windows
CS703 – Advanced Operating Systems
Structure of Processes
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
More examples How many processes does this piece of code create?
Processes in Unix, Linux, and Windows
System Structure and Process Model
Exceptional Control Flow Part I Mar. 11, 2003
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Lecture Topics: 11/1 General Operating System Concepts Processes
CS 105 “Tour of the Black Holes of Computing!”
Processes Prof. Ikjun Yeom TA – Mugyo
CS 105 “Tour of the Black Holes of Computing!”
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
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.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
Chapter 3 Process Description and Control
Presentation transcript:

Program Translation and Execution II: Processes Oct 1, 1998 Topics User-level view of processes Implementation of processes setjmp/longjmp class12.ppt Introduction to Computer Systems

CS 213 F’98– 2 – class12.ppt Processes A process is an instance of a running program runs concurrently with other processes (multitasking) managed by a shared piece of OS code called the kernel –kernel is not a separate process, but rather runs as part of some user process each process has its own data space and process id (pid) data for each protected protected from other processes Process A Process B user code kernel code user code kernel code user code Time Just a stream of instructions!

CS 213 F’98– 3 – class12.ppt Fork 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 F’98– 4 – class12.ppt Exit void exit(int status) exits a process atexit() function registers functions to be executed on 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 F’98– 5 – class12.ppt Wait int wait(int child_status) waits for a child to terminate and returns status and pid 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 F’98– 6 – class12.ppt Exec int execl(char *path, char *arg0, char *arg1,...) loads and runs executable at path with args arg0, arg1,... 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 F’98– 7 – class12.ppt Example: Concurrent network server void main() { master_sockfd = sl_passivesock(port); /* create master socket */ while (1) { slave_sockfd = sl_acceptsock(master_sockfd); /* await request */ switch (fork()) { case 0: /* child closes its master and manipulates slave */ close(master_sockfd); /* code to read and write to/from slave socket goes here */ exit(0); default: /* parent closes its copy of slave and repeats */ close(slave_sockfd); case -1: /* error */ fprintf("fork error\n"); exit(0); }

CS 213 F’98– 8 – class12.ppt Process hierarchy shell child grandchild init (1) (0) Daemon e.g. snmp

CS 213 F’98– 9 – class12.ppt Unix startup (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 F’98– 10 – class12.ppt Unix startup (2) init [1] [0] forks a getty (get tty or get terminal) for the console getty Daemons e.g. snmp /etc/inittab init forks new processes as per the /etc/inittab file

CS 213 F’98– 11 – class12.ppt Unix startup (3) init [1] [0] getty execs a login program login

CS 213 F’98– 12 – class12.ppt Unix startup (4) init [1] [0] login gets user’s login and passw if OK, it execs a shell if not OK, it execs another getty tcsh

CS 213 F’98– 13 – class12.ppt Loading and running programs from a shell /* read command line until EOF */ while (read(stdin, buffer, numchars)) { if ( ) amper = 1; else amper = 0; } /* for commands not in the shell command language */ if (fork() == 0) { execl(cmd, cmd, 0) } if (amper == 0) retpid = wait(&status); }

CS 213 F’98– 14 – class12.ppt Process memory image (Alpha) Reserved for kernel Reserved for shared libraries and dynamic loader Available for heap Heap (via malloc() or sbrk() Grows up Bss segment Data segment Text segment Stack Not accessible Available for stack Grows down to zero Not accessible by convention (64KB) $gp $sp 0x x ffff 0x x fff ffff 0x x ff 7fff ffff 0x ff x ff ffff ffff 0x xffff fbff ffff ffff 0xffff fc xffff ffff ffff ffff

CS 213 F’98– 15 – class12.ppt Kernel block diagram hardware (processor and devices) hardware control (interrupt and exception handlers) device drivers buffer cache charblock file systemprocess control system call interface libraries User programs User-level Kernel level Hw level

CS 213 F’98– 16 – class12.ppt User and kernel modes User mode Process can –execute its own instructions and access its own data. Process cannot –execute kernel instructions or privileged instructions (e.g. halt) –access kernel data or data from other processes. Kernel mode Process can –execute kernel instructions and privileged instructions –access kernel and user addresses Processes transition from user to kernel mode via interrupts and exceptions system calls (traps)

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

CS 213 F’98– 18 – class12.ppt Hardware control Interrupts and exceptions are unexpected hardware events Interrupts events external to the processor –I/O device asking for attention –timer interrupt typically indicated by setting an external pin Exceptions events internal to processor (as a result of executing an instruction) – divide by zero Same mechanism handles both Interrupt or exception triggers transfer of control from user code to interrupt handlers in the hardware control part of the kernel kernel services interrupt or exception If a timer interrupt, kernel might decide to give control to a new process (context switch)

CS 213 F’98– 19 – class12.ppt Process control: Context of a process The context of a process is the state that is necessary to restart the process if its interrupted. Union of... user-level context register context system-level context. User-level context text, data, and bss segments, and user stack Register context PC, general purpose integer and floating point regs, IEEE rounding mode, kernel stack pointer, process table address,... System-level context various OS tables process and memory tables, kernel stack,...

CS 213 F’98– 20 – class12.ppt Process control: Context switch The kernel can decide to pass control to another process if: the current process puts itself to sleep the current process exits when the current process returns from a system call when the current process returns after being interrupted Control passed to new process via context switch: save current process context. select new process (scheduling) restore (previously save) context of new process pass control to new process

CS 213 F’98– 21 – class12.ppt Process control: Process states 1. User Running: Process is executing in user mode. 2. Kernel Running: Process is executing in kernel mode. 3. Ready to Run: Process is not executing, but is ready to as soon as the kernel schedules it. 4. Asleep: Process is sleeping. 5. Preempted: Process is returning from kernel mode to user mode, but the kernel preempts it and does a context switch to schedule another process. 6. Created: Process is newly created, but it is not yet ready to run, nor is it sleeping (This is the start state for all process created with fork). 7. Zombie: The process executed the exit system call and is in the zombie state (until wait’ed for by its parent)

CS 213 F’98– 22 – class12.ppt Process states and state transitions 1 User Running 2 Kernel Running 5 Preempted 7 Zombie 3 Ready to Run 4 6 Created Asleep preempt interrupt, interrupt return exit return syscall, interrupt, exception return to user reschedule process enough mem awake sleep fork

CS 213 F’98– 23 – class12.ppt Setjmp/Longjmp Powerful (and dangerous) user-level mechanism for transferring control to an arbitrary location. int setjmp(jmp_buf j) must be called before longjmp meaning: –remember where you are by storing the current register context and PC value in jmp_buf –return 0 void longjmp(jmp_buf j, int i) called after setjmp meaning: –return from the setjmp remembered by jump buffer j with a value of i –restores register context from jump buf j, sets register $ra to i, sets PC to the PC stored in jump buf j.

CS 213 F’98– 24 – class12.ppt Setjmp/Longjmp example Useful for : error recovery implementing user-level threads packages #include jmp_buf buf; main() { if (setjmp(buf)) { printf(“back in main\n”); else printf(“first time through\n”); p1(); /* p1->p2->p3 */ }... p3() { if (error) longjmp(buf, 1) }