lab16b.c

来自「ALL LINUX LAB PROGRAM MIT MANIPAL 2008-0」· C语言 代码 · 共 104 行

C
104
字号
#include<stdio.h>
//#include<process.h>
#include<fcntl.h>

typedef struct
{
        int pid;
        int bt;
}process;
process p[30];

 main()
{
        int ch;
        char c;
        do
        {
                system("clear");
                printf("\n Main MENU ");
                printf("\n PROCESS SCHEDULING ");
                printf("\n\n1.FCFS");
                printf("\n2.SJF");
                printf("\n3.EXIT");

                printf("\n Enter ur choice :");
                scanf(" %d",&ch);
                switch(ch)
                {
                        case 1: fcfs();break;
                        case 2: sjf();break;
                        case 3: exit(0);
                        default : printf("\n Invalid Input");
                }
                printf("\n Do u wanna to continue :");
                scanf(" %c",&c);
        }while( c =='y' || c =='Y');
}

fcfs()
{
        int n,i,start=0,end;
        printf("\n Enter the no. of processes :");
        scanf(" %d",&n);
        for(i=0;i<n;i++)
        {
                printf("\n enter the Process no :");
                scanf("%d",&p[i].pid);

                printf("\n Enter the brust time :");
                scanf("%d",&p[i].bt);

        }
//FCFS Rule - process which enetr first will proceed first for execution
        printf(" FCFS ALGORITHM \n");
        for(i=0;i<n;i++)
        {
                printf(" Process[%d] \t",p[i].pid);
                end=start+p[i].bt;
                printf(" =\t%d - %d\n",start,end);
                start=end;
        }
        printf("TOTAL BURST TIME IS %d",end);
}

sjf()
{
        int i,j,n,start=0,end;
        process temp;
              printf("\n Enter the no. of processes :");
        scanf(" %d",&n);
        for(i=0;i<n;i++)
        {
                printf("\n enter the Process no :");
                scanf("%d",&p[i].pid);

                printf("\n Enter the brust time :");
                scanf("%d",&p[i].bt);

        }

  //SJF refeers to the process which has shortest brust time will execute
//first
        for(i=0;i<n-1;i++)
        for(j=i+1;j<n;j++)
        {
                if(p[i].bt>p[j].bt)
                {
                        temp=p[i];
                        p[i]=p[j];
                        p[j]=temp;
                }
        }
        printf("\n SJF ALGORITHM \n");
        for(i=0;i<n;i++)

        {        printf(" Process{%d]\t",p[i].pid);


                end=start+p[i].bt;
                printf(" =\t%d - %d\n",start,end);
                start=end;
        }
        printf("TOTAL BURST TIME IS %d",end);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?