24
21 TOPIC: FILES CHAPT 6 22 TOPIC: SECURITY, CHAPT 9 REVIEW OF THE TOPICS TO BE TREATED IN DETAIL 18 TOPIC: INPUT-OUTPUT BACKGROUND HARDWARE: CHAPT 5[2] 20 TOPIC: MEMORY MANAGEMENT:PROCESSES-CONTINUOUS; PAGED CHAPT 4 19 TOPICS: PROCESSES , DEADLOCK CHAPT 2, 3 23 THREADS (GLOBAL MEMORY, CONTROL OS OR PROCESS) CHAPT 2 24 SHARED MEMORY PROCESSORS AND THEIR OPERATING SYSTEMs CHAPT 8 12 UNIX PROCESS CREATION, THE FORK 13 SHELL STRUCTURES USING FORK 14 A SHELL USING FORK-TRACE 15 CREATING PROCESSES FORK PARENT-CHILD RELATIONS PROCESS CREATION AND USE IN UNIX-BASIC SYSTEM CALLS 16 UNIX OPERATING SYSTEM CALLS, LIBRARY FUNCTIONS, IPC OPERATING SYSTEM (OS:) ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF COMPUTER SYSTEM AND HISTORIC OUTLINE 4 BOOTSTRAPPING Incremental Development Of Compilers And OS 6 HISTORY-SOME HIGHLIGHTS 3 OPERATING SYSTEM CALLS ARE “INTERPRETED” with LIBRARY AND KERNEL CODE 2 PURPOSE OF OPERATING SYSTEM THE SHELL SYSTEM FUNCTIONS 5 THE OPERATING SYSTEM POSITION IN THE COMPUTER LANGUAGE HIERARCHY BASIC: OPERATING SYSTEM IS USED AND IS NEEDED TO IMPLEMENT: (PSEUDO) PARALLEL PROCESSES [TIME-SHARING, MULTI-PROGRAMMING)CHAPTERS: PROCESSES AND MEMMORY MANAGEMENT. 7 MULTIPROGRAMMING-MANY PROCESS SIMULTANEOUSLY PRESENT (EACH GIVEN EPISODES OF CONTINUOUS TIME) 8 PROCESSES, THREADS, STATES 11 PARALLEL VS PSEUDO PARALLEL -INVALID SEQUENCING OF MUTUALLY EXCLUSIVE REGIONS 9 MULTIPROGRAMMING WITH ONE CPU Single Threaded Processes 10 PARALLEL VS PSEUDO PARALLEL- VALID SEQUENCING OF MUTUALLY EXCLUSIVE REGIONS © 2007 M.C. PAULL 1 CONTENTS 17 MORE ABOUT IPC WITH PIPES

ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

21 TOPIC: FILES CHAPT 622 TOPIC: SECURITY, CHAPT 9

REVIEW OF THE TOPICS TO BE TREATED IN DETAIL

18 TOPIC: INPUT-OUTPUT BACKGROUND HARDWARE: CHAPT 5[2]

20 TOPIC: MEMORY MANAGEMENT:PROCESSES-CONTINUOUS; PAGED CHAPT 419 TOPICS: PROCESSES , DEADLOCK CHAPT 2, 3

23 THREADS (GLOBAL MEMORY, CONTROL OS OR PROCESS) CHAPT 224 SHARED MEMORY PROCESSORS AND THEIR OPERATING SYSTEMs CHAPT 8

12 UNIX PROCESS CREATION, THE FORK13 SHELL STRUCTURES USING FORK14 A SHELL USING FORK-TRACE15 CREATING PROCESSES FORK PARENT-CHILD RELATIONS

PROCESS CREATION AND USE IN UNIX-BASIC SYSTEM CALLS

16 UNIX OPERATING SYSTEM CALLS, LIBRARY FUNCTIONS, IPC

OPERATING SYSTEM (OS:)ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF COMPUTER SYSTEM AND HISTORIC OUTLINE

4 BOOTSTRAPPING Incremental Development Of Compilers And OS

6 HISTORY-SOME HIGHLIGHTS

3 OPERATING SYSTEM CALLS ARE “INTERPRETED” with LIBRARY ANDKERNEL CODE

2 PURPOSE OF OPERATING SYSTEM THE SHELL SYSTEM FUNCTIONS

5 THE OPERATING SYSTEM POSITION IN THE COMPUTER LANGUAGE HIERARCHY

BASIC: OPERATING SYSTEM IS USED AND IS NEEDED TO IMPLEMENT: (PSEUDO) PARALLEL PROCESSES[TIME-SHARING, MULTI-PROGRAMMING)CHAPTERS: PROCESSES AND MEMMORY MANAGEMENT.

7 MULTIPROGRAMMING-MANY PROCESS SIMULTANEOUSLY PRESENT(EACH GIVEN EPISODES OF CONTINUOUS TIME)

8 PROCESSES, THREADS, STATES

11 PARALLEL VS PSEUDO PARALLEL -INVALID SEQUENCING OF MUTUALLYEXCLUSIVE REGIONS

9 MULTIPROGRAMMING WITH ONE CPU Single Threaded Processes10 PARALLEL VS PSEUDO PARALLEL- VALID SEQUENCING OF MUTUALLY

