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.

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 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
1 Processes Professor Jennifer Rexford
Processes CSCI 444/544 Operating Systems Fall 2008.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
CSE 451: Operating Systems Winter 2009 Module 4 Processes Mark Zbikowski Gary Kimura.
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.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CSE 451: Operating Systems Winter 2012 Processes Mark Zbikowski Gary Kimura.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Lecture 3 Page 1 CS 111 Summer 2015 Processes and Threads CS 111 Operating System Principles Peter Reiher.
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Threads, Thread management & Resource Management.
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).
Lecture 5 Page 1 CS 111 Winter 2014 Processes CS 111 Operating Systems Peter Reiher.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Processes & Threads Emery Berger and Mark Corner University.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes.
CE Operating Systems Lecture 10 Processes and process management in Linux.
System calls for Process management
CS 153 Design of Operating Systems Spring 2015 Lecture 5: 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.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Operating Systems Process Creation
4300 Lines Added 1800 Lines Removed 1500 Lines Modified PER DAY DURING SUSE Lab.
Department of Computer Science and Software Engineering
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
15-410, S’ The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process “System call abuse for fun & profit”
System calls for Process management Process creation, termination, waiting.
Lecture 5 Page 1 CS 111 Online Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
Outline What are processes?
Protecting Memory What is there to protect in memory?
Process Management Process Concept Why only the global variables?
Protecting Memory What is there to protect in memory?
CS 6560: Operating Systems Design
Protecting Memory What is there to protect in memory?
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Outline What are processes?
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Cs561 Presenter: QIAOQIAO CHEN Spring 2012
Processes in Unix, Linux, and Windows
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CSE 451: Operating Systems Winter 2011 Processes
CSE 451: Operating Systems Winter 2010 Module 4 Processes
Threads Chapter 4.
Tutorial: The Programming Interface
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
Lecture 6: Multiprogramming and Context Switching
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
Threads vs. Processes Hank Levy 1.
Processes in Unix and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
Processes Creation and Threads
CSE 153 Design of Operating Systems Winter 2019
CS703 – Advanced Operating Systems
Lecture 6 Introduction to Process Management
Presentation transcript:

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 Some at the behest of the operating system How do we create a new process?

Creating a Process Descriptor The process descriptor is the OS’ basic per-process data structure So a new process needs a new descriptor What does the OS do with the descriptor? Typically puts it into a process table The data structure the OS uses to organize all currently active processes

What Else Does a New Process Need? A virtual address space To hold all of the segments it will need So the OS needs to create one And allocate memory for code, data and stack OS then loads program code and data into new segments Initializes a stack segment Sets up initial registers (PC, PS, SP)

Choices for Process Creation Start with a “blank” process No initial state or resources Have some way of filling in the vital stuff Code Program counter, etc. This is the basic Windows approach Use the calling process as a template Give new process the same stuff as the old one Including code, PC, etc. This is the basic Unix/Linux approach

Starting With a Blank Process Basically, create a brand new process The system call that creates it obviously needs to provide some information Everything needed to set up the process properly At the minimum, what code is to be run Generally a lot more than that Other than bootstrapping, the new process is created by command of an existing process

Windows Process Creation The CreateProcess() system call A very flexible way to create a new process Many parameters with many possible values Generally, the system call includes the name of the program to run In one of a couple of parameter locations Different parameters fill out other critical information for the new process Environment information, priorities, etc.

Process Forking The way Unix/Linux creates processes Essentially clones the existing process On assumption that the new process is a lot like the old one Most likely to be true for some kinds of parallel programming Not so likely for more typical user computing

Why Did Unix Use Forking? Avoids costs of copying a lot of code If it’s the same code as the parents’ . . . Historical reasons Parallel processing literature used a cloning fork Fork allowed parallelism before threads invented Practical reasons Easy to manage shared resources Like stdin, stdout, stderr Easy to set up process pipe-lines (e.g. ls | more) Share exclusive-access resources (e.g. tape drives)

What Happens After a Fork? There are now two processes With different IDs But otherwise mostly exactly the same How do I profitably use that? Program executes a fork Now there are two programs With the same code and program counter Write code to figure out which is which Usually, parent goes “one way” and child goes “the other”

Forking and the Data Segments Forked child shares the parent’s code But not its stack It has its own stack, initialized to match the parent’s Just as if a second process running the same program had reached the same point in its run Child should have its own data segment, though Forked processes do not share their data segments

