Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2.

Slides:



Advertisements
Similar presentations
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Advertisements

CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Implementing Processes, Threads, and Resources.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
Using the OS. Basic Abstractions Idea Program Result Idea Program Result Program Result … …
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
Process Management. External View of the OS Hardware fork() CreateProcess() CreateThread() close() CloseHandle() sleep() semctl() signal() SetWaitableTimer()
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.
Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Chapter 6 Implementing Processes, Threads, and Resources.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Slide 3-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 3 Operating System Organization.
Slide 3-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 3.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
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 (Processes and Files)
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.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
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.
Threads.
Slide 3-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 3.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
System calls for Process management
 For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Implementing Processes, Threads, and Resources 6.
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Threads, Thread management & Resource Management.
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.
STRUCTURE OPERATING SYSTEMS. I. PROCESS MANAGEMENT Process a program in execution More than one process can be associated with a single program Each is.
2 Processor(s)Main MemoryDevices Process, Thread & Resource Manager Memory Manager Device Manager File Manager.
Chapter 1 Introduction  What is an operating system  History of operating systems  The operating system zoo  Computer hardware review  Operating system.
What is a Process ? A program in execution.
System calls for Process management Process creation, termination, waiting.
2.1 Processes  process = abstraction of a running program  multiprogramming = CPU switches from running program to running program  pseudoparallelism.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
Chapter 3 The Programming Interface Chien-Chung Shen CIS/UD
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” –
Protection of System Resources
Operating Systems: A Modern Perspective, Chapter 1
Operating Systems: A Modern Perspective, Chapter 6
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Using the Operating System
Processes in Unix, Linux, and Windows
Tutorial: The Programming Interface
Concurrency, Processes and Threads
Operating Systems: A Modern Perspective, Chapter 3
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
Implementing Processes, Threads, and Resources
Processes in Unix and Windows
Outline Process Management Process manager Hardware process
Implementing Processes, Threads, and Resources
Presentation transcript:

Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2

Slide 2-2 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2

Slide 2-3 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 The Airplane Pilot’s Abstract Machine

Slide 2-4 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Basic Abstractions Program Result Program Result Program Result … … Idea

Slide 2-5 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Basic Abstractions(2) Program Result Abstract Machine Abstract Machine Program Result Program Result Abstract Machine Physical Machine … … Idea

Slide 2-6 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Abstract Machine Entities Process: A sequential program in execution Resource: Any abstract resource that a process can request, and which may can cause the process to be blocked if the resource is unavailable. File: A special case of a resource. A linearly-addressed sequence of bytes. “A byte stream.”

Slide 2-7 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Algorithms, Programs, and Processes Data Files Other Resources Algorithm Idea Source Program Source Program Binary Program Execution Engine Process Stack Status

Slide 2-8 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Classic Process OS implements {abstract machine} – one per task Multiprogramming enables N programs to be space-muxed in executable memory, and time- muxed across the physical machine processor. Result: Have an environment in which there can be multiple programs in execution concurrently*, each as a processes * Concurrently: Programs appear to execute simultaneously

Slide 2-9 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Process Abstraction Hardware Data Process Stack Processor Executable Memory Program Operating System

Slide 2-10 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Processes Sharing a Program Shared Program Text P1P1 P2P2 P3P3 P1P1 P2P2 P3P3 Files

Slide 2-11 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Multithreaded Accountant Purchase Orders Invoice (a) Separate Processes Purchase Orders Invoice First Accountant Second Accountant Accountant & Clone (b) Double Threaded Process

Slide 2-12 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Modern Process & Thread Divide classic process: –Process is an infrastructure in which execution takes place – address space + resources –Thread is a program in execution within a process context – each thread has its own stack Data Process Stack Program Operating System Thread Stack …

Slide 2-13 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 A Process with Multiple Threads Data Files Other Resources Binary Program Process Stack Status Stack Status Stack Status Thread (Execution Engine)

Slide 2-14 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 More on Processes Abstraction of processor resource –Programmer sees an abstract machine environment with spectrum of resources and a set of resource addresses (most of the addresses are memory addresses) –User perspective is that its program is the only one in execution –OS perspective is that it runs one program with its resources for a while, then switches to a different process (context switching) OS maintains –A process descriptor data structure to implement the process abstraction Identity, owner, things it owns/accesses, etc. Tangible element of a process –Resource descriptors for each resource

Slide 2-15 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Address Space Process must be able to reference every resource in its abstract machine Assign each unit of resource an address –Most addresses are for memory locations –Abstract device registers –Mechanisms to manipulate resources Addresses used by one process are inaccessible to other processes Say that each process has its own address space

Slide 2-16 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Shared Address Space Classic processes sharing program  shared address space support Thread model simplifies the problem –All threads in a process implicitly use that process’s address space, but no “unrelated threads” have access to the address space –Now trivial for threads to share a program and data If you want sharing, encode your work as threads in a process If you do not want sharing, place threads in separate processes