EXCLUSIVE REGIONS

© 2007 M.C. PAULL 1CONTENTS

17 MORE ABOUT IPC WITH PIPES

Page 2: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

read(cmd)generateprocess

implementcmd

suicidewait

SHELL

IN

OUTdatesort

standard

cat

read(cmd)generateprocess

implementcmd

suicidewait

SHELL

INdatesort

standard

terminals

cat

OUT

OS

. . . .

I/O

FILES

PROCESSES. . .

MEMORY

TIMERS

. . . .

In fact the actual machine may be many machines, either all together in one location, or distributed. Somemay be specialized for handling one aspect of the necessary work, ex. a File Server. However, for the mostpart we will assume one actual main machine, and some specialized I/O handling machines.

PURPOSE OF OPERATING SYSTEM THE SHELL EXAMPLE

SHELL COMMANDS (Script Language)Uses System calls for implementation and provided with system, but not part of the operating system

% date >file /*date into file*/

% sort <file1 > file2 /*file1 into sort- result into file2*/=( file2,sort(file1) )

% cat file1 file2 file3 | sort >/dev/lp /* catenate file1, file2 result to file3 to sort then to output=( dev/lp, sort( cat(file1,file2,file3) ) )

% cat file1 file2 file3 | sort >/dev/lp & /*background*/

% ls a*.pdf; /*>=0 */ ls a?.pdf; / ?=1 char*/ ls a[0-9].pdf; /*any of 0 or 1, or ,...,or 9*/

%for fig in *.eps; do ps2pdf $fig; done /*creates pdf files for all eps files*/

EXAMPLES

EXAMPLE OF AN OPERATING SYSTEM SUPPLIED FUNCTIONS - THE SHELL

OPERATING SYSTEM PROVIDES A VIRTUAL MULTIPROCESSOR BY MANAGING RESOURCES

© 2007 M.C. PAULL 2

ACTUAL IO DEVICESMACHINE(S)

ACTUAL

MEMORY(S)

VIRTUAL LARGE MEMORY MULTI-FILE MULTIPROCESSOR MACHINE

OPERATING SYSTEMMANAGES RESOURCESterminals

controller

Disks! Tapes!What are they?I use Files.

It’s nice to havethe computer tomyself

Page 3: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

InterpretationsCompilations

libraryOS

x = x + y* z ----> ----> ; mv y,temp; mlt z,temp; add temp,x

os_open

Co

mp

iler+

LibraryR

outines

Code To Find File on disk,( rotate position headetct)bring part of File into MM-etc. this takes time so

+ + + + + +

if (fork() == 0)------> --->P1;

elseP2

MultiProcesses

-->sto “notes”,x1; sto “505“, x2, sto fdloc, y;call libraryforkstub

a[5]=7 ---> --->int a[100); ---> ---> establish location of a[0], say 18

mv 17,temp; add 5,temp; sto* 7,temp.

FilesIO

Dat Def

Arithmetic

stubswitch to supervisor mode

System Calls

User Code

SubroutineandSubroutineCall

returneventually

int *x,*y;int swap(int x,int y)

{int T;T=x; x=y; y=T; }

swap(pt1,pt2) ----->

machine code for swap procedure

load pt1 and pt2 retun address in stacktransfer to machine code for swap

------->return

----->

----->

Compiled Code

Check if resources for new process is available-NO return -1Yes: Allocate a Process Table, and space for

Chilld Process and a Unique pid.Set Child state to “Create” and copy Parent

information into space allocated for Child(including all memory andOpen file information] see (b).return

eventually

Code to set up ANOTHER processes

x=read(fd,buf,byts)------->

+ + + + + +switch to supervisor mode

--> sto &“buf”,x1; sto “byts“, x2, sto fd , y;call libraryread

stub

trap os_open

trap os_open

trap os_open

switch to supervisor modeThe Disk Driver is given fd number. From the in-coreInode in MM and File pointer, it determines the file blockit needs to read and sends this to the Disk ControllerIt blocks the calling process and does a context switch.When interrupt indicating completion arrives it

changes process state to ready

Compiler

Linker

LibraryRoutines

C-code+

LibraryFunctionNames

returneventually

fd= open(“notes/505”) ---> ->sto “notes”,x1; sto “505“, x2, sto fdloc, y; call library-open

#include<stdio>#include<stdlib>

Libraries

Memory

OPERATING SYSTEM CALLS ARE “INTERPRETED” with LIBRARY AND KERNEL CODE© 2005 M.C. PAULL 3

Page 4: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

Machine Language

C compiler writtenin Machine code

carries ball & platform

Machine Language Machine Language

Compiler

Machine Language

C compiler writtenin Machine code

C compiler writtenin Machine code

C++ compilerwritten in C code

C++ compilerwritten in C code

OS0

Compilerfor

L1 to L0

Compilerfor L2writtenin

L1

Compilerfor

L2 to L0

Compilerfor L3writtenin

L2

Compilerfor

L3 to L0

Compiler for L1 to L0 Writtenin L0 (Machine Language, ML)

