SS-Lab Manual (2018-22) - Student

Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

CS331 System Software Lab – Lab Manual Version 1.

DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

CS 331 SYSTEM SOFTWARE LAB

(AY:2020-21)

LAB MANUAL
Version number: 1.3

Prepared by:

Ms. Asha Raj


Ms. Sreenu G.
Assistant Professor
CSEDepartment

Department of Computer Science and Engineering Page 2


CS331 System Software Lab – Lab Manual Version 1.3

SYLLABUS

Course code Course Name L-T-P Year of


Credits Introduction

CS331 SYSTEM SOFTWARE LAB 0-0-3-1 2016


Prerequisite: Nil
Course Objectives

software.
Expected Outcome
The students will be able to
i. Compare and analyze CPU Scheduling Algorithms like FCFS, Round Robin, SJF, and
Priority.
ii. Implement basic memory management schemes likepaging.
iii. Implement synchronization techniques using semaphoresetc.
iv. Implement banker‘s algorithm for deadlockavoidance.
v. Implement memory management schemes and page replacement schemes and file allocation
and organizationtechniques.
vi. Implement system software such as loaders, assemblers and macroprocessor.

List of Exercises/Experiments: (Exercises/experiments marked with * are mandatory from each


part. Total 12 Exercises/experiments are mandatory)
Part A
1. Simulate the following non-preemptive CPU scheduling algorithms to find turnaround time
and waitingtime.
a)FCFS b) SJF c) RoundRobin (pre-emptive) d)Priority
2. Simulate the following file allocation strategies.
a) Sequential b) Indexed c) Linked
3. Implement the different paging techniques of memorymanagement.
4. Simulate the following file organization techniques*
a) Singlelevel directory b) Twolevel directory c) Hierarchical
5. Implement the banker‘s algorithm for deadlockavoidance.*
6. Simulate the following disk scheduling algorithms.*
a)FCFS b)SCAN c) C-SCAN
7. Simulate the following page replacement algorithms
a)FIFO b)LRU c)LFU
8. Implement the producer-consumer problem using semaphores.*
9. Write a program to simulate the working of the dining philosopher‘sproblem.*

Department of Computer Science and Engineering Page 3


CS331 System Software Lab – Lab Manual Version 1.3

Part B
10. Implement the symbol table functions: create, insert, modify, search, anddisplay.
11. Implement pass one of a two pass assembler.*
12. Implement pass two of a two pass assembler.*
13. Implement a single pass assembler.*
14. Implement a two pass macro processor *
15. Implement a single pass macro processor.
16. Implement an absoluteloader.
17. Implement a relocatingloader.
18. Implement pass one of a direct-linkingloader.
19. Implement pass two of a direct-linkingloader.
20. Implement a simple text editor with features like insertion / deletion of a character, word,and
sentence.
21. Implement a symbol table with suitable hashing.*

Department of Computer Science and Engineering Page 4


CS331 System Software Lab – Lab Manual Version 1.3

TABLE OF CONTENTS

CONTENT PAGE NO.


SL. NO.
I LIST OF EXPERIMENTS
PART – A

1. Simulate Dining Philosopher‘s Problem 6


Simulate Producer-Consumer Problem 8
2
Simulate CPU Scheduling Algorithms 11
3.
Implement Banker‘s Algorithm for Deadlock Avoidance 15
4.
Simulate File Organization Strategies 18
5.
Simulate Disk Scheduling Algorithms 25
6.
LIST OF EXPERIMENTS
II
PART – B
Implement Pass 1 of Two Pass Assembler 29
7.
Implement Pass 2 of Two Pass Assembler 32
8.
Implement Single Pass Assembler 35
9.
Implement Two Pass Macro Processor 38
10.
11. Implement Symbol Table Implementation 41
43
12. Implement Symbol Table with Hashing
Additional Experiments 45
III
Sample Viva Questions 48
IV
Sample Lab Exam Questions 50
V

Department of Computer Science and Engineering Page 5


CS331 System Software Lab – Lab Manual Version 1.3

EXPERIMENT 1

SIMULATE DINING PHILOSOPHER’S PROBLEM

AIM

To simulate Dining Philosopher‘s problem in C language.

DESCRIPTION

The Dining Philosopher Problem – The dining philosopher‘s problem is a classic synchronization
problem which is used to evaluate situations where there is a need of allocating multiple resources to
multiple processes. It assumes that 5 philosophers (represents processes) are seated around a circular
table with one chopstick (represents a resource) between each pair of philosophers. A philosopher
spends her life thinking and eating. In the center of the table is a bowl of noodles. When a
philosopher thinks, she does not interact with her colleagues. From time to time, a philosopher gets
hungry and tries to pick up the chopsticks that are between her and her left and right neighbours. A
philosopher may pick up only one chopstick at a time. A philosopher may eat if she can pick up the
two chopsticks adjacent to her. She eats without releasing the chopsticks. When she is finished eating,
she puts down both chopsticks and starts thinkingagain.

Semaphore Solution to Dining Philosopher – There are three states of philosopher : THINKING,
HUNGRY and EATING. Here there are two semaphores :Mutex and a semaphore array for the
philosophers. Mutex is used such that no two philosophers may access the pickup or putdown at the
same time. The array is used to control the behavior of each philosopher. But, semaphores can result
in deadlock due to programming errors.

ALGORITHM

1. START
2. Declare the number ofphilosophers.
3. Declare a semaphore mutex to acquire and release lock on eachchopstick.
4. Declare one semaphore perchopstick.

Department of Computer Science and Engineering Page 6


CS331 System Software Lab – Lab Manual Version 1.3

5. Create a thread for each philosopher.


6. Set initial state of each philosopher toTHINKING.
7. For each philosopher, repeat the followingsteps:
a. Set the state toHUNGRY.
b. Try to acquire the left and rightchopsticks.
c. If both chopsticks are available,then:
i. Set the state toEATING.
ii. Perform eating.
iii. Release the chopsticks.
iv. Signal the waiting philosophers, ifany.
d. Otherwise, wait till both the chopsticks areavailable.
8. STOP

OUTPUT

Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 2 is Hungry
Philosopher 1 is Hungry
Philosopher 3 is Hungry
Philosopher 4 is Hungry
Philosopher 5 is Hungry
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 5 putting fork 4 and 5 down
Philosopher 5 is thinking
Philosopher 4 takes fork 3 and 4
Philosopher 4 is Eating
Philosopher 1 takes fork 5 and 1
Philosopher 1 is Eating
Philosopher 4 putting fork 3 and 4 down
Philosopher 4 is thinking
Philosopher 3 takes fork 2 and 3
Philosopher 3 is Eating
Philosopher 1 putting fork 5 and 1 down
Philosopher 1 is thinking
Philosopher 5 is Hungry
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 3 putting fork 2 and 3 down
Philosopher 3 is thinking
Philosopher 2 takes fork 1 and 2
Philosopher 2 is Eating
Philosopher 1 is Hungry

RESULT

The Dining Philosopher‘s problem is simulated and output is verified.

Department of Computer Science and Engineering Page 7


CS331 System Software Lab – Lab Manual Version 1.3

EXPERIMENT 2

SIMULATE PRODUCER CONSUMER PROBLEM

AIM

To simulate producer consumer problem in C language.