Slide 2-17 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Process & Address Space Address Space Code Resources Abstract Machine Environment Stack Data Resources

Slide 2-18 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Creating a Process Here is the classic model for creating processes: FORK(label): Create another process in the same address space beginning execution at instruction label QUIT(): Terminate the process. JOIN(count): disableInterrupts(); count--; if(count > 0) QUIT(); enableInterrupts();

Slide 2-19 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Example procA() { while(TRUE) { ; update(x); ; retrieve(y); } procB() { while(TRUE) { retrieve(x); ; update(y); ; } Process A Process B x y

Slide 2-20 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Example (cont) L0: count = 2; ; update(x); FORK(L2); ; L1: JOIN(count); retrieve(y); goto L0; L2: retrieve(x); ; update(y); FORK(L3); goto L1; L3: QUIT();

Slide 2-21 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Example (cont) L0: count = 2; ; update(x); FORK(L2); ; L1: JOIN(count); retrieve(y); goto L0; L2: retrieve(x); ; update(y); FORK(L3); goto L1; L3: QUIT(); L0: count = 2; ; update(x); FORK(L2); retrieve(x); update(y>; FORK(L3) L1: JOIN(count); retrieve(y); goto L0; L2: ; goto L1; L3: QUIT();

Slide 2-22 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 UNIX Processes UNIX Kernel Data Segment Files Other Resources Text Segment Stack Segment Status Process

Slide 2-23 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 UNIX Processes Each process has its own address space –Subdivided into text, data, & stack segment –a.out file describes the address space OS kernel creates descriptor to manage process Process identifier (PID): User handle for the process (descriptor) Try “ ps ” and “ ps -aux ” (read man page)

Slide 2-24 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Creating/Destroying Processes UNIX fork() creates a process –Creates a new address space –Copies text, data, & stack into new adress space –Provides child with access to open files UNIX wait() allows a parent to wait for a child to terminate UNIX exec  () allows a child to run a new program

Slide 2-25 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Creating a UNIX Process int pidValue;... pidValue = fork(); /* Creates a child process */ if(pidValue == 0) { /* pidValue is 0 for child, nonzero for parent */ /* The child executes this code concurrently with parent */ childsPlay(…); /* A procedure linked into a.out */ exit(0); } /* The parent executes this code concurrently with child */ parentsWork(..); wait(…);...

Slide 2-26 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Child Executes a Different Program int pid;... /* Set up the argv array for the child */... /* Create the child */ if((pid = fork()) == 0) { /* The child executes its own absolute program */ execve(childProgram.out, argv, 0); /* Only return from an execve call if it fails */ printf(“Error in the exec … terminating the child …”); exit(0); }... wait(…); /* Parent waits for child to terminate */...

Slide 2-27 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Example: Parent #include #define NULL 0 int main (void) { if (fork() == 0){ /* This is the child process */ execve("child",NULL,NULL); exit(0); /* Should never get here, terminate */ } /* Parent code here */ printf("Process[%d]: Parent in execution...\n", getpid()); sleep(2); if(wait(NULL) > 0) /* Child terminating */ printf("Process[%d]: Parent detects terminating child \n", getpid()); printf("Process[%d]: Parent terminating...\n", getpid()); }

Slide 2-28 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Example: Child int main (void) { /* The child process's new program This program replaces the parent's program */ printf("Process[%d]: child in execution...\n", getpid()); sleep(1); printf("Process[%d]: child terminating...\n", getpid()); }

Slide 2-29 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Threads -- The NT Model Data Files Other Resources Binary Program Process Stack Status Stack Status Stack Status Thread (Execution Engine)

Slide 2-30 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Windows NT Process #include... int main(int argv, char *argv[]) {... STARTUPINFO startInfo; PROCESS_INFORMATION processInfo;... strcpy(lpCommandLine, “C:\\WINNT\\SYSTEM32\\NOTEPAD.EXE temp.txt)”; ZeroMemory(&startInfo, sizeof(startInfo)); startInfo.cb = sizeof(startInfo); if(!CreateProcess(NULL, lpCommandLine, NULL, NULL, FALSE, HIGH_PRIORITY_CLASS | CREATE_NEW_CONSOLE, NULL, NULL, &startInfo, &processInfo)) { fprintf(stderr, “CreateProcess failed on error %d\n”, GetLastError()); ExitProcess(1); }; /* A new child process is now executing the lpCommandLine program */... CloseHandle(&processInfo.hThread); CloseHandle(&processInfo.hProcess); t_handle = CreateProcess(…, lpCommandLine, …); }

Slide 2-31 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 NT Threads #include... int main(int argv, char *argv[]) { t_handle = CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes DWORD dwStackSize, // initial thread stack size, in bytes LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function LPVOID lpParameter,// argument for new thread DWORD dwCreationFlags,// creation flags LPDWORD lpThreadId // pointer to returned thread identifier ); /* A new child thread is now executing the tChild function */ Sleep(100) /* Let another thread execute */ } DWPRD WINAPI tChild(LPVOID me) { /* This function is executed by the child thread */... SLEEP(100); /* Let another thread execute */... }

Slide 2-32 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 _beginthreadex() unsigned long _beginthreadex( void *security, unsigned stack_size, unsigned ( __stdcall *start_address )( void * ), void *arglist, unsigned initflag, unsigned *thrdaddr ); Single copy of certain variables in a process Need a copy per thread

Slide 2-33 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Resources Anything that a process requests from an OS –Available  allocated –Not available  process is blocked Examples –Files –Primary memory address space (“virtual memory”) –Actual primary memory (“physical memory”) –Devices (e.g., window, mouse, kbd, serial port, …) –Network port –… many others …

Slide 2-34 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Files Data must be read into (and out of) the machine – I/O devices Storage devices provide persistent copy Need an abstraction to make I/O simple – the file A file is a linearly-addressed sequence of bytes –From/to an input device –Including a storage device

Slide 2-35 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 The File Abstraction Hardware Data Process Stack Processor Executable Memory Program Operating System Storage Device File Descriptor File Descriptor

Slide 2-36 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 UNIX Files open Specifies file name to be used close Release file descriptor read Input a block of information write Output a block of information lseek Position file for read/write ioctl Device-specific operations UNIX and NT try to make every resource (except CPU and RAM) look like a file Then can use a common interface:

Slide 2-37 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 UNIX File Example #include int main() { int inFile, outFile; char *inFileName = “in_test”; char *outFileName = “out_test”; int len; char c; inFile = open(inFileName, O_RDONLY); outFile = open(outFileName, O_WRONLY); /* Loop through the input file */ while ((len = read(inFile, &c, 1)) > 0) write(outFile, &c, 1); /* Close files and quite */ close(inFile); close(outFile); }

Slide 2-38 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Windows File Manipulation Program #include #define BUFFER_LEN... // # of bytes to read/write /* The producer process reads information from the file name in_test then writes it to the file named out_test. */ int main(int argc, char *argv[]) { // Local variables char buffer[BUFFER_LEN+1]; // CreateFile parameters DWORD dwShareMode = 0; // share mode LPSECURITY_ATTRIBUTES lpFileSecurityAttributes = NULL; // pointer to security attributes HANDLE hTemplateFile = NULL; // handle to file with attributes to copy // ReadFile parameters HANDLE sourceFile; // Source of pipeline DWORD numberOfBytesRead; // number of bytes read LPOVERLAPPED lpOverlapped = NULL; // Not used here

Slide 2-39 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Windows File Manipulation Program(2) // WriteFile parameters HANDLE sinkFile; // Source of pipeline DWORD numberOfBytesWritten; // # bytes written // Open the source file sourceFile = CreateFile ( "in_test", GENERIC_READ, dwShareMode, lpFileSecurityAttributes, OPEN_ALWAYS, FILE_ATTRIBUTE_READONLY, hTemplateFile ); if(sourceFile == INVALID_HANDLE_VALUE) { fprintf(stderr, "File open operation failed\n"); ExitProcess(1); }

Slide 2-40 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Windows File Manipulation Program(3) // Open the sink file sinkFile = CreateFile ( "out_test", GENERIC_WRITE, dwShareMode, lpSecurityAttributes, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, hTemplateFile ); if(sinkFile == INVALID_HANDLE_VALUE) { fprintf(stderr, "File open operation failed\n"); ExitProcess(1); }

Slide 2-41 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Windows File Manipulation Program(4) // Main loop to copy the file while ( ReadFile( sourceFile, buffer, BUFFER_LEN, &numberOfBytesRead, lpOverlapped ) && numberOfBytesRead > 0 ) { WriteFile(sinkFile, buffer, BUFFER_LEN, &numberOfBytesWritten, lpOverlapped); } // Terminating. Close the sink and source files CloseHandle(sourceFile); CloseHandle(sinkFile); ExitProcess (0); }

Slide 2-42 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Shell Command Line Interpreter OS Interactive User Shell Program Application & System Software OS System Call Interface

Slide 2-43 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 The Shell Strategy Shell Process Process to execute command Process to execute command % grep first f3 f3 read keyboard fork a process read file

Slide 2-44 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Bootstrapping Computer starts, begins executing a bootstrap program -- initial process Loads OS from the disk (or other device) Initial process runs OS, creates other processes

Slide 2-45 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Initializing a UNIX Machine Serial Port A Serial Port B Serial Port C Serial Port Z login getty /etc/passwd

Slide 2-46 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Objects A recent trend is to replace processes by objects Objects are autonomous Objects communicate with one another using messages Popular computing paradigm Too early to say how important it will be...