Compilerfor L4writtenin L3

Compiled Code [Compiler]in L0 for L2 to L0

Compiled Codein L0 for L3 to L0 [Compiler]

Compiled Codein L0 for L4 to L0

[Compiler]

Ex:CompilerL0=Machine LangL1=C,L2=C++,L3=JAVA

[C->ML]in ML

[C++->ML]in C

[JAVA->ML]in C++

BOOTSTRAPPING Incremental Development Of Compilers And OS© 2005 M.C. PAULL 4

OS0

HandlesMultiProcesses

ProcessesFor IO

DISK etc

OS0

ProcessesFor Files

OS11-OS1n

OS11-OS1n

FileProcesses

In Cor M. L.

In C++

In C++

Page 5: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

THE OPERATING SYSTEM POSITION IN THE COMPUTER LANGUAGE HIERARCHY

Modularity, Information Hiding-Changes in codeand data structure at lower levels do not requirechanges at higher levels--abstraction

© 2005 M.C. PAULL 5

BASIC HARDWARE CONTROLMain memory: get,put, address in MD RegisterAlu:: add, or contents of register alu1 and alu2Disk :: start motor, position head information inregsTimers :: respond after time given in specifiedregister.Gates: between register pairs, virtual mem.

MMU,TLB

Each operation below can be specified by a bit string.Some bit positions represent an operation- otherpositions represent names of devices to be used.Thesebit strings are instructions which manipulate devicesbelow. The operations defined at this level are definedby a sequence of these instructions Those operationsinclude:.reg1--> reg2, reg --> memloc, send memory to fixedaddress on interrupt.

MICRO PROGRAM INSTRUCTION

MACHINE LANGUAGE INSTRUCTIONSEach operation at this level results from a sequence

ofµ-instructions which are called when these are issuedThese instructions must be translatedto their binary equivalents and loadedinto memory. This is done by arudimentary program in binary andBootstrapping*.

ADD r1, r2, r3JMP locMV r, memlocMV r1, r2

BASIC LANGUAGE FEATURESsubroutine calls, and interrupt handling again these

must be handled by an assembler. Develop rudimentarylevel language & bootstrap to higher level:

3

2

1

0

PRIMITIVE MULTIPLE PROCESS HANDLING forkBasic scheduling many processes between blocked

readya, active states, simple communication-shared MM(semaphores) between processes.

BASIC SECONDARY MEM HANDLING I/ODisk: Divers - Positioning heads transfer of data blocks

VIRTUAL MEMORY MANAGEMENT (VM)Paging allow user to assume a memory of size>n MM,

SOPHISTICATED MULTIPLE PROCESSESUsing virtual addresses of processes, files it

can access sophisticated scheduling--priorities,.

PROCESS COMMUNICATION, PIPESMessage Passing Between Processes-Computers

EXTERNAL DEVICES I/O printers, keyboards, monitor

FILES

structured create, destroy, read, write, open, closeAbstract unlike previous memory handling , tree

TABLES: EXTERNAL<->INTERNAL RELATIONSaccess rights

Operating System

Higher Level Assembly Language C, C++

BASIC HARDWARE CONTROL Gates in CPU-IOcontrol-DISK, TIMERS etc.

MICRO PROGRAM INSTRUCTIONSReg Transfers Memory, I/O Control, CPU Functions

MACHINE LANGUAGE INSTRUCTIONSBinary Machine Language-IO-control

BASIC LANGUAGE FEATURES Symbolic Machine --Assembly Language

System Calls, Assembly, Reserved Machine LanguageOperating System

Level i Instructions Call a Sequence OfLevel i-1 Instructions

Application Programs

0

1

2

3

5

6

4

7

Editors CommandCompilersex. EMACS ex. UNIX Shellex. FORTRAN

(+ System Calls + Reserved Machine)

IncrementalDevelopment

Compilers

Ref: Stallings pg69

Page 6: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

She walks in beauty, like the nightOf cloudless climes ansd starry skies;

And all thats best of dark and brightMeet in her aspect and her eyes:

Thus melllowed to that tender lightWich heaven to gaudy day denies.

One shade the more, on ray the lessHad half impaired the nameless grace

Which waves in every raven tress,Or softly lightens o’er her face;.................. Byron

Time Sharing-Scheduling

MM

control

nprogs

CPU

computerComputer =Central Command=CPU

MM=Fast Memory

computerdisk->print

computer

computer

card->disk disk->printcomputer

Spooling

MultiProgramming

$end

$run$load

$Fortran

prog

$job id

card->tape tape->printcomputer

operators

bins

programmer operators

punchedcard

MM

control1prog

CPUprog

ComponentsEvents

HISTORY-Some Highlights© 2005 M.C. PAULL 6

So we’ll go no more a-rovingSo late into the night,

Though the heart be still as loving,And the moon be still as bright.

For the sword outwears its sheath,And the soul wears out the breast,

And the heart must pause to breathe,And love itself have rest.

Though the night was made for loving,And the day returns too soon,

Yet we’ll go no more a rovingBy the light of the moon.....Byron

1845 Babbage, Ada Lovelace-Lord Byron*’s Daughter Before Civil War Gears1945 Aiken, Von Neumann US, End Of WW II Relays

