0% found this document useful (0 votes)
17 views40 pages

Spos Program

The document contains code for several programs: 1) A program that performs pass 1 of an assembler by tokenizing source code and generating an intermediate representation. 2) A program that performs macro expansion by processing macro definitions and replacing macro calls with defined code. 3) A program to simulate CPU scheduling algorithms like FCFS, SJF, priority, and round robin.

Uploaded by

Silent killler
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views40 pages

Spos Program

The document contains code for several programs: 1) A program that performs pass 1 of an assembler by tokenizing source code and generating an intermediate representation. 2) A program that performs macro expansion by processing macro definitions and replacing macro calls with defined code. 3) A program to simulate CPU scheduling algorithms like FCFS, SJF, priority, and round robin.

Uploaded by

Silent killler
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1)

Code
import [Link].*;
import [Link].*;
public class Pass1
{
static int address=0;
static int sadd[]=new int[10];
static int ladd[]=new int[10];
public static void main(String args[])
{

BufferedReader br;
OutputStream oo;
String input=null;

String IS[]={"ADD","SUB","MUL","MOV"};
String UserReg[]={"AREG","BREG","CREG","DREG"};
String AD[]={"START","END"};
String DL[]={"DC","DS"};
int lc=0;
int scount=0,lcount=0;
int flag=0,flag2=0,stored=0;

String tokens[]=new String[30];


String tt=null;

String sv[]=new String[10];


String lv[]=new String[10];

try
{
br=new BufferedReader(new
FileReader("[Link]"));
File f = new File("[Link]");
File f1 = new File("[Link]");
File f2 = new File("[Link]");
PrintWriter p = new PrintWriter(f);
PrintWriter p1 = new PrintWriter(f1);
PrintWriter p2 = new PrintWriter(f2);
int k=0,l=0;
while ((input = [Link]()) != null)
{
StringTokenizer st = new
StringTokenizer(input," ");
while ([Link]())
{

tt=[Link]();

//[Link](tt);
if([Link]("\\d*")&& [Link]() > 2)
{

lc=[Link](tt);

[Link](lc);

address=lc-1;
}
else
{

for(int i=0;i<[Link];i++)

if([Link](AD[i]))

[Link]("AD "+(i+1)+" ");

for(int i=0;i<[Link];i++)

if([Link](IS[i]))

[Link]("IS "+(i+1)+" ");

for(int i=0;i<[Link];i++)

if([Link](UserReg[i]))

[Link]((i+1)+" ");

flag=1;
}

for(int i=0;i<[Link];i++)
{

if([Link](DL[i]))

[Link]("DL "+(i+1)+" ");

if([Link]()==1 && !([Link]()) && flag==1)

if ( [Link](sv).contains(tt) )

for(int i=0;i<scount;i++)

if(sv[i].equals(tt))

[Link]("S"+i);

flag2=1;

else

flag2=0;

}
}

else

[Link]("S"+scount);

sv[scount]=tt;

flag2=1;

scount++;

if([Link]()==1 && ([Link]()))

[Link](tt+" ");

sadd[k]=address;k++;

if([Link](0)=='=')

[Link]("L"+lcount);

lv[lcount]=tt;

lcount++;

if(![Link]())

[Link]();

}
if([Link]("DS"))

int a=[Link]([Link]());

address=address+a-1;

[Link]();

}
//[Link]();
address++;
} [Link]();
address--;

for(int i=0;i<lcount;i++)
{
ladd[i]=address;
address++;
}

for(int i=0;i<scount;i++)
{
[Link](i+"\t"+sv[i]+"\t"+sadd[i]);
}[Link]();

for(int i=0;i<lcount;i++)
{
[Link](i+"\t"+lv[i]+"\t"+ladd[i]);
}[Link]();
}
catch(Exception e)
{
[Link]();
} } }
2)Macro Code

