Lecture 9: SHELL PROGRAMMING (continued) Creating shell scripts!

28
Lecture 9: SHELL PROGRAMMING (continued) Creating shell scripts!
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    241
  • download

    2

Transcript of Lecture 9: SHELL PROGRAMMING (continued) Creating shell scripts!

Lecture 9:SHELL PROGRAMMING

(continued)Creating shell scripts!

Making a File ExecutableNAMEchex-change a file to be executable

SYNOPSISchex filename

DESCRIPTIONThis is the outline for chexSelect shApply chmod u+x to file named as argument ($1)Inform user that file is executableUse ls-l to show the file modes

Labeling the output from wc: mywc

NAMEmywc-labeled word count SYNOPSISmywc filename DESCRIPTIONThis is the outline for mywcSelect shRun wc on $1 and capture output with setPrint the filename ($4)Print the number of lines ($1)Print the number of words ($2)Print the number of characters ($3)

Removing files safely: delNAMEdel- delete a file interactively SYNOPSISdel filename DESCRIPTIONThis is the outline for delSelect shGet the filename from the command line ($1)If there is no file with that nameprint an error message OtherwiseAsk if the user wants to delete the fileRead the user's choice (y/n)If the choice is yes (y)remove the file and print a messageotherwise print a message

A daily reminder system: tickle

NAMEtickle- a daily reminder service

SYNOPSIStickle

DESCRIPTIONThis is the outline for tickleSelect shUse set to capture the output from datePrint a messageCheck day ($1) and print an appropriate message

An improved spell programNAMEmyspell- an improved spelling-checker

SYNOPSISmyspell file

DESCRIPTIONHere is the outline for myspell:Select shRun shell on the file; for each misspellingdoRun grep to find lines with misspellingsPrint the misspelled wordPrint the line(s) containing misspellingsdone

Memory Allocation• Information in memory are used in many different

ways. Some classifications are:• Programming Language

– Instructions: specify the operations to be performed– Variables: information that change during the program

execution– Constants: information used as operands, but never change

• Changeable– Read-only: program code , constants– Read-Write: variables

Memory Allocation (ctd)• Initialised

– Yes: for code, constants, and some variables– No: Most variables

• Static or Dynamic location– Static: data are stored at a fixed address– Dynamic: the location can be changed if the memory is

rearranged (garbage collection, relocation)

• Binding time (when the memory is allocated to objects)– Static: location is determined before the program

execution (compiler, linker, loader)– Dynamic: location is defined at runtime and it may change

Memory Allocation (ctd)• What does a process in

memory look like?– In Unix OS, it is divided into

areas called segments

– Code (text) segment, Data and Stack segments

• Why a process has different segments?– Isolate read-only data from read-

write data

Code

Data

Heap

Stack

0x000...

0x7fff...

Memory Management• Memory management is vital in computer systems:

memory needs to be allocated efficiently to pack as many processes into memory as possible

• The OS process that manages memory is called memory manager

• The memory manager process has the following roles:– Keep track of which parts of memory is in use which parts are

not– Allocate memory to processes when they need it and de-

allocates it when they are done– Manage swapping between main memory and disk when the

main memory can not hold all the processes

Multiprogramming Systems

• Multiprogramming– Providing interactive service to several people

simultaneously

– Ability to have more than one process in memory at once in order to achieve reasonable performance

– Improving performance by attempting to keep the processor(s) busy all the time

• suspend processes when they are executing I/O operations

• run processes which are not executing I/O operations

I/O Performance

0

0.2

0.4

0.6

0.8

1

1.2

0 5 10 15

XY (Scatter) 1XY (Scatter) 2XY (Scatter) 3

Memory Systems

• During the process execution the memory unit– sees only a stream of memory addresses

– does not know how addresses are generated

• The memory unit is interested only in the sequence of memory addresses

Binding and Relocation• Compile Time

– Absolute code can be generated– Need to recompile if the starting location has changed– Example: MS-DOS “.COM” programs

• Load Time– Re-locatable code is generated at compile time– The binding is done at load time– No need to recompile if the starting location has changed

• Run-Time– The binding is done at the execution time– The process can be moved from one memory segment to another– Special hardware must be provided to implement this scheme (MMU)

MM Systems• MM systems can be divided into 2 classes:

– no swapping and no paging – Mono-programming– swapping and paging – Multiprogramming

OS in ROM

UserProgram

OS in RAM

OS in ROM

UserProgram

OS in RAM

UserProgramNo swapping &

No paging

Memory Organisation

• How should memory be organised in order to have more than one process in it ?

• First solution: divide the memory into n fixed partitions not necessarily equal

• Second solution: divide the memory into variable partitions (dynamic partitioning)

• Third solution: virtual memory technique

Fixed Partitions

OS

Partition 1

Partition 2

Partition 3

Partition 4

Multipleinput queues

OS

Partition 1

Partition 2

Partition 3

Partition 4

Singleinput queue

Fixed Partitions (ctd)• Advantages

– simple implementation and effective on batch systems

• Disadvantages– Create internal fragmentation problem– A multiple input queues strategy can lead to dramatic loss

of memory performance (when large partitions are empty and the queue for small jobs is full)

– Not suitable for a large number of processes– Not suitable for time-sharing model because of the

dynamic behaviour of the system– The size of processes is not taken into account

Variable Partitions

• Features

– Used when the number and size of the processes in memory vary dynamically

– The size of processes is taken into account– Number, location and size of the partitions vary

dynamically depending on the processes running in the system

– Creates external fragmentation problem– The external fragmentation can be solved by

compaction

Variable Partitions

O/S

Process space

128K

896K

O/S

Process space

128K

576K

Process 1 320K

O/S128K

352K

Process 1 320K

Process 2 224K

O/S128K

64K

Process 1 320K

Process 2 224K

Process 3288K

O/S128K

O/S128K

Process 1 320KProcess 1 320K

224K

Process 3288K

64K

Process 4 128K96K

Process 3288K

64K

O/S128K

320K

Process 4 128K96K

Process 3288K

64K

O/S128K

224K

Process 4 128K

96K

Process 3

96K

288K

Process 2

64K

Paging Technique

• Overlays– When a program is too big to fit in the available

memory, the programmer has to split it into pieces

– The swapping is done by the OS

• Paging– The physical memory is partitioned into small equal

fixed-size chunks (called frames)

– The logical memory is also divided into small and equal fixed-size chunks (called pages)

Paging Technique (ctd)

• Features– Internal fragmentation: only a fraction of the last page

of a process– No external fragmentation– In order to load the pages of a process into non

contiguous frames the OS needs page table for each process

– Logical address consists of a page number and an offset within the page

– Translation logical-to-physical address is done by the processor hardware

Paging Hardware

CPU

PhysicalMemory

Page table

P d

F d

P

LogicalAddress

PhysicalAddress

Paging Hardware

CPU

PhysicalMemory

Page table

P d

F d

P

Logical Address

PhysicalAddress

TLB hit

TLB miss

Analysis

• Definition– Hit ratio: percentage of times that a page

number is found in the associative registers

• Performance– Effective Access Time: – Hit ratio x TLB access time + Miss ratio x

Memory access time

Segmentation Technique• Segmentation

– The logical memory is divided into a number of segments of different length

– Logical address consists of two parts: segment number and an offset

• Features– With segmentation a process may occupy more than one

partition

– Internal fragmentation: solved

– External fragmentation: Not solved

CPU

PhysicalMemory

Segment table

S d

S

+

<

limit Base

YesNo

trap: addressing error