Archive for the ‘Operating System’ Category

import java.util.*;
class sjf
{
public static void main(String args[])
{
int n,i;
float avg_wt,trn_around;
Scanner sc=new Scanner(System.in);
System.out.println(“enter no of processes”);
n=sc.nextInt();
int p[][]=new int[4][n];
System.out.println(“enter process no. and burst time of the processes”);
for(i=0;i<n;i++)
{
p[0][i]=sc.nextInt();
p[1][i]=sc.nextInt();
}
sort(p,n);
avg_wt=0.0f;
for(i=0;i<n;i++)
avg_wt=avg_wt+p[2][i];
avg_wt=avg_wt/n;
System.out.println(“the average waiting time of the processes using shortest job first scheduling is”+avg_wt);
trn_around=0.0f;
for(i=0;i<n;i++)
trn_around=trn_around+p[3][i];
trn_around=trn_around/n;
System.out.println(“the average turn around time of the processes using shortest job first scheduling is”+trn_around);
}
static void sort(int p[][],int n)
{
int i,j,temp1,temp2,temp3,temp4;
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(p[1][j]>p[1][j+1])
{
temp1=p[0][j];
temp2=p[1][j];
temp3=p[2][j];
temp4=p[3][j];
p[0][j]=p[0][j+1];
p[1][j]=p[1][j+1];
p[2][j]=p[2][j+1];
p[3][j]=p[3][j+1];
p[0][j+1]=temp1;
p[1][j+1]=temp2;
p[2][j+1]=temp3;
p[3][j+1]=temp4;
}
p[2][0]=0;
for(i=1;i<n;i++)
p[2][i]=p[2][i-1]+p[1][i-1];
p[3][0]=p[1][0];
for(i=1;i<n;i++)
p[3][i]=p[2][i]+p[1][i];
}
}

import java.util.*;
class fcfs
{
public static void main(String args[])
{
int n,i;
float avg_wt,trn_around;
Scanner sc=new Scanner(System.in);
System.out.println(“enter no of processes”);
n=sc.nextInt();
int p[][]=new int[4][n];
System.out.println(“enter process no.,and burst time of the processes”);
for(i=0;i<n;i++)
{
p[0][i]=sc.nextInt();
p[1][i]=sc.nextInt();
}
order(p,n);
avg_wt=0.0f;
for(i=0;i<n;i++)
avg_wt=avg_wt+p[2][i];
avg_wt=avg_wt/n;
System.out.println(“the average waiting time of the processes using fcfs scheduling is”+avg_wt);
trn_around=0.0f;
for(i=0;i<n;i++)
trn_around=trn_around+p[3][i];
trn_around=trn_around/n;
System.out.println(“the average turn around time of the processes using fcfs scheduling is”+trn_around);
}
static void order(int p[][],int n)
{
int i,j;
p[2][0]=0;
for(i=1;i<n;i++)
p[2][i]=p[2][i-1]+p[1][i-1];
p[3][0]=p[1][0];
for(i=1;i<n;i++)
p[3][i]=p[2][i]+p[1][i];
}
}

import java.util.*;
class fcfs_dsk
{
public static void main(String args[])
{
int n,i,hs,flag,total=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter no of cylinders”);
n=sc.nextInt();
int c[]=new int[n];
int hm[]=new int[n];
System.out.println(“Enter the cylinder no.”);
for(i=0;i<n;i++)
c[i]=sc.nextInt();
System.out.println(“enter head start”);
hs=sc.nextInt();
flag=hs;
for(i=0;i<n;i++)
{
if(c[i] > flag)
hm[i]=c[i]-flag;
else
hm[i]=flag-c[i];
flag=c[i];
}
for(i=0;i<n;i++)
total=total+hm[i];
System.out.println(“The total no of cylinders traversed during disk movement is:”+ total);
}
}