import [Link].*;
import [Link].*;
public class Macro
{
public static void main(String args[])
{

BufferedReader br;
OutputStream oo;
String input=null;
String tt=null;
String arg=null;
String macroTokens=null;
String mnt[]=new String[10];
String mdt[]=new String[20];
String AR[]=new String[20];
int macroindex[]=new int[10];
int mcount=0,arg_count=0;
int middlecount=0;
int index=1;
int macro_enc=0;

try
{
br=new BufferedReader(new
FileReader("[Link]"));
File f3 = new File("[Link]");
File f4 = new File("[Link]");
File f5 = new File("[Link]");
PrintWriter p3 = new PrintWriter(f3);
PrintWriter p4 = new PrintWriter(f4);
PrintWriter p5 = new PrintWriter(f5);
while ((input = [Link]()) != null)
{
StringTokenizer st = new StringTokenizer(input," ");
tt=[Link]();
if([Link]("MACRO"))
{
macro_enc=1;
tt=[Link]();
mnt[mcount]=tt;
macroindex[mcount]=index;

[Link](mnt[mcount]+"\t"+macroindex[mcount]);
[Link](mnt[mcount]);
[Link](mnt[mcount]);
mcount++;

tt=[Link]();
StringTokenizer t = new
StringTokenizer(tt,",");
while ([Link]())
{
arg=[Link]();
if([Link](0)=='&')
{
AR[arg_count]=arg;
[Link](AR[arg_count]);
arg_count++;
}
}
}

else
{
if(macro_enc==1)
{
if([Link]("MEND"))
{
macro_enc=0;
[Link]("MEND");
}
else
{
StringTokenizer t=new
StringTokenizer(input," ");
while([Link]())
{
macroTokens=[Link]();
for(int
i=0;i<arg_count;i++)
{

if([Link](0)=='&' && [Link](AR[i]))


{

[Link]("AR"+i);
}
}

if([Link](0)=='&'){}
else
{

[Link](macroTokens+" ");
}
if(![Link]())
{

[Link]();
}

}
}

}
}
index++;
}
[Link]();
[Link]();
[Link]();

}
catch(Exception e)
{
[Link]();
}
}

}
5)Write a program to simulate CPU Scheduling Algorithms: FCFS, SJF (Preemptive),
Priority (Non- Preemptive) and Round Robin (Preemptive).
import [Link].*;
import [Link].*;

public class Fcfs


