Showing posts with label OS Lab. Show all posts
Showing posts with label OS Lab. Show all posts

Wednesday, June 3, 2015

DISTRIBUTED OPERATING SYSTEMS

AIM:

To write a java program for RMI lottery application in distributed operating systems.

ALGORITHM:

Step 1:        Start the program.

Step 2:        Consider a distributed system that consists of two processes which   
                            communicate with each other.

Step 3:        Let P be a state predicate on the local state of one process and Q be a state
                           predicate on the local state of the other process. Assume that neither P nor Q        
                           are stable (i.e. closed)

Step 4:        Design a superimposed computation which detects that there exists an  
  interleaving of underlying events in this system where at some state P ^Q    
  holds.

Step 5:        Design a RMI Lottery application

Step 6:        Each time you run the client program -- “java LotteryClient n”, the server  
                           program “LotteryServer” will generate n set of Lottery numbers

Step 7:        Here n is a positive integer, representing the money you will spend on Lottery  
  in sterling pounds

Step 8:        Events in a superposed computation may occur in at the same instant as the  
  Underlying events and/or at different instants

Step 9:        End of program


 PROGRAM:

import java.util.Random;
import java.util.Scanner;

 class Lottery
{
      private int g1;
      private int g2;
      private int g3;
      private int num1;
      private int num2;
      private int num3;
      private int match1;
      private int match2;
      private int match3;
      private int order1;
      private int order2;
      private int order3;
      private int totalMatch;
      private int match;

          public Lottery()
          {   
              
                  Scanner inputDevice = new Scanner(System.in);
                  Random random = new Random();
                  num1 = random.nextInt(10);
                  num2 = random.nextInt(10);
                  num3 = random.nextInt(10);
                     //System.out.println("Random number = " + number);
                  System.out.println(" Enter a number  between 0 - 9");
                  g1 = inputDevice.nextInt();
                  System.out.println("Enter another number between 0-9");
                  g2 = inputDevice.nextInt();
                  System.out.println("Enter another number between 0-9");
                  g3 = inputDevice.nextInt();
                     //System.out.println("Your guess was " + guess);
          }
         

