os sp lab manual

Upload: computerstudent

Post on 04-Apr-2018

250 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Os Sp Lab Manual

    1/28

    2002-2003

    JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY, HYDERABAD

    II Year B.Tech II Semester 0-3-2 CS2227

    OPERATING SYSTEMS & SYSTEMS PROGRAMMING LAB

    Operating Systems Related exercises:PART B:

    Program 1:Simulate the following CPU scheduling algorithms.a) Round Robin b) SJF c) FCFS d) Priority

    a) Round Robin:

    Aim: Write a program to implement Round Robin

    Code:

    /* simulate the CPU scheduling algorithm for Round Robin. */#include

    main(){

    int i,j,n,pro[10],bu[10],wa[10],tat[10],t,ct[10],max;float sum=0,tot=0,temp=0;

    clrscr();

    printf("enter the no of processes");scanf("%d",&n);

    printf("enter the processes");

    for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    2/28

    if(bu[i]!=0)

    {

    if(bu[i]

  • 7/29/2019 Os Sp Lab Manual

    3/28

    b) SJF:

    Aim: - to write a program on CPU scheduling technique shortest job first (SJF).

    /* Shortest Job First Scheduling */#include

    main()

    {int pro[10],bt[6],ar[10],st[10],ft[10],i,j,n,temp;

    float tat[10],wa[10],sum=0,tot=0;

    clrscr();

    printf("enter the no of processes");scanf("%d",&n);

    printf("enter the processes");

    for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    4/28

    }

    for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    5/28

    printf(Enter n value :);

    scanf(%d,&n);

    for(I=0;I

  • 7/29/2019 Os Sp Lab Manual

    6/28

    main()

    {

    int pri[10],ar[10],pro[10],i,j,n,ct=0;float wa[10],tat[10],bu[10],ft[10],st[10],temp,t1=0,w1=0;

    clrscr();

    printf("enter the no of processes");scanf("%d",&n);

    printf("enter the processes");

    for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    7/28

    tat[i]=ft[i]-ar[i];

    wa[i]=st[i]-ar[i];

    t1+=tat[i];w1+=wa[i];

    }

    printf("the mean turnaround time is:%f\n",t1/n);printf("the mean waiting time is:%f\n",w1/n);

    printf("process\tpriority\tburst\tarrival\twaiting\t\t turn\n");

    for(i=0;i

    struct file{

    int rno,m1,m2;

    char name[10];};

    main()

    {

  • 7/29/2019 Os Sp Lab Manual

    8/28

    FILE *fp;

    Float avg;

    Int I,n,total;Struct file x[30];

    Clrscr();

    Printf(Enter no. of students );Scanf(%d, &n);

    Fp=fopen(Student.txt,w);

    Printf(input student list data \n\n);Printf(reg. No. name marks1 marks2marks3);

    For(I=1;I

  • 7/29/2019 Os Sp Lab Manual

    9/28

    regno name marks1 marks2 marks3 total

    Avg1 A 5 1 4 10

    3.33

    2 B 1 4 4 93.00

    3 C 3 5 5 13

    4.00

    b) Indexed:

    Aim: Program to implement Indexed file allocation

    /* Indexed File Allocation */

    #include

    #include

    main()

    {int sb[20],size[20],n,I,j,x;

    int s=0,c[10][101];clrscr();

    printf(Enter number of files:);

    scanf(%d,&n);printf(enter starting block size);

    for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    10/28

    2 3

    9 5

    18 830 2

    26 3

    enter block occupied by each of them18 2 6 21 36 42

    9 16 15 19 26 41 16

    32 1 33 39 25 4999 12

    filename index size blocksoccupied

    p[0] 2 3 18 2 6p[1] 9 5 21 36 42 9 16

    p[2] 18 8 15 19 26 41 61

    p[3] 30 2 39 25

    p[4] 26 3 49 99 12

    c) Linked:

    Aim: Program to implement linked file allocation

    /* Linked file allocation */

    #include#include

    typedef struct file

    {

    char name[10];int start,size,block[10];

    }fil;

    main()

    {fil f[10];

    int i,j,n;

    printf("enter number of files");

    scanf("%d",&n);for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    11/28

    {

    printf("enter block numbers:");

    scanf("%d",&f[i].block[j]);}

    printf("file \t start \t size \t block \n");

    for(i=0;i4-->-1b 5 3 6->7->8-->-1

    Program 3: Simulate MVT & MFT

    Aim: Write a program that implements Multiprogramming with Variable number of tasks

    Code:

    /* MVT */

    #include < stdio.h>

    #include

    main()

    {int m,I,p[15],j,a[10],pr[15],re[15];

    char ch;

    clrscr();

    printf(Enter memory to be allocated);

  • 7/29/2019 Os Sp Lab Manual

    12/28

    scanf(%d,&m);

    printf(Enter process size :);

    scanf(%d, &p[0]);I=0;

    Do

    {m=m-p[I];

    printf(remaining memory is %d,m);

    printf(do u want to continue);scanf(%c,&ch);

    flushall();

    I++;

    If(ch==Y){

    printf(Enter the process);

    scanf(%d,&p[i]);

    }if(m=0;j--)

    {if(p[i]p[i]));

    getch();}

    Output:-

    Enter memory to be allocated : 200

    Enter process size : 100

    remaining memory is : 100

    do u want to continue Enter the process : 40

    remaining memory is: 60

    do u want to continue Enter the process : 30

    remaining memory is : 30

    do u want to continue Enter the process : 70

  • 7/29/2019 Os Sp Lab Manual

    13/28

    External fragmentation :30

    Aim: Write a program that implements Multiprogramming with Fixed number of tasks

    Code:

    /* MFT */

    #include < stdio.h>#include

    main(){

    int p,j,a[10],pr[15],c[15],temp=0,total=0;

    clrscr();

    printf(Enter the total memory );scanf(%d,&t);

    printf(Enter the processors :);

    scanf(%d, &p);

    b=t/p;for(i=0;I

  • 7/29/2019 Os Sp Lab Manual

    14/28

    Enter memory for 2 processors : 280

    Required memory is not available

    Enter memory for 2 processors : 250

    Internal fragmentation for this block :50Enter memory for 3 processors : 200

    Internal fragmentation for this block :50

    Total internal fragmentation :250

    Program 4: Simulate all File Organization Techniques

    a) Single level directory b) Two level

    a) Single level directory:

    Aim: Program to implement single level directory

    Code:/* memory memory management */

    #include#include

    main(0

    {int master,s[20],f[20][20];

    int d[10],i,j;

    clrscr();

    printf("enter number of directories:");scanf("%d",&master);

    printf("enter names of directories");

    for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    15/28

    printf("\t \n");

    }

    Input:enter number of directories: 2

    enter names:1 2

    size:2 2enter filenames:5 6 7 8

    directory size filename

    1 2 5 62 2 7 8

    b) Two level:

    Aim: Program to implement two level directory

    Code:

    /* two level directory */

    #include

    #include

    struct st{

    char dname[10];char sdname[10][10];

    char fname[10][10][10];

    int ds,sds;}dir[10];

    main()

    {

    int i,j,k,n;clrscr();

    printf("enter number of directories");

    scanf("%d",&n);for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    16/28

    printf("\n DIRNAME \t SIZE \t SUB DIR NAME\t SIZE \t FILES \n");

    for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    17/28

    printf("enter the no of resources");

    scanf("%d",&res);

    printf("enter the maximum resources");for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    18/28

    if(need[i][j]

  • 7/29/2019 Os Sp Lab Manual

    19/28

    Program 6: Simulate all page replacement algorithms

    a) FIFO b) LRU c) LFU d)optimal Etc

    a) FIFO:

    Aim: Write a program that implements page replacement algorithm FIFO

    Code:/*FIFO page replacement algorithm */

    #include

    int f[30],fs;main()

    {

    int ps[30],p,i,pos=0,j,pf=0;

    float pfr;clrscr();

    printf("\n enter the no of pages");

    scanf("%d",&p);

    printf("enter the pages");for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    20/28

    }

    return 0;

    }

    Input:-

    Enter the no.of pages:12

    Enter the pages:2 3 2 1 5 2 4 5 3 2

    5 2

    Frame size:3

    Output:-

    2 0 02 3 0

    2 3 02 3 1

    5 3 1

    5 2 15 2 4

    5 2 4

    3 2 4

    3 2 43 5 4

    3 5 2

    Total no.of page faults:9

    The page fault:0.75000

    b) LRU:

    Aim: Write a program that implements page replacement algorithm

    Code:

    /* LRU page replacement algorithm*/

    #includeint f[30],fs;

    int cnt[30];

    int flag,ps[30];main()

    {

    int p,i,pos=0,j,k,max,s,pf=0;

    float pfr;

  • 7/29/2019 Os Sp Lab Manual

    21/28

    void increment();

    int check();

    clrscr();printf("enter no of pages");

    scanf("%d",&p);

    printf("enter the pages");for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    22/28

    printf("total no of page faults:%d\n",pf);

    printf("Page fault rate:%f",pfr);

    getch();}

    void increment(int ele)

    { int k;

    for(k=0;k

  • 7/29/2019 Os Sp Lab Manual

    23/28

    c) LFU :

    Aim: Program to implement LFU page replacement policy

    /* Least recently used */

    #include#include

    main(){

    int a[20],b[10],c[20],i,n,m,j,x,k,pf=0,p,q,r,l;

    clrscr();

    printf("enter number of frames & pages");scanf("%d%d",&n,&m);

    for(i=1;i

  • 7/29/2019 Os Sp Lab Manual

    24/28

    }

    }

    pf++;b[x]=a[i];

    }

    }printf("number of page faults",pf);

    getch();

    }Input:

    enter number of frames & pages:

    3 12

    enter page numbers:2 3 2 1 5 2 4 5 3 2 5 2

    number of page faults:7

    d) Optimal:Aim: Write a program to implement Optimal page replacement algorithm

    Code:

    /* Optimal page replacement algorithm*/

    #include

    int n;main()

    {

    int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s,pf=0;

    int count=1,p=0;float pfr;

    clrscr();

    printf("enter max limit of the sequence");scanf("%d",&max);

    printf("enter the sequence");

    for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    25/28

    flag=0;

    }

    if(flag!=0){

    fr[count]=seq[i];

    printf("%d\t",fr[count]);count++;

    pf++;

    }i++;

    }

    printf("\n");

    for(i=p;i

  • 7/29/2019 Os Sp Lab Manual

    26/28

    {

    if(pos[k]==-1)

    {s=k;

    break;

    }}

    }

    fr[s]=seq[i];for(k=0;k

  • 7/29/2019 Os Sp Lab Manual

    27/28

    2 3 0

    2 3 0

    2 3 12 3 5

    2 3 5

    4 3 54 3 5

    4 3 5

    2 3 52 3 5

    2 3 5

    Total no.of page faults:3Page fault rate:0.5

    Program 7: Simulate paging Technique of memory management

    Aim: Write a program to implement paging Technique

    Code:

    /* Memory management using paging technique */

    #include#include

    main()

    {

    int pn[10],s[10],n,i,pro[10][20],fno[10][20],x,y,z,i,pa;int offset,ps;

    clrscr();

    printf("enter number of process & offset");scanf("%d%d",&n,&offset);

    for(i=0;i

  • 7/29/2019 Os Sp Lab Manual

    28/28

    pa=fno[x][y]*1200*ps+offset;

    printf("physical address",pa);

    getch();}

    Input:

    enter number of process & offset:2 2enter name of process:1

    enter size of process & page:1 1

    enter name of process:2enter size of process & page:2 1

    enter pagetable for p[0]:1 3

    enter page table for p[1]:1 4

    2 5for logical address,enter process,page number,offset:1 1 2

    physical address:6002