Lecture 5: Process Creation

Slides:



Advertisements
Similar presentations
1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
Advertisements

Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CSE 451: Operating Systems Winter 2006 Module 4 Processes Ed Lazowska Allen Center 570.
Processes CSCI 444/544 Operating Systems Fall 2008.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
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.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
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 5 Process, Thread and Task September 22, 2015 Kyu Ho Park.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Creating and Executing Processes
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
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  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
Process Management Azzam Mourad COEN 346.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
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
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Section 8: Processes What is a process Creating processes Fork-Exec
Chapter 3: Processes.
Using Processes.
Unix Process Management
Processes A process is a running program.
Processes in Unix, Linux, and Windows
UNIX PROCESSES.
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
IT 344: Operating Systems Module 4 Processes
Processes in Unix, Linux, and Windows
Fork and Exec Unix Model
System Structure B. Ramamurthy.
LINUX System Programming with Processes (additional)
More examples How many processes does this piece of code create?
Processes in Unix, Linux, and Windows
System Structure and Process Model
Tutorial 3 Tutorial 3.
Process Programming Interface
CS 105 “Tour of the Black Holes of Computing!”
Processes Prof. Ikjun Yeom TA – Mugyo
Tutorial: The Programming Interface
CS 105 “Tour of the Black Holes of Computing!”
Jonathan Walpole Computer Science Portland State University
CS 105 “Tour of the Black Holes of Computing!”
Lecture 6: Multiprogramming and Context Switching
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
CSE 451: Operating Systems Autumn 2009 Module 4 Processes
CSE 451: Operating Systems Winter 2007 Module 4 Processes
Processes in Unix and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
Chapter 3 Process Description and Control
Presentation transcript:

Lecture 5: Process Creation

Review: Linux process memory layout Stack: Automatically allocated and deallocated Local variables, parameters Heap: Manually allocated (malloc) and deallocated (free) Dynamic sized data structures Static data Global variables Text Program

In this lecture Process context Process creation

Process context: registers Stack Heap Static data Text (program) Stack pointer (SP) As discussed three pages ago, most programs perform file I/O using library code layered on top of kernel code. In this section we discuss just the kernel aspects of file I/O, looking at the abstraction and the high-level aspects of how this abstraction is implemented. The Unix file abstraction is very simple: files are simply arrays of bytes. Many systems have special system calls to make a file larger. In Unix, you simply write where you’ve never written before, and the file “magically” grows to the new size (within limits). The names of files are equally straightforward—just the names labeling the path that leads to the file within the directory tree. Finally, from the programmer’s point of view, all operations on files appear to be synchronous—when an I/O system call returns, as far as the process is concerned, the I/O has completed. (Things are different from the kernel’s point of view, as discussed later.) Program counter (PC)

Process context: file management Root directory Working directory Opened files

Process context: others Process id Parent process …

Process creation: fork() system call #include <unistd.h> pid_t fork(void); //prototype fork() returns a process id (a small integer) fork() returns twice! In the parent – fork returns the id of the child process In the child – fork returns a 0

Parent and child processes The child process is a copy of the parent process Same core image Same context (except process id) Registers Files Others

Creating a process: before fork() Stack Heap Static data Text (program) Fork() The only way to create a new process is to use the fork system call. PC parent process

Creating a process: after fork() Stack Heap Static data Text (program) fork() //return p Stack Heap Static data Text (program) fork() //return 0 The only way to create a new process is to use the fork system call. PC PC parent process child process process id = p

Example #include <unistd.h> #include <stdio.h> void main(void) { pid_t pid = fork(); if (pid > 0) printf(“I am the parent\n”); else if (pid == 0) printf(“I am the child\n”); else printf(“ERROR!\n”); }

Process hierarchies UNIX: Parent creates a child process, child can create more processes Forms a hierarchy UNIX calls this a "process group” All processes within a group are logically related Windows has no concept of process hierarchy all processes are created equal

Death and destruction All processes usually end at some time during runtime (with the exception of init) Processes may end either by: executing a return from the main function calling the exit(int) function calling the abort(void) function When a process exits, the OS delivers a termination status to the parent process.

Waiting Parent processes often wait for their child processes to end Parent processes do that via a wait() call pid_t wait(int * status); pid_t waitpid( pid_t pid, int * status,…);

Switching programs fork() creates a new process This would be almost useless if there was not a way to switch which program is associated with the new process The exec() system call is used to load a new program into an existing process

exec(): Loading a new image Stack Heap Static data Text (program) exec(prog, args) prog’s stack prog’s heap prog’s static data prog’s Text before after

exec() example: #include <unistd.h> main() { printf(“executing ls\n”); execl(“/bin/ls”, “ls”, “l”, (char*)0); exit(1); }

A stripped-down shell while (TRUE) { /* repeat forever */ type_prompt( ); /* display prompt */ read_command (command, parameters) /* input from terminal */ if (fork() != 0) { /* fork off child process */ /* Parent code */ waitpid( -1, &status, 0); /* wait for child to exit */ } else { /* Child code */ execve (command, parameters, 0); /* execute command */ }

More examples How many processes does this piece of code create? int main() { fork(); }

Bad example (don’t try this!) #include <unistd.h> #include <stdio.h> void main(void) { while (!fork()) printf("I am the child %d\n“, getpid()); printf("I am the parent %d\n“, getpid()); } fork bomb!

What is the output of this? int main() { int i; for (i=0; i<2; i++) { fork(); printf(“%d\n”,i); } return (0);