Operating System Lab-Aman

Embed Size (px)

Citation preview

  • 8/2/2019 Operating System Lab-Aman

    1/32

  • 8/2/2019 Operating System Lab-Aman

    2/32

    PROGRAM 1CPU SCHEDULING

    CPU scheduling is the process where CPU is scheduled to do a particular process for agiven amount of time. Since we use single processor system, so only one process can takeover CPU, the time of CPU is so configured to make sure that it doesnt sit idle. For this

    multiple processes are kept in memory at the same time and whenever suitable the CPUtime is given to the appropriate process.

    CPU scheduling decisions may take place under the following four circumstances:Under following conditions, CPU scheduling is done:

    1. When a process switches from the running state to the waiting state.

    2. When a process switches from the running state to the ready state.

    3. When a process switches from the waiting state to the ready state

    4. When a process terminates

    First Come, First Serve Scheduling (FCFS)

    Under this algorithm the process that requests the CPU first is given the CPU time first. Inthis algorithm, the new process enters the ready queue at its end. When processes thatcame before it have been terminated, this process executes.

    Shortest Job First Scheduling (SJF)

    This is another algorithm of CPU scheduling. In this algorithm the shortest process in theready queue gets the CPU time first followed by the next shortest process in the queueThe SJF algorithm more advantageous because using this algorithm we get minimumaverage waiting time processes. Moving a short process before long one decreases thewaiting time of the short process more than it increases the waiting time of the longprocess. Consequently, the average waiting time decreases.

    It is further divided into 2 parts- preemptive and non preemptive. In case of preemptiveSJF, in case a process arrives at some time with burst time less than the remaining time ofongoing process, then this process takes over the CPU and the other process is put onhold.

    In case of non-preemptive SJF, the new process doesnt take over the CPU immediately,

    rather it waits until the current process ends and then takes over the CPU.

    Round robin(RR)

    This algorithm uses circular queue. All the processes that are ready are kept in a circularqueue. A time slice called quantum is initially, at the max this much amount of time isgiven to any process in one go. If the process doesnt terminate during this time, then CPUtime is given to the next process in the circular queue. So on the process continues until allprocesses terminate.

    Priority scheduling

  • 8/2/2019 Operating System Lab-Aman

    3/32

    Here a number is associated with each process. This number is called the priority of theprocess. Generally lower number means higher priority. The CPU time is given to theprocess with higher priority.As in case of SJF, this scheduling can also be divided into 2 parts- preemptive and nonpreemptive. Here preemption is based on priority of the process.#include

    #include#include#includestruct proc{

    proc(){cpub=pri=at=wt=tt=tr=0;

    }char name[20];

    int pri;int cpub; //CPU Burst Timeint at; //Arrival Timeint wt; //Waiting Timeint tt; //Turnaround Time

    int tr; //Time Remainingvirtual void input(){

    coutname>>cpub>>at;

    tr = cpub;}

    void inputwithpri(){input();coutpri;}

    void calcvalue(int endtime){

    tt = endtime - at;wt = tt- cpub;

    }void display(){

    cout

  • 8/2/2019 Operating System Lab-Aman

    4/32

    for(int i=0;i

  • 8/2/2019 Operating System Lab-Aman

    5/32

    void fcfs(proc ps[], int n){

    int i=0;int terminated=0 ;//processes finished

    int todispatch;int timeline = ps[0].at;// time line variable

    dispdispatch();todispatch = -1;i=0;while(i

  • 8/2/2019 Operating System Lab-Aman

    6/32

    while(i 0 )&& (ps[i].at

  • 8/2/2019 Operating System Lab-Aman

    7/32

    elseps[i].input();}

    sortbyat(ps,num);switch(algo)

    {case 1: fcfs(ps,num);

    break;case 2:

    sjf(ps,num);break;

    case 3: roundrobin(ps,num,timeslice);break;

    case 4: prischedule(ps,num);break;

    }

    printchart(ps,num);

    printavgs(ps, num);getch();

    }

    OUTPUT

    List of algos :1. FCFS2. SJF

  • 8/2/2019 Operating System Lab-Aman

    8/32

    3. ROUND ROBIN4. PRIORITY

    Which algo to implement ? 1

    No. of processes : 3

    For proc 1

    Enter Name, CPU burst, Arrival Time : p1120

    For proc 2

    Enter Name, CPU burst, Arrival Time : p2104

    For proc 3

    Enter Name, CPU burst, Arrival Time : p3

    612

    Start Time End Time Name TimeRemaining0 12 p1 012 22 p2 022 28 p3 0

    Name : Arrival Time : CPU Burst : Waiting Time : Turnaround Timep1 0 12 0 12p2 4 10 8 18p3 12 6 10 16

    Average Turnaround Time : 15.333333Average Waiting Time : 6Average CPU Time : 9.333333

    List of algos :1. FCFS2. SJF3. ROUND ROBIN4. PRIORITY

    Which algo to implement ? 2

    No. of processes : 3

    For proc 1

    Enter Name, CPU burst, Arrival Time : P1120

    For proc 2

    Enter Name, CPU burst, Arrival Time : P2104

    For proc 3

    Enter Name, CPU burst, Arrival Time : P3

    612

  • 8/2/2019 Operating System Lab-Aman

    9/32

    Start Time End Time Name TimeRemaining0 12 P1 012 18 P3 018 28 P2 0

    Name : Arrival Time : CPU Burst : Waiting Time : Turnaround TimeP1 0 12 0 12

    P2 4 10 14 24P3 12 6 0 6

    Average Turnaround Time : 14Average Waiting Time : 4.666667Average CPU Time : 9.333333

    List of algos :1. FCFS2. SJF3. ROUND ROBIN4. PRIORITY

    Which algo to implement ? 3

    No. of processes : 3

    Enter timeslice : 5For proc 1

    Enter Name, CPU burst, Arrival Time : P1120

    For proc 2

    Enter Name, CPU burst, Arrival Time : P2104

    For proc 3

    Enter Name, CPU burst, Arrival Time : P3612

    Start Time End Time Name TimeRemaining0 5 P1 75 10 P2 510 15 P1 2

    15 20 P2 020 25 P3 125 27 P1 027 28 P3 0

    Name : Arrival Time : CPU Burst : Waiting Time : Turnaround TimeP1 0 12 15 27P2 4 10 6 16P3 12 6 10 16

    Average Turnaround Time : 19.666666Average Waiting Time : 10.333333Average CPU Time : 9.333333

    List of algos :

  • 8/2/2019 Operating System Lab-Aman

    10/32

    1. FCFS2. SJF3. ROUND ROBIN4. PRIORITY

    Which algo to implement ? 4

    No. of processes : 3For proc 1

    Enter Name, CPU burst, Arrival Time : P1120

    Enter priority : 3For proc 2

    Enter Name, CPU burst, Arrival Time : P210

    4

    Enter priority : 1For proc 3

    Enter Name, CPU burst, Arrival Time : P3612

    Enter priority : 2Start Time End Time Name TimeRemaining

    0 12 P1 012 22 P2 022 28 P3 0

    Name : Arrival Time : CPU Burst : Waiting Time : Turnaround TimeP1 0 12 0 12P2 4 10 8 18P3 12 6 10 16

    Average Turnaround Time : 15.333333Average Waiting Time : 6Average CPU Time : 9.333333

    PAGE REPLACEMENT ALGORITHMSPROGRAM 2FIFO PAGE REPLACEMENT ALGORITHM

    #include#include#include

    void main()

    { int num,frames,x,page[50],frame[20],y,z,fl=0,flag=0,k=0,total_pag_faults;clrscr();

  • 8/2/2019 Operating System Lab-Aman

    11/32

    coutnum;coutframes;cout

  • 8/2/2019 Operating System Lab-Aman

    12/32

    PAGE FAULT 4:

    Frame 1:: 2Frame 2:: 0Frame 3:: 1

    PAGE FAULT 5:

    Frame 1:: 2Frame 2:: 3Frame 3:: 1

    PAGE FAULT 6:

    Frame 1:: 2Frame 2:: 3Frame 3:: 0

    PAGE FAULT 7:

    Frame 1:: 4Frame 2:: 3Frame 3:: 0

    PAGE FAULT 8:

    Frame 1:: 4Frame 2:: 2Frame 3:: 0

    PAGE FAULT 9:

    Frame 1:: 4Frame 2:: 2Frame 3:: 3

    PAGE FAULT 10:

    Frame 1:: 0Frame 2:: 2Frame 3:: 3

    PAGE FAULT 11:

    Frame 1:: 0Frame 2:: 1Frame 3:: 3

    PAGE FAULT 12:

    Frame 1:: 0Frame 2:: 1Frame 3:: 2

    PAGE FAULT 13:

    Frame 1:: 7

    Frame 2:: 1Frame 3:: 2

  • 8/2/2019 Operating System Lab-Aman

    13/32

    PAGE FAULT 14:

    Frame 1:: 7Frame 2:: 0Frame 3:: 2

    PAGE FAULT 15:

    Frame 1:: 7Frame 2:: 0Frame 3:: 1

    Total number of page faults = 15

    PROGRAM 3LRU PAGE REPLACEMENT ALGO

    #include

    #include#include

    struct fr{int value;int index;

    }frame[20];

    void main(){

    int num,frames,x,page[50],fl=0,y,z,k=0,flag=0,minimum,apnt,m=0,total_pag_faults;clrscr();

    coutnum;

  • 8/2/2019 Operating System Lab-Aman

    14/32

    coutframes;

    cout

  • 8/2/2019 Operating System Lab-Aman

    15/32

    cout

  • 8/2/2019 Operating System Lab-Aman

    16/32

    Frame 1:: 4Frame 2:: 0Frame 3:: 3

    PAGE FAULT 7:

    Frame 1:: 4Frame 2:: 0Frame 3:: 2

    PAGE FAULT 8:

    Frame 1:: 4Frame 2:: 3Frame 3:: 2

    PAGE FAULT 9:

    Frame 1:: 0Frame 2:: 3Frame 3:: 2

    PAGE FAULT 10:

    Frame 1:: 1Frame 2:: 3Frame 3:: 2

    PAGE FAULT 11:

    Frame 1:: 1Frame 2:: 0Frame 3:: 2

    PAGE FAULT 12:

    Frame 1:: 1Frame 2:: 0Frame 3:: 7

    Total number of page faults = 12

  • 8/2/2019 Operating System Lab-Aman

    17/32

    PROGRAM 4OPTIMAL PAGE REPLACEMENT ALGORITHM

    #include#include#include

    struct fr{int value;int index;

    }frame[20];

    void main(){

    int num,frames,x,page[50],fl=0,y,z,k=0,flag=0,maximum,apnt,m=0,total_pag_faults;

    clrscr();

    coutnum;coutframes;

    cout

  • 8/2/2019 Operating System Lab-Aman

    18/32

    {frame[x].value=page[x];

    }

    for(x=0;x

  • 8/2/2019 Operating System Lab-Aman

    19/32

    }total_pag_faults=m+frames;cout

  • 8/2/2019 Operating System Lab-Aman

    20/32

    PAGE FAULT 8:

    Frame 1:: 2Frame 2:: 0Frame 3:: 1

    PAGE FAULT 9:

    Frame 1:: 7Frame 2:: 0Frame 3:: 1

    Total number of page faults = 9

    PROGRAM 5BANKERS ALGORITHM

    #include#include#include#define P 2#define R 3

    int ava[R] , max[P][R] ,alc[P][R], need[P][R];

    void disp(int pid,int a[R]){ int i=0,j=0;

    clrscr();cout

  • 8/2/2019 Operating System Lab-Aman

    21/32

    for(;i

  • 8/2/2019 Operating System Lab-Aman

    22/32

    getch();result=simulate(pno,req);if(result==0) // No dead lock{ cout

  • 8/2/2019 Operating System Lab-Aman

    23/32

    Process No :1 1 1 0Process No :2 0 0 0AVAILABLE MATRIX

    321REQUEST MATRIX

    110

    REQUESTING PROCESS2

    PROGRAM 6MEMORY ALLOCATION ALGORITHMS

    #include#include

    class mem_alloc{ int siz[25],n;int req;

    public :void getdata();void firstfit();void bestfit();void worstfit();

    };

    void mem_alloc :: getdata()

    { coutn;cout

  • 8/2/2019 Operating System Lab-Aman

    24/32

    int k;for(int j=1;j

  • 8/2/2019 Operating System Lab-Aman

    25/32

    OUTPUT

    Enter the total number of available blocks: 10

    Enter the size of block 1: 1

    Enter the size of block 2: 2

    Enter the size of block 3: 3

    Enter the size of block 4: 4

    Enter the size of block 5: 5

    Enter the size of block 6: 6

    Enter the size of block 7: 7

    Enter the size of block 8: 8

    Enter the size of block 9: 9

    Enter the size of block 10: 10

    Enter the memory space required for the process :5

    Memory Allocation Method1. First Fit2. Best Fit3. Worst Fit4. Exit

    Enter your choice :1

    block no 6 is allocated of size 6

    Memory Allocation Method1. First Fit2. Best Fit3. Worst Fit4. Exit

    Enter your choice :2

    The best possible fit is at block 5 of size 5Memory Allocation Method

    1. First Fit2. Best Fit3. Worst Fit4. Exit

  • 8/2/2019 Operating System Lab-Aman

    26/32

    Enter your choice :3

    block no 10 is allocated of size 10

    PROGRAM 7

    PRODUCER COMSUMER PROBLEM

    #include #include #include #include #include #include #include #include #define BUFFER_SIZE 10 /* Number of elements in shared memory buffer */#define SEM_MUTEX 0

    #define SEM_EMPTY 1#define SEM_FULL 2int rc, semID, shmID, i;char elem;union semun{int val;struct semid_ds *buf;unsigned short *array;

    } seminfo;struct sembuf WaitMutex={SEM_MUTEX, -1, 0};struct sembuf SignalMutex={SEM_MUTEX, 1, 0};struct sembuf WaitEmpty={SEM_EMPTY, -1, 0};

    struct sembuf SignalEmpty={SEM_EMPTY, 1, 0};struct sembuf WaitFull={SEM_FULL, -1, 0};struct sembuf SignalFull={SEM_FULL, 1, 0};struct shmid_ds shminfo;char *shmPtr;void initialize();void producer();void consumer();void myhandler(int signo){ if(signo==SIGINT){printf("\n Caught Ctrl-C\n Aborting after cleaning up shared memory and

    semaphores...\n");

    sleep(2);/* Remove shared memory */

    shmctl(shmID, IPC_RMID, &shminfo);

    /* Remove semaphores */semctl(semID, SEM_MUTEX, IPC_RMID, seminfo);

    exit(1);}

    }main()

    {

    /* Initialize shared memory and semaphores */

  • 8/2/2019 Operating System Lab-Aman

    27/32

    initialize();/* Start a child process and proceed accordingly*/if (fork()==0){signal(SIGINT,myhandler);/* Child becomes the consumer */

    consumer();/* Child quits after consuming 26 characters */exit(0);}else{/* Parent becomes the producer */producer();/* Returns after producing 26 characters */

    /* Wait for child to finish */wait(NULL);

    //printf("\n child(consumer) finished.....now sleeping for 10 seconds\n");//sleep(10);/* Remove shared memory */shmctl(shmID, IPC_RMID, &shminfo);/* Remove semaphores */semctl(semID, SEM_MUTEX, IPC_RMID, seminfo);/* Parent is done cleaning up, so now quits */exit(0);}

    }void initialize(){

    /* Init semaphores *//* Three semaphores (Empty, Full, Mutex) are created in one set */if((semID=semget(IPC_PRIVATE, 3, 0666 | IPC_CREAT))==-1){perror("semget");

    exit(1);}

    /* printing the contents of struct semid_dsprintf("\n sem_perm.mode= %d\n",obsemid_ds.sem_perm.mode);*//* Init Mutex to one, allowing access to critical section */seminfo.val=1;semctl(semID, SEM_MUTEX, SETVAL, seminfo);

    /* Init Empty to number of elements in shared memory (circular buffer) */seminfo.val=BUFFER_SIZE;semctl(semID, SEM_EMPTY, SETVAL, seminfo);

    /* Init Full to zero, no elements are produced yet */seminfo.val=0;semctl(semID, SEM_FULL, SETVAL, seminfo);

    /* Init Shared memory */shmID=shmget(IPC_PRIVATE, BUFFER_SIZE, 0666 | IPC_CREAT);

    }

    void producer(){

  • 8/2/2019 Operating System Lab-Aman

    28/32

    /* attach shared memory to process */if((shmPtr=(char*)shmat(shmID, 0, SHM_W))==(void *)-1)

    {perror("shmat");exit(1);}

    for(i=0; i < 26; i++){printf(" (Inside parent)\n");/* Wait(Empty) - pause if no empty spots in circular buffer (i.e. all filled) */semop(semID, &WaitEmpty, 1);

    /* Produce element. Example element are chars 'a'-'z' */elem='a'+i;printf("Produced elem '%c'\n", elem);

    /* Wait(Mutex) - don't touch shared memory while consumer is using it */semop(semID, &WaitMutex, 1);

    /* Put element into shared memory buffer (circular buffer) */

    *(shmPtr + (i%BUFFER_SIZE))=elem;/* Signal(Mutex) - allow consumer to access shared memory now */semop(semID, &SignalMutex, 1);

    /* Signal(Full) - record one more filled spot in circular buffer */semop(semID, &SignalFull, 1);

    sleep(1);}

    }void consumer(){/* attach shared memory to process*/if((shmPtr=(char*)shmat(shmID, 0, SHM_R))==(void *)-1)

    {perror("shmat");exit(1);}

    for(i=0; i < 26; i++){printf("\t\t (Inside child)\n");/* Wait(Full) - pause if no filled spots in circular buffer (i.e. all empty) */semop(semID, &WaitFull, 1);

    /* Wait(Mutex) - don't touch shared memory while producer is using it */

    semop(semID, &WaitMutex, 1);

    /* Get element from the shared memory buffer (circular buffer) */elem=*(shmPtr + (i%BUFFER_SIZE));

    /* Signal(Mutex) - allow producer to access shared memory now */semop(semID, &SignalMutex, 1);

    /* Display character */printf("\t\t Consumed elem '%c'\n", elem);

    //sleep(1);/* Signal(Empty) - record one more empty spot in circular buffer */semop(semID, &SignalEmpty, 1);}

    }OUTPUT

  • 8/2/2019 Operating System Lab-Aman

    29/32

    (Inside child)

    (Inside parent)

    Produced elem 'a'

    Consumed elem 'a'

    (Inside child)

    (Inside parent)

    Produced elem 'b'

    Consumed elem 'b'

    (Inside child)

    (Inside parent)

    Produced elem 'c'

    Consumed elem 'c'

    (Inside child)

    (Inside parent)

    Produced elem 'd'

    Consumed elem 'd'

    (Inside child)

    (Inside parent)

    Produced elem 'e'

    Consumed elem 'e'

    (Inside child)

    (Inside parent)

    Produced elem 'f'

    Consumed elem 'f'

    (Inside child)

    (Inside parent)

    Produced elem 'g'

    Consumed elem 'g'

    (Inside child)

  • 8/2/2019 Operating System Lab-Aman

    30/32

    (Inside parent)

    Produced elem 'h'

    Consumed elem 'h'

    (Inside child)

    (Inside parent)

    Produced elem 'i'

    Consumed elem 'i'

    (Inside child)

    (Inside parent)

    Produced elem 'j'

    Consumed elem 'j'

    (Inside child)

    (Inside parent)

    Produced elem 'k'

    Consumed elem 'k'

    (Inside child)

    (Inside parent)

    Produced elem 'l'

    Consumed elem 'l'

    (Inside child)

    (Inside parent)

    Produced elem 'm'

    Consumed elem 'm'

    (Inside child)

    (Inside parent)

    Produced elem 'n'

    Consumed elem 'n'

    (Inside child)

    (Inside parent)

    Produced elem 'o'

  • 8/2/2019 Operating System Lab-Aman

    31/32

    Consumed elem 'o'

    (Inside child)

    (Inside parent)

    Produced elem 'p'

    Consumed elem 'p'

    (Inside child)

    (Inside parent)

    Produced elem 'q'

    Consumed elem 'q'

    (Inside child)

    (Inside parent)

    Produced elem 'r'

    Consumed elem 'r'

    (Inside child)

    (Inside parent)

    Produced elem 's'

    Consumed elem 's'

    (Inside child)

    (Inside parent)

    Produced elem 't'

    Consumed elem 't'

    (Inside child)

    (Inside parent)

    Produced elem 'u'

    Consumed elem 'u'

    (Inside child)

    (Inside parent)

    Produced elem 'v'

    Consumed elem 'v'

  • 8/2/2019 Operating System Lab-Aman

    32/32

    (Inside child)

    (Inside parent)

    Produced elem 'w'

    Consumed elem 'w'

    (Inside child)

    (Inside parent)

    Produced elem 'x'

    Consumed elem 'x'

    (Inside child)

    (Inside parent)

    Produced elem 'y'

    Consumed elem 'y'

    (Inside child)

    (Inside parent)

    Produced elem 'z'

    Consumed elem 'z'