Monday, September 25, 2017

MVT

AIM: The main of this program is to implement the Multi programming with a Variable number of Tasks in c
Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int  ms,mp[10],i, temp,n=0;
char ch = 'y';
clrscr();
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemory is allocated for Process %d ",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal Memory Allocated is %d",ms-temp);
printf("\nTotal External Fragmentation is %d",temp);
getch();
}                     


Input:                                
Enter the total memory available (in Bytes)               --          1000
Enter memory required for process 1 (in Bytes)         --          400
Memory is allocated for Process 1                 
Do you want to continue(y/n)                                     --          y         
Enter memory required for process 2 (in Bytes)         --          275
Memory is allocated for Process 2                 
Do you want to continue(y/n)                                     --          y         
Enter memory required for process 3 (in Bytes)         --          550
 Output:                                
Memory is Full                                   
Total Memory Available -- 1000                    
PROCESS      MEMORY ALLOCATED   
1                           400                 
2                           275                 
Total Memory Allocated is 675                     
Total External Fragmentation is 325  

 Result:


MFT

AIM: The main of this program is to implement the Multiprogramming with a Fixed number of Tasks in c
MFT:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int  ms, bs, nob, ef,n, mp[10],tif=0;
int i,p=0;
clrscr();
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
printf("\nEnter the number of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memory required for process %d (in Bytes)-- ",i+1); scanf("%d",&mp[i]);
}
printf("\nNo. of Blocks available in memory -- %d",nob);
printf("\n\nPROCESS\tMEMORY REQUIRED\t ALLOCATED\tINTERNAL FRAGMENTATION");
for(i=0;i<n && p<nob;i++)
{
printf("\n  %d\t\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\t\tNO\t\t---");
else
{
printf("\t\tYES\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("\nMemory is Full, Remaining Processes cannot be accomodated");
printf("\n\nTotal Internal Fragmentation is %d",tif);
printf("\nTotal External Fragmentation is %d",ef);
getch();
}
                                               
Input:                                                           
Enter the total memory available (in Bytes)               --          1000   
Enter the block size (in Bytes)                                    --          300                             
Enter the number of processes                                    –          5                                             
Enter memory required for process 1 (in Bytes)         --          275     
Enter memory required for process 2 (in Bytes)         --          400     
Enter memory required for process 3 (in Bytes)         --          290     
Enter memory required for process 4 (in Bytes)         --          293     
Enter memory required for process 5 (in Bytes)         --          100     
No. of Blocks available in memory                             --          3                     

Output:                                                         
PROCESS      MEMORY REQUIRED        ALLOCATED            INTERNAL    FRAGMENTATION
      1                            275                                          YES                                        25
      2                            400                                          NO                                          -----
      3                            290                                          YES                                        10
      4                            293                                          YES                                        7
Memory is Full, Remaining Processes cannot be accommodated     
Total Internal Fragmentation is           42                               
Total External Fragmentation is          100                             
Result:

Monday, September 18, 2017

Producer & Consumer Problem

public class ProducerConsumerTest {
   public static void main(String[] args) {
      A a1 = new A();
      Producer p1 = new Producer(a1, 1);
      Consumer c1 = new Consumer(a1, 1);
      p1.start();
      c1.start();
   }
}
class A 
   private int contents;
   private boolean available = false;
  
   public synchronized int get() {
      while (available == false) {
         try {
            wait();
         } catch (InterruptedException e) {}
      }
      available = false;
      notifyAll();
      return contents;
   }
   public synchronized void put(int value) {
      while (available == true) {
         try {
            wait();
         } catch (InterruptedException e) { }
      }
      contents = value;
      available = true;
      notifyAll();
   }
}
class Consumer extends Thread {
   private A a1;
   private int number;
  
   public Consumer(A a, int number) {
      a1= a;
      this.number = number;
   }
   public void run() {
      int value = 0;
      for (int i = 0; i < 10; i++) {
         value = a1.get();
         System.out.println("Consumer #" + this.number + " got: " + value);
      }
   }
}
class Producer extends Thread {
   private A a1;
   private int number;
   public Producer(A a, int number) {
      a1 = a;
      this.number = number;
   }
   public void run() {
      for (int i = 0; i < 10; i++) {
         a1.put(i);
         System.out.println("Producer #" + this.number + " put: " + i);
         try {
            sleep(1000);//((int)(Math.random() * 100));
         } catch (InterruptedException e) { }
      }
   }
}

lock an object by using Semaphores

AIM: the main aim this program is to use a Synchronized method to lock an object by using Semaphores 
import java.util.concurrent.Semaphore;
public class SemaphoreTest1 {

    Semaphore binary = new Semaphore(1);
 
    public static void main(String args[]) {
        final SemaphoreTest1 test = new SemaphoreTest1();
        new Thread(){
            @Override
            public void run(){
              test.mutualExclusion();
            }
        }.start();
     
        new Thread(){
            @Override
            public void run(){
              test.mutualExclusion();
            }
        }.start();
     
    }
 
    private void mutualExclusion() {
        try {
            binary.acquire();

            //mutual exclusive region
            System.out.println(Thread.currentThread().getName() + " inside mutual exclusive region");
            Thread.sleep(1000);

        } catch (InterruptedException ie) {
            ie.printStackTrace();
        } finally {
            binary.release();
            System.out.println(Thread.currentThread().getName() + " outside of mutual exclusive region");
        }
    }
 

}

Monitors

AIM: the main aim this program is to use a Synchronized method to lock an object by using monitors 
Source Code:

public class MonitorsExample{
public synchronized void m() { 
    n(); 
    System.out.println("this is m() method"); 
    } 
    public synchronized void n() { 
    System.out.println("this is n() method"); 
    }
public static void main(String args[]){ 
final MonitorsExample re=new MonitorsExample(); 
Thread t1=new Thread(){ 
public void run(){ 
re.m();//calling method of Reentrant class 
}; 
t1.start(); 

}} 


 Input & Output


Accessing Shared Variable

AIM: the main aim this program is to use a Synchronized method to lock an object for any shared resource.
Source Code:

class Table{ 
 synchronized void printTable(int n){//synchronized method 
   for(int i=1;i<=5;i++){ 
     System.out.println(n*i); 
     try{ 
      Thread.sleep(400); 
     }catch(Exception e){System.out.println(e);} 
   } 
 } 
 class MyThread1 extends Thread{ 
Table t; 
MyThread1(Table t){ 
this.t=t; 
public void run(){ 
t.printTable(5); 
}  
class MyThread2 extends Thread{ 
Table t; 
MyThread2(Table t){ 
this.t=t; 
public void run(){ 
t.printTable(100); 
public class TestSynchronization2{ 
public static void main(String args[]){ 
Table obj = new Table();//only one object 
MyThread1 t1=new MyThread1(obj); 
MyThread2 t2=new MyThread2(obj); 
t1.start(); 
t2.start(); 

}

Input & Output 


Algorithm for Deadlock Avoidance

AIM: The main aim of this program is to implement bankers algorithm for Deadlock Avoidance in C
Source Code 
#include< stdio.h>
#include< conio.h>
void main()
{
 int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15];
 int pno,rno,i,j,prc,count,t,total;
 count=0;
 clrscr();

 printf("\n Enter number of process:");
 scanf("%d",&pno);
 printf("\n Enter number of resources:");
 scanf("%d",&rno);
 for(i=1;i< =pno;i++)
 {
  flag[i]=0;
 }
 printf("\n Enter total numbers of each resources:");
 for(i=1;i<= rno;i++)
  scanf("%d",&tres[i]);


 printf("\n Enter Max resources for each process:");
 for(i=1;i<= pno;i++)
 {
  printf("\n for process %d:",i);
  for(j=1;j<= rno;j++)
   scanf("%d",&max[i][j]);
 }


 printf("\n Enter allocated resources for each process:");
 for(i=1;i<= pno;i++)
 {
  printf("\n for process %d:",i);
  for(j=1;j<= rno;j++)
   scanf("%d",&allocated[i][j]);


 }

 printf("\n available resources:\n");
 for(j=1;j<= rno;j++)
 {
  avail[j]=0;
  total=0;
  for(i=1;i<= pno;i++)
  {
   total+=allocated[i][j];
  }
  avail[j]=tres[j]-total;
  work[j]=avail[j];
  printf("     %d \t",work[j]);
 }


 do
 {



 for(i=1;i<= pno;i++)
 {
  for(j=1;j<= rno;j++)
  {
   need[i][j]=max[i][j]-allocated[i][j];
  }
 }

 printf("\n Allocated matrix        Max      need");
 for(i=1;i<= pno;i++)
 {
  printf("\n");
  for(j=1;j<= rno;j++)
  {
   printf("%4d",allocated[i][j]);
  }
  printf("|");
  for(j=1;j<= rno;j++)
  {
   printf("%4d",max[i][j]);
  }
   printf("|");
  for(j=1;j<= rno;j++)
  {
   printf("%4d",need[i][j]);
  }
 }




  prc=0;

  for(i=1;i<= pno;i++)
  {
   if(flag[i]==0)
   {
    prc=i;

    for(j=1;j<= rno;j++)
    {
     if(work[j]< need[i][j])
     {
       prc=0;
       break;
     }
    }
   }

   if(prc!=0)
   break;
  }

  if(prc!=0)
  {
   printf("\n Process %d completed",i);
   count++;
   printf("\n Available matrix:");
   for(j=1;j<= rno;j++)
   {
    work[j]+=allocated[prc][j];
    allocated[prc][j]=0;
    max[prc][j]=0;
    flag[prc]=1;
    printf("   %d",work[j]);
   }
  }

 }while(count!=pno&&prc!=0);

 if(count==pno)
  printf("\nThe system is in a safe state!!");
 else
  printf("\nThe system is in an unsafe state!!");

getch();
}

Input & Output



Enter number of process:5

 Enter number of resources:3

 Enter total numbers of each resources:10 5 7

 Enter Max resources for each process:
 for process 1:7 5 3

 for process 2:3 2 2

 for process 3:9 0 2

 for process 4:2 2 2

 for process 5:4 3 3

 Enter allocated resources for each process:
 for process 1:0 1 0

 for process 2:3 0 2


 for process 3:3 0 2

 for process 4:2 1 1

 for process 5:0 0 2

 available resources:
     2       3       0


 Allocated matrix        Max      need
   0   1   0|   7   5   3|   7   4   3
   3   0   2|   3   2   2|   0   2   0
   3   0   2|   9   0   2|   6   0   0
   2   1   1|   2   2   2|   0   1   1
   0   0   2|   4   3   3|   4   3   1
 Process 2 completed
 Available matrix:   5   3   2
 Allocated matrix        Max      need
   0   1   0|   7   5   3|   7   4   3
   0   0   0|   0   0   0|   0   0   0
   3   0   2|   9   0   2|   6   0   0
   2   1   1|   2   2   2|   0   1   1
   0   0   2|   4   3   3|   4   3   1
 Process 4 completed
 Available matrix:   7   4   3
 Allocated matrix        Max      need
   0   1   0|   7   5   3|   7   4   3
   0   0   0|   0   0   0|   0   0   0
   3   0   2|   9   0   2|   6   0   0
   0   0   0|   0   0   0|   0   0   0
   0   0   2|   4   3   3|   4   3   1
 Process 1 completed
 Available matrix:   7   5   3
 Allocated matrix        Max      need
   0   0   0|   0   0   0|   0   0   0
   0   0   0|   0   0   0|   0   0   0
   3   0   2|   9   0   2|   6   0   0
   0   0   0|   0   0   0|   0   0   0
   0   0   2|   4   3   3|   4   3   1
 Process 3 completed
 Available matrix:   10   5   5
 Allocated matrix        Max      need
   0   0   0|   0   0   0|   0   0   0
   0   0   0|   0   0   0|   0   0   0
   0   0   0|   0   0   0|   0   0   0
   0   0   0|   0   0   0|   0   0   0
   0   0   2|   4   3   3|   4   3   1
 Process 5 completed
 Available matrix:   10   5   7
The system is in a safe state!!*/