Forking and Copy on Write If the parent had a big data area, setting up a separate copy for the child is expensive And fork was supposed to be cheap If neither parent nor child write the parent’s data area, though, no copy necessary So set it up as copy on write If one of them writes it, then make a copy and let the process write the copy The other process keeps the original

Sample Use of Fork Parent and child code could be very different if (fork() ) { /* I’m the parent! */ execute parent code } else { /* I’m the child! */ execute the child code } Parent and child code could be very different In fact, often you want the child to be a totally different program And maybe not share the parent’s resources

But Fork Isn’t What I Usually Want! Indeed, you usually don’t want another copy of the same process You want a process to do something entirely different Handled with exec A Unix system call to “remake” a process Changes the code associated with a process Resets much of the rest of its state, too Like open files

The exec Call A Linux/Unix system call to handle the common case Replaces a process’ existing program with a different one New code Different set of other resources Different PC and stack Essentially, called after you do a fork

Using exec The parent goes on to whatever is next if (fork() ) { /* I’m the parent! */ continue with what I was doing before } else { /* I’m the child! */ exec(“new program”, <program arguments>); } The parent goes on to whatever is next The child replaces its code with “new program”

Is Exec Really All That Different? Fork without exec Fork with exec if (fork() ) { /* I’m the parent! */ <execute parent code> } else { /* I’m the child! */ <execute the child code> } if (fork() ) { /* I’m the parent! */ <execute parent code> } else { /* I’m the child! */ exec(“new program”, <program arguments>; } Here, the child code is part of this program Here, the child code is an entirely separate program Specified at compile time Potentially not specified until run time Not a different program at all A totally different program

How Does the OS Handle Exec? Must get rid of the child’s old code And its stack and data areas Latter is easy if you are using copy-on-write Must load a brand new set of code for that process Must initialize child’s stack, PC, and other relevant control structure To start a fresh program run for the child process This sounds like a lot of trouble. Why did Linux stick with fork/exec, rather than using something more like the Windows approach? Why don’t they change it now?

New Processes and Threads All processes have at least one thread In some older OSes, never more than one In which case, the thread is not explicitly represented In newer OSes, processes typically start with one thread As process executes, it can create new threads New thread stacks allocated as needed

A Thread Implementation Choice Threads can be implemented in one of two ways The kernel implements them User code implements them These alternatives have fundamental differences

User Threads The kernel doesn’t know about multiple threads per process The process itself knows So the process must schedule its threads Since the kernel doesn’t know the process has multiple threads, The process can’t run threads on more than one core Switching threads doesn’t require OS involvement, though Which can be cheaper

Typical Use of User Threads A server process that expects to have multiple simultaneous clients Server process can spawn a new user thread for each client And can then use its own scheduling methods to determine which thread to run when OS need not get involved in running threads No context switch costs to change from one client to another

Kernel Threads The OS is aware that processes can contain more than one thread Creating threads is an OS operation Scheduling of threads handled by OS Which can schedule several process threads on different cores simultaneously Saves the program complexity of handling threads But somewhat more heavyweight

Typical Use of Kernel Threads Can I get the best of both worlds by using both kernel and user threads? Or would I inherit the problems of each along with their advantages? Or would it be even worse than that? A program that can do significant parallel processing on its data Each parallel operation is run as a kernel thread All sharing the same data space and code But each with its own stack If multiple cores available, OS can achieve true parallelism for the program

Process Termination Most processes terminate All do, of course, when the machine goes down But most do some work and then exit before that Others are killed by the OS or another process When a process terminates, the OS needs to clean it up Essentially, getting rid of all of its resources In a way that allows simple reclamation

Ways That a Process Terminates The process itself exits Another process kills it Typically only the parent can kill it Using an explicit system call The operating system kills it E.g., many systems kill all child processes when a parent process dies Or OS can simply point to a process and shoot it dead The entire machine crashes What per-process actions, if any, might we want to take to deal with this possibility?

Parents, Children, and Process Termination Often a parent needs to know when a child terminates Parent can issue a system call waiting on the child’s termination E.g., Linux waitpid() system call Parent remains in a busy loop until child terminates A little difficulty: What if the child already terminated before the system call was made?

Zombie Processes Some systems maintain minimal state for terminated child processes Until parent waits on their termination Or parent itself terminates Since the zombie child has exited, it doesn’t get run any more And it uses no resources Except an OS process control structure

What If the Parent Doesn’t Clean Up? Zombie proliferation Each takes up very little state information But they can clutter the OS process table If the parent ever exits, the zombies go with him Suggests that long-running processes need to be careful about spawning temporary children