C program to implement the round robin scheduling algorithm


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJfShSmc4uzJ32B_Cnh6KDE7EeSGsUpqwnmXpsZMSrMSp0wjgo7gGWZu_JIDHC_zUiWwWkPBhm3Pz_vS0k-tjHPGbd4J7sxNkEVv5q-YbMtId0PtXLQqi2n_k4Dee10jKN6F-IOkz4Wqgg/s1600/c+and+c%252B%252B+meansofmine.jpg
 IMPLEMENTATION OF ROUND ROBIN SCHEDULING ALGORITHM


AIM

To write a c program to implement the round robin scheduling algorithm

ALGORITHM

1. Start the process

2. Declare the array size

3. Get the number of elements to be inserted

4. Get the value

5. Set the time sharing system with preemption

6. Define quantum is defined from 10 to 100ms

7. Declare the queue as a circular

8. Make the CPU scheduler goes around the ready queue allocating CPU to each process
    for the time interval specified

9. Make the CPU scheduler picks the first process and sets time to interrupt after quantum
    expired dispatches the process

9. If the process have burst less than the time quantum than the process release the CPU

10. If the process have bust greater then time quantum then time will go off and cause
      interrupt to OS and the process put into the tail of ready queue and the schedule select
      next process

11. Display the results

12. Stop the process


PROGRAM:
#include<stdio.h>
int ttime,i,j,temp;
main()
{
        int pname[10],btime[10],pname2[10],btime2[10];
        int n,x,z;
        printf("Enter the no. of process:");
        scanf("%d",&n);
        printf("Enter the process name and burst time for the process\n");
        for(i=0;i<n;i++)
        {     
    printf("Enter the process name:");
                scanf("%d",&pname2[i]);
                printf("Enter burst time for the process %d:",pname2[i]);
                scanf("%d",&btime2[i]);
        }
        printf("PROCESS NAME \t\t BURST TIME\n");
        for(i=0;i<n;i++)
            printf("%d\t\t\t %d\n",pname2[i],btime2[i]);
            z=1;
        while(z==1)
        {
ttime=0;
        for(i=0;i<n;i++)
        { 
pname[i]=pname2[i];
            btime[i]=btime2[i];
        }

            printf ("PRESS 1.ROUND ROBIN 2.EXIT\n");
            scanf("%d",&x);
        switch(x)
        {
        case 1:
                rrobin(pname,btime,n);
                break;
        case 2:
                exit(0);
                break;
        default:
                printf("Invalid option");
               break;
       }
       printf("\n\n If you want to continue press 1:");
       scanf("%d",&z);
       }
}
     
 rrobin(int pname[],int btime[],int n)
       {
            int tslice;
            j=0;
            printf("\n\t ROUND ROBIN SCHEDULING \n\n");
            printf("Enter the time slice:\n");
            scanf("%d",&tslice);
            printf("PROCESS NAME \t REMAINING TIME\t TOTAL TIME");
    

while(j<n)
            { 
      for(i=0;i<n;i++)
{
      if(btime[i]>0)
                        { 
if(btime[i]>=tslice)
                                    { 
ttime+=tslice;
                                                btime[i]=btime[i]-tslice;
                                                printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
                                                if(btime[i]==0)
                                                 j++;
                                    }
                                    else
                                    { 
ttime+=btime[i];
                                                btime[i]=0;
                                                printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
                                    }
}
 }
       }
}


OUTPUT:

Enter the no. of process: 4
Enter the process name and burst time for the process
Enteer the process name: 1
Enter burst time for the process 1: 8
Enteer the process name: 2
Enter burst time for the process 2: 3
Enteer the process name: 3
Enter burst time for the process 3: 6
Enteer the process name: 4
Enter burst time for the process 4: 1

PROCESS NAME             BURST TIME
             1                                         8
             2                                         3
             3                                         6
             4                                         1

PRESS 1.ROUND ROBIN 2.EXIT
1
ROUND ROBIN SCHEDULING

Enter the time slice:
2
PROCESS NAME     REMAINING TIME  TOTAL TIME
1                                     6                                2
2                                     1                                4
3                                     4                                6
4                                     0                                7
1                                     4                                9
2                                     0                                10
            3                                     2                                12
1                                     2                                14
3                                     0                                16

7 comments:

Anonymous said...

hey i think there is a casee missing in it dude

Anonymous said...

der shld b a j++ in d else loop.. den it will run properly

Anonymous said...

total burst time is 18 unit but total time shown in scheduling is up to 16 unit so there should be a case for process 1 that is ;

1 0 18

Anonymous said...

gud one... :)

Anonymous said...

good but remember to put a "j++" also in the "else" case in rrobin"! :)

Anonymous said...

j++ is missing in the 'else' loop. If it is so then this is a good code.

Anonymous said...

so the program is properly run or not..............

Post a Comment