Eckert-Maukley US, Zuse Ger., VacuumTuring England(Turing Machine GPC,All Computable Functions, Halting ProbNumerical-Calculations ) ( No Compilers)Binary-Plug Boards--->Punched CardsOne person did all

1955 2nd Gen1965 Assembler Rudementary Compiler Symbolic Addresses: Korean War Transistors

Numerical&Non-Numerical (Compilers-Fortran)(Personell, Inventories, etc) Punched Cards1st

Op Sys: Batch (Background Jobs)

1966 3rd GenOp Sys: CTS, MULTICS (Computer As UTILITY IBM 360)Shared with Many Users (G.E, AT&T, etc.)First Modern OPSys ICsFirst CS DepartmentsMINICOMPUTERS (vs UTILITY)Op Sys: UNIX

1980 4th Gen Microcomputers-PCs LSIOp Sys:One Users CP/M (Sold to user),DOS (Sold to IBM( bundle) B. GatesCommands typed at terminal.Op Sys:GUI-(Mouse, Menus, Icons,

Windows)Englhardt, Invents, XeroxImplements,

Jobs Apple ImplementsMuch later in MicrosoftWindow, SUN X VLSI

Op Sys:Distributed andNetwork Operating

Systems

Tubes

Vietnam War

Mem Cost1 cent/bit

magneticcore

memory

Iraq War I

Page 7: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

WAITING(P1)

BUSY(P1) READY(P2)

BUSY(P2)

IDLE

PRINT(P1)

P1 P2PrinterCPUIO

DONE PRINT

P1's P1' P2OSP1'sP1' P2OS

WithI/O

Context SwitchTime for Pi to leave and give control to PjTime To Save State of Pi and give control to Pj

Mono Programming1Process in MM* 2 or more Processes in MM*

© 2007 M.C. PAULL 7

MULTIPROGRAMMING-MANY PROCESS SIMULTANEOUSLY PRESENTEACH GIVEN EPISODES OF CONTINUOUS TIME

P1 P2OS P1 P2OS

NoI/O

Time

1 Efficient Use Of CPU (If IO and CPU can operate in Parallel)2 Problem conceptualization as Parallel Processes

1. Overhead of space, OS code, and Time Penalty Of ContextSwitch (Particularly when it is due to Quanta since the Processcould keep going-(When it is due to IO or mutual exclusion,blockage, etc. Process could not continue).

Multi Programming

Time

* MM=Main Memory=High Speed Memory Of Computer

I/OI/O

Pro

Con

Page 8: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

I/O Call

InterruptCompletion

Timeout

WaitingI/O,

Scheduled

Active

Ready

ThreadThread

Thread

quanta

Createsuccessfullycreated

using fork()

Basic Process States

P1 P2 PnPn-1....

Process Shared Mem

Multi-Programming With Threaded Processes

PROCESSES, THREADS, STATES

....ProcessTables

© 2007 M.C. PAULL 8

Threads

ProcessInternal

MEM

stateetc

Pj

OS Scheduler

ThreadScheduler?

Page 9: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

Operating System Involvement1 On Sys Calls: I/O, fork, exec,wait, 2 OnQuanta 3 I/O Interrupts

else

exec

I/O

f

proc X proc Y

end

?

read2

read1

write3

I2

end

I3

end

XShell

parent(P) child(ch)

loop

copy-code(cp)

I/O

I1

START a

e

i

j

b

c

d

g

h

k

i m

?

?

?

??

?

?

?

?

?

? Scheduler DecidesClock interrupt I/O I/O interrupt

I/O

end

proc X

statereturnaddr

coreimage

regs...proc Y

statereturnaddr

coreimage

regs...

Process Tables

Scheduler

InterruptHandlerparent

statereturnaddr

coreimage

regs...

Disks,Terminals,Clocks

I/O

...

FileHandler

MEMORY MANAGER

proc X proc Y

end

end

read1

read2

write3

if fork

exec

I1

I2I3

X

Shell

parent

loop

child

cp

START a

ef

i

j

OPERATING SYSTEM

X Y P copy overa A R Rb W A Rc R “ “d R R Ae A R Wf “ “ Rg R R Ah “ “ Af C-Ri A R R Rj W A R R

k R ‘ ‘ “l R -- R Achld

m R -- R -- Acpcdn A -- R -- R

Process

Ev

en

t

States

State Transitions

MULTIPROGRAMMING WITH ONE CPU Single Threaded Processes© 2007 M.C. PAULL 9

I/O

I/OI/Ok

I/O Call

InterruptCompletionTimeout

WaitingI/O,fork,

timeW

Scheduled

ActiveA

ReadyR

quanta

Createsuccessfullycreatedusing fork()

(completion of Ik)Ik

n

?

if fork

elseexec

read2

loop

if fork

~~

overwrite

copy

read2

fork

else

exec

elseexec

elseexec

elseexec

copy-code(cpcd)

overwritecopy

Page 10: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

CRIT-1

Process-1 Process-2

CRIT-2Subroutine

Can Share Code In CRITs since only once a CRIT starts thecorresponding Crit can not be started till the first completes

The end ofan instruction

The start of thenext instruction

1

2

CRIT-1

CRIT-3

CRIT-2

Process-2

Process-3CRIT-1

CRIT-2

Process-1

Process-3

True Parrallel In Separate Processors Pseudo-Parrallel In Single Processor

CRIT-3

at the same time Process-2

MDR <--- 1;MAR <--- IR.Shared(IR..<localreg>)<--MEM(MAR);MEM(MAR) <--- MDR

MDR <--- 1;MAR <--- IR.Shared>;(IR..<localreg>)<-- MEM(MAR);MEM(MAR) <--- MDR

Every Sequencing In Parallel Processor Operation In which Instruction executions do not OverlapIs Simulated By A Possible Multiprogramming Sequence: In Multiprogramming the InstructionExecutions are Atomic while in Parallel Operation,of Processors there can be overlap in Instruction executionand even in sub-instruction, i.e., register transfersPARALLEL VS PSEUDO PARALLEL- VALID SEQUENCING OF MUTUALLY EXCLUSIVE

REGIONS CRIT-1 and CRIT-3 are MUTUALLY EXCLUSIVE© 2005 M.C. PAULL 10

mv Shared, locAadd1, locAsto locA Shared

mv Shared, locbsub1, locBsto locB Shared

3Process-1

RegistersTranfersRegisters Tranfers Machine Instructions Machine Instructions

17

Code in CRIT-1 must not overlap with code in CRIT-2

Any overlapping of Register Transfers is possible Any overlapping of complete instructions is possible

1+

2

4

1

3

2

3

0

1

2+

3+

4+

0

4

t+

tt

t

Page 11: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

CRIT-1

CRIT-2

CRIT-3

Process-1 Process-2

Process-3

1

3

2

1

2

CRIT-1

CRIT-3

CRIT-2

3

Process-1 Process-2

Process-3

Parrallel Pseudo-Parrallel

PARALLEL VS PSEUDO PARALLEL -INVALID SEQUENCING OFMUTUALLY EXCLUSIVE REGIONS

at the same time

© 2005 M.C. PAULL 11

Every Sequencing In Parallel Processor Operation In which Instruction executionsdo not Overlap Is Simulated By A Possible Multiprogramming Sequence:

If CRIT Consists of 1 Atomic Operation therecannot be a problem

Even If CRIT Consists of 1 Atomic Operation therecan be a problem

18

Page 12: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

seperate pointerseven if same file

fork

Parent

Child

file which wasopened beforefork share acommon pointerin Par & Child

same MM till write

openparent

file

openchildfile

COMMON CODEif ( fork() != 0)

{ Parent Code }else{ Child Code }

COMMON CODEif ( fork() != 0)

{ Parent Code }else{ Child Code }

Parent Process

Child Process“Almost Clone”

At this point in Process an “Almost Clone” is produced, “Almost” becausefork() returns Child’s pid to Parent, and at this point returns 0 to Child .

?

If fork() returns -1 the systemwas unable to produceAlmost Clone.

main(){ printf(“my pid is %d\n”, getpid() );

printf(“my parent’s pid is %d\n”, getppid() );printf(“my group pid is %d\n”, getpgrp() );}

OS action on System Call: fork:Is resources for new Process available?-NO return -YES:1 Allocate a Process Table, and space for

Child Process and a Unique pid.2 Set Child state to “Create”and3 Copy Parent information into space

allocated for Child(including all memoryand Open file information] see (b) below

© 2005 M.C. PAULL 12UNIX PROCESS CREATION, THE FORK

?fork

Note: Information can be passed bewteen Processes thru a File, but not through MM as normally declared.(b)

About IDs

(a)

?fork

fork

Parent Process

(a)Child Process“Almost Clone”

Page 13: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

forkParent Process

execve

Child Process cmd Process

Parent Process

cmd Process

Parent Process

while(TRUE){ type_prompt();

read_cmd(cmd, parameters)

if (fork() != 0){ /*Parent code*/

waitpid(-1, &status,0);}

else{ /*Child code*/

execve(cmd, pars,0);}

}

waitpid

waitpid

Periods When Child Does Not Exist

Periods When Parent is is stopped at waitpid

Possible Sequence of CPU ActivityPar Child Cmd Par Child Cmd Par Child Cmdfork

Even though there are a number of different Processes involved only one is runnable at anyinstant,the others are either non-existent or in waiting mode during that time. This is not pseudoparalleloperation. Its more like Parent is calling a subroutine, or making a system call. The shelldesigner is scheduling the involved Processes herself.

SHELL STRUCTURES USING FORK

fork(Par and Child in MM Either one may run first)

FORK, WAITPID, EXECVE

Child Cmd Par Child Cmd Par Child Cmd Parfork Possible Sequence of CPU Activity

© 2005 M.C. PAULL 13

Periods When Child Does Not Exist

Periods When Parent is is stopped at waitpid

Page 14: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

fork()

parent code

waitpid(-1,&status,0)

child code

read_cmd(cmd,pars)

execve(cmd,pars,0)

fork()

parent code

waitpid(-1,&status,0)

child code

read_cmd(cmd,pars)

=0

execve(cmd,pars,0)

fork()

parent code

waitpid(-1,&status,0)

child code

read_cmd(cmd,pars)cp f1 f2

cmd: cppar1: f1par2: f2 =0

execve(cmd,pars,0)

fork()

parent code

waitpid(-1,&status,0)

child code

read_cmd(cmd,pars)

execve(cmd,pars,0)

fork()

parent code

waitpid(-1,&status,0)

child code

read_cmd(cmd,pars)

cmd: cppar1: f1par2: f2

execve(cmd,pars,0)

fork()

parent code

waitpid(-1,&status,0)

child code

read_cmd(cmd,pars)

=0

cmd: cppar1: f1par2: f2

execve(cp,f1,f2,0)

=0=0

=0

=0

GENERATE

main(3,ad,0)

ad:end

exit

main(3,ad,0)

cpf1f2

ad:end

exit

main(3,ad,0)

ad: cpf1f2

end

REPLACE

exit

main(3,ad,0)

ad:end

exit

cmd: cppar1: f1par2: f2

A SHELL USING FORK-TRACE

Immediately after the fork, parent and child have identical memory contents(until a store by either). Also they both have all files opened before the forkand have a common pointer to their location in these files.

© 2005 M.C. PAULL 14

childpid= 0

Page 15: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

fork

Par

fork

Par(Par) Par(Child)

Child

Par1Par)

fork

Par

ChildPar) Child(Child)