import java.util.*;
class rrs
{
public static void main(String args[])
{
int n,i,quanta;
float avg_wt,trn_around;
Scanner sc=new Scanner(System.in);
System.out.println(“enter no of processes”);
n=sc.nextInt();
int p[][]=new int[4][n];
System.out.println(“enter process no.,and burst time of the processes”);
for(i=0;i<n;i++)
{
p[0][i]=sc.nextInt();
p[1][i]=sc.nextInt();
}
System.out.println(“enter the time quantum for round robin scheduling”);
quanta=sc.nextInt();
rrobin(p,n,quanta );
avg_wt=0.0f;
for(i=0;i<n;i++)
avg_wt=avg_wt+p[2][i];
avg_wt=avg_wt/n;
System.out.println(“the average waiting time of the processes using round robin scheduling is:”+avg_wt);
trn_around=0.0f;
for(i=0;i<n;i++)
trn_around=trn_around+p[3][i];
trn_around=trn_around/n;
System.out.println(“the average turn around time of the processes using round robin scheduling is:”+trn_around);
}
static void rrobin(int p[][],int n,int q)
{
int i,j;
int rem[]=new int[n];
for(i=0;i<n;i++)
p[3][i]=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
rem[j]=p[1][j];
if(p[0][i]!=1)
init(p,rem,i,q);
while(rem[i]>0)
{
rem[i]=rem[i]-q;
if(rem[i]<0)
p[3][i]=p[3][i]+rem[i]+q;
else
p[3][i]=p[3][i]+q;
for(j=i+1;j!=i && rem[i]>0;j++)
{
if(j==n)
j=0;
rem[j]=rem[j]-q;
if(rem[j]<0)
p[3][i]=p[3][i]+rem[j]+q;
else
p[3][i]=p[3][i]+q;
}
}
}
for(i=0;i<n;i++)
p[2][i]=p[3][i]-p[1][i];
}
static void init(int p[][],int rem[],int n,int q)
{
int i;
for(i=0;i<n;i++)
{
rem[i]=rem[i]-q;
if(rem[i]>0)
p[3][n]=p[3][n]+q;
else
p[3][n]=p[3][n]+p[1][i];
}
}
}

import java.util.*;
class priority
{
public static void main(String args[])
{
int n,i;
float avg_wt,trn_around;
Scanner sc=new Scanner(System.in);
System.out.println(“enter no of processes”);
n=sc.nextInt();
int p[][]=new int[5][n];
System.out.println(“enter process no.,burst time and priority of the processes”);
for(i=0;i<n;i++)
{
p[0][i]=sc.nextInt();
p[1][i]=sc.nextInt();
p[2][i]=sc.nextInt();
}
prior(p,n);
avg_wt=0.0f;
for(i=0;i<n;i++)
avg_wt=avg_wt+p[3][i];
avg_wt=avg_wt/n;
System.out.println(“the average waiting time of the processes using priority scheduling is”+avg_wt);
trn_around=0.0f;
for(i=0;i<n;i++)
trn_around=trn_around+p[4][i];
trn_around=trn_around/n;
System.out.println(“the average turn around time of the processes using priority scheduling is”+trn_around);
}
static void prior(int p[][],int n)
{
int i,j,temp1,temp2,temp3,temp4,temp5;
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(p[2][j]<p[2][j+1])
{
temp1=p[0][j];
temp2=p[1][j];
temp3=p[2][j];
temp4=p[3][j];
temp5=p[4][j];
p[0][j]=p[0][j+1];
p[1][j]=p[1][j+1];
p[2][j]=p[2][j+1];
p[3][j]=p[3][j+1];
p[4][j]=p[4][j+1];
p[0][j+1]=temp1;
p[1][j+1]=temp2;
p[2][j+1]=temp3;
p[3][j+1]=temp4;
p[4][j+1]=temp5;
}
p[3][0]=0;
for(i=1;i<n;i++)
p[3][i]=p[3][i-1]+p[1][i-1];
p[4][0]=p[1][0];
for(i=1;i<n;i++)
p[4][i]=p[3][i]+p[1][i];
}
}