{

public static void main(String args[])


{
int n,sum=0;
float total_tt=0,total_waiting=0;

Scanner s=new Scanner([Link]);


[Link]("Enter Number Of Process you want to
Execute---");
n=[Link]();
int arrival[]=new int[n];
int cpu[]=new int[n];
int finish[]=new int[n];
int turntt[]=new int[n];
int wait[]=new int[n];
int process[]=new int[n];

// int pro[][]=new int[3][3];


for(int i=0;i<n;i++)
{
[Link]("Enter arrival time of "+(i+1)+"
Process : ");
arrival[i]=[Link]();
[Link]("Enter CPU time of "+(i+1)+" Process
: ");
cpu[i]=[Link]();

process[i]=i+1;
}

for(int i=0;i<n;i++)
{
sum=sum+cpu[i];
finish[i]=sum;
}
for(int i=0;i<n;i++)
{
turntt[i]=finish[i]-arrival[i];

total_tt=total_tt+turntt[i];

wait[i]=turntt[i]-cpu[i];

total_waiting+=wait[i];
}

[Link]("\n\nProcess\t\tAT\tCPU_T");
for(int i=0;i<n;i++)
{

[Link](process[i]+"\t\t"+arrival[i]+"\t"+cpu[i]);
}

[Link]("\n\n");
[Link]("Total turn around time is :
"+(total_tt/n));
[Link]("Total waiting time is :
"+(total_waiting/n));

}
}
import [Link].*;
import [Link].*;

public class Robbin


{

public static void main(String args[])


{
int n,sum=0;
float total_tt=0,total_waiting=0;

Scanner s=new Scanner([Link]);


[Link]("Enter Number Of Process you want to
Execute---");
n=[Link]();
int arrival[]=new int[n];
int cpu[]=new int[n];
int ncpu[]=new int[n];
int pri[]=new int[n];
int finish[]=new int[100];
int turntt[]=new int[n];
int wait[]=new int[n];
int process[]=new int[n];
int t_quantum,difference,temp_sum=0,k=0;
int seq[]=new int[100];

// int pro[][]=new int[3][3];


for(int i=0;i<n;i++)
{
[Link]("Enter arrival time of "+(i+1)+"
Process : ");
arrival[i]=[Link]();
[Link]("Enter CPU time of "+(i+1)+" Process
: ");
ncpu[i]=cpu[i]=[Link]();

process[i]=i+1;
}

[Link]("Enter time quantum : ");


t_quantum = [Link]();
int tv=0;
for(int i=0;i<n;i++){temp_sum=temp_sum+cpu[i];}
//[Link](temp_sum);

[Link]("Process execution sequence : ");


while(sum!=temp_sum){
for(int i=0;i<n;i++)
{
if(ncpu[i]<t_quantum)
{
difference=ncpu[i];
tv=ncpu[i];
ncpu[i]=0;
}
else
{
difference = ncpu[i]-t_quantum;
tv=t_quantum;
ncpu[i]=difference;
}
if(tv > 0)
{
sum=sum+tv;
finish[k]=sum;
seq[k]=i;
[Link](seq[k]+1+" ");

k++;

}
}
}
[Link]();

for(int i=0;i<n;i++)
{
int carr=0,tt=0;
carr=arrival[i];

for(int j=0;j<k;j++)
{
if(seq[j]==i)
{
tt=tt+(finish[j]-carr);
carr=finish[j];
}
}

turntt[i]=tt;
[Link]("Turn around time for "+(i+1)+"
process : "+turntt[i]);
total_tt=total_tt+turntt[i];

wait[i]=turntt[i]-cpu[i];

[Link]("Waiting time for "+(i+1)+" process


: "+wait[i]);

total_waiting+=wait[i];
}

[Link]("\n\nProcess\t\tAT\tCPU_T");
for(int i=0;i<n;i++)
{

[Link](process[i]+"\t\t"+arrival[i]+"\t"+cpu[i]);
}

[Link]("\n\n");
[Link]("Total turn around time is :
"+(total_tt/n));
[Link]("Total waiting time is :
"+(total_waiting/n));

}
}
import [Link].*;
import [Link].*;

public class Priority{

public static void main(String args[])


{
int n,sum=0;
float total_tt=0,total_waiting=0;

Scanner s=new Scanner([Link]);


[Link]("Enter Number Of Process U want 2
Execute---");
n=[Link]();
int arrival[]=new int[n];
int cpu[]=new int[n];
int pri[]=new int[n];
int finish[]=new int[n];
int turntt[]=new int[n];
int wait[]=new int[n];
int process[]=new int[n];

// int pro[][]=new int[3][3];


for(int i=0;i<n;i++)
{
[Link]("Enter arrival time of "+(i+1)+"
Process : ");
arrival[i]=[Link]();
[Link]("Enter CPU time of "+(i+1)+" Process
: ");
cpu[i]=[Link]();

[Link]("Enter Priority of "+(i+1)+" Process


: ");
pri[i]=[Link]();

process[i]=i+1;
}

for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pri[i]>pri[j])
{
int temp=cpu[i];
cpu[i]=cpu[j];
cpu[j]=temp;

//temp=arrival[i];
//arrival[i]=arrival[j];
//arrival[j]=temp;

temp=process[i];
process[i]=process[j];
process[j]=temp;

temp=pri[i];
pri[i]=pri[j];
pri[j]=temp;

}
}
}

for(int i=0;i<n;i++)
{
sum=sum+cpu[i];
finish[i]=sum;
}

for(int i=0;i<n;i++)
{
turntt[i]=finish[i]-arrival[i];

total_tt=total_tt+turntt[i];

wait[i]=turntt[i]-cpu[i];

total_waiting+=wait[i];
}

[Link]("\n\nProcess\t\tAT\tCPU_T");
for(int i=0;i<n;i++)
{

[Link](process[i]+"\t\t"+arrival[i]+"\t"+cpu[i]);
}

[Link]("\n\n");
[Link]("Total turn around time is :
"+(total_tt/n));
[Link]("Total waiting time is :
"+(total_waiting/n));

}
}
import [Link].*;
import [Link].*;

public class Sfj


{

public static void main(String args[])


{
int n,sum=0;
float total_tt=0,total_waiting=0;

Scanner s=new Scanner([Link]);


[Link]("Enter Number Of Process U want 2
Execute---");
n=[Link]();
int arrival[]=new int[n];
int cpu[]=new int[n];
int finish[]=new int[n];
int turntt[]=new int[n];
int wait[]=new int[n];
int process[]=new int[n];

// int pro[][]=new int[3][3];


for(int i=0;i<n;i++)
{
[Link]("Enter arrival time of "+(i+1)+"
Process : ");
arrival[i]=[Link]();
[Link]("Enter CPU time of "+(i+1)+" Process
: ");
cpu[i]=[Link]();

process[i]=i+1;
}

for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(cpu[i]>cpu[j])
{
int temp=cpu[i];
cpu[i]=cpu[j];
cpu[j]=temp;

temp=arrival[i];
arrival[i]=arrival[j];
arrival[j]=temp;

temp=process[i];
process[i]=process[j];
process[j]=temp;

}
}
}

for(int i=0;i<n;i++)
{
sum=sum+cpu[i];
finish[i]=sum;
}

for(int i=0;i<n;i++)
{
turntt[i]=finish[i]-arrival[i];

total_tt=total_tt+turntt[i];

wait[i]=turntt[i]-cpu[i];

total_waiting+=wait[i];
}

[Link]("\n\nProcess\t\tAT\tCPU_T");
for(int i=0;i<n;i++)
{

[Link](process[i]+"\t\t"+arrival[i]+"\t"+cpu[i]);
}

[Link]("\n\n");
[Link]("Total turn around time is :
"+(total_tt/n));
[Link]("Total waiting time is :
"+(total_waiting/n));

}
}
7)
import [Link];
public class Bankers{
private int need[][],allocate[][],max[][],avail[][],np,nr;

private void input(){


Scanner sc=new Scanner([Link]);
[Link]("Enter no. of processes and resources : ");
np=[Link](); //no. of process
nr=[Link](); //no. of resources
need=new int[np][nr]; //initializing arrays
max=new int[np][nr];
allocate=new int[np][nr];
avail=new int[1][nr];

[Link]("Enter allocation matrix -->");


for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
allocate[i][j]=[Link](); //allocation matrix

[Link]("Enter max matrix -->");


for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
max[i][j]=[Link](); //max matrix

[Link]("Enter available matrix -->");


for(int j=0;j<nr;j++)
avail[0][j]=[Link](); //available matrix

[Link]();
}

private int[][] calc_need(){


for(int i=0;i<np;i++)
for(int j=0;j<nr;j++) //calculating need matrix
need[i][j]=max[i][j]-allocate[i][j];

return need;
}
private boolean check(int i){
//checking if all resources for ith process can be allocated
for(int j=0;j<nr;j++)
if(avail[0][j]<need[i][j])
return false;

return true;
}

public void isSafe(){


input();
calc_need();
boolean done[]=new boolean[np];
int j=0;

while(j<np){ //until all process allocated


boolean allocated=false;
for(int i=0;i<np;i++)
if(!done[i] && check(i)){ //trying to allocate
for(int k=0;k<nr;k++)
avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
[Link]("Allocated process : "+i);
allocated=done[i]=true;
j++;
}
if(!allocated) break; //if no allocation
}
if(j==np) //if all processes are allocated
[Link]("\nSafely allocated");
else
[Link]("All proceess cant be allocated safely");
}

public static void main(String[] args) {


new Bankers().isSafe();
}
}
6)Write a Java Program (using OOP features) to implement paging simulation using
1. FIFO
2. Least Recently Used (LRU)
3. Optimal algorithm
6.1
import [Link].*;

public class Fifo {


public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
int frames, pointer = 0, hit = 0, fault = 0, ref_len;
int buffer[];
int reference[];
int mem_layout[][];
[Link]("Please enter the number of Frames: ");
frames = [Link]([Link]());
[Link]("Please enter the length of the Reference
string:");
ref_len = [Link]([Link]());
reference = new int[ref_len];
mem_layout = new int[ref_len][frames];
buffer = new int[frames];
for (int j = 0; j < frames; j++)
buffer[j] = -1;
[Link]("Please enter the reference string: ");
for (int i = 0; i < ref_len; i++) {
reference[i] = [Link]([Link]());
}
[Link]();
for (int i = 0; i < ref_len; i++) {
int search = -1;
for (int j = 0; j < frames; j++) {
if (buffer[j] == reference[i]) {
search = j;
hit++;
break;
}
}
if (search == -1) {
buffer[pointer] = reference[i];
fault++;
pointer++;
if (pointer == frames)
pointer = 0;
}
for (int j = 0; j < frames; j++)
mem_layout[i][j] = buffer[j];

}
for (int i = 0; i < frames; i++) {
for (int j = 0; j < ref_len; j++)
[Link]("%3d ", mem_layout[j][i]);
[Link]();
}
[Link]("The number of Hits: " + hit);
[Link]("Hit Ratio: " + (float) ((float) hit /
ref_len));
[Link]("The number of Faults: " + fault);
}
}
6.2)Least Recently Used
import [Link].*;
class LruAlgo
{
int p[],n,fr[],m,fs[],index,k,l,flag1=0,flag2=0,pf=0,frsize=3,i,j;
Scanner src=new Scanner([Link]);
void read()
{

[Link]("Enter page table size");


n=[Link]();
p=new int[n];
[Link]("Enter element in page table");
for(int i=0;i<n;i++)
p[i]=[Link]();

[Link]("Enter page frame size");


m=[Link]();
fr=new int[m];
fs=new int[m];
}

void display()
{

[Link]("\n");
for(i=0;i<m;i++)
{

if(fr[i]==-1)
[Link]("[ ]");
else
[Link]("["+fr[i]+"]");
}
}

void lru()
{
for(i=0;i<m;i++)
{
fr[i]=-1;
}
for(j=0;j<n;j++)
{
flag1=0;flag2=0;
for(i=0;i<m;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}
}
for(i=0;i<3;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;
}
[Link]("Page : "+p[j]);
display();
}
[Link]("\n no of page faults :"+pf);
}

public static void main(String args[])


{
LruAlgo a=new LruAlgo();
[Link]();
[Link]();
[Link]();
}
}

PS D:\engineering\TE\spos> javac [Link]


PS D:\engineering\TE\spos> java LruAlgo
Enter page table size
10
Enter element in page table
1
5
1
2
6
2
7
1
5
1
Enter page frame size
3
Page : 1

[1]
[]
[]
Page : 5

[1]
[5]
[]
Page : 1

[1]
[5]
[]
Page : 2

[1]
[5]
[2]
Page : 6

[1]
[6]
[2]
Page : 2

[1]
[6]
[2]
Page : 7

[7]
[6]
[2]
Page : 1

[7]
[1]
[2]
Page : 5
[7]
[1]
[5]
Page : 1

[7]
[1]
[5]

no of page faults :4

[7]
[1]
[5]
6.3) Optimal algorithm

import [Link];
import [Link];
import [Link];
public class OptimalReplacement {

public static void main(String[] args) throws IOException


{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
int frames, pointer = 0, hit = 0, fault = 0,ref_len;
boolean isFull = false;
int buffer[];
int reference[];
int mem_layout[][];

[Link]("Please enter the number of Frames: ");


frames = [Link]([Link]());

[Link]("Please enter the length of the Reference string: ");


ref_len = [Link]([Link]());

reference = new int[ref_len];


mem_layout = new int[ref_len][frames];
buffer = new int[frames];
for(int j = 0; j < frames; j++)
buffer[j] = -1;

[Link]("Please enter the reference string: ");


for(int i = 0; i < ref_len; i++)
{
reference[i] = [Link]([Link]());
}
[Link]();
for(int i = 0; i < ref_len; i++)
{
int search = -1;
for(int j = 0; j < frames; j++)
{
if(buffer[j] == reference[i])
{
search = j;
hit++;
break;
}
}
if(search == -1)
{
if(isFull)
{
int index[] = new int[frames];
boolean index_flag[] = new boolean[frames];
for(int j = i + 1; j < ref_len; j++)
{
for(int k = 0; k < frames; k++)
{
if((reference[j] == buffer[k]) && (index_flag[k] == false))
{
index[k] = j;
index_flag[k] = true;
break;
}
}
}
int max = index[0];
pointer = 0;
if(max == 0)
max = 200;
for(int j = 0; j < frames; j++)
{
if(index[j] == 0)
index[j] = 200;
if(index[j] > max)
{
max = index[j];
pointer = j;
}
}
}
buffer[pointer] = reference[i];
fault++;
if(!isFull)
{
pointer++;
if(pointer == frames)
{
pointer = 0;
isFull = true;
}
}
}
for(int j = 0; j < frames; j++)
mem_layout[i][j] = buffer[j];
}
for(int i = 0; i < frames; i++)
{
for(int j = 0; j < ref_len; j++)
[Link]("%3d ",mem_layout[j][i]);
[Link]();
}

[Link]("The number of Hits: " + hit);


[Link]("Hit Ratio: " + (float)((float)hit/ref_len));
[Link]("The number of Faults: " + fault);
}

You might also like