Using the Operating System

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

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 … …
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.
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.
Process Concept An operating system executes a variety of programs
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
Introduction (Processes and Files)
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.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
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.
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.
CS333 Intro to Operating Systems Jonathan Walpole.
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.
H.-H. S. Lee 1 ECE3055 Computer Architecture and Operating Systems Lecture 10 Process, Thread Prof. Hsien-Hsin Sean Lee School of Electrical and Computer.
Silberschatz, Galvin and Gagne  Operating System Concepts Process Concept An operating system executes a variety of programs:  Batch system.
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
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.
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.
What is a Process ? A program in execution.
System calls for Process management Process creation, termination, waiting.
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2.
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.
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” –
Processes and threads.
Chapter 3: Processes.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
Protection of System Resources
Concurrency, Processes and Threads
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 3: Process Concept
Operating Systems: A Modern Perspective, Chapter 6
Chapter 3: Processes.
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Threads and Concurrency
Operating System Concepts
Chapter 2: Operating-System Structures
Jonathan Walpole Computer Science Portland State University
Concurrency, Processes and Threads
Chapter 3: Processes.
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
CS510 Operating System Foundations
Outline Process Management Process manager Hardware process
CSE 153 Design of Operating Systems Winter 2019
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Concurrency, Processes and Threads
Implementing Processes, Threads, and Resources
Chapter 2: Operating-System Structures
Presentation transcript:

Using the Operating System

Using the O.S. For an application programmer, the operating system interface is most important The functions provided by the OS Abstract resources that are available Reduces the amount of detailed knowledge the programmer must know

Basic Abstractions Program Result … Idea Machine Machine Machine

… … Basic Abstractions (2) Idea Abstract Machine Program Result Physical Machine Idea Abstract Machine Program Result … … Idea Abstract Machine Program Result

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.”

Algorithms, Programs, and Processes Data Files Other Resources Algorithm Idea Source Program Binary Execution Engine Process Stack Status

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 process * Concurrently: Programs appear to execute simultaneously

Process Abstraction Operating System Hardware Data Process Stack Program Stack Operating System Processor Hardware Executable Memory

Processes Sharing a Program Files P1 P2 P3 P1 Files P2 Files P3 Shared Program Text

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 Stack Data Thread Thread Stack Process … Program Stack Thread Operating System

A Process with Multiple Threads Thread (Execution Engine) Stack Status Files Stack Status Data Stack Status Other Resources Binary Program Process

Threads A thread (or lightweight process) is an execution engine component of a process; it is a basic unit of CPU utilization It consists of: program counter register set stack space

Threads – cont.

Threads – cont. A thread shares with its peer threads its: code section data section operating-system resources collectively known as a task A traditional or heavyweight process is equal to a task with one thread

Threads – cont. Benefits of multithreading Improve application responsiveness Firefox uses multithreaded programming Use multiprocessors efficiently Improve program structure Use fewer system resources Faster to switch between threads than processes

Threads in a spreadsheet program

Disk server using threads

Kinds of threads Lightweight processes threads implemented by the OS A process can implement user threads threads implemented (solely) by the user these are more efficient but if the OS blocks one of them, they are all blocked Kernel threads are threads that run inside the OS (a.k.a. the kernel)

Thread Related Functions and System Calls POSIX Thread pthread_create pthread_join pthread_exit Solaris Thread thr_create thr_join thr_exit

Resources Anything that a process requests from an OS Examples 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 …

Processes & Resources Operating System Hardware Resource Resource Data Program Process Resource Data Program Resource Data Program Process Resource Process Resource Resource Operating System Processor Device Hardware Device Device Executable Memory

More on Processes Abstraction of processor resource OS maintains 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

The Process’s Abstract Machine Resources Data Resources Code Resources Stack Abstract Machine Environment Process Descriptor Resource Descriptor Other Data Structures Resource Descriptor Resource Descriptor

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

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

Process & Address Space Resources Data Resources Code Resources Stack Abstract Machine Environment Address Space

Creating a Process Here is the classic model for creating processes: FORK(label): Create another process in a copy of the original address space beginning execution at instruction label QUIT(): Terminate the process. JOIN(count): An atomic statement; count is shared disableInterrupts(); count--; if(count > 0) QUIT(); enableInterrupts();

Example – Concurrency Problem: A is reading y and B is reading x. procA() { while(TRUE) { <compute section A1>; update(x); <compute section A2>; retrieve(y); } procB() { while(TRUE) { retrieve(x); <compute section B1>; update(y); <compute section B2>; } Process A Process B x y Problem: A is reading y and B is reading x. What if the processes are not synchronized?

Example – Solution? L0: count1 = 2; count2 = 2; <compute section A1>; update(x); FORK(L2); <compute section A2>; L1: JOIN(count1); retrieve(y); JOIN(count2); goto L0; L2: retrieve(x); <compute section B1>; update(y); FORK(L3); goto L1; L3: <compute section B2>

UNIX Processes Process Files Stack Segment Status Text Segment Data Other Resources Process UNIX Kernel

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 -ef” (read man page)

Creating/Destroying Processes UNIX fork() creates a process Creates a new address space Copies text, data, & stack into new address space Provides child with access to open files UNIX wait() allows a parent to wait for a child to terminate UNIX execa() allows a child to run a new program

Creating a UNIX Process Do the parent and the child have the same sequence of instructions when fork() is successful? Why do we say this is the child and this the parent? How about here? pid = fork(); if (pid == ((pid_t)-1)) { // Something must be wrong with the fork // error processing ......... } else { if (pid == 0) { // This is the child process ........ // This is the parent process } //How about here, the parent or the child ? ...........

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 */

Example: Parent #include <sys/wait.h> #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());

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()); }

Threads -- The NT Model Thread (Execution Engine) Process Stack Status Files Stack Status Data Stack Status Other Resources Binary Program Process

Windows NT Process #include <cthreads.h> ... 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, …); }

NT Threads #include <cthreads.h> ... 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 */

_beginthreadex() Old style C runtime functions use a single copy of certain variables in a process Need a copy per thread Alternative to CreateThread() -> _beginthreadex() unsigned long _beginthreadex( void *security, unsigned stack_size, unsigned ( __stdcall *start_address )( void * ), void *arglist, unsigned initflag, unsigned *thrdaddr );

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/output device Including a storage device

The File Abstraction Operating System Hardware Storage Device Data Process Program Stack Operating System File Descriptor Processor Storage Device Hardware Executable Memory

UNIX Files UNIX and NT try to make every resource (except CPU and RAM) look like a file Then can use a common interface: 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 File Example #include <stdio.h> #include <fcntl.h> 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 quit */ close(inFile); close(outFile); }

Windows File Manipulation Program #include <windows.h> #include <stdio.h> #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

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); }

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); }

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);

Shell Command Line Interpreter Interactive User Application & System Software Shell Program OS System Call Interface OS

The Shell Strategy % grep first f3 fork a process read keyboard Shell Process Process to execute command grep f3 read file

Objects A recent trend is to replace processes by objects Object-oriented design models A system is modeled as a set of autonomous entities, called objects Each object responds to messages and interacts with other objects through message passing Object-oriented Operating Systems A new mechanism to specify a system Specify the individual units of serial computation Specify the model of interactions among units Too early to say how important it will be ...

Summary Operating systems provide a virtual computing environment for application programmers Functions provided by O.S. Abstract resources by O.S. Files Processes and threads Objects Programmers can develop applications more efficiently