SystemSoftware Lab ManualProf - Malik
SystemSoftware Lab ManualProf - Malik
Program No:1
FCFS- CPU SCHEDULING ALGORITHM
Aim:
Simulate FCFS non-preemptive CPU scheduling algorithm to find turnaround time and
waiting time.
Note:
It is the simplest of all the scheduling Algorithms. Key concept of this Algorithm is
“allocate the CPU in the order in which the processes arrive”. It assumes that ready queue
is managed as FIFO (First in first out).This Algorithm is non-preemptive.
Algorithm
1. Start
2. Declare the array size
3. Read the number of processes to be inserted
4. Read the Burst times of processes
5. Calculate the waiting time of each process wt[i+1]=bt[i]+wt[i]
6. Calculate the turnaround time of each process tt[i+1]=tt[i]+bt[i+1]
7. Calculate the average waiting time and average turnaround time.
8. Display the values
9. Stop
Source code:
Sample Output:
Input
Enter no of processes 3
Enter burst time 12
8
20
Output
bt wt tt
12 0 12
8 12 20
20 20 40
aw=10.666670
at=24.00000
Result:
Thus the process FCFS was executed and verified successfully.
Program No:2
Note:
The key concept of this Algorithm is: “CPU is allocated to the process with least CPU
burst time.” Amongst the processes in the ready queue, CPU is always assigned to the
process with the least CPU burst requirement. If there are two processes with the CPU
burst, the one which arrived first, will be taken up first by the CPU. This SJF Algorithm
can be either preemptive or non-preemptive. Preemptive SJF scheduling is sometimes
called Shortest-Remaining-Time-First (SRTF) scheduling. SJF is an optimal Algorithm,
as it gives the minimum average waiting time.
Algorithm
1. Start
2. Declare the array size
3. Read the number of processes to be inserted
4. Read the Burst times of processes
5. Sort Burst times in ascending order and process with shortest burst time is first
executed.
6. Calculate the waiting time of each process wt[i+1]=bt[i]+wt[i]
7. Calculate the turnaround time of each process tt[i+1]=tt[i]+bt[i+1]
Source code:
Sample Output
Input:
Enter no of processes 3
Enter burst time 12
8
20
Output:
bt wt tt
12 8 20
8 0 8
20 20 40
aw=9.33
at=22.64
Result:
Thus the process SJF was executed and verified successfully.
Program No:3
Aim:
To simulate Sequential File Allocation strategy.
Algorithm:
1. Start
2. Read the number of files
3. For each file, read the number of blocks required and the starting block of the file.
4. Allocate the blocks sequentially to the file from the starting block.
5. Display the file name, starting block, and the blocks occupied by the file.
6. Stop
Source code:
Sample output:
1 2 4
2 5 10
Result:
Thus the process sequential file allocation strategy was executed and verified
successfully.
Program No:4
Note:
Linked allocation of disk space overcomes all the problems of contiguous allocation. In
linked allocation each file is a linked list of disk blocks where the disk blocks may be
scattered anywhere on the disk. The directory contains a pointer to the first and last
blocks of the file.
Disadvantages: Space required maintaining pointers.
Algorithm
1. Start
2. Read the number of files
3. For each file, read file name, starting block, number of blocks and block numbers
of file.
4. Start from the starting block and link each block of the file to the next block in a
linked list fashion.
5. Display the file name, starting block, size of the file, and the blocks occupied by
the file.
6. Stop
Source code:
Sample Output:
Result:
Thus the process linked file allocation strategy was executed and verified successfully.
Program No:5
Theory:
It is the simplest form of disk scheduling algorithms. The I/O requests are
served or processes according to their arrival. The request arrives first will be
accessed and served first. Since it follows the order of arrival, it causes the
wild swings from the innermost to the outermost tracks of the disk and vice
versa . The farther the location of the request being serviced by the read/write
head from its current location, the higher the seek time will be.
Source code:
Sample output:
Enter number of location 8
Enter position of head 53
Enter elements of disk queue 98
183
37
122
14
124
65
67
Movement of total cylinders 640
Result:
Thus the program was successfully executed.
Program No:6
Aim:
To write a program to simulate the SCAN disk Scheduling Algorithm
Note:
Here the disk arm starts at one end of the disk and moves towards the other end,
servicing requests as it reaches each cylinder until it gets to the other end of the disk. At
the other end, the direction of head movement is reversed and servicing continues.
Source code:
Result:
Thus the program was successfully executed.
Program No:7
Aim:
To write a program to simulate the C-SCAN disk scheduling Algorithm
Theory:
Circular SCAN (C-SCAN) scheduling is a variant of SCAN designed to provide a more
uniform wait time. Like SCAN, C-SCAN moves the head from one end of the disk to the
other, servicing requests along the way. When the head reaches the other end, however, it
immediately returns to the beginning of the disk without servicing any requests on the
return trip (Figure 10.7). The C-SCAN scheduling Algorithm essentially treats the
cylinders as a circular list that wraps around from the final cylinder to the first one.
Source code:
Program No:8
Aim:
To implement producer/consumer problem using semaphore.
Note:
A producer process produces information that is consumed by a consumer process. For
example, a compiler may produce assembly code that is consumed by an assembler.To
allow producer and consumer processes to run concurrently, we must have available a
buffer of items that can be filled by the producer and emptied by the consumer. This
buffer will reside in a region of memory that is shared by the producer and consumer
processes. A producer can produce one item while the consumer is consuming another
item. The producer and consumer must be synchronized, so that the consumer does not
try to consume an item that has not yet been produced.
Algorithm:
1. Declare variable for producer & consumer as pthread-t-tid produce tid consume.
2. Declare a structure to add items, semaphore variable set as struct.
3. Read number the items to be produced and consumed.
4. Declare and define semaphore function for creation and destroy.
5. Define producer function.
6. Define consumer function.
7. Call producer and consumer.
8. Stop the execution.
Source code:
Sample output:
Result:
Thus the producer consumer program was executed and verified successfully.
Program No:9
DINING PHILOSOPHER’S PROBLEM
Aim:
To simulate the working of the dining philosopher’s problem
Note:
There are some Philosophers whose work is just thinking and eating. Let there are 5 (for
example) philosophers. They sat at a round table for dinner. To complete dinner each must
need two Forks (spoons). But there are only 5 Forks available (Forks always equal to no. of
Philosophers) on table. They take in such a manner that, first take left Fork and next right
Fork. But problem is they try to take at same time. Since they are trying at same time, Fork 1,
2, 3, 4, 5 taken by Philosopher 1, 2, 3, 4, 5 respectively (since they are left side of each). And
each one tries to ta ke right side Fork. But no one found available Fork. And also that each
one thinks that someone will release the Fork and then I can eat. This continuous waiting
leads to Dead Locksituation.
Algorithm:
1. Define the number ofphilosophers
2. Declare one thread perphilosopher
3. Declare one semaphore (represent chopsticks) perphilosopher
4. When a philosopher ishungry
1. See if chopsticks on both sides arefree
2. Acquire both chopsticksor
3. Eat
4. restore thechopsticks
5. If chopsticks aren’tfree
5. Wait till they areavailable
Source code
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N
sem_tmutex;
sem_t S[N];
int state[N];
intphil_num[N]={0,1,2,3,4};
int main()
{
int i;
pthread_tthread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
printf("Philosopher %d is thinkingn",i+1);
}
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}
void take_fork(intph_num)
{
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("Philosopher %d is Hungryn",ph_num+1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}
void test(intph_num)
{
if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] !=
EATING)
{
state[ph_num] = EATING;
sleep(2);
void put_fork(intph_num)
{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d putting fork %d and %d
downn",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is thinkingn",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
Program No:10
BANKERS ALGORITHM
AIM
Program to implement Banker`s algorithm.
IMPLEMENTATION DETAILS
The Banker's algorithm is run by the operating system whenever a process requests
resources. The algorithm avoids deadlock by denying or postponing the request if it
determines
that accepting the request could put the system in an unsafe state (one where deadlock
could
occur). When a new process enters a system, it must declare the maximum number of
instances
of each resource type that it may ever claim; clearly, that number may not exceed the
total
number of resources in the system. Also, when a process gets all its requested resources it
must
return them in a finite amount of time.
ALGORITHM
Step 1: Start
Step 2: Read the number of available resource to r
Step 3: for i=1 to r
Step 4: Read the available resource to rsrc[i]
Step 5: Read the number of process to p
Step 6: for i=0 to p
Step 7: for j=1 to r do step 8
Step 8: Read maximum claim and allocation details to claim [i][j] and alloc[i][j]
respectively
Step 9: End for loop
PROGRAM
#include<stdio.h>
void main()
{
intpr,re,i,j,k=0,count1=0,count2=0,insta;
printf("\n \t enter no of process\n");
printf("\t\t");
scanf("%d",&pr);
printf("\n enter the no of resources");
printf("\t\t");
scanf("%d",&re);
int avail[re],max[pr][re],allot[pr][re],need[pr][re],completed[pr];
for(i=0;i<pr;i++)
completed[i]=0;
printf("\n enter no of instances\n");
for(i=0;i<re;i++)
{
printf("\t\t");
scanf("%d",&insta);
avail[i]=insta;
}
printf("\n\t enter max no of instances of resources thet a process need:\n");
for(i=0;i<pr;i++)
{
printf("\n \t for p[%d]",i);
for(j=0;j<re;j++)
{
printf("\t");
scanf("%d",&insta);
max[i][j]=insta;
}
}
printf("\n \t enter no of instances already located to process of a resource \n");
for(i=0;i<pr;i++)
{
printf("\n \t for p[%d] \t",i);
for(j=0;j<re;j++)
{
printf("\t\t");
scanf("%d",&insta);
allot[i][j]=insta;
need[i][j]=max[i][j]-allot[i][j];
}
}
printf("\n \t state sequence is:\t");
while(count1!=pr)
{
count2=count1;
for(i=0;i<pr;i++)
{
for(j=0;j<re;j++)
{
if(need[i][j]<=avail[j])
{
k++;
}
}
if(k==re && completed[i]==0)
{
printf("p[%d]\t",i);
completed[i]=1;
for(j=0;j<re;j++)
{
avail[j]+allot[i][j];
}
count1++;
}
k=0;
}
if(count1==count2)
{
printf("\t \t stop after this deadlock \n");
break;
}
}
Program No:11
SINGLE LEVELDIRECTORY
Aim:
To write a C program to implement File Organization concept using the
technique Single leveldirectory.
Note:
It is the simplest of all directory structures, in this the directory system having only one
directory, it consisting of the all files. Sometimes it is said to be root directory. The
following Fig. shows single level directory that contains four files (A, B, C, D). It has the
simplicity and ability to locate files quickly. It is not used in the multi-user system; it is
used on small embeddedsystem.
Algorithm:
Step 1: Start the Program
Step 2: Initialize values gd=DETECT,gm,count,i,j,mid,cir_x.
Step 3: Initialize graph function
Step 4: Set back ground color with setbkcolor();
Step 5: Read number of files in variable count.
Step 6: check i<count; mid=640/count;
Step 7: Stop the execution
Source code:
Result:
Program No:12
PASS ONE OF A TWO PASS ASSEMBLER
Aim:
To write a program to implement pass one of a two pass assembler.
Note:
An assembler is a translator, that translates an assembler program into a
conventional machine language program. Basically, the assembler goes
through the program one line at a time and generates machine code for that
instruction. Then the assembler procedes to the next instruction.In this way,
the entire machine code program is created. For most instructions this
process works fine, for example for instructions that only reference registers,
the assembler can compute the machine code easily, since the assembler
knows where the registersare.
Algorithm:
1 begin
2 read first inputline;
3 if OPCODE = 'START'then
begin
i. save #[OPERAND] as startingaddress
ii. initialized LOCCTR to startingaddress
iii. write line to intermediatefile
iv. r
e
a
d
n
e
x
t
i
n
p
u
t
l
i
n
e
e
n
d
{
i
f
S
T
A
R
T
}
4 e
lse
initia
lized
LOC
CTR
to 0
5
whil
e
OPC
ODE
!=
'EN
D'
do
begi
n
a. i. if this is not a comment line then
ii. begin
iii. if there is a
symbol in the
LABEL fieldthen
begin
1. search SYMTAB forLABEL
2. if foundthen
3. set error flag (duplicatesymbol)
4. else
5. insert
(LABEL,
LOCCTR) into
SYMTAB end
{ifsymbol}
iv. search OPTAB forOPCODE
v. if found then add 3 {instruction length} toLOCCTR
vi. else if OPCODE = 'WORD' then add 3 toLOCCTR
vii. else if OPCODE = 'RESW' thenadd 3 * #[OPERAND] toLOCCTR
viii. else if OPCODE = 'RESB' then add #[OPERAND] toLOCCTR
ix. e
lse if
OPCOD
E=
x.
xi. '
BYTE'
then
begin
1. find length of constant inbytes
2. add length toLOCCTRend {if BYTE}
xii. else set
error flag (invalid
operationcode) end {if
not acomment}
Source code:
ouput.txt
2003 ** STA ALPHA
Result:
Thus pass one of two passes assembler is implemented and the result is verified successfully
PROGRAM No:13
PASS TWO OF A TWO PASS ASSEMBLER
Aim:
To write a program to implement pass two of a two pass assembler.
Note:
.An assembler is a translator, that translates an assembler program into a conventional
machine language program. Basically, the assembler goes through the program one line
at a time and generates machine code for that instruction. Then the assembler procedes to
the next instruction. In this way, the entire machine code program is created. For most
instructions this process works fine, for example for instructions that only reference
registers, the assembler can compute the machine code easily, since the assembler knows
where the registers are
Algorithm:
1 begin
2 read first input file {from intermediate file} 3 if OPCODE = 'START' then
4 begin
a) write listing line
b) read next input line 5 end {if START}
6 write header record to object program 7 initialized first Text record
8 while OPCODE != 'END' do 9 begin
a) if this is not a comment line then
i) begin
ii) search OPTAB for OPCODE
iii) if found then
iv) begin
(1) if there is a symbol in OPERAND field then
(2) begin
(a) search SYMTAB for OPERAND
(b) if found then
(c) store symbol value as operand address
(d) else
(e) begin
(f) store 0 as operand address
(g) set error flag (undefined symbol)
(h) end
(3) end {if symbol}
(4) else
(5) store 0 as operand address
(6) assemble the object code instruction
i) end {if opcode found}
Source code:
Sample output:
Result:
Thus the program for two pass assembler is implemented and the output is verified accordingly.
Aim
To write a program to implement a single pass assembler.
Algorithm
1 Begin
2 Read first input line
3 if OPCODE=‟START‟ then
a. save #[operand] as starting address
b. initialize LOCCTr as starting address
c. read next input line end
4 else initialize LOCCTR to 0
5 while OPCODE != „END‟ do
d. if there is not a comment line then
e. if there is a symbol in the LABEL field then
i. search SYMTAB for LABEL
ii. if found then
1. if symbol value as null
2. set symbol value as LOCCTR and search the linked list with the
corresponding operand
3. PTR addresses and generate operand addresses as corresponding
symbol values
4. set symbol value as LOCCTR in symbol table and delete the linked
list
iii. end
iv. else insert (LABEL,LOCCTR) into SYMTAB
v. end
6search OPTAB for OPCODE
7 if found thensearch SYMTAB for OPERAND address
8 if found then
f. if symbol value not equal to null then
i) store symbol value as operand address
else insert at the end of the linked list with a node with address as LOCCTR
9 else insert (symbol name, null) add 3 to LOCCTR.
10 elseif OPCODE=‟WORD‟ then
add 3 to LOCCTR & convert comment to object code
11 elseif OPCODE = „RESW‟ then add 3 #[OPERND] to LOCCTR
12 elseif OPCODE = „RESB‟ then add #[OPERND] to LOCCTR
13 elseif OPCODE = „BYTE‟ then
Source Code
Sample output:
Result:
Thus the program for single pass assembler is implemented and the output is verified
accordingly.
Program No:15
TWO PASS MACRO PROCESSOR
Aim:
Write a program to implement two pass macro processor
Theory:
Ageneral-purpose macro processor or general purpose preprocessor is a macro processor that is not
tied to or integrated with a particular language or piece of software. A macro processor is a program
that copies a stream of text from one place to another, making a systematic set of replacements as it
does so. Macro processors are often embedded in other programs, such as assemblers and
compilers. Sometimes they are standalone programs that can be used to process any kind of text.
Macro processors have been used for language expansion (defining new language constructs that can
be expressed in terms of existing language components), for systematic text replacements that
require decision making, and for text reformatting (e.g. conditional extraction of material from an
HTML file).
Algorithm:
1 Start the macro processorprogram.
2 Include the necessary
header files and variable. 3
Open the threefiles
a.f1=macin.dat with readprivilege
b. f2=macout.dat with writeprivilege
c. f3= deftab.dat with writeprivilege
4 Get the variable form f1 file macin.dat for
label,opcode,operand 5 Read the variable until
the opcode is not is equal to zero
6 Then check if the opcode is equal to Macro if Macro. Then
Copymacroname=label
a. Get the variable label, opcode,operand
b. In these if condition perform the while loop until opcode is not equal toMEND
c. Copy thevariable
d. close while loop and ifcondition
e. else if opcode is
equal to macro name
Perform the for loop
from 0 tolength
7 Finally terminate theprogram.
Source code:
Sample output:
INPUT FILE:
macin.txt
** macro m1
** move a,b
** mend ---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** call m1
** call m2
** add a,b
OUPUT FILE:
No. of macros=2
Enter the text filename outmac
macin
outmac.txt
** macro m1
** move a,b
** mend ---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** move a,b
** lda b
** add a,b
Results:
Thus the macro processor is implemented and the result is verified successfully.
Program No:16
ABSOLUTE LOADER
Aim
To write a C program to implement an absolute loader.
Algorithm:
1. Start theprogram
2. Assign the requiredvariable
3. Open the files
fp1=fopen("input2.dat","r")
;
fp2=fopen("out2.dat","w");
4. Read the content. Using while loop perform the loop until character is not equal toE.
5. Then compare whether the character is equal toH
6. If H then get the starting address, length andinput
7. Else if the character is T then store the string as the three address in the output file with
input[0],inuput[1] foraddress
input[2],inuput[3] for address+1
input[4],inuput[5] for address+2
8. Else if it is not H or T then perform the following fprintf
fprintfin fp2 for output file for ,
input[0],inuput[1] foraddress
input[2],inuput[3] for address+1
input[4],inuput[5]
inuput[5] for address+2
9. Increment the address value by3.
10. Read the next input string and repeat from step4.
1005 39
1006 10
1007 20
1008 36
2000 29
2001 83
2002 00
2003 23
2004 00
2005 00
2006 28
2007 20
2008 30
2009 30
2010 20
2011 15
Result:
Thus the absolute loader is implemented and the result is verified successfully.