Showing posts with label Operating System. Show all posts
Showing posts with label Operating System. Show all posts

Wednesday, 16 July 2014

The Dining Philosophers Problem

A Solution: Breaking the Dependency” improves upon the Broken Solution”.

The Dining Philosophers problem is a classic concurrency or synchronization problem in Operating Systems. E. Dijkstra posed and solved this problem in 1965. In this problem, there are 5 philosophers around a dining table and 5 forks available for eating. The life of the philosophers consists of alternative period of eating and thinking, and eating needs two forks (left and right). When philosopher gets hungry, he/she tries to acquire two forks, eats for a while, and then puts down the forks and continues to think.


The broken solution is that when the philosopher is hungry, he/she picks up the left fork first and waits for right fork, when gets it eats for a while and put both forks back to the table (Arpaci-Dusseau & Arpaci-Dusseau, 2014). The problem with this solution, because of which it is called “broken”, is that if all philosophers pick up the forks to their left and wait for the ones on the right, nobody will get to eat (as all forks are already acquired) leading to a deadlock. This phenomenon can be said to be “Circular Wait”.

The solution “Breaking the Dependency” improves upon it by changing the way the forks are acquired by one philosopher. More specifically, Philosopher 4 (or any other philosopher) can be made to pick up the right fork first, instead of the one on the left (Arpaci-Dusseau & Arpaci-Dusseau, 2014).  
void getforks() {
if (p == 4) {
sem_wait(forks[right(p)]);
sem_wait(forks[left(p)]);
} else {
sem_wait(forks[left(p)]);
sem_wait(forks[right(p)]);
}
}

Friday, 31 May 2013

Java program to simulate FIFO and LRU page replacement algorithms

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

import javax.swing.JFileChooser;

/**
 * This class reads a user chosen file for the reference string
 * (there should be spaces between the values in the string)
 * and simulates the selected page replacement policy i.e.
 * FIFO or LRU. It displays the status of page frames at each
 * new value in the string and saves the paging report in a plain
 * text file.
 * @author Dixit Bhatta
 * **/
public class pgReplace {

public static void main(String[] args) throws IOException {
ArrayList<Integer> ref = new ArrayList<Integer>();//creating new HashSet

try {
Scanner filein = new Scanner(getInputFileNameFromUser()); //open file
while(filein.hasNext()) {
String frame = filein.next();
int frm = Integer.parseInt(frame);
ref.add(frm);// add the integers to HashSet.
}//end of while

filein.close();//close the file

} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
}

//display the size if it the set is not empty
if(!ref.isEmpty()){
int size = ref.size();
System.out.println("The length of the Reference String is: " + size);
}
//printing the read reference string using an iterator
Iterator<Integer> iter = ref.iterator();
while (iter.hasNext())
System.out.print(iter.next() +" ");

//converting the arraylist to an array
Integer frame[] = new Integer[ref.size()];
   frame = ref.toArray(frame);

   System.out.printf("\nSelect the Page Replacement Algorithm: ");
   System.out.printf("\n1.FIFO\n2.LRU\n? ");