Child

fork

fork

Parent

Child

file which wasopened beforefork share acommon pointerin Par & Child

same MM till write

openparent

file

openchildfile

seperate pointerseven if same file

COMMON CODEif ( fork() != 0)

{ Parent Code }else{ Child Code } fork() returns 0 in child

fork() returns >0 (childs pid) in parent

(c)(b)

CREATING PROCESSES FORK PARENT-CHILD RELATIONS© 2005 M.C. PAULL 15

Par1(Child)

cond

Par2(Child)Par2(Par)

(a)

COMMON CODEif( fork() != 0)

{ Par }else

/*Child*/{ if( fork() != 0)

{ Child(Par) Code }else

{ Child(Child) }}

COMMON CODEif( fork() != 0){ /*Par*/if( fork() != 0) }Par(Par) }

elsePar(Child) }

else{Child }

COMMON CODEif(cond)

/*Par1 */{ if( fork() != 0)

{Par1(Par) }else{ Par1(Child) }

}else

/*Par2*/{ if (fork() != 0)

{ Par2(Par) }else{ Par2(Child) }

}

USES OF TWO FORKS

FORK BASICS

fork fork

Par1 Par2

Page 16: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

. . .

. . .

. . .

Application Code

CLibrary Functions

OS Kernel

system callslibrary calls

