Os p
Fcfs
// FCFS
#include<stdio.h>
#include<stdlib.h>
int addrq();
int selectionjob();
int deleteq(int);
int fsahll();
struct job{
int atime; //arraival time.
int btime; //brust time.
int ft; //finish time.
int tat; //Trun around time.
int wt; // waiting time.
}p[10];
int arr[10],brust[10],n,rq[10],no_rq=0,time=0;
int main(){
int i,j;
printf("Enter the job/process number:");
scanf("%d",&n);
printf("\n");
for(i = 0;i<n;i++){
printf("Enter the arrival time p%d:",i);
scanf("%d",&p[i].atime); //Assigning the arrival time.
arr[i] = p[i].atime;
}
printf("\n");
for(i = 0;i<n;i++){
printf("The arrival time of p%d:",i);
printf("%d\n",p[i].atime); //Printing the arrival time.
// arr[i] = p[i].atime;
}
printf("\n");
for(i = 0;i<n;i++){
printf("Enter the brust time p%d:",i);
scanf("%d",&p[i].btime); //Assigning the brust time.
brust[i] = p[i].btime;
}
printf("\n");
for(i = 0;i<n;i++){
printf("The brust time of p%d:",i);
printf("%d\n",p[i].btime); //Printing the brust time.
// brust[i] = p[i].btime;
}
printf("\n");
addrq(); //Adding the process.
// Process start now.
printf("Gantt Chart is:");
while(1){
j = selectionjob(); //sercah the process now.
if(j == -1){
printf("CPU is ideal");
time++;
addrq();
}else{
while(brust[j]!=0){
printf("\t j %d",j);
brust[j]--;
time++;
addrq();
}
p[j].ft = time;
}
if(fsahll() == 1)
break;
}
int Tat = 0,Twt =0;
printf("\n");
printf("\nJob \t FT \t TAT \t WT");
for(i=0;i<n;i++){
p[i].tat = p[i].ft-p[i].atime;
p[i].wt = p[i].tat-p[i].btime;
printf("\n JOb %d \t %d \t %d \t %d",i,p[i].ft,p[i].tat,p[i].wt);
Tat += p[i].tat;
Twt += p[i].wt;
}
float avgtat = Tat /n;
float avgwt = Twt /n;
printf("\nAverage of trun around time is:%f",avgtat);
printf("\nAverage of wait time is:%f",avgwt);
}
int addrq(){
int i;
for(i=0;i<n;i++){
if(arr[i] == time){
rq[no_rq] = i;
printf("\n\n%d",rq[no_rq]);
no_rq++;
}
}
}
int selectionjob(){
int i,j;
if(no_rq == 0)
return 1;
j = rq[0];
deleteq(j);
return j;
}
int deleteq(int j){
int i;
for(i=0;i<no_rq;i++)
if(rq[i] == j)
break;
for(i= i+1;i<no_rq;i++)
rq[i-1] = rq[i];
no_rq--;
}
int fsahll(){
int i;
for(i=0;i<n;i++)
if(brust[i]!=0)
return -1;
return 1;
}
Sjf non preemptive
// NON-Premptive SJF
#include<stdio.h>
#include<stdlib.h>
struct job{
int atime; //arraival time.
int btime; //brust time.
int ft; //finish time.
int tat; //Trun around time.
int wt; // waiting time.
}p[10];
int arr[10],brust[10],n,rq[10],no_rq=0,time=0;
void main(){
int i,j;
printf("Enter the job number:");
scanf("%d",&n);
printf("\n");
for(i = 0;i<n;i++){
printf("Enter the arrival time p%d:",i);
scanf("%d",&p[i].atime); //Assigning the arrival time.
arr[i] = p[i].atime;
}
printf("\n");
for(i = 0;i<n;i++){
printf("Enter the arrival time p%d:",i);
printf("%d\n",p[i].atime); //Printing the arrival time.
arr[i] = p[i].atime;
}
printf("\n");
for(i = 0;i<n;i++){
printf("Enter the brust time p%d:",i);
scanf("%d",&p[i].btime); //Assigning the brust time.
brust[i] = p[i].btime;
}
printf("\n");
for(i = 0;i<n;i++){
printf("Enter the brust time p%d:",i);
printf("%d\n",p[i].btime); //Printing the brust time.
brust[i] = p[i].btime;
}
printf("\n");
addrq(); //Adding the process.
// Process start now.
printf("Gnatt Chart is:");
while(1){
j = selectionjob(); //sercah the process now.
if(j == -1){
printf("CPU is ideal");
time++;
addrq();
}else{
while(brust[j]!=0){
printf("\t j %d",j);
brust[j]--;
time++;
addrq();
}
p[j].ft = time;
}
if(fsahll() == 1)
break;
}
int Tat = 0,Twt =0;
printf("\n");
printf("\nJob \t FT \t TAT \t WT");
for(i=0;i<n;i++){
p[i].tat = p[i].ft-p[i].atime;
p[i].wt = p[i].tat-p[i].btime;
printf("\n JOb %d \t %d \t %d \t %d",i,p[i].ft,p[i].tat,p[i].wt);
Tat += p[i].tat;
Twt += p[i].wt;
}
float avgtat = Tat /n;
float avgwt = Twt /n;
printf("\nAverage of trun around time is:%f",avgtat);
printf("\nAverage of wait time is:%f",avgwt);
}
int addrq(){
int i;
for(i=0;i<n;i++){
if(arr[i] == time){
rq[no_rq] = i;
no_rq++;
}
}
}
int selectionjob(){
int i,j;
if(no_rq == 0)
return 1;
j = rq[0];
for(i=1;i<no_rq;i++)
if(brust[j]>brust[rq[i]])
j = rq[i];
deleteq(j);
return j;
}
int deleteq(int j){
int i;
for(i=0;i<no_rq;i++)
if(rq[i] == j)
break;
for(i= i+1;i<no_rq;i++)
rq[i-1] = rq[i];
no_rq--;
}
int fsahll(){
int i;
for(i=0;i<n;i++)
if(brust[i]!=0)
return -1;
return 1;
}
/*
Output:-
Enter the job number:3
Enter the arrival time p0:1
Enter the arrival time p1:2
Enter the arrival time p2:0
Enter the arrival time p0:1
Enter the arrival time p1:2
Enter the arrival time p2:0
Enter the brust time p0:3
Enter the brust time p1:2
Enter the brust time p2:5
Enter the brust time p0:3
Enter the brust time p1:2
Enter the brust time p2:5
Gnatt Chart is: j 2 j 2 j 2 j 2 j 2 j 1 j 1 j 0 j 0 j 0
Job FT TAT WT
JOb 0 10 9 6
JOb 1 7 5 3
JOb 2 5 5 0
Average of trun around time is:6.000000
Average of wait time is:3.000000
*/
Pre sjf
// Premptive SJF
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
struct job{
bool isc;
int at; //arraival time.
int bt; //brust time.
int st; //Start time.
int nst; //new start time.
int oft; //old finish time;
int pcount; // preocess to be count.
int ft; //finish time.
int tat; //Trun around time.
int wt; // waiting time.
}p[100];
int n,arr[100],tm=0,arrv=0,count=0;
float Tat,Wt;
void main(){
int i,k=0,a[100];
printf("How many Process: ");
scanf("%d",&n);
printf("Enter :-\nProcess BT AT\n");
for(i=1;i<=n;i++){
printf("p%d\t",i);
scanf("%d%d",&p[i].bt,&p[i].at);
p[i].isc=false;
p[i].pcount=0;
count += p[i].bt;
p[i].wt=0;
}
printf("Process BT AT \n");
for(i=1;i<=n;i++)
printf("p%d %d %d\n",i,p[i].bt,p[i].at);
printf("\nGantt chart\n");
for(i=1;i<=n;i++)
{
if(p[i].at==0)
{
a[++k]=i;
}
}
minbrust(a,k);
while(tm!=count)
{
selecta();
if(arrv==0)
{
printf("|idl");
tm+=1;
count+=1;
}
else
{
minbrust(arr,arrv);
arrv=0;
}
}
printf("\n _________________________#######################______________________\n");
printf("\nProcess\tBT\tAT \tST\tWT\tFT\tTAT\n");
for(i=1;i<=n;i++)
printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n",i,p[i].bt,p[i].at,p[i].st,p[i].wt,p[i].ft,p[i].tat);
printf("\nAvg wait time=%f",Wt/n);
printf("\nAvg TAT=%f\n",Tat/n);
}
void minbrust(int a[],int k){
int min=p[a[1]].bt,i,m=a[1];
for(i=1;i<=k;i++)
{
if(p[a[i]].bt<min)
{
min=p[a[i]].bt;
m=a[i];
}
}
process(m);
}
process(int s)
{
int k;
p[s].pcount++;
if(p[s].pcount==1)
{
p[s].wt=p[s].st-p[s].at;
p[s].st=tm;
}
p[s].nst=tm;
tm++;
k=p[s].nst-p[s].oft;
p[s].oft=tm;
if(k>0)
p[s].wt+=k;
if(p[s].pcount==p[s].bt)
{
p[s].isc=true;
p[s].ft=tm;
p[s].tat=p[s].ft-p[s].at;
Tat+=p[s].tat;
Wt+=p[s].wt;
}
printf("p%d ",s);
}
selecta(){
int i;
for(i=1;i<=n;i++)
{
if(p[i].at<=tm && p[i].isc==false)
arr[++arrv]=i;
}
}
/*
Output:-
How many Process: 3
Enter :-
Process BT AT
p1 2 1
p2 3 2
p3 5 0
Process BT AT
p1 2 1
p2 3 2
p3 5 0
Gantt chart
p3 p1 p1 p2 p2 p2 p3 p3 p3 p3
_________________________#######################______________________
Process BT AT ST WT FT TAT
P1 2 1 1 0 3 2
P2 3 2 3 1 6 4
P3 5 0 0 5 10 10
Avg wait time=2.000000
Avg TAT=5.333333
--------------------------------
Process exited after 13.45 seconds with return value 18
Press any key to continue . . .
*/
Demand paging FIFO
#include<stdio.h>
int main()
{
int i,j,n,ref_str[50],frame[10],no,k,avail,fcount=0; // Declaration of variable required
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n); // total no. of pages in reference string
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++) // accept entire reference string
scanf("%d",&ref_str[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1; // initialize all page frame to -1
j=0; // initialize page frame pointer
printf("\n ref string \t page frames \tHit/Fault\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",ref_str[i]);
avail=0; // Default value of available flag is 0
for(k=0;k<no;k++)
if(frame[k]==ref_str[i]) // input of page requested is compared with existing content of FRAME
{
avail=1; // as page found available is turned 1
for(k=0;k<no;k++)
printf("%d\t",frame[k]); // Print Current state of FRAME
printf("H"); // Indication of Page Hit
}
if (avail==0) // input page requested NOT existing in FRAME
{
frame[j]=ref_str[i]; // place page requested at j th location in FRAME
j=(j+1)%no; // Update J for next Cycle
fcount++; // Increment Counter for Page Fault
for(k=0;k<no;k++)
printf("%d\t",frame[k]); // Print Current state of FRAME
printf("F"); // Indication of Page Fault
}
printf("\n");
}
printf("Page Fault Is %d",fcount);
return 0;
}
Assignment 5 set b 2)
import java.awt.*;
import java.awt.event.*;
class InvalidPasswordException extends Exception
{
InvalidPasswordException()
{
System.out.println("User name and Password is not same");
}
}
class PasswordDemo extends Frame implements ActionListener
{
Label uname,upass;
TextField nametext;
TextField passtext,msg;
Button login,Clear;
Panel p;
int attempt=0;
char c= '*';
public void login()
{
p=new Panel();
uname=new Label("Use Name: " ,Label.CENTER);
upass=new Label ("Password: ",Label.RIGHT);
nametext=new TextField(20);
passtext =new TextField(20);
passtext.setEchoChar(c);
msg=new TextField(10);
msg.setEditable(false);
login=new Button("Login");
Clear=new Button("Clear");
login.addActionListener(this);
Clear.addActionListener(this);
p.add(uname);
p.add(nametext);
p.add(upass);
p.add(passtext);
p.add(login);
p.add(Clear);
p.add(msg);
add(p);
setTitle("Login");
setSize(290,200);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
Button btn=(Button)(ae.getSource());
if(attempt<3)
{
if((btn.getLabel())=="Clear")
{
nametext.setText("");
passtext.setText("");
}
if((btn.getLabel()).equals("Login"))
{
try
{
String user=nametext.getText();
String upass=passtext.getText();
if(user.compareTo(upass)==0)
{
msg.setText("Valid");
System.out.println("Username is valid");
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e)
{
msg.setText("Error");
}
attempt++;
}
}
else
{
System.out.println("you are using 3 attempt");
System.exit(0);
}
}
public static void main(String args[])
{
PasswordDemo pd=new PasswordDemo();
pd.login();
}
}
import java.awt.*;
ReplyDeleteimport java.awt.event.*;
class InvalidPasswordException extends Exception
{
InvalidPasswordException()
{
System.out.println("User name and Password is not same");
}
}
class PasswordDemo1 extends Frame implements ActionListener
{
Label uname,upass;
TextField nametext;
TextField passtext,msg;
Button login,Clear;
Panel p;
int attempt=0;
char c= '*';
public void login()
{
p=new Panel();
uname=new Label("User Name: ");
upass=new Label ("Password: ");
nametext=new TextField(20);
passtext =new TextField(20);
passtext.setEchoChar(c);
msg=new TextField(10);
msg.setEditable(false);
login=new Button("Login");
Clear=new Button("Clear");
login.addActionListener(this);
Clear.addActionListener(this);
p.add(uname);
p.add(nametext);
p.add(upass);
p.add(passtext);
p.add(login);
p.add(Clear);
p.add(msg);
add(p);
setTitle("Login");
setSize(290,200);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
//Button btn=(Button)(ae.getSource());
if(attempt<3)
{
if((ae.getSource()==Clear))
{
nametext.setText("");
passtext.setText("");
nametext.requestFocus();
}
if((ae.getSource()==login))
{
try
{
String user=nametext.getText();
String upass=passtext.getText();
if(user.compareTo(upass)==0)
{
msg.setText("Valid");
System.out.println("Username is valid");
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e)
{
msg.setText("Error");
}
attempt++;
}
}
else
{
System.out.println("you are using 3 attempt");
System.exit(0);
}
}
public static void main(String args[])
{
PasswordDemo1 pd=new PasswordDemo1();
pd.login();
}
}