DESCRIPTION
In computing, the producer–consumer problem (also known as the bounded-buffer problem) is a
classic example of a multi-process synchronization problem. The problem describes two processes,
the producer and the consumer, who share a common, fixed-size buffer used as a queue. The
producer's job is to generate data, put it into the buffer, and start again. At the same time, the
consumer is consuming the data (i.e., removing it from the buffer), one piece at a time. The problem
is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer
won't try to remove data from an emptybuffer.
The solution for the producer is to either go to sleep or discard data if the buffer is full. The next time
the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer
again. In the same way, the consumer can go to sleep if it finds the buffer empty. The next time the
producer puts data into the buffer, it wakes up the sleeping consumer. The solution can be reached by
means of inter-process communication, typically using semaphores.
A semaphore S is an integer variable that can be accessed only through two standard operations:
wait() and signal(). The wait() operation reduces the value of semaphore by 1 and the signal()
operation increases its value by 1. In this solution, we use one binary semaphore – mutex and two
counting semaphores – full and empty. ―Mutex‖ is for acquiring and releasing the lock on shared
buffer. ―Full‖ keeps track of number of items in the buffer at any given time and ―empty‖ keeps track
of number of unoccupied slots.

Initialization of semaphores
mutex = 1
full = 0 // Initially, all slots are empty. Thus full slots are 0
empty = n // All slots are empty initially

Solution for Producer


When producer produces an item then the value of ―empty‖ is reduced by 1 because one slot will be
filled now. The value of mutex is also reduced to prevent consumer to access the buffer. Now, the
producer has placed the item and thus the value of ―full‖ is increased by 1. The value of mutex is also
increased by 1 beacuse the task of producer has been completed and consumer can access the buffer.

Solution for Consumer


As the consumer is removing an item from buffer, therefore the value of ―full‖ is reduced by 1 and
the value is mutex is also reduced so that the producer cannot access the buffer at this moment. Now,
the consumer has consumed the item, thus increasing the value of ―empty‖ by 1. The value of mutex
is also increased so that producer can access the buffer now.

AGORITHM:
1. Declare 3 semaphores mutex, full,empty.
2. Initialize mutex to 1, full to 0 and empty ton.

Department of Computer Science and Engineering Page 8


CS331 System Software Lab – Lab Manual Version 1.3

3. Create separate thread for each producer andconsumer.


Producer
1. Produce theitem.
2. Wait on empty (if all n buffers are full) until a consumer consumes item from the bufferand
signals theproducer.
3. Acquire mutex lock to access the sharedbuffer.
4. Insert item intobuffer.
5. Release the mutexlock.
6. Signal (on full) the waiting consumer when a buffer isfull.
Consumer
1. Wait on full (if no buffer is full) until a producer inserts item into the buffer and signalsthe
consumer.
2. Acquire the mutex lock to access the sharedbuffer.
3. Consume item from thebuffer.
4. Release the mutexlock.
5. Signal (on empty) the waiting producer when a buffer isempty.

OUTPUT

Producer produces 1 item


Consumer consumes 1 item
Producer produces 1 item
Producer produces 2 item
Producer produces 3 item
Producer produces 4 item
Consumer consumes 4 item
Producer produces 4 item
Producer produces 5 item
Producer produces 6 item
Producer produces 7 item
Producer produces 8 item
Consumer consumes 8 item
Producer produces 8 item
Producer produces 9 item
Producer produces 10 item
Buffer isfull
Consumer consumes 10 item
Producer produces 10 item
Buffer isfull
Consumer consumes 10 item
Producer produces 10 item
Buffer isfull
Consumer consumes 10 item
Producer produces 10 item
Buffer isfull

Department of Computer Science and Engineering Page 9


CS331 System Software Lab – Lab Manual Version 1.3

Consumer consumes 10 item


Producer produces 10 item
Buffer isfull
Consumer consumes 10 item
Producer produces 10 item
Buffer isfull
Consumer consumes 10 item
Producer produces 10 item
Buffer isfull
Consumer consumes 10 item

RESULT

The Producer consumer‘s problem is simulated and output is verified.

Department of Computer Science and Engineering Page 10


CS331 System Software Lab – Lab Manual Version 1.3

EXPERIMENT 3

SIMULATE CPU SCHEDULING ALGORITHMS

AIM
To simulate the following CPU Scheduling techniques:
a) First Come First Serve(FCFS)
b) Shortest Job First(SJF)
c) Round Robinscheduling
d) Priorityscheduling

DESCRIPTION

CPU scheduling deals with the problem of deciding which of the processes in the ready queue is to be
allocated the CPU.

1. FCFS

First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest
scheduling algorithm. FIFO simply queues processes in the order that they arrive in the ready
queue. In this, the process that comes first will be executed first and next process starts only after
the previous gets fully executed. Here we are considering that arrival time for all processes is0.

Completion Time: Time at which process completes its execution.

Turn Around Time (Total time): Time difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time

Waiting Time: Time difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time (Service time)

2. SJF

Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process
with the smallest execution time to execute next. SJN is a non-preemptive algorithm. It has the
advantage of having minimum average waiting time among all scheduling algorithms. It is a
greedy algorithm. It may cause starvation if shorter processes keep coming. This problem can be
solved using the concept of aging. It is practically infeasible as operating system may not know
burst time and therefore may not sort them. While it is not possible to predict execution time,
several methods can be used to estimate the execution time for a job, such as a weighted average
of previous execution times. SJF can be used in specialized environments where accurate
estimates of running time are available.

3. Round RobinScheduling
Round Robin is a CPU scheduling algorithm where each process is assigned a fixed time slot in
a cyclic way. It is simple, easy to implement, and starvation-free as all processes get fair share of
CPU. One of the most commonly used technique in CPU scheduling as a core. It is preemptive
as processes are assigned CPU only for a fixed slice of time at most. The disadvantage of it is
more overhead of contextswitching.

4. PriorityScheduling

Department of Computer Science and Engineering Page 11


CS331 System Software Lab – Lab Manual Version 1.3

Priority scheduling is one of the most common scheduling algorithms in batch systems. Each
process is assigned a priority. Process with the highest priority is to be executed first and so on.
Processes with the same priority are executed on first come first served basis. Priority can be
decided based on memory requirements, time requirements or any other resource requirement.

ALGORITHM

FCFS
1. Get the number ofprocesses.
2. Get the ID and service time for eachprocess.
3. Initially, waiting time of first process is zero and total time for the first process is the service time
of thatprocess.
4. Calculate the total time and processing time for the remaining processes asfollows:
a. Waiting time of a process is the total time of the previousprocess.
b. Total time of a process is calculated by adding its waiting time and servicetime.
5. Total waiting time is calculated by adding the waiting time of eachprocess.
6. Total turn around time is calculated by adding the total time of eachprocess.
7. Calculate average waiting time by dividing the total waiting time by total number ofprocesses.
8. Calculate average turn around time by dividing the total turn around time by the number of
processes.
9. Display theresult.