                public void checkGuesses()
             {
                if (g1 == num1)
                        {
                         match1 = 1;
                        }
                else if(g1 == num2)
                    {
                          match1 = 2;
                        }
                else if(g1 == num3)
                        {
                          match1 = 3;
                        }
               
               
                if (g2 == num1)
                   {
                         match2 =1 ;
                   }
                    else if (g2 == num2)
                        {
                         match2 =2 ;
                        }
                    else if (g2 == num3)
                        {
                          match2 = 3 ;
                        }
               if (g3 == num1)
                            {
                             match3 = 1;
                            }
                         else if (g3 == num2)
                                    {
                                     match3 = 2;
                                    }
                         else if (g3 == num3)
                                    {
                                      match3 = 3 ;
                                    }
            
                           
           
            if(match1 > 0 || match2 > 0 || match3 >0)
            {
                if(match1 == match2 || match1 == match3)
                {
                        totalMatch = 1;
                }
            else if (match1 > 0 && match2 > 0 && match3 > 0)
                {
                        totalMatch = 2;
                }
            else if (match1 > 0 && match2 > 0 && match3 > 0)
                    {
                        totalMatch = 3;     
                    }
                else
               {
                        totalMatch = 1;
                   }
            }
           }
                public void checkExact()
             {
                        
                 if (g1== num1 && g2 == num2 && g3 == num3)
                 {
                        System.out.println("You have won the jackpot of $1,000,000");
                 }
             }

Tuesday, June 2, 2015

DATABASE OPERATING SYSTEMS

AIM:
            To write a java program for transactions and concurrency in database operating systems


ALGORITHM:

Step 1:         Start the program

Step 2:         Creation of  bindings to the bank account object

Step 3:         Investigate and implement the object store’s concurrency options

Step 4:         The  executions of transactions are all or none

Step 5:          The interleaving of multiple transactions is serializable

Step 6:         .Update is atomic

Step 7:         Start of the application transaction

Step 8:         Method  invocations; as a part of a given invocation the object will be locked in read or write mode

Step 9:         Commit/abort of the transaction

Step 10:     End of program.



PROGRAM:

server side:

import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class server
{
public static void main(String args[])
{
try
{
ServerSocket s=new ServerSocket(7001);
while(true)
{
Socket incoming=s.accept();
Runnable r=new bankserver(incoming);
Thread t=new Thread(r);
t.start();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class bankserver implements Runnable
{
private Socket incoming;
public bankserver(Socket i)
{
incoming=i;
}
public void run()
{
try
{
try
{
DataOutputStream out=new DataOutputStream(incoming.getOutputStream());
DataInputStream in=new DataInputStream(incoming.getInputStream());

System.out.println(in.readInt());
while(true)
{
int choice=in.readInt();
System.out.println("You have selected choice"+choice);
switch(choice)
{
case 1:
String name=null;
int acno1=0;
if(in.readBoolean())
{
name=in.readUTF();
acno1=in.readInt();
}
else
{
break;
}
int a=0;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:customer");
Statement st=con.createStatement();
String str1="insert into customer values('"+name+"','"+acno1+"','"+a+"')";
st.executeUpdate(str1);
}
catch(Exception e)
{
System.out.println("Account Number is already allocated");
}
break;
case 2:
int acno=in.readInt();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:customer");
Statement st=con.createStatement();
String s1="Select * from customer where accnumber="+acno;
System.out.println(s1);
int bal=0;
ResultSet rs=st.executeQuery(s1);
boolean check=true;
String name2=null;
while(rs.next())
{
name2=rs.getString(1);
bal=rs.getInt(3);
}
if(name2==null)
{
check=false;
out.writeBoolean(check);
}
else
{
out.writeBoolean(check);
out.writeInt(bal);
}
}
catch(Exception e)
{
e.printStackTrace();
}
break;
case 3:
int acno3=in.readInt();
int amt3=in.readInt();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:customer");
Statement st=con.createStatement();
String s1="Select * from customer where accnumber="+acno3;
System.out.println(s1);
int bal=0;
ResultSet rs=st.executeQuery(s1);
String name3=null;
boolean check3=true;
while(rs.next())
{
bal=rs.getInt(3);
name3=rs.getString(1);
}
if(name3== null)
{
check3=false;
}
boolean chstatus=false;
if(bal>0 && bal>amt3)
{
Object lock2=new Object();
synchronized(lock2)
{
int tmp=bal-amt3;
String s2="update customer set balance="+tmp+" where accnumber="+acno3;
System.out.println(s2);
st.executeUpdate(s2);
chstatus=true;
}
}

Monday, June 1, 2015

REAL TIME OPERATING SYSTEMS

AIM:
            To write a java program for real-time program for implementing an alarm clock shall be developed


ALGORITHM:

Step 1:         Start the program

Step 2:         Display init is used for initialization, and shall be called from the main function of the program.

Step 3:         Before the processes are created.

Step 4:         Display time displays the current time, and shall be called by the clock process

Step 5:         Display alarm time shows the current alarm time, and shall be called when a new alarm time is set.

Step 6:         Erase alarm time erases the displayed alarm time, and shall be called when the user acknowledges an alarm

Step 7:         Display alarm text is used to show an alarm activation, and shall be called when the user shall be informed that the alarm has been activated

Step 8:         When the alarm is activated the first time, and when the alarm is activated repeatedly (which is every 10 seconds, according to the above stated requirements).

Step 9:         End of programs





PROGRAM

#include<stdio.h>
#include<conio.h>
#include<dos.h>
struct clk
{
int hh,mm,ss;
}c1,c2;
void clock(int *h1,int *m1,int *s1)
{
*s1=*s1+1;
if(*s1==60)

{
*s1=0; *m1=*m1+1;
if(*m1==60)

{
*m1=0;*h1=*h1+1;
if(*h1==24)
*h1=0;
}
}
}
void timer(int *h,int *m,int *s)
{
if((*s)!=0)

{
*s=*s-1;
}
else if((*s)==0)

{
if(*m!=0)
{
*s=59;*m=*m-1;
}
else if(*m==0)
{

if(*h!=0)
{
*m=59;*h=*h-1;
}
}
}
}
void alarm()
{
int i;
while(!kbhit())
{
for(i=0;i<2;i++)
{
sound(5000);
delay(100);
nosound();
delay(200);
}
delay(500);
}
}

Sunday, May 31, 2015

NETWORK OPERATING SYSTEMS

AIM:
            To write a java program for network operating systems


ALGORITHM:


Step 1:         Start the program

Interface program
Step 2:         Create an interface ReceiveMessageInterface which extends Remote class
Step 3:         Declare method receiveMessage inside interface


Server Program
Step 4:         Create class RmiServer which extends UnicastRemoteObject which again extends the ReceiveMessageInterface
Step 5:         Define the method receiveMessage which get parameter x
Step 6:         Declare a Random object which is used to get random integers
Step 7:         Using the value of x get the Random number one by one and store it in val[i]
Step 8:         Define a constructor RmiServer in that get the local address of the Server by using InetAddress.getLocalHost()
Step 9:         Declare a port and register it in the registry object
Step 10:     Using Rebind method we register the port and a string “rmiserver” into the rmiregistry table
Step 11:     Create a object using RmiServer so that the Constructor will be initialized automatically



Client program
Step 12:     Create class RmiClient
Step 13:     Declare an object rmiServer for ReceiveMessageInterface
Step 14:     Get the server’s local address and the port in command line arguments
Step 15:     Locate the registry object using the server port
Step 16:     Using registry object call the method lookup of the server string “rmiserver” by casting the interface which is already defined
Step 17:     Now using the rmiServer object call the method receiveMessage and receive all random number one by one from the server
Step 18:     End of programs




PROGRAM:-

ReceiveMessageInterface:


import java.rmi.*;
public interface ReceiveMessageInterface extends Remote
{
   public int [] receiveMessage(int x) throws RemoteException;
}


RmiClient.java:

import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;

public class RmiClient
{
    static public void main(String args[])throws IOException
    {
       ReceiveMessageInterface rmiServer;
       Registry registry;
       String serverAddress=args[0];
       String serverPort=args[1];
       int no;
       no=Integer.parseInt(args[2]);
       int rval[]=new int[no];
       System.out.println("sending "+no+" to "+serverAddress+":"+serverPort);
       try{
           registry=LocateRegistry.getRegistry(serverAddress,(new Integer(serverPort)).intValue());
           rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer"));
           rval=rmiServer.receiveMessage(no);
           for(int i=0;i<no;i++)
           System.out.println(rval[i]);
          }
       catch(RemoteException e)
       {
            e.printStackTrace();
       }
       catch(NotBoundException e)
       {
        e.printStackTrace();
       }
    }
}


Saturday, May 30, 2015

MULTIPLE SLEEPING BARBERS - MULTIPROCESSOR OPERATING SYSTEMS

AIM:

To write a java program for the multiple sleeping barbers in multi-processor operating system.

ALGORITHM:


Step 1:              Start the program

Step 2:              while the barber in process is true runs an infinite loop

Step 3:              Barber tries to acquire a customer.

Step 4:              If none is available the barber goes to sleep.

Step 5:              At this time, he has been awakened and wants to modify the number of available seats.

Step 6:              Barber waits for a customer to arrive

Step 7:              If customer arrived run an infinite loop.

Step 8:              Checks if the NumberOfFreeSeats is greater than zero, sit down on the chair. Otherwise goto step 13.

Step 9:              V(Customers) notify the barber, who's waiting that there is a customer

Step 10:          V(accessSeats) don't need to lock the chairs anymore

Step 11:          P(Barber) now it's this customers turn, but wait if the barber is busy

Step 12:          Here the customer is having his hair cut

Step 13:          V(accessSeats) The lock on the seats is removed. Customer leaves without a haircut.

Step 14:          End of program.



PROGRAM:

import java.io.*;
import java.lang.*;
class cust {
    public int disp(int cn)
    {
        return(cn);
    }

}
class em1 extends Thread{
    main2 m=new main2();
    cust c=new cust();
public synchronized void run()
{
    try
    {
    while(m.cnum<=m.n)
    {
        int t=c.disp(m.cnum++);
    System.out.println("Barber2 serves Customer "+t);
        Thread.sleep(2000);
    }
    System.out.println("Barber2 sleeps ");
  
   }
    catch(Exception e){}
}
   
}
public class main2 {
static int cnum=1,n,ch,n1;
 
    public static void  main(String[] args) {
        try
        {
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
         em1 e=new em1();
         cust c=new cust();             
         int j;

             System.out.println("Enter no of Chairs including two barber Chairs: ");
         ch=Integer.parseInt(br.readLine());

             System.out.println("Enter no of Customers : ");
         n=Integer.parseInt(br.readLine());

         e.start();

Friday, May 29, 2015

MULTITHREADING – MULTIPROCESSOR OPERATING SYSTEMS

AIM:

To write a java program for multithreading in multiprocessor operating systems.

ALGORITHM:

Step 1:              Start the program

Step 2:              There are two processes namely agent and smoker

Step 3:              Pick a random number from 1 to 3.

Step 4:              If random number is 1, then put tobacco on table, put paper on table.

Step 5:              Wake up smoker with match

Step 6:              If random number is 2, then put tobacco on table, put match on table

Step 7:              Wake up smoker with paper

Step 8:              If random number is 3, then put match on table, put paper on table

Step 9:              Wake up smoker with tobacco

Step 10:          Lock the agent process

Step 11:          For smoker process, pick up match, pick up paper

Step 12:          Lock the smoker process

Step 13:          Smoke

Step 14:          End of the program





Program:
import java.util.*;
 
public class Cigarette {

            public class Table {

                          public static final int Nothing = 0;
                          public static final int Tobacco = 1;
                          public static final int Paper = 2;
                          public static final int Matches = 4;

                          public static final int Tobacco_Paper = Tobacco + Paper;
                          public static final int Paper_Matches = Paper + Matches;
                          public static final int Matches_Tobacco = Matches + Tobacco;
                          public static final int Everything = Tobacco + Paper + Matches;
                         
                          private int contains;
                         
                          public Table () {
                                    contains = Nothing;
                          }

                          public synchronized void Put(int what) {
                                    System.out.println(Thread.currentThread().getName() +
                                                               ": putting "+Contains(what));
                                    contains = contains | what;
                                    notifyAll();
                                    try {
                                      wait();
                                    } catch (InterruptedException e) {}
                          }

                          public synchronized void Get(int what) {
                                    while ((contains & what) != what) {
                                      try {
                                    System.out.println(Thread.currentThread().getName() +
                                                               ": Getting " + Contains(what) + " - No!");
                                    wait();
                                      } catch (InterruptedException e) {}
                                    }
                                    System.out.println(Thread.currentThread().getName() +
                                                               ": Getting " + Contains(what) + " - Yes!");
                                    contains = contains ^ what;
                          }

                          public synchronized void DoneSmoking() {
                                    notifyAll();
                          }
                         
                          public String Contains(int what) {
                                    String s = "";
                                    if ((what & Tobacco) == Tobacco)
                                      s = s + "tobacco ";
                                    if ((what & Paper) == Paper)
                                      s = s + "paper ";
                                    if ((what & Matches) == Matches)
                                      s = s + "matches ";
                                    return s;
                          }
            }
            public class TableCS extends Table{
                        TableCS Table;
            }
            public class Agent extends Thread {

                          private Table table;
                          private Random rand;
                         
                          public Agent(Table tab, String name) {
                                    super (name);
                                    table = tab;
                                    rand = new Random();
                          }
                         
                          public void run() {
                                    while (true) {
                                      switch (Math.abs(rand.nextInt()) % 3) {
                                      case 0:
                                    table.Put(Table.Tobacco_Paper);
                                    break;
                                      case 1:
                                    table.Put(Table.Paper_Matches);
                                    break;
                                      case 2:
                                    table.Put(Table.Matches_Tobacco);
                                    break;
                          }
                        }
              }
            }
           
            public class Smoker extends Thread {

                          private Table table;
                          private Random rand;
                          private int needs;
                         
                          public Smoker(Table tab, String name, int what) {
                                    super (name);
                                    table = tab;
                                    rand = new Random();
                                    needs = Table.Everything ^ what;
                          }
                         
                          public void run() {
                                    while (true) {
                                      try {
                                    table.Get(needs);
                                    System.out.println(getName() + ": I got what I needed!");
                                   
                                    System.out.println(getName() + ": Rolling.");
                                    sleep(Math.abs(rand.nextInt()) % 1000);
                                   
                                    System.out.println(getName() + ": Smoking.");
                                    sleep(Math.abs(rand.nextInt()) % 1000);
                                   
                                    System.out.println(getName() + ": Done smoking.");
                                    table.DoneSmoking();
                                      }
                                      catch (InterruptedException e) {}
                        }
              }
            }
           

Thursday, May 28, 2015

SEMAPHORES - MULTIPROCESSOR OPERATING SYSTEMS

AIM:
            To write a Java program for semaphores in multiprocessor operating systems


ALGORITHM



Step 1:              Start the program

Step 2:              Assume there are three processes namely Pa, Pb and Pc.

Step 3:              Only Pa can output the letter A, Pb, B and Pc, C.

Step 4:              Utilizing only semaphores(and no other variables) the processes are synchronized

Step 5:              So that the output satisfies the following conditions

Step 6:              a) A B must be output before any C’s can be output.

Step 7:              b) B’s and C’s must alternate in the output string, that is after the first B is                   
                         output, another B cannot be output until a C is output.

Step 8:              Similarly, once a C is output, another C cannot be output until a B is output

Step 9:              The total  number of B’s and C’s which have been output at any given point in the   
output string cannot exceed the number of A’s which have been output up to that
point

Step 10:          For more than two processes busy wait until they allowed to enter the critical
section

Step 11:          End of program.



  
Program:

import java.io.*;
class printing
{
    boolean Bsem=true,Csem=false;
    int sum=0;
    printing()
    {
    }
    synchronized void printa(String str)
    {
        System.out.println(str);
        try
        {
        Thread.sleep(1000);
        }
        catch(Exception e)
        {

        }
        sum++;
    }


     synchronized void printb(String str)
    {
       if(sum>1&&Bsem==true)
       {
        System.out.println(str);
        try
        {
        Thread.sleep(1000);
        }
        catch(Exception e)
        {

        }
        sum--;
        Bsem=false;
        Csem=true;
        notifyAll();
       }
       else
       {
           try
           {
           wait();
           }
           catch(Exception e)
           {
           }
       }
    }


     synchronized void printc(String str)
    {
       if(sum>=1&&Csem==true)
       {
        System.out.println(str);
        try
        {
        Thread.sleep(1000);
        }
        catch(Exception e)
        {

        }
        sum--;
        Bsem=true;
        Csem=false;
        notifyAll();
       }
       else
       {
           try
           {
           wait();
           }
           catch(Exception e)
           {

           }
       }

    }

}


class processA implements Runnable
{
   printing p1;
    processA(printing p)
    {
         p1=p;

    }
    public void run()
    {

        for(int i=0;i<10;i++)
             p1.printa("A");
    }
}

class processB implements Runnable
{
    printing p2;
    processB(printing p)
    {

        p2=p;

    }
    public void run()
    {
      for(int i=0;i<10;i++)
       p2.printb("B");
    }
}