system calls

User Process

context switch

© 2005 M.C. PAULL 16

UNIX OPERATING SYSTEM CALLS, LIBRARY FUNCTIONS, IPC

Through File or “Pipe” (On Disk)

DISK

Through Shared Memoryset up through OS

P1 Signals Through OS To P2Set Up in P2 through OS detected by P2as soon as it runs no matter where it isin its Process.

Exceptions can be detected anywherewithin the run of the Process

SYNCHRONOUS(OCCURENCE WHERE SPECIFIED IN CODE)

ASYNCHRONOUSCan interrupt Process anywhere within aProcess sending control to a Handler Routine

INTER-PROCESS COMMUNICATION UNIX

C-code+

LibraryFunctionNames

Compiler

Linker

LibraryRoutines

S=Shared

mv S,L2mv S,L2

read Fwrite F

signalOS

OS

OS

Page 17: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

pipe fd[0]fd[1]

Parent fd[1]fd[0] Childpipe

X

X

Parent fd[1]fd[0]

Childpipe

Inter Process Communication Through A Pipe

Initially: access to both sides of pipe

#include “ourhdr.h”#define MAXLINE 1024int main(void);{ int fd[2];

pid_t pid;char line[MAXLINE];if ( pipe(fd) < 0 ) err_sys(“pipe error”); /*open-no name*/

if ( (pid = fork() ) < 0 ) err_sys(“fork error”);else

{ if (pid > 0){ /*parent code */

close( fd[0] );write(fd(1), “hello world\n”, 12); };

else{ /*child code */

close( fd[1] );n = read(fd(0), line, MAXLINE); };

}exit(0);

}

[Before Fork]

IPC: PIPES and FILESMutual Exlusive Access To Shared Memory Achieved by Passing All Requests (read writes) from

Involved Processes to be Executed by a ANOTHER (THIRD PARTY) Single Process (usually the kernel):In general pipes are circular buffers through which communication between two Processes is

established. When a pipe is created it is given a fixed size, (~4k bytes). A write of any length, or readrequest from a Process is executed atomically,i.e., mutually exclusively. If a pipe write will overflowthe pipe, or a read sees an empty pipe the Process will block or optionally returns a value which indicateswhether it worked. It behaves like a File (assumming that two references to a the same File by twoProcesses are mutually exclusive) with reads and writes of the same form, but unlike a File, thedata is stored can be MM in the kernel-making access to it faster and the restricted point of access forreading and writing. Like accessing Files the required mutual exclusion of reads and writes is achievedby having one Process, the handling requests from different Processes , executing one at a time.