SJF
1. Get the number ofprocesses.
2. Get the ID and service time for eachprocess.
3. Initially the waiting time of first short process is set as 0 and total time of first short process is
taken as the service time of thatprocess.
4. Calculate the total time and waiting time of remaining processes asfollows:
a. Waiting time of a process is the total time of the previousprocess.
b. Total time of a process is calculated by adding the waiting time and service timeof
eachprocess.
5. Total waiting time is calculated by adding the waiting time of eachprocess.
6. Total turn around time is calculated by adding the total time of eachprocess.
7. Calculate average waiting time by dividing the total waiting time by total number of
processes.
8. Calculate average turn around time by dividing the total turn around time by total number of
processes.
9. Display theresult.

Round Robin
1. Get the number ofprocesses.
2. Get the process id, burst time and arrival time for each of theprocesses.
3. Create an array rem_bt[] to keep track of remaining burst time of processes. This arrayis
initially a copy of bt[] (burst timesarray).
4. Create another array wt[] to store waiting times of processes. Initialize this array as0.
5. Initialize time : t =0
6. Find waiting time for each process by traversing all the processes while all processes arenot
done. Do the following for i-th process if it is not doneyet.
a. If rem_bt[i] > quantum
i) t = t +quantum
ii) bt_rem[i] = bt_rem[i] -quantum;

Department of Computer Science and Engineering Page 12


CS331 System Software Lab – Lab Manual Version 1.3

b. else // Last cycle for this process


i) t = t +bt_rem[i];
ii) wt[i] = t -bt[i]
iii) bt_rem[i] = 0; // This process isover

7. Compute turn around time for each process ias:


tat[i] = wt[i] +bt[i]
8. Total waiting time is calculated by adding the waiting time of eachprocess.
9. Total turn around time is calculated by adding the total time of eachprocess.
10. Calculate average waiting time by dividing the total waiting time by total numberof
processes.
11. Calculate average turn around time by dividing the total turn around time by total number of
processes.
12. Display theresult.

Priority Scheduling

1. Get the number ofprocesses.


2. Get the process id, service time and priority for eachprocess.
3. Sort the processes according to thepriority.
4. Now, apply FCFSalgorithm:
a. Initially, waiting time of first process is zero and total time for the first process isthe
service time of thatprocess.
b. Calculate the total time and processing time for the remaining processes asfollows:
i. Waiting time of a process is the total time of the previousprocess.
ii. Total time of a process is calculated by adding its waiting time and servicetime.
5. Total waiting time is calculated by adding the waiting time of eachprocess.
6. Total turn around time is calculated by adding the total time of eachprocess.
7. Calculate average waiting time by dividing the total waiting time by total number of
processes.
8. Calculate average turn around time by dividing the total turn around time by total number of
processes.
9. Display theresult.

OUTPUT

FCFS

SJF

Department of Computer Science and Engineering Page 13


CS331 System Software Lab – Lab Manual Version 1.3

Round Robin Scheduling

Priority Scheduling

RESULT
The CPU scheduling algorithms are simulated and output is verified.

Department of Computer Science and Engineering Page 14


CS331 System Software Lab – Lab Manual Version 1.3

EXPERIMENT 4

BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE

AIM

To implement Banker‘s Algorithm for deadlock avoidance in C language.

DESCRIPTION
The Banker's algorithmis a resource allocation and deadlock avoidance algorithm developed by
EdsgerDijkstra that tests for safety by simulating the allocation of predetermined maximum possible
amounts of all resources, and then makes an "s-state" check to test for possible deadlock conditions
for all other pending activities, before deciding whether allocation should be allowed to continue.
Following Data structures are used to implement the Banker‘s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available :
 It is a 1-d array of size ‘m’ indicating the number of available resources of eachtype.
 Available[ j ] = k means there are ‘k’ instances of resource type Rj.
Max :
 It is a 2-d array of size ‗nxm’ that defines the maximum demand of each process in asystem.
 Max[ i, j ] = k means process Pimay request at most ‘k’ instances of resource type Rj.
Allocation :
 It is a 2-d array of size ‘nxm’ that defines the number of resources of each type currently
allocated to eachprocess.
 Allocation[ i, j ] = k means process Piis currently allocated ‘k’ instances of resource type Rj
Need :
 It is a 2-d array of size ‘nxm’ that indicates the remaining resource need of eachprocess.
 Need[i, j ] = k means process Picurrently need ‘k’ instances of resource type Rjfor its
execution.
 Need [ i, j ] = Max [ i, j ] – Allocation [ i, j]

Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional
resources that process Pi may still request to complete its task.
Banker‘s algorithm consists of Safety algorithm and Resource request algorithm.
ALGORITHM
Safety Algorithm
The algorithm is for finding out whether or not a system is in a safe state and can be described as
follows:
1) Let Work and Finish be vectors of length ‗m‘ and ‗n‘respectively.
Initialize: Work =Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such thatboth
a) Finish[i] =false
b) Needi<=Work
If no such i exists goto step (4).

Department of Computer Science and Engineering Page 15


CS331 System Software Lab – Lab Manual Version 1.3

3) Work = Work + Allocationi


Finish[i] =true
Goto step (2)
4)If Finish [i] = true for all i then
the system is in a safestate.

Resource-RequestAlgorithm

Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k instances of
resource type Rj. When a request for resources is made by process Pi, the following actions are taken:

1) If Requesti<=Needi
Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum claim.
2) If Requesti<=Available
Goto step (3); otherwise, Pi must wait, since the resources are not available.
3) Assuming that the system has allocated the requested resources to process Pimodifying the stateas
follows:
Available = Available –
RequestiAllocationi = Allocationi +
RequestiNeedi = Needi – Requesti

If the resulting resource-allocation state is safe, the transaction is completed, and process Pi is
allocated its resources. However, if the new state is unsafe, then Pi must wait for Requesti , and the od
resource-allocation state is restored.

OUTPUT

Enter the number of resources: 4


Enter the number of processes: 5
Enter the process id for process initiating request: 3
Enter Claim Vector: 8 5 9 7
Enter Allocated Resource Table: 2 0 1 1 0 1 2 1 4 0 0 3 0 2 1 0 1 0 3 0
Enter Maximum Claim table: 3 2 1 4 0 2 5 2 5 1 0 5 1 5 3 0 3 0 3 3
The Claim Vector is: 8 5 9 7
The Allocated Resource Table:
2 0 1 1
0 1 2 1
4 0 0 3
0 2 1 0
1 0 3 0
The Maximum Claim Table:
3 2 1 4
0 2 5 2

Department of Computer Science and Engineering Page 16


CS331 System Software Lab – Lab Manual Version 1.3

5 1 0 5
1 5 3 0
3 0 3 3
Allocated resources: 7 3 7 5
Available resources: 1 2 2 2
Process3 isexecuting.
The process is in safe state.
Available vector: 5 2 2 5
Process1 isexecuting.
The process is in safe state.
Available vector: 7 2 3 6
Process2 isexecuting.
The process is in safe state.
Available vector: 7 3 5 7
Process4 isexecuting.
The process is in safe state.
Available vector: 7 5 6 7
Process5 isexecuting.
The process is in safe state.
Available vector: 8 5 9 7

RESULT

The Banker‘s algorithm for deadlock avoidance is simulated and output is verified.

Department of Computer Science and Engineering Page 17


CS331 System Software Lab – Lab Manual Version 1.3
EXPERIMENT 5

SIMULATE FILE ORGANIZATION TECHNIQUES

AIM

To implement the following file organization techniques:


a) Single leveldirectory
b) Two level directory
c) Hierarchical

DESCRIPTION

The directory structure is the organization of files into a hierarchy of folders. In a single-level
directory system, all the files are placed in one directory. There is a root directory which has all files.
It has a simple architecture and there are no sub directories. Advantage of single level directory
system is that it is easy to find a file in the directory.

In the two-level directory system, each user has own user file directory (UFD). The system maintains
a master block that has one entry for each user. This master block contains the addresses of the
directory of the users. When a user job starts or a user logs in, the system's master file directory
(MFD) is searched. When a user refers to a particular file, only his own UFD is searched. This
effectively solves the name collision problem and isolates users from one another.

Hierarchical directory structure allows users to create their own subdirectories and to organize their
files accordingly. A tree is the most common directory structure. The tree has a root directory, and
every file in the system has a unique path name. A directory (or subdirectory) contains a set of files or
subdirectories.

Department of Computer Science and Engineering Page 18


CS331 System Software Lab – Lab Manual Version 1.3

ALGORITHM

a) Single leveldirectory

1. Create a directory structure to store a single directory and multiplefiles.


2. Enter a directoryname.
3. Perform the followingoperations:
a. Createfile
i. Acceptfilename.
ii. Increment filecount.
iii. Update the file informationtable.
b. Deletefile
i. Accept the name of file to bedeleted.
ii. Compare the filename with names of existingfiles.
iii. If a match is found,then
1. Delete the file by updating file informationtable.
2. Decrement the filecount.
iv. Otherwise, display ‗File notfound!‘.
c. Searchfile
i. Accept the name of file to besearched.
ii. Compare the filename with names of existingfiles.
iii. If a match is found,then
1. Display ‗File isfound!‘
iv. Otherwise, display ‗File notfound!‘.
d. Displayfiles
i. Check if directory isempty.
ii. If yes, print ‗Directoryempty!‘.
iii. Otherwise, print information of each file from the file informationtable.
4. EXIT

Department of Computer Science and Engineering Page 19


CS331 System Software Lab – Lab Manual Version 1.3

b) Two leveldirectory
1. Create a structure to store details of multiple directories and multiple files for each of the
directories.
2. Perform the followingoperations:
a. Create directory
i. Accept the directoryname.
ii. Increment the directorycount.
iii. Set file count as0.
iv. Update the directory informationtable.
v. Display ‗Directorycreated!‘.
b. Createfile
i. Accept the directoryname.
ii. Compare the directory name with names of existingdirectories.
iii. If match is found, then
1. Accept the filename.
2. Increment file count for thisdirectory.
3. Update corresponding file informationtable.
iv. Otherwise, print ‗Directory notfound!‘.
c. Deletefile
i. Accept the directoryname.
ii. Compare the directory name with names of existingdirectories.
iii. If match is found, then
1. Accept the name of file to bedeleted.
2. Compare the filename with names of existing files in thisdirectory.
3. If a match is found,then
a. Delete the file by updating corresponding file informationtable.
b. Decrement the file count for thisdirectory.
4. Otherwise, display ‗File notfound!‘.
iv. Otherwise, display ‗Directory notfound!‘
d. Searchfile
i. Accept the directoryname.
ii. Compare the directory name with names of existingdirectories.
iii. If match is found, then
1. Accept the name of file to bedeleted.
2. Compare the filename with names of existing files in thisdirectory.
3. If a match is found,then
a. Display ‗Filefound!‘
4. Otherwise, display ‗File notfound!‘.
iv. Otherwise, display ‗Directory notfound!‘
e. Displayfiles
i. Accept the directoryname.
ii. Compare the directory name with names of existingdirectories.
iii. If match is found, then
1. Check if directory isempty.
2. If yes,then
a. Display ‗Directoryempty!‘
3. Otherwise, display the information of files in thatdirectory.
iv. Otherwise, display ‗Directory notfound!‘.
3. EXIT

Department of Computer Science and Engineering Page 20


CS331 System Software Lab – Lab Manual Version 1.3

c) Hierarchical
1. Create a rootdirectory.
2. Create subdirectories and files under root directory asrequired.
3. Create subdirectories and files under each subdirectory as required resulting in a tree
structure.
4. Display the treestructure.

OUTPUT

Single level:

Enter name of directory -- CSE

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- A

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- B

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 4

The Files are -- A B C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 3

Enter the name of the file – ABC

File ABC not found


1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 2

Enter the name of the file – B


File B is deleted

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 5

Two Level

1. Create Directory 2. Create File 3. Delete File

Department of Computer Science and Engineering Page 21


CS331 System Software Lab – Lab Manual Version 1.3

4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR1


Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR2

Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR1


Enter name of the file -- A1
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR1


Enter name of the file -- A2
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR2


Enter name of the file -- B1
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 5
Directory Files
DIR1 A1 A2
DIR2 B1

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 4

Enter name of the directory – DIR


Directory not found

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 3
Enter name of the directory – DIR1
Enter name of the file -- A2

File A2 is deleted

Department of Computer Science and Engineering Page 22


CS331 System Software Lab – Lab Manual Version 1.3

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice – 6

Hierarchical

Enter Name of dir/file (under root): ROOT


Enter 1 for Dir / 2 For File : 1
No of subdirectories / files (for ROOT) :2
Enter Name of dir/file (under ROOT):USER 1
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER 1):1
Enter Name of dir/file (under USER 1):SUBDIR
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR):2
Enter Name of dir/file (under USER 1):
JAVA Enter 1 for Dir /2 for file:1
No of subdirectories /files (for JAVA): 0
Enter Name of dir/file (under SUBDIR):VB
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for VB): 0

Enter Name of dir/file (under ROOT):USER2


Enter 1 for Dir /2 forfile:1
No of subdirectories /files (for USER2):2
Enter Name of dir/file (under ROOT):A
Enter 1 for Dir /2 forfile:2
Enter Name of dir/file (under USER2):SUBDIR2
Enter 1 for Dir /2 forfile:1
No of subdirectories /files (for SUBDIR 2):2

Enter Name of dir/file (under SUBDIR2):PPL


Enter 1 for Dir /2 forfile:1
No of subdirectories /files (for PPL):2
Enter Name of dir/file (under PPL):B
Enter 1 for Dir /2 forfile:2
Enter Name of dir/file (under
PPL):CEnter 1 for Dir /2 forfile:2
Enter Name of dir/file (under
SUBDIR):AIEnter 1 for Dir /2 forfile:1
No of subdirectories /files (for AI): 2
Enter Name of dir/file (under AI):D
Enter 1 for Dir /2 forfile:2
Enter Name of dir/file (under

AI):EEnter 1 for Dir /2 forfile:2

Department of Computer Science and Engineering Page 23


CS331 System Software Lab – Lab Manual Version 1.3

RESULT

Single level, two-level and hierarchical directory structures were simulated and the programs
executedsuccessfully.

Department of Computer Science and Engineering Page 24


CS331 System Software Lab – Lab Manual Version 1.3

EXPERIMENT 6

SIMULATE DISK SCHEDULING ALGORITHMS

AIM

To implement the following disk scheduling algorithms:


a) FCFS – First Come First Serve
b) SCAN
c) C-SCAN

DESCRIPTION

