Professional Documents
Culture Documents
Document (4)
Document (4)
Document (4)
Class: TE computer,
Div: TE-1 Roll No.: 305A029
Subject: Laboratory Practice -1
College: Sinhgad College Of Engineering
Pass – 1 Assembler Program
import java.io.*;
class SymbTab
{
public static void main(String args[])throws Exception
{
FileReader FP=new FileReader("/Desktop/Java/input.txt");
BufferedReader bufferedReader = new BufferedReader(FP);
String line=null;
int line_count=0,LC=0,symTabLine=0,opTabLine=0,litTabLine=0,poolTabLine=0;
//Data Structures final
int MAX=100;
String SymbolTab[][]=new String[MAX][3];
String OpTab[][]=new String[MAX][3];
String LitTab[][]=new String[MAX][2]; int
PoolTab[]=new int[MAX];
// int litTabAddress=0;
/*---------------------------------------------------------------------------------------------------*/
System.out.println("___________________________________________________"); while((line
= bufferedReader.readLine()) != null)
{
String[] tokens = line.split("\t");
if(line_count==0)
{
LC=Integer.parseInt(tokens[1]); //set
LC to operand of START
for(int i=0;i<tokens.length;i++) //for printing the input program
System.out.print(tokens[i]+"\t");
System.out.println("");
} else
{
for(int i=0;i<tokens.length;i++) //for printing the input program
System.out.print(tokens[i]+"\t");
System.out.println(""); if(!tokens[0].equals(""))
{
//Inserting into Symbol Table
SymbolTab[symTabLine][0]=tokens[0];
SymbolTab[symTabLine][1]=Integer.toString(LC); SymbolTab[symTabLine]
[2]=Integer.toString(1);
symTabLine++;
}
else if(tokens[1].equalsIgnoreCase("DS")||tokens[1].equalsIgnoreCase("DC")) {
//Entry into symbol table for declarative statements
SymbolTab[symTabLine][0]=tokens[0];
SymbolTab[symTabLine][1]=Integer.toString(LC); SymbolTab[symTabLine]
[2]=Integer.toString(1);
symTabLine++;
}
System.out.println("___________________________________________________");
___________________________________________________
START 100
READ A
LTORG
='5'
='1'
='6'
='7'
MOVEM A,B
LTORG
='2'
LOOP READ B
A DS 1
B DC '1'
='1'
END ___________________________________________________
SYMBOL TABLE
--------------------------
--------------------------
LABLE 102 1
LOOP 111 1
A 112 1
B 113 1
--------------------------
OPCODE TABLE
----------------------------
READ IS (04,1)
MOVER IS (04,1)
LTORG AD R11
MOVEM IS (04,1)
LTORG AD R11
READ IS (04,1)
DS DL R7
DC DL R7
END AD
R11
----------------------------
LITERAL TABLE
-----------------
LITERAL ADDRESS
-----------------
='5' 104
='1' 105
='6' 106
='7' 107
='2' 110
='1'
114
------------------
POOL TABLE
-----------------
LITERAL NUMBER
-----------------
------------------
Pass-2 Assembler Program
Progarm:
package spos;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter; import
java.io.IOException; import
java.util.HashMap;
Intermediate.txt
(AD,01)(C,200)
(IS,04)(1)(L,1)
(IS,05)(1)(S,1)
(IS,04)(1)(S,1)
(IS,04)(3)(S,3) (IS,01)(3)(L,2)
(IS,07)(6)(S,4)
(DL,01)(C,5)
(DL,01)(C,1)
(IS,02)(1)(L,3) (IS,07)(1)(S,5)
(IS,00)
(AD,03)(S,2)+2
(IS,03)(3)(S,3)
(AD,03)(S,6)+1
(DL,02)(C,1)
(DL,02)(C,1)
(AD,02)
(DL,01)(C,1)
littab.txt
5 206
1 207
1 213
symtab.txt
A 211 1
LOOP 202 1
B 212 1
NEXT 208 1
BACK 202 1
LAST 210 1
Output
Pass2.txt
+ 04 1 206 +
05 1 211
+ 04 1 211 +
04 3 212
+ 01 3 207
+ 07 6 208
+ 00 0 005
+ 00 0 001
+ 02 1 213
+ 07 1 202
+ 00 0 000
+ 03 3 212
+ 00 0 001
Input.txt
MACRO
INCR1 &FIRST,&SECOND=DATA9
A 1,&FIRST
L 2,&SECOND
MEND
MACRO
INCR2 &ARG1,&ARG2=DATA5
L 3,&ARG1
ST 4,&ARG2
MEND
PRG2 START
USING *,BASE
INCR1 DATA1
INCR2 DATA3,DATA4
FOUR DC F'4'
FIVE DC F'5'
BASE EQU 8
TEMP DS 1F
DROP 8
END
MACRO.java
import java.util.*;
MACRO
String mdt[][]=new String[20][1]; //assuming 4 LOC for each macro static int
mntc=0,mdtc=0,alac=0;
pass1();
System.out.println("\n*********PASS-1 MACROPROCESSOR***********\n");
display(mnt,mntc,3);
System.out.println("\n");
display(ala,alac,2);
System.out.println("\n");
System.out.println("\n");
int index=0,i;
String s,prev="",substring;
try
op = new File("pass1_output.txt");
if (!op.exists()) op.createNewFile();
while((s=inp.readLine())!=null)
if(s.equalsIgnoreCase("MACRO"))
prev=s;
for(;!(s=inp.readLine()).equalsIgnoreCase("MEND");mdtc++,prev=s)
if(prev.equalsIgnoreCase("MACRO"))
{
StringTokenizer st=new StringTokenizer(s); String str[]=new
String[st.countTokens()]; for(i=0;i<str.length;i++)
for(i=0;i<string.length;i++)
string[i]=st.nextToken(); ala[alac]
index=string[i].indexOf("="); if(index!=-1)
ala[alac++][1]=string[i].substring(0,index);
else ala[alac++][1]=string[i];
substring=s.substring(index);
for(i=0;i<alac;i++) if(ala[i]
[1].equals(substring))
s=s.replaceAll(substring,"#"+ala[i][0]); }
mdt[mdtc-1][0]=s;
mdt[mdtc-1][0]=s;
else
output.write(s); output.newLine();
output.close();
}
catch(FileNotFoundException ex)
catch(IOException e)
e.printStackTrace();
{ int
i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println();
output:
Microsoft Windows [Version 10.0.19044.2006] (c)
Microsoft Corporation. All rights reserved.
C:\Users\Prerana>cd Desktop
C:\Users\Prerana\Desktop>javac MACRO.java
C:\Users\Prerana\Desktop>java MACRO
*********PASS-1 MACROPROCESSOR***********
i macro loc
1 INCR1 1
2 INCR2 5
0 &FIRST
1 &SECOND
2 &ARG1
3 &ARG2
INCR1 &FIRST,&SECOND=DATA9
A 1,#0
L 2,#1
MEND
INCR2 &ARG1,&ARG2=DATA5
L 3,#2
ST 4,#3
MEND
#include<pthread.h>
#include<stdio.h>
#include<semaphore.h>
#include<unistd.h>
void *funl();
void *fun2();
int shared=1; //shared
variable sem_t s; //semaphore
variable
int
main()
{
sem_init(&s,0,1); //initialize semaphore variable - Ist argument is
address of variable, 2nd is number of
processes sharing semaphore, 3rd argument is the initial value of
semaphore variable
pthread_join(thread1, NULL);
pthread_join(thread2,NULL);
}
void *fun!()
{ int
x;
sem_wait(&s); //executes wait Operation on s
x = shared;//thread] reads value of shared
variable printf("Thread! reads the value as %d\
n",x);
sem_post(&s);
}
void *fun2()
{ int
y;
sem_wait(&s);
y=shared;//thread2 reads value of shared
variable printf("Thread2 reads the value as %d\
n",y);
sem_post(&s);
}
Output :-
1.FCFS
*/ import java.io.*;
import java.util.Scanner;
public class FCFS
{
public static void main(String args[])
{
int i,no_p,burst_time[],TT[],WT[];
float avg_wait=0,avg_TT=0;
burst_time=new int[50]; TT=new
int[50];
WT=new int[50];
WT[0]=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter the number of process: ");
no_p=s.nextInt();
System.out.println("\nEnter Burst Time for processes:");
for(i=0;i<no_p;i++)
{
System.out.print("\tP"+(i+1)+": ");
burst_time[i]=s.nextInt();
}
for(i=1;i<no_p;i++)
{
WT[i]=WT[i-1]+burst_time[i-1];
avg_wait+=WT[i];
}
avg_wait/=no_p;
for(i=0;i<no_p;i++)
{
TT[i]=WT[i]+burst_time[i];
avg_TT+=TT[i];
}
avg_TT/=no_p;
System.out.println("\n************************************************************
****");
System.out.println("\tProcesses:");
System.out.println("*************************************************************
*
**");
System.out.println(" Process\tBurst Time\tWaiting Time\tTurn Around Time");
for(i=0;i<no_p;i++)
{
System.out.println("\tP"+(i+1)+"\t "+burst_time[i]+"\t\t "+WT[i]+"\t\t "+TT[i]);
}
System.out.println("\n----------------------------------------------------------------");
System.out.println("\nAverage waiting time : "+avg_wait);
System.out.println("\nAverage Turn Around time : "+avg_TT+"\n");
}
}
/*Output:
Enter the number of process:
3
****************************************************************
Processes:
****************************************************************
Process Burst Time Waiting Time Turn Around Time
P1 24 0 24
P2 3 24 27
P3 3 27 30
----------------------------------------------------------------
Average waiting time : 17.0
Average Turn Around time : 27.0 */
/*Round Robin(Preemptive)*/
import java.util.*; import
java.io.*; class RoundR
{
public static void main(String args[])
{
int Process[]=new int[10];
int a[]=new int[10]; int
Arrival_time[]=new int[10]; int
Burst_time[]=new int[10]; int
WT[]=new int[10]; int TAT[]=new
int[10]; int Pno,sum=0;;
int TimeQuantum;
for(int i=0;i<Pno;i++)
TAT[i]=WT[i]+a[i];
System.out.println("process\t\tBT\tWT\tTAT");
for(int i=0;i<Pno;i++)
{
System.out.println("process"+(i+1)+"\t"+a[i]+"\t"+WT[i]+"\t"+TAT[i]);
}
float avg_wt=0; float
avg_tat=0; for(int
j=0;j<Pno;j++)
{
avg_wt+=WT[j];
}
for(int j=0;j<Pno;j++)
{
avg_tat+=TAT[j];
}
System.out.println("average waiting time "+(avg_wt/Pno)+"\n Average turn around time"+
(avg_tat/Pno));
}
}
/*OUTPUT::
Enter the no. of Process::
5
Enter each process::
1
2
3
4
5
Enter the Burst Time of each process::
2
1
8
4
5
Enter the Time Quantum::
2
process BT WT TAT process1 0 0 0
process2 0 2 2 process3 0 12 12
process4 0 9 9 process5 0 13 13
average waiting time 7.2
Average turn around time7.2 */ import java.util.Scanner; class SJF1{ public static void main(String
args[]){ int burst_time[],process[],waiting_time[],tat[],i,j,n,total=0,pos,temp; float wait_avg,TAT_avg;
Scanner s = new Scanner(System.in);
//Sorting for(i=0;i<n;i++)
{
pos=i; for(j=i+1;j<n;j++)
{
if(burst_time[j]<burst_time[pos]) pos=j;
}
temp=burst_time[i]; burst_time[i]=burst_time[pos];
burst_time[pos]=temp;
temp=process[i]; process[i]=process[pos];
process[pos]=temp;
}
//First process has 0 waiting time
waiting_time[0]=0; //calculate
waiting time for(i=1;i<n;i++)
{
waiting_time[i]=0; for(j=0;j<i;j++)
waiting_time[i]+=burst_time[j];
total+=waiting_time[i];
}
}
}
System.out.println(“Enter no of process”);
n = sc.nextInt(); BT
= new int[n + 1];
WT = new int[n + 1]; TAT
= new int[n + 1]; float
AWT = 0;
System.out.println(“Enter Burst time for each process”); for
(int i = 0; i < n; i++) {
System.out.println(“Enter BT for process ” + (i + 1));
BT[i] = sc.nextInt(); }
/*
O/P for SJF shortest job first scheduling algorithm is :
Enter no of process
4
Enter Burst time for each process
Enter BT for process 1
5
Enter BT for process 2
3
Enter BT for process 3
3
Enter BT for process 4
1
PROCESS BT WT TAT
0101
1314
2347
3 5 7 12
***********************************************
Avg waiting time=3.0
***********************************************
*/
sc.close();
}
return need;
}
return true;
}
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];
System.out.println("Allocated process : "+i); allocated=done[i]=true;
j++;
}
if(!allocated) break; //if no allocation
}
if(j==np) //if all processes are allocated System.out.println("\
nSafely allocated");
else
System.out.println("All proceess cant be allocated safely");
}
/*
OUTPUT:
Enter no. of processes: 5
Enter no. of Resources : 3
Enter allocation matrix -->
010
200
302
211
002
Enter max matrix -->
753
322
902
222
433
Enter available matrix -->
332
Allocated process : 1
Allocated process : 3
Allocated process : 4
Allocated process : 0
Allocated process : 2
Safely allocated
*/
//Java implementation of First - Fit algorith
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
1 212 2
2 417 5
3 112 2
4 426 Not Allocated
PS C:\Users\Mandar\OneDrive\Desktop>
Output :-
****LRU****
import java.io.*;
class lru
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int f,page=0,ch,pgf=0,n,chn=0; boolean flag; int pages[];
//pgf-page fault
System.out.println("1.LRU");
int pt=0;
System.out.println("enter no. of frames: ");
f=Integer.parseInt(obj.readLine());
int frame[]=new int[f];
for(int i=0;i<f;i++)
{
frame[i]=-1;
}
pages=new int[n];
System.out.println("enter the page no ");
for(int j=0;j<n;j++)
pages[j]=Integer.parseInt(obj.readLine());
int pg=0;
for(pg=0;pg<n;pg++)
{
page=pages[pg];
flag=true; for(int
j=0;j<f;j++)
{
if(page==frame[j])
{
flag=false;
break;
}
}
int temp,h=3,i;
if(flag) { if(
frame[1]!=-1 && frame[2]!=-1 && frame[0]!=-1)
{
temp=pages[pg-3];
if(temp==pages[pg-2] || temp==pages[pg-1])
temp=pages[pg-4];
for(i=0;i<f;i++)
if(temp==frame[i])
break;
frame[i]=pages[pg];
}
else
{
if(frame[0]==-1)
frame[0]=pages[pg];
else if(frame[1]==-1)
frame[1]=pages[pg]; else
if(frame[2]==-1)
frame[2]=pages[pg];
}
System.out.print("frame :");
for(int j=0;j<f;j++)
System.out.print(frame[j]+" ");
System.out.println(); pgf++;
} else
{
System.out.print("frame :"); for(int j=0;j<f;j++)
System.out.print(frame[j]+" ");
System.out.println();
}
}//for
System.out.println("Page fault:"+pgf);
}//main
}//class
/*
OUTPUT:-
*/
****Optimal****
if(flag2 == 0){
flag3 =0;
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < numberOfFrames; ++j){
if(temp[j] > max){
max = temp[j]; pos = j;
}
}
}
frame[pos] = pages[i];
faults++;
}
// System.out.print();
Output:-
Enter number of Pages:
10
Enter the pages:
1
0
1
2
3
7
8
1
5
2
1 -1 -1 -1 1 0 -1 -1 1 0 -1 -1 1 0 2 -1 1 0 2 3
1 7 2 3
Total Page Faults: 7