When IPC is done with pipes the information is read out in the order it was written. Data is read and writtenin fixed size units. Pipes are read and written in sizes consistent with the maximum contents of the pipe andaccess to specified memory is atomic with respect to other accesses to the same specified memory

into pipe

OS out

int fd;char buf[7];

fd=open(/paull/inoutfile,RWONLY) /*open by name*/write(fd, “test it”, 7);read(fd, buf, 7);

File/paull/inoutfile

#include <unlistd.h>int fd[2];char buf[7];

pipe(fd); /*open-no name*/write(fd[1], “test it”, 7);read(fd[0], buf, 7);

PIPE (anonymous) In SINGLE PROCESS FILE IN SINGLE PROCESS

fd

pipefd[0]

fd[1]

Process

MORE ABOUT IPC WITH PIPES© 2007 M.C. PAULL 17

Page 18: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

TOPIC: INPUT-OUTPUT BACKGROUND HARDWARE:

If instruction is not in cache-page faultop sys must get instruction

Interrupt test aftereach machine instr.

interruptor trap-on which

instruction?

MMCPU FetchDecodeEx

Fetch Decode Ex

Fetch Decode Ex

Ex

Ex

Buffer

Simple

Pipelined

SuperScalar

Fetch Decode

Fetch Decode

CONTROL

ALUREGS

IO

micro-code

Operating System Works Directly With IO

With DMAMM buffermust not

be swapped

Video

VideoController

RAM

PrinterController

DISK

(driver) sectors, tracks, cylinder,

(user) read, write, file

(controller) move arms, heads, detect block#

DiskController

InterruptController

GpRegsPrgCntr StckPtr

PrgStatWd

InstrReg

On Chip Mem

Cache 1Cache 2

MainMem

BRIDGE

printerdriver

diskdriver

Op Sys

Graphics Adapter

DMA

C P U(Active Process)

© 2005 M.C. PAULL 18

User I/O -> Drivers -> Controller -> Device

RAM

Disk Cache

I/OBuffer

MM Mngmnt

MM Mngmnt

ChangingScreenContolWordsPictures

Page 19: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

I/O Call

IO Interrupt

Timeout(quantaused up)

WaitingI/O,

Sch

edul

edActive

Ready

Sleep(time)

Semaphor(down)

Page Fault

Page FaultFixed

Waitingtime xwaitpid

Timeout

up

WaitingExit

WaitingPage Flt

WaitingSemaphor

Createsuccessfullycreatedusing fork()

Scheduling:time consumedpriority

Memory:Page Tablestext,data,stackdisk loc

Event AwaitedState

Identity

Signals

PID,UID,GID

Blocked, SentRegisters SavedSystem Call Info

File Descriptor Tbl

AccountingCPU time usedLimits

fd

Process Table

fd

Must maintain much info for each Active Process

Process States

TOPICS: PROCESSES , DEADLOCK

Proc B

Proc CProc A’

Deadlock

wait(x)

signal(y)

wait(y)

signal(x)

Monitor: Higer Level Construct for

Holds

Needs

Proc A Proc B Proc C

CommonMemory*

fork Message

IPC Inter-Process Communication

Proc A’

*Not Global-Must Be SpeciallyDeclared.

CRIT

CRIT

CRIT Protection:Mutual Exclusion-Synchronization

semaphors, tsl, Peterson

Par

Chld

© 2005 M.C. PAULL 19

SCHEDULING

J3J4J5J1

0 1 2 3 4 5 6 7 8 9

Arriv

als

Shortest First Optimal

J2 turnaround

Turnaround=(1+2+3+5+9)/5=20/5=4

wr wrWITH I/O

97 |--- 6 ---| 91 |---5--| 109 109 109 109tmrd rd(6/5)90

80 80 80 80 |--- 6---| 74 74

90 90 90 |--4--| 135 135 |---6---| 129(6/4)90

Page 20: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

MainMemory(MM) RAM

..

......

....

....

....

MainMemory

RAMPartitionedInto Pages

}Page

MM

...

....

....

..

...

....

....

DISK

Virtual Memory} Page

...

....

....

VM

Partitioned Into Pages Paged & Virtual Memory

VM address

MMaddress

page|offset

page

DATA STRUCTURESTo Transform

VM to MM Address

to MMaddress

contains V address

PageTable

Inverted(Hashed)

PageTable