Disk scheduling is done by operating systems to schedule I/O requests arriving for the disk. Disk
scheduling is also known as I/O scheduling.
a) FCFS
FCFS is the simplest of all the disk scheduling algorithms. In FCFS, the requests are addressed in the
order they arrive in the disk queue.

Advantages:
 Every request gets a fairchance.
 No indefinite postponement of anyrequest.
Disadvantages:
 Does not try to optimize the seektime.
 May not provide the best possibleservice.

b) SCAN
In SCAN algorithm, the disk arm moves in a particular direction and services the requests coming in
its path and after reaching the end of disk, it reverses its direction and again services the request
arriving in its path. So, this algorithm works as an elevator and hence also known as elevator
algorithm. As a result, the requests at the midrange are serviced more and those arriving behind the
disk arm will have to wait.

Advantages:
 Highthroughput
 Low variance of responsetime
 Average responsetime
Disadvantages:
 Long waiting time for requests for locations just visited by diskarm

c) C-SCAN
In SCAN algorithm, the disk arm again scans the path that has been scanned, after reversing its
direction. So, it may be possible that too many requests are waiting at the other end or there may be
zero or few requests pending at the scanned area. These situations are avoided in C-SCAN algorithm
in which the disk arm instead of reversing its direction goes to the other end of the disk and starts
servicing the requests from there. So, the disk arm moves in a circular fashion and this algorithm is
also similar to SCAN algorithm and hence it is known as C-SCAN (Circular SCAN).

Advantages:
 Provides more uniform wait time compared toSCAN.

Department of Computer Science and Engineering Page 25


CS331 System Software Lab – Lab Manual Version 1.3

ALGORITHM

FCFS:
1. Let request array represent an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‗head‘ is the position of diskhead.
2. One by one take the tracks in default order and calculate the absolute distance of the track from
thehead.
3. Add this distance to the total headmovement.
4. Currently serviced track position now becomes the new headposition.
5. Go to step 2 until all tracks in request array have not beenserviced.

SCAN:
1. Let request array represent an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‗head‘ is the position of diskhead.
2. Let direction represent whether the head is moving towards left orright.
3. In the direction in which head is moving, service all tracks one byone.
4. Calculate the absolute distance of the track from thehead.
5. Add this distance to the total headmovement.
6. Currently serviced track position now becomes the new headposition.
7. Go to step 3 until one of the ends of the disk isreached.
8. If end of the disk is reached, reverse the direction and go to step 2 until all tracks in request array
have not beenserviced.

C-SCAN:
1. Let request array represent an array storing indexes of tracks that have been requestedin
ascending order of their time of arrival. ‗head‘ is the position of diskhead.
2. Let direction represent whether the head is moving towards left orright.
3. In the direction in which head is moving, service all tracks one byone.
4. Calculate the absolute distance of the track from thehead.
5. Add this distance to the total headmovement.
6. Currently serviced track position now becomes the new headposition.
7. Go to step 3 until one of the ends of the disk isreached.
8. If end of the disk is reached, set the head position at the other end and go to step 3 until all tracks
in request array have not beenserviced.

OUTPUT

Enter the maximum number of cylinders : 200


Enter number of queue elements8
Enter the work queue95
180
34
119
11
123
62
64

Enter the disk head startingposition:


50

Department of Computer Science and Engineering Page 26


CS331 System Software Lab – Lab Manual Version 1.3

MENU
1. FCFS
2. SCAN
3. C-SCAN
4. EXIT

Enter your choice: 1


FCFS
*****
Total seek time : 644

MENU
1. FCFS
2. SCAN
3. C-SCAN
4. EXIT

Enter your choice: 3


Enter the previous disk head position:
199

C-SCAN
*****
Total seek time : 386
MENU
1. FCFS
2. SCAN
3. C-SCAN
4. EXIT

Enter your choice: 3


Enter the previous disk head position:
5

C-SCAN
*****
Total seek time : 382

MENU
1. FCFS
2. SCAN
3. C-SCAN
4. EXIT

Enter your choice: 2


Enter the previous disk head position:
50

SCAN
*****
Total seek time : 230

Department of Computer Science and Engineering Page 27


CS331 System Software Lab – Lab Manual Version 1.3

MENU
1. FCFS
2. SCAN
3. C-SCAN
4. EXIT

Enter your choice:4

RESULT

Thus the C program to implement the disk scheduling algorithms namely FCFS, SCAN and C-SCAN
algorithms was written and executed successfully. The obtained outputs were verified.

Department of Computer Science and Engineering Page 28


CS331 System Software Lab – Lab Manual Version 1.3

PART – B

EXPERIMENT 7

IMPLEMENTATION OF PASS ONE OF A TWO PASS ASSEMBLER

AIM

To write a program to implement pass one of a two pass assembler.

DESCRIPTION

Assembler is a program for converting instructions written in low-level assembly code into
relocatable machine code and generating along information for the loader. It generates instructions
by evaluating the mnemonics (symbols) in operation field and find the value of symbol and literals
to produce machine code. Now, if assembler do all this work in one scan then it is called single pass
assembler, otherwise if it does in multiple scans then called multiple pass assembler. Here,
assembler divides these tasks in twopasses:

Pass-1:
a) Define symbols and literals and remember them in symbol table and literal tablerespectively.
b) Keep track of locationcounter
c) Processpseudo-operations

ALGORITHM
Begin
read first input line;
if OPCODE = 'START' then
begin
save #[OPERAND] as starting address
initialized LOCCTR to starting address
write line to intermediate file
read next input line
end {if START}
else
initialize LOCCTR to 0
while OPCODE != 'END'
do begin
if this is not a comment line then
begin
if there is a symbol in the LABEL field then begin
search SYMTAB for LABEL
if found then
set error flag (duplicate symbol)
else
insert (LABEL, LOCCTR) into SYMTAB
end {if symbol}
search OPTAB for OPCODE
if found then
add 3 {instruction length} to LOCCTR

Department of Computer Science and Engineering Page 29


CS331 System Software Lab – Lab Manual Version 1.3

else if OPCODE = 'WORD' then


add 3 to LOCCTR
else if OPCODE = 'RESW' then
add 3 * #[OPERAND] to LOCCTR
else if OPCODE = 'RESB' then
add #[OPERAND] to LOCCTR
else if OPCODE = 'BYTE' then
begin
find length of constant in bytes
add length to LOCCTR
end {if BYTE}
else
set error flag (invalid operation code)
end {if not a comment}
write line to intermediate file
read next inputline
end {while not END}
write last line to intermediate file
save (LOCCTR - starting address) as program length
end

OUTPUT
INPUT FILES:

input.txt
COPY START 1000
LDA ALPHA
ADD ONE
SUB TWO
STA BETA
ALPHA BYTE C‘KLNCE‘
ONE RESB 2
TWO WORD 5
BETA RESW 1
END

OPTAB.txt
LDA 00
STA 0C
ADD 18
SUB 1C

OUTPUT FILES:
The length of the program is 19

SYMTAB.txt
ALPHA 100C
ONE 1011
TWO 1013
BETA 1016

Department of Computer Science and Engineering Page 30


CS331 System Software Lab – Lab Manual Version 1.3