import java.util.*;
class optimal
{
public static void main(String args[])
{
int n,m,i,j,k,mn,flag=0,pgfault=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter no of pages”);
n=sc.nextInt();
System.out.println(“enter page nos”);
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println(“Enter no of frames”);
m=sc.nextInt();
int frm[]=new int[m];
int whn[]=new int[m];
for(i=0;i<m;i++)
{
frm[i]=a[i];
whn[i]=0;
}
for(;i<n;i++)
{
for(j=0;j<m;j++)
{
if(frm[j]==a[i])
{
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
{
for(j=0;j<m;j++)
{
for(k=i+1;k<n;k++)
{
if(frm[j]==a[k])
whn[j]=k;
else
whn[j]=0;
}
}
mn=ltr(whn,m);
frm[mn]=a[i];
pgfault++;
}
}
pgfault=pgfault+m;
System.out.println(“Optimal algorithm”);
System.out.println(“page fault is:”+pgfault);
System.out.println(“page hits are:”+(n-pgfault));
}
static int ltr(int whn[],int m)
{
int i,mn=0;
for(i=1;i<m;i++)
if((whn[i] > whn[i-1]) && (whn[i]!=0))
mn=i;
return mn;
}
}

import java.util.*;
class lru
{
public static void main(String args[])
{
int n,m,i,j,mn,flag=0,pgfault=0,timestamp=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter no of pages”);
n=sc.nextInt();
System.out.println(“enter page nos”);
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println(“Enter no of frames”);
m=sc.nextInt();
int frm[]=new int[m];
int ts[]=new int[m];
for(i=0;i<m;i++)
{
frm[i]=a[i];
ts[i]=timestamp++;
}
for(;i<n;i++)
{
for(j=0;j<m;j++)
{
if(frm[j]==a[i])
{
ts[j]=timestamp++;
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
{
mn=min(ts,m);
frm[mn]=a[i];
ts[mn]=timestamp++;
pgfault++;
}
}
pgfault=pgfault+m;
System.out.println(“least recently used(LRU) algorithm”);
System.out.println(“page fault is:”+pgfault);
System.out.println(“page hits are:”+(n-pgfault));
}
static int min(int ts[],int m)
{
int i,mn=0;
for(i=1;i<m;i++)
if(ts[i]<ts[i-1])
mn=i;
return mn;
}
}

import java.util.*;
class lfu
{
public static void main(String args[])
{
int n,m,i,j,mn,flag=0,pgfault=0,cnt=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter no of pages”);
n=sc.nextInt();
System.out.println(“enter page nos”);
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println(“Enter no of frames”);
m=sc.nextInt();
int frm[]=new int[m];
int count[]=new int[m];
int cunt[]=new int[m];
for(i=0;i<m;i++)
{
frm[i]=a[i];
count[i]=1;
cunt[i]=cnt++;
}
for(;i<n;i++)
{
for(j=0;j<m;j++)
{
if (frm[j]==a[i])
{
count[j]++;
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
{
mn=min(count,cunt,m);
frm[mn]=a[i];
cunt[mn]=cnt++;
pgfault++;
}
}
pgfault=pgfault+m;
System.out.println(“least frequently used(LFU) algorithm”);
System.out.println(“page fault is:”+pgfault);
System.out.println(“page hits are:”+(n-pgfault));
}
static int min(int count[],int cunt[],int m)
{
int i,j,mn=0;
for(i=1;i<m;i++)
if(count[i]<count[i-1])
mn=i;
for(i=1;i<m;i++)
{
if(count[i]==count[i-1] && count[mn]==count[i])
for(j=1;j<m;j++)
if(cunt[j]<cunt[j-1])
mn=i;
}
return mn;
}
}

import java.util.*;
class fifo
{
public static void main(String args[])
{
int n,m,i,j,mn,flag=0,pgfault=0,cnt=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter no of pages”);
n=sc.nextInt();
System.out.println(“enter page nos”);
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println(“Enter no of frames”);
m=sc.nextInt();
int frm[]=new int[m];
int count[]=new int[m];
for(i=0;i<m;i++)
{
frm[i]=a[i];
count[i]=cnt++;
}
for(;i<n;i++)
{
for(j=0;j<m;j++)
{
if(frm[j]==a[i])
{
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
{
mn=min(count,m);
frm[mn]=a[i];
count[mn]=cnt++;
pgfault++;
}
}
pgfault=pgfault+m;
System.out.println(“First in first out(FIFO) algorithm”);
System.out.println(“page fault is:”+pgfault);
System.out.println(“page hits are:”+(n-pgfault));
}
static int min(int count[],int m)
{
int i,mn=0;
for(i=1;i<m;i++)
if(count[i]<count[i-1])
mn=i;
return mn;
}
}

 

 

Creating file

exp1.1

Displaying file

exp1.2

Displays first n lines

exp1.6

Displays last n lines

exp1.7

Displays number of lines,words and characters respectively exp1.3exp1.4   exp1.5

Displays present working directory(pwd);

Displays list of files,sub-directories in the directory(ls)

exp1.9

Displays calendar

exp1.8

Copies content from one file to another

exp1.10

Pattern matching command

exp2.1

exp2.2

Sort in ascending/descending order

exp2.3

Here,it prints column 2(awk ‘{print $2}’) &

If condition matches,it prints columns 2,3,4 and 6(awk ‘{if($1==”897″)print $2,$3,$4,$6 }’)

 exp2.4

 

PS: Utkarsha Raikar and I collectively performed this.