TOPIC: MEMORY MANAGEMENT (PROCESSES CONTINUOUS AND PAGED

offset

Pg Tbl Size, MM Page Access/ReplacementSpeed Page Replacement Algs

..

..

outin

freeList

..

© 2005 M.C. PAULL 20

DATA STRUCTURESTo Record Locations

Of Holes And Processes

LinkedLists

BuddyAlgorithm

011000111

..

.

Bit Map MemoryBlocks

2n

Holes and Processes

MM

hole

process

Finding A Hole Of Sufficient Size ReconstitutingHoles When Process Leaves

Indexed by VMaddr

Page 21: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

MS-DOS SYSTEM

DISK BLOCKS IN FILE

sampts tolinkedlin MMtable(1entry/block)

sally

Notestel#s

class/sam

/sally

416344

pic4txt

/sally/Notes

416505

/sam/notes

pic4txt

/sally/Notes/416

sam

/sally/tel#s

link(“/sam/notes/416" “sally/Notes/416")

/sam/notes/505 416505

/notes

/dev/fd0/notes/505

root

notes

mount(“/dev/fd0/notes”,”/sam/notes“ ,0)

DISK

notes

Links Hard and Symbolic

/dev/fd0

UNIX

I-NodesPoints to

Disk BlocksIn File

root

File Name: /sam/notes/505Working Directory: cd /sam

fd = open(“sally/notes/505”, O_RDONLY)n = read(fd, buffer, nbytes)

Special Files-DevisesBlock: Disk, /dev/hdlCharacter: Keyboard /dev/tty

Permanent or Semi-Permanent Recording Of:Information:Text,Program,Tables,Drawings,etc

A FILE IS

File Table/Process

in core file info

TOPIC: FILES© 2005 M.C. PAULL 21

UNIX and CP/M Point to Blocks,UNIX Indirectly CP/M Directly

DISK BLOCKS IN FILE

Link

File Structure On Disk

Data

Directory

System Calls For Files

File’s Name Associated With Disk Blocks Containing File

sampts toINODEwhich points toDisk Blocks in sam

Page 22: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

K1 K1

K2

K2

K3

K3

Secret Key

PrK1

PubK2 Pub

K 2

PubK1

PrK2

PubK 3

PubK1PrK3

PubK3

Public Key

Encryption

f (s)

f f (s)-passwd 4

f f f (s)-passwd 3

f f f f (s)-passwd 2

f f f f f (s)-passwd 1

f f f f f f (s)=e-givenf-1(e)

f-1f-1(e)

f-1f-1f-1(e)

f-1f-1f-1f-1(e)

f-1f-1f-1f-1f-1(e)

Hard

Easy

apply f(given)

fe 1(f-1(e))

initiallyknows e,f 2 f-1f-1(e) 3 f-1f-1f-1(e) 4 f-1f-1f-1f-1(e) 5 f-1f-1f-1f-1f-1(e)

1 f f-1(e )= e OK

2 f f-1f-1(e) = f-1(e)

3 f f-1f-1f-1(e) = f-1f-1(e)

4 f f-1f-1f-1f-1(e) = f-1f-1f-1(e)

5 f f-1f-1f-1f-1f-1(e) = f-1f-1f-1f-1(e)

1-Way (Trap-Door) Functions For Passing Password By Wire or Air

name Encrypted Password

paull

rbackwani

brussell

Unix Basic Password Table (Plan For Breakin Foiled!)

TOPIC: SECURITY

f

© 2005 M.C. PAULL 22

namepassword

encrypt

ξψζ....345 ωθρpaull

yesyes

1 or 2 / pair2 / individual

Passwords

1-way

(with inverse)

To All

Page 23: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

DataText1Text2Text3

OpenFiles

REGsPC:

Stack

REGsPC:

Stack

REGsPC:

Stack

UShared

process- iThread1 Thread2 Thread3

Data

Text

OpenFiles

REGsPC:

Stack

process-jindependent

Virtual Parallel Machine

TWO TYPES OF CONTEXT SWITCHES -PROCESS MUCH MEMORY SLOWSWITCH, THREAD LESS MEMORY SWITCH FAST SWITCH IMPROVED EFFICIENCY

I/OCompute

KERNEL

100 t0 1

Thread Quanta

I/O, Pg Flt, I/O, Pg Flt,

Data

Text1Text2

OpenFiles

REGsPC:

Stack

REGsPC:

Stack

UShared

process-kThread1 Thread2

I/O, Pg Flt,Thread Quanta

Virtual 3-CPU independent Parrallel MachineVirtual 2-CPU Parrallel MachineVirtual 1-CPU Parrallel Machine

sharedmemory ?

© 2005 M.C. PAULL 23

THREADS (GLOBAL MEMORY, CONTROL OS OR PROCESS)

Control (Scheduling): In Process? or In Kernel(OS) ?(Kernel must handle IO)

Page 24: ITS PURPOSE, INTEGRATION WITH OTHER COMPONENTS OF …paull/OS_INTRO.pdfprocess creation and use in unix-basic system calls 16 unix operating system calls, library functions, ipc operating

SHARED MEMORY MULTIPLE PROCESSOR COMPUTER

lock os

UserProcesses

system calls-acquire lock

IOlock

lock lock

MM

CPU 1 CPU 2 CPU nCPU 0

CPU 0 CPU 1 CPU 2 CPU ncache

IO

MM

LocalMemory

cache cachecache

LocalMemory

LocalMemory

LocalMemory

SHARED MEMORY PROCESSORS AND THEIR OPERATING SYSTEMS© 2005 M.C. PAULL 24

1 OS

1 Process at a time goes to OS (Bottleneck)

If Processes use different parts of OS then > 1 can go. Problem with interference.