output.txt
COPY START 1000
1000 LDA ALPHA
1003 ADD ONE
1006 SUB TWO
1009 STA BETA
100C ALPHA BYTE C‘KLNCE‘
1011 ONE RESB 2
1013 TWO WORD 5
1016 BETA RESW 1
1019 END

RESULT

The pass 1 of a two pass assembler was simulated successfully and the output was verified.

Department of Computer Science and Engineering Page 31


CS331 System Software Lab – Lab Manual Version 1.3

EXPERIMENT 8

IMPLEMENTATION OF PASS TWO OF A TWO PASS ASSEMBLER


AIM

To write program to implement pass two of a two pass assembler.

DESCRIPTION

Pass-2 of assembler generates machine code by converting symbolic machine-opcodes into their
respective bit configuration (machine understandable form). It stores all machine-opcodes in MOT
table (op-code table) with symbolic code, their length and their bit configuration. It will also process
pseudo-ops and will store them in POT table(pseudo-op table).

Pass-2:
a) Generate object code by converting symbolic op-code into respective numeric op-
code.
b) Generate data for literals and look for values ofsymbols

ALGORITHM
Begin
read first input file {from intermediate file}
if OPCODE = 'START' then
begin
write listing line
read next input line
end {if START}
write header record to object program
initialize first text record
while OPCODE != 'END' do
begin
if this is not a comment line then
begin
search OPTAB for OPCODE
if found then
begin
if there is a symbol in OPERAND field then
begin
search SYMTAB for OPERAND
if found then
store symbol value as operand address
else
begin
store 0 as operand address
set error flag (undefined symbol)
end
end {if symbol}

Department of Computer Science and Engineering Page 32


CS331 System Software Lab – Lab Manual Version 1.3

else
store 0 as operand address
assemble the object code instruction
end {if opcode found}
else if OPCODE = 'BYTE' or 'WORD' then
convert constant to object code
if object code not fit into the current Text record then
begin
write Text record to object program
initialize new Text record
end
add object code to Text record
end {if not comment}
write listing line
read next input line
end {while not END}
write last Text record to object program
write End record to object program
write last listing line
end

OUTPUT
INPUT FILES:

The length of the program is 19

OPTAB.txt
LDA 00
STA 0C
ADD 18
SUB 1C

SYMTAB.txt
ALPHA 100C
ONE 1011
TWO 1013
BETA 1016

input.txt
COPY START 1000
1000 LDA ALPHA
1003 ADD ONE
1006 SUB TWO
1009 STA BETA
100C ALPHA BYTE C‘KLNCE‘
1011 ONE RESB 2
1013 TWO WORD 5
1016 BETA RESW 1
1019 END

OUTPUT FILES:

Department of Computer Science and Engineering Page 33


CS331 System Software Lab – Lab Manual Version 1.3
objectpgm.txt
H^COPY^001000^000019
T^001000^09^00100C^181011^1C1013
T^001009^08^0C1016^4B4C4E4345
T^001013^03^000005
E^001000

RESULT

Pass 2 of a two pass assembler was simulated successfully and the output was verified.

Department of Computer Science and Engineering Page 34


CS331 System Software Lab – Lab Manual Version 1.3

EXPERIMENT 9

IMPLEMENTATION OF A SINGLE PASS ASSEMBLER


AIM

To write a program to implement a single pass assembler.

DESCRIPTION

A single pass assembler scans the program only once and creates the equivalent binary program. The
assembler substitute all of the symbolic instruction with machine code in one pass. One-pass
assemblers are used when it is necessary or desirable to avoid a second pass over the source program
the external storage for the intermediate file between two passes is slow or is inconvenient to use.
Main problem: forward references to both data and instructions.
One simple way to eliminate this problem: require that all areas be defined before they are referenced.
It is possible, although inconvenient, to do so for data items. Forward jump to instruction items cannot
be easily eliminated.

ALGORITHM

Stepno Details of the step


1 Begin
2 Read first input line
if OPCODE=‘START‘ then
a. save #[operand] as startingaddress
3 b. initialize LOCCTR as startingaddress
c. read next inputline
end
4 else initialize LOCCTR to 0
while OPCODE != ‗END‘ do
d. if there is not a comment linethen
e. if there is a symbol in the LABEL fieldthen
i. search SYMTAB forLABEL
ii. if foundthen
1. if symbol value asnull
2. set symbol value as LOCCTR and search thelinked
5 list with the corresponding operand
3. PTR addresses and generate operand addressesas
corresponding symbol values
4. set symbol value as LOCCTR in symbol tableand
delete the linked list
iii. end
iv. else insert (LABEL,LOCCTR) intoSYMTAB
v. end
6 search OPTAB for OPCODE
7 if found then
search SYMTAB for OPERAND address
8 if found then

Department of Computer Science and Engineering Page 35


CS331 System Software Lab – Lab Manual Version 1.3

f. if symbol value not equal to null then


i) store symbol value as operandaddress
else insert at the end of the linked list with a node with address as LOCCTR
9 else insert (symbol name, null) add 3 toLOCCTR.
elseif OPCODE=‘WORD‘then
10
add 3 to LOCCTR & convert comment to object code
11 elseif OPCODE = ‗RESW‘ then add 3 #[OPERND] toLOCCTR
elseif OPCODE = ‗RESB‘then
12
add #[OPERND] to LOCCTR
elseif OPCODE = ‗BYTE‘then
13 g. find length of the constant inbytes
h. add length to LOCCTR
convert constant to object code
if object code will not fit into current text record then
14 i. write text record to objectprogram
j. initialize new textrecord
o. add object code to textrecord
15 write listingline
16 read next input line
17 write last text record to objectprogram
18 write end record to objectprogram
19 write last listingline
20 End

OUTPUT
INPUT FILES:
input1.txt
COPY START 1000
- LDA ALPHA
- STA BETA

ALPHA RESW 1
BETA RESW 1
- END -

optab1.txt
LDA 00
STA 0C

OUTPUT FILES:
SYMTAB.txt
ALPHA 1006
BETA 1009

Department of Computer Science and Engineering Page 36


CS331 System Software Lab – Lab Manual Version 1.3
Output.txt
H^COPY^001000^00000C
T^001000^0C^000000^0C0000
T^001001^02^1006
T^001004^02^1009
E^001000

RESULT

A single pass assembler was implemented successfully and the output was verified.

Department of Computer Science and Engineering Page 37


CS331 System Software Lab – Lab Manual Version 1.3

EXPERIMENT 10
IMPLEMENTATION OF A MACRO PROCESSOR
AIM

To write a program to implement a macro processor.

DESCRIPTION
In assembly language programming it is often that some set or block of statements get repeated every
now. In this context the programmer uses the concept of macro instructions (often called as macro)
where a single line abbreviation is used for a set of line. For every occurrence of that single line the
whole block of statements gets expanded in the main source code. This gives a high level feature to
assembly language that makes it more convenient for the user to write code easily.

A macro instruction (macro) is simply a notational convenience for the programmer. It allows the
programmer to write shorthand version of a program (module programming). A macro represents a
commonly used group of statements in the source program. The macro processor replaces each macro
instruction with the corresponding group of source statements. This operation is called ―expanding
the macro‖ Using macros allows a programmer to write a shorthand version of a program. For
example, before calling a subroutine, the contents of all registers may need to be stored. This routine
work can be done using amacro.

