Download presentation
Presentation is loading. Please wait.
2
Lecture 9: SHELL PROGRAMMING (continued) Creating shell scripts!
3
Making a File Executable NAME chex-change a file to be executable SYNOPSIS chex filename DESCRIPTION This is the outline for chex Select sh Apply chmod u+x to file named as argument ($1) Inform user that file is executable Use ls-l to show the file modes
4
Labeling the output from wc: mywc NAME mywc-labeled word count SYNOPSIS mywc filename DESCRIPTION This is the outline for mywc Select sh Run wc on $1 and capture output with set Print the filename ($4) Print the number of lines ($1) Print the number of words ($2) Print the number of characters ($3)
5
Removing files safely: del NAME del- delete a file interactively SYNOPSIS del filename DESCRIPTION This is the outline for del Select sh Get the filename from the command line ($1) If there is no file with that name print an error message Otherwise Ask if the user wants to delete the file Read the user's choice (y/n) If the choice is yes (y) remove the file and print a message otherwise print a message
6
A daily reminder system: tickle NAME tickle- a daily reminder service SYNOPSIS tickle DESCRIPTION This is the outline for tickle Select sh Use set to capture the output from date Print a message Check day ($1) and print an appropriate message
7
An improved spell program NAME myspell- an improved spelling-checker SYNOPSIS myspell file DESCRIPTION Here is the outline for myspell: Select sh Run shell on the file; for each misspelling do Run grep to find lines with misspellings Print the misspelled word Print the line(s) containing misspellings done
9
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
10
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
11
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...
12
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
13
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
14
I/O Performance
15
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
16
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)
17
MM Systems MM systems can be divided into 2 classes: –no swapping and no paging – Mono-programming –swapping and paging – Multiprogramming OS in ROM User Program OS in RAM OS in ROM User Program OS in RAM User Program No swapping & No paging
18
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
19
Fixed Partitions OS Partition 1 Partition 2 Partition 3 Partition 4 Multiple input queues OS Partition 1 Partition 2 Partition 3 Partition 4 Single input queue
20
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
21
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
22
Variable Partitions O/S Process space 128K 896K O/S Process space 128K 576K Process 1320K O/S 128K 352K Process 1320K Process 2 224K O/S 128K 64K Process 1320K Process 2 224K Process 3 288K O/S 128K O/S 128K Process 1320K Process 1320K 224K Process 3 288K 64K Process 4128K 96K Process 3 288K 64K O/S 128K 320K Process 4128K 96K Process 3 288K 64K O/S 128K 224K Process 4128K 96K Process 3 96K 288K Process 2 64K
23
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)
24
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
25
Paging Hardware CPU Physical Memory Page table P d F d P Logical Address Physical Address
26
Paging Hardware CPU Physical Memory Page table P d F d P Logical Address Physical Address TLB hit TLB miss
27
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
28
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
29
CPU Physical Memory Segment table S d S + < limitBase Yes No trap: addressing error
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.