   Scanner sc = new Scanner(System.in);
   while (!sc.hasNextInt()) {
       sc.next(); // discard next token, which isn't a valid int
   }
   int ch = sc.nextInt();

switch(ch){
   case 1: fifo(frame,ref.size());
    break;
   case 2: lru(frame,ref.size());
    break;
   default: System.out.printf("\nInvlaid Choice");
   }
 
}
/**Simulates LRU page replacement for given reference string
* @throws IOException **/
public static void lru(Integer[] page, int n) throws IOException {
int [] frame = new int[10];
int []used = new int[10];
int index = 0;
int i,j,k,temp;
int flag=0,pf=0;
BufferedWriter write = new BufferedWriter(new FileWriter("file.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf("\tLRU Page Replacement");
System.out.printf("\nEnter number of Frames: ");
Scanner sc = new Scanner(System.in);
   while (!sc.hasNextInt()) {
       sc.next(); // discard next token, which isn't a valid int
   }
   int nf = sc.nextInt();
for(i=0;i<nf;i++)
frame[i]= -1;

for(i=0;i<n;i++){
flag=0;
for(j=0;j<nf;j++){
if(frame[j]==page[i]){//no fault
System.out.printf("\n%d: ", page[i]);
out.printf("\n%d: ", page[i]);
flag=1;
break;
}
}
if(flag==0){//fault occurs
for(j=0;j<nf;j++)
used[j]=0;//all unused initially
//moving through pages and searching recently used pages
try{
for(j = 0,temp= i-1;j < nf-1;j++,temp--){
for(k = 0;k < nf;k++){
if(frame[k]==page[temp])
used[k]=1;
//mark the recently used pages
}
}
}
catch(ArrayIndexOutOfBoundsException e){
}
for(j=0;j<nf;j++)
if(used[j]==0)
index=j;
//replace the lru page with new page
frame[index]=page[i];
System.out.printf("\n%d: ", page[i]);
System.out.printf("--->F ");
out.printf("\n%d: ", page[i]);
out.printf("--->F ");
pf++;//no of page faults
}

for(k= nf-1;k>=0;k--)
if(frame[k] != -1){
System.out.printf(" %d",frame[k]);//print frames
out.printf(" %d",frame[k]);
}
}

System.out.printf("\nNumber of page faults is: %d ",pf);
out.printf("\nNumber of page faults is: %d ",pf);
out.close();
write.close();
}

/**Simulates FIFO page replacement for given reference string
* @throws IOException **/
public static void fifo(Integer[] pages, int pg) throws IOException {
int [] frame = new int[25];
int i,k,avail,count=0;
BufferedWriter write = new BufferedWriter(new FileWriter("file.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf("\tFIFO Page Replacement");
System.out.printf("\nEnter number of Frames: ");
Scanner sc = new Scanner(System.in);
   while (!sc.hasNextInt()) {
       sc.next(); // discard next token, which isn't a valid int
   }
   int nof = sc.nextInt();
for(i=0;i<nof;i++)
frame[i]= -1;

   int j=0;
   System.out.printf("\n");
   out.printf("\n");
for(i=0;i<pg;i++){
System.out.printf("%d\t",pages[i]);
out.printf("%d\t",pages[i]);
avail=0;
for(k=0;k<nof;k++)
if(frame[k]==pages[i])
avail=1;

  if (avail==0){
frame[j]=pages[i];
j=(j+1) % nof;
count++;

for(k=0;k<nof;k++)
if(frame[k]!=-1){
System.out.printf("%d",frame[k]);
out.printf("%d",frame[k]);
}
   
System.out.printf("-->F");
out.printf("-->F");
     }

     if(avail==1){
   for(k=0;k<nof;k++)
    if(frame[k]!=-1){
    System.out.printf("%d",frame[k]);
    out.printf("%d",frame[k]);
    }
     }
     System.out.printf("\n");
     out.printf("\n");
}
System.out.printf("\nNo of Faults: %d",count);
out.printf("\nNo of Faults: %d",count);
out.close();
write.close();
}
/**
* Lets the user select an input file using a standard file
* selection dialog box. If the user cancels the dialog
* without selecting a file, the return value is null.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(null);
if (option != JFileChooser.APPROVE_OPTION)
return null;
else
return fileDialog.getSelectedFile();
}//end of choosing file

}

©Dixit Bhatta 2013

Monday, 22 April 2013

C program for Round Robin Scheduling Policy


#include<stdio.h>
#include<conio.h>
#define MAX 5
void main (){
                clrscr();
                int pname[MAX],brtime[MAX];
                int end[MAX+1],start[MAX];
                int qtm,total=0,rem =0;
                int num = 0, i;
                for(i=0;i<MAX;i++){
                                brtime[i] = 0;
                }
                printf("\t\tRound-Robin SCHEDULING");
                printf("\nEnter the number of processes  (max. 5): ");
                scanf("%d",&num);
                printf("\nEnter the quantum size: ");
                scanf("%d",&qtm);
                for(i=0;i<num;i++){
                                printf("\nEnter process name, burst time: ");
                                scanf("%d %d",&pname[i],&brtime[i]);
                                total= total + brtime[i];
                }
                rem = total;
                printf("\n");
                while(rem!=0){
                                for(i=0;i<num;i++){
                                                if(brtime[i]==0){
                                                                //do nothing
                                                }
                                                else if(brtime[i]>=qtm){
                                                                brtime[i] = brtime[i] - qtm;
                                                                printf("\n%d ->%d",pname[i],qtm);
                                                                rem = rem- qtm;
                                                }

                                                else{
                                                                printf("\n%d ->%d",pname[i],brtime[i]);
                                                                rem= rem - brtime[i];
                                                                brtime[i]=0;
                                                }

                                }
                }
                printf("\n\nTotal Time = %d",total);
                getch();
 }

Sample Output:

                       Round-Robin SCHEDULING
Enter the number of processes (max. 5): 3
Enter the quantum size: 3
Enter process name, burst time: 1     6
Enter process name, burst time: 2     2
Enter process name, burst time: 3     7

1 ->3
2 ->2
3 ->3
1 ->3
3 ->3
3 ->1

Total Time = 15_

©Dixit Bhatta 2013