Features of macro processor:


 Recognized the macrodefinition
 Save macrodefinition
 Recognized the macro call
 Perform macroexpansion

Data Structures used:


(1) DEFTAB - Stores the macro definition including macro prototype and macro body.
Comment lines are omitted. References to the macro instruction parameters are
converted to a positional notation for efficiency in substituting arguments.

(2) NAMTAB - Stores macro names, which serves an index to DEFTAB contain
pointers to the beginning and end of the definition.

(3) ARGTAB - Used during the expansion of macro invocations. When a macro
invocation statement is encountered, the arguments are stored in this table according
to their position in the argument list.

Department of Computer Science and Engineering Page 38


CS331 System Software Lab – Lab Manual Version 1.3

ALGORITHM

Department of Computer Science and Engineering Page 39


CS331 System Software Lab – Lab Manual Version 1.3

OUTPUT

INPUT FILES:
Input.txt
SAMPLE START 1000
EX1 MACRO &A, &B
- LDA &A
- STA &B
- MEND -
- EX1 N1, N2
N1 RESW 1
N2 RESW 1
- END -

OUPUT FILES:

DEFTAB.txt
1 //EX1 &A, &B
2 LDA ?1
3 STA ?2
4 MEND

NAMTAB.txt
EX1 1 4

Department of Computer Science and Engineering Page 40


CS331 System Software Lab – Lab Manual Version 1.3
ARGTAB.txt
1 N1
2 N2

Output.txt
SAMPLE START 1000
- LDA N1
- STA N2
N1 RESW 1
N2 RESW 1
- END -

RESULT

A macro processor was implemented successfully and the output was verified.

Department of Computer Science and Engineering Page 41


CS331 System Software Lab – Lab Manual Version 1.3

EXPERIMENT 11
IMPLEMENTATION OF A SYMBOL TABLE
AIM

To write a program to implement symbol table.

DESCRIPTION

A Symbol table is a data structure used by the compiler, where each identifier in program‘s source
code is stored along with information associated with it relating to its declaration. It stores identifier
as well as it‘s associated attributes like scope, type, line-number of occurrence, etc.

ALGORITHM

1. Initialize all the variables.


2. Create a symbol table with fields as variable and value using create option. Entries may
be added to the table while it is created itself.
3. Perform operations as insert, modify, search and display.
4. Append new contents to the symbol table with the constraint that there is no duplication
of entries, using insert option.
5. Modify existing content of the table using modify option.
6. Use display option to display the contents of the table.

OUTPUT
1. CREATE
2. INSERT
3. MODIFY
4. SEARCH
5. DISPLAY
6. EXIT:1

Enter the no. of entries:2


Enter the variable and the values:-
a 23
c 45

The table after creation is:


Variable value
a 23
c 45
1.CREATE 2.INSERT 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT:2

Enter the variable and the value:b 34


The table after insertion is:
Variable value
a 23
c 45
b 34
1.CREATE 2.INSERT 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT: 3

Department of Computer Science and Engineering Page 42


CS331 System Software Lab – Lab Manual Version 1.3

Enter the variable to be modified:c

The current value of the variable c is 45. Enter the new variable and its value 44 The table after
modification is:
Variable value
a 23
c 44
b 34

1.CREATE 2.INSERT 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT: 4

Enter the variable to be searched:a


The location of the variable is 1
The value of a is 23.

1. CREATE 2.INSERT 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT:5


Variable value
a 23
c 44
b 34

RESULT

The symbol table was implemented successfully and the output was verified.

Department of Computer Science and Engineering Page 43


CS331 System Software Lab – Lab Manual Version 1.3
EXPERIMENT 12

IMPLEMENTING SYMBOL TABLE WITH HASHING

AIM

To implement a symbol table with suitable hashing in C language.

DESCRIPTION

Symbol table can be implemented using various data structures like:


 LinkedList
 HashTable
 Tree

A common data structure used to implement a symbol table is HashTable.

ALGORITHM

1. Create m number of nodes in a symbol table, where m is the size of the table.
2. Perform operations, insert, search and display.
3. Add entries to the symbol table by converting the string to be inserted as integers and applying
the hash function - key mod m.
4. Entries may be added to the table while it is created itself.
5. Append new contents to the symbol table using hash function with the constraint that there is no
duplication of entries, using ―insert‖option.
6. Modify existing content of the table using modify option.
7. Use display option to display the contents of the table.

OUTPUT

Enter your choice:

1.create a symbol table


2.search in the symbol table
1
Enter the
address:1000enter the
label:system
Continue(y/n)?y

Enter the address:2000


enter the label: software
Continue(y/n)?n

The Symbol Tableis


hashvalues address label
0 0
1 0
Department of Computer Science and Engineering Page 43
CS331 System Software Lab – Lab Manual Version 1.3

2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 2000 software
10 1000 system

enter your choice:


1.create a symbol table
2.search in the symbol table
2
Enter the label:system
The label — system — is present in the symbol table at address:1000

enter your choice:


1. create a symbol table
2.search in the symbol table
2
Enter the label: cse
The label is not present in the symbol table

RESULT

The symbol table with suitable hashing was implemented in C language, and the output has been
verified.

Department of Computer Science and Engineering Page 44


CS331 System Software Lab – Lab Manual Version 1.3

ADDITIONAL EXPERIMENTS

SIMULATING PAGE REPLACEMENT ALGORITHMS


AIM

To implement the following page replacement algorithms:


a) First In First Out(FIFO)
b) Least Recently Used(LRU)

DESCRIPTION

In an operating system that uses paging for memory management, a page replacement algorithm is
needed to decide which page needs to be replaced when new page comes in.
Page Fault – A page fault happens when a running program accesses a memory page that is mapped
into the virtual address space, but not loaded in physical memory.
Since actual physical memory is much smaller than virtual memory, page faults happen. In case of
page fault, Operating System might have to replace one of the existing pages with the newly needed
page. Different page replacement algorithms suggest different ways to decide which page to replace.
The target for all algorithms is to reduce the number of page faults.
Page Replacement Algorithms

 First In First Out (FIFO)


This is the simplest page replacement algorithm. In this algorithm, the operating system keeps
track of all pages in the memory in a queue, the oldest page is in the front of the queue. When a
page needs to be replaced page in the front of the queue is selected for removal.

 Least Recently Used(LRU)


In this algorithm, the page which is least recently used is replaced.

ALGORITHM
1. FIFO

1. Start traversing thepages.


i) If set holds less pages thancapacity.
a) Insert page into the set one by one until the size of set reaches capacity or all page
requests are processed.
b) Simultaneously maintain the pages in the queue to performFIFO.
c) Increment pagefault.
ii) Else
If current page is present in set, do nothing.
Else

Department of Computer Science and Engineering Page 45


CS331 System Software Lab – Lab Manual Version 1.3

a) Remove the first page from the queue as it was the first to be entered in the
memory.
b) Replace the first page in the queue with the current page in thestring.
c) Store current page in thequeue.
d) Increment page faults.
2. Return the number of pagefaults.
2. LRU

