Top Banner
52

For an application programmer, the operating system interface is most important The functions provided by the OS Abstract resources that are available.

Jan 05, 2016

Download

Documents

Lily Clark
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.
Page 2: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

2

Page 3: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

3

ProgramProgram ResultResult

ProgramProgram ResultResult

ProgramProgram ResultResult

… …

Idea

Idea

Idea

Machine

Machine

Machine

Page 4: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

4

ProgramProgram ResultResult

AbstractMachine

AbstractMachine

ProgramProgram ResultResult

ProgramProgram ResultResult

AbstractMachine

PhysicalMachine

… …

Idea

Idea

Idea

Page 5: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

5

Page 6: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

6

Data

FilesFiles

Files

OtherResources

AlgorithmAlgorithm

Idea

SourceProgram

SourceProgram

BinaryProgram

Execution Engine

Process

StackStatus

Page 7: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

7* Concurrently: Programs appear to execute simultaneously

Page 8: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

8

Hardware

DataData

ProcessProcess

Sta

ck

Processor

ExecutableMemory

ProgramProgram

Operating SystemOperating System

Page 9: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

9

Shared Program Text

P1

P2

P3

P1 P2 P3

FilesFiles

FilesFiles

FilesFiles

Page 10: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

10

DataData

ProcessProcess

Sta

ck

ProgramProgram

Operating SystemOperating System

ThreadThread

Thread

Sta

ck

Sta

ck

Page 11: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

11

Data

FilesFiles

Files

OtherResources

BinaryProgram

Process

StackStatus

StackStatus

StackStatus

Thread (Execution Engine)

Page 12: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

12

Page 13: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

13

Page 14: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

14

Page 15: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

Benefits of multithreading Improve application responsiveness

Netscape uses multithreaded programming Use multiprocessors efficiently Improve program structure Use fewer system resources Faster to switch between threads than

processes

15

Page 16: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

16

Page 17: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

17

Page 18: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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)

18

Page 19: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

POSIX Thread pthread_create pthread_join pthread_exit

Solaris Thread thr_create thr_join thr_exit

19

Page 20: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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 …

20

Page 21: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

21

Hardware

ProcessProcess

Processor

ExecutableMemory

DataData

ProgramProgram

Operating SystemOperating System

DataData

ProgramProgram

DataData

ProgramProgram

ProcessProcessProcessProcess ResourceResourceResourceResourceResourceResource

ResourceResourceResourceResourceResourceResource

ResourceResourceResourceResourceResourceResource

DeviceDevice

Device

Page 22: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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 22

Page 23: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

23

CodeResources

Resources

Abstract Machine Environment

Stack

DataResources

ProcessDescriptorProcess

Descriptor

ResourceDescriptorResource

DescriptorResource

DescriptorResource

DescriptorResource

DescriptorResource

Descriptor

OtherData

Structures

OtherData

Structures

Page 24: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

24

Page 25: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

25

Page 26: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

26

Address Space

CodeResources

Resources

Abstract Machine Environment

Stack

DataResources

Page 27: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

Here is the classic model for creating processes:

27

FORK(label): Create another process in a copy of the originaladdress space beginning execution at instruction labelQUIT(): Terminate the process.JOIN(count): An atomic statement; count is shared disableInterrupts(); count--; if(count > 0) QUIT(); enableInterrupts();

Page 28: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

28

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 AProcess A Process BProcess B

x

y

Problem: A is reading y and B is reading x.What if the processes are not synchronized?

Page 29: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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> JOIN(count2); goto L0;

29

Page 30: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

30

UNIX Kernel

DataSegment

FilesFiles

Files

OtherResources

TextSegment

StackSegment

Status

Process

Page 31: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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)

31

Page 32: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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 execve() allows a child to run a new program

32

Page 33: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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 ........ } else { // This is the parent process ......... } //How about here, the parent or the child ? ...........}

33

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?

Page 34: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

34

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

Page 35: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

35

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

Page 36: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

36

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

Page 37: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

37

Data

FilesFiles

Files

OtherResources

BinaryProgram

Process

StackStatus

StackStatus

StackStatus

Thread (Execution Engine)

Page 38: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

38

#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, …);}

Page 39: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

39

#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 threadDWORD dwCreationFlags, // creation flagsLPDWORD 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 */ ...}

Page 40: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

40

unsigned long _beginthreadex( void *security, unsigned stack_size, unsigned ( __stdcall *start_address )( void * ), void *arglist, unsigned initflag, unsigned *thrdaddr);

• Old style C runtime functions use a single copy of certain variables in a process• Need a copy per thread• Alternative to CreateThread() -> _beginthreadex()

Page 41: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

41

Page 42: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

42

Hardware

DataData

ProcessProcess

Sta

ck

Processor

ExecutableMemory

ProgramProgram

Operating SystemOperating System

StorageDevice

FileFile

FileDescriptor

FileDescriptor

Page 43: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

43

open Specifies file name to be usedclose Release file descriptorread Input a block of informationwrite Output a block of informationlseek Position file for read/writeioctl 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:

Page 44: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

44

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

Page 45: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

45

#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

Page 46: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

46

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

Page 47: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

47

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

Page 48: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

48

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

Page 49: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

49

OS

Interactive User

Shell Program

Application& SystemSoftware

OS System Call InterfaceOS System Call Interface

Page 50: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

50

Shell ProcessShell Process

Processto executecommand

Processto executecommand

% grep first f3

f3

read keyboard fork a process

read file

grep

Page 51: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

51

Page 52: For an application programmer, the operating system interface is most important  The functions provided by the OS  Abstract resources that are available.

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

52