Let capacity be the number of pages that memory can hold. Let set be the current set of pages in
memory.
1. Start traversing thepages.
i) If set holds less pages than capacity
a) Insert page into the set one by one until the size of set reaches capacity or all pagerequests
areprocessed.
b) Simultaneously maintain the recent occurred index of each page in a map calledindexes.
c) Increment pagefault
ii) Else
If current page is present in set, do nothing.
Else
a) Find the page in the set that was least recently used using index array.Replace
the page with minimum index with current page.
b) Increment page faults.
c) Update index of currentpage.
2. Return number of pagefaults.

OUTPUT

Enterdata

Enter length of page reference sequence:20

Enter the page reference sequence:7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1

Enter no of frames:3

Page Replacement Algorithms


1.FIFO
2. LRU
3. Exit
Enter your choice:1

For 7 : 7
For 2 : 7 2
For 3 : 7 23
For 1 : 2 31
For 2 :No page fault

Department of Computer Science and Engineering Page 46


CS331 System Software Lab – Lab Manual Version 1.3

For 5 : 3 15
For 3 :No page fault
For 4 : 1 54
For 6 : 5 46
For 7 : 4 67
For 7 :No page fault
For 1 : 6 71
For 0 : 7 10
For 5 : 1 05
For 4 : 0 54
For 6 : 5 46
For 2 : 4 62
For 3 : 6 23
For 0 : 2 30
For 1 : 3 01
Total no of page faults:17
Enter data

Enter length of page reference sequence:10

Enter the page reference sequence:1 2 3 2 4 1 3 2 4 1

Enter no of frames:3

Page Replacement Algorithms


1.FIFO
2. LRU
3. Exit
Enter your choice:2

For 1 : 1
For 2 : 1 2
For 3 : 1 23
For 2 :No page fault!
For 4 : 4 23
For 1 : 4 21
For 3 : 4 31
For 2 : 2 31
For 4 : 2 34
For 1 : 2 14
Total no of page faults:9

RESULT

The program to implement FIFO and LRU page replacement algorithms was executed successfully
and the output was verified.

Department of Computer Science and Engineering Page 47


CS331 System Software Lab – Lab Manual Version 1.3

SAMPLE VIVA QUESTIONS


PART – A
1. List the ProcessStates.
– New State – means a process is beingcreated
– Running – means instructions are beingexecuted
– Waiting – means a process is waiting for certain conditions or events tooccur
– Ready – means a process is waiting for an instruction from the mainprocessor
– Terminate – means a process is stoppedabruptly

2. What is a short-term scheduler?


It selects which process has to be executed next and allocates CPU.

3. What is preemptive and non-preemptive schedulingalgorithm?


A scheduling algorithm which allow a process to be temporarily suspended is called preemptive
scheduling and the algorithm that do not allow any process to be suspended till its completion is called
non-preemptive scheduling.

4. What are different performance metrics forscheduler?


There are five performance metrics for scheduler, those are as follows:
 CPU Utilization: Percentage of time that the CPU is doing usefulwork.
 Waiting time: Average time a process spends for its turn to getexecuted.
 Throughput: Number of processes completed / time unit.
 Response Time: Average time elapsed from when process is submitted until useful output is
obtained.
 Turnaround Time: Average time elapsed from when process is submitted to when it has
completed.

5. What ismutex?
Mutex is a locking mechanism which allows only one process to access the resource at a time. It
stands for mutual exclusion and ensures that only one process can enter the critical section at atime.

6. What is critical section ofcode?


As the name suggests, it is that section or part of code which has variables or other resources which is
being shared by two processes, and if allowed to access may lead to race condition. To avoid this, we
need a mutex which ensures only one process can enter the critical section of code at a time.

7. How is synchronization achieved bysemaphore?


There are two operations which are performed on semaphore which helps in synchronization, those
are:
 Wait: If the semaphore value is not negative, decrement the value by1.
 Signal: Increments the value of semaphore by1.

8. What isdeadlock?
A deadlock is a situation where two or more processes or threads sharing the same resources are
effectively preventing each other from accessing the resources. Thus, none of the process can continue
executing leading to deadlock.

9. What are the conditions required for deadlock tohappen?


There are four conditions required for deadlock which are stated below

Department of Computer Science and Engineering Page 48


CS331 System Software Lab – Lab Manual Version 1.3

Mutual Exclusion: At least one resource is held in a non-sharable mode that is only one process at a
time can use the resource. If another process requests that resource, the requesting process has to wait
till it is released.

Hold and Wait: There must exist a process that is holding at least one resource and is waiting to
acquire additional resources that are currently being held by other processes.

No Preemption: Resources cannot be preempted; that is, a resource can only be released after the
process has completed its task.

Circular Wait: There must exist a set {p0, p1,…..pn} of waiting processes such that p0 is waiting for a
resource which is held by p1, p1 is waiting for a resource which is held by p2,…, pn-1 is waiting for a
resource which is held by pn and pn is waiting for a resource which is held by p0.

10. What is racecondition?


Race condition is an undesirable situation where two or more threads/process are trying to access and
change the value of data shared by them and the outcome of the operation depends upon the order in
which the execution happens. Thus it lead to data inconsistency and loss of its value as both threads
are racing to change/access the shared data.

PART - B

1. Define SystemSoftware.
System software is a type of computer program that is designed to run a computer's hardware and
application programs.

2. Define the basic functions of anassembler.


a) Translating mnemonic operation codesto their machine languageequivalents.
b) Assigning machine addresses to symbolic labels used by theprogrammer.

3. What are forwardreferences?


It is reference to a label that is defined later in a program.

4. What are the 3 different records used in an objectprogram?


a) Header record – contains program name, starting address and length of theprogram.
b) Text record – contains translated instructions and data of theprogram.
c) Endrecord - marks the end of the object program and specifies the address in the program
where execution is tobegin.

5. What is the need of SYMTAB inassembler?


The symbol table includes the name and value for each symbol in the source program, together with
flags to indicate error conditions.

6. What is the need of OPTAB inassembler?


The operation code table contains the mnemonic code and its machine language equivalent.

7. What are the symbol defining statements generally used inassemblers?


a) EQU – to define symbols and specify their valuesdirectly.
b) ORG – to indirectly assign values tosymbols.

Department of Computer Science and Engineering Page 49


CS331 System Software Lab – Lab Manual Version 1.3

8. What is the use of the variable LOCCTR inassembler?


This variable is used to assign addresses to symbols. LOCCTR is initialized to the beginning address
specified in the START statement. After each source statement is processed, the length of the
assembled instruction or data area to be generated is added to LOCCTR and hence whenever we reach
a label in the source program, the current value of LOCCTR gives the address associated with the
label.

9. Define load and go assembler.


One pass assembler that generates the object code in memory for immediate execution is known as
load and go assembler.

10. Differentiate between the assembler directives RESW andRESB.


RESW reserves the indicated number of words for data area. RESB reserves the indicated number of
bytes for data area.

SAMPLE LAB EXAM QUESTIONS

1) Write a program to simulate producer-consumer problem with multiple producers and


consumers.
2) Implement FCFS CPU scheduling algorithm.
3) Implement C-SCAN disk schedulingalgorithm.
4) Write a program to print the contents of DEFTAB, NAMTAB andARGTAB.
5) Write a program to count all the forward references in a givenprogram.
6) Write a program to print all the forward references in a givenprogram.
7) Implement Pass 1 of a two pass assembler.

Department of Computer Science and Engineering Page 50

You might also like