C

时间:2018-09-06 17:14:29

标签: c struct scheduling priority-queue

这是一个问题 最初,一组作业的名称为job_id,arrival_time(将为0)和execution_time。有一个可以用来执行这些作业的处理器。每个作业将获得固定时间(例如d)在处理器中执行。一旦持续时间结束,下一个作业将有机会执行,当前作业(如果尚未完成)将转到作业队列的末尾。该作业切换时间被认为可以忽略不计。在作业调度过程开始后,新作业也可能到达。在这种情况下,假定到达时间大于0。它将被添加到作业队列中。如果在任何时间点有新作业到达,并且现有作业在处理器中也完成了其“ d”时间,则首先将新作业添加到作业队列中,然后将现有作业添加到作业中。作业队列。执行作业调度并在特定时间后显示作业调度的状态。显示具有job_id,arrival_time,execution_time,start_time,finish_time,remaining_time,状态(已完成或未完成)的每个作业。

下面是我的实现。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct job{
        int pid;
        int at;
        int et;
        };

//Node for creating a jobq
struct jobq{
        int pid;
        int at;
        int et;
         struct jobq *next;
        };
struct Queue
{
    struct jobq *front, *rear;
};

struct jobq* newNode(int k,int l, int j)
{
    struct jobq *temp = (struct jobq*)malloc(sizeof(struct jobq));
    temp->pid = k;
    temp->at = l;
    temp->et = j;
    temp->next = NULL;
    return temp;
}

// A utility function to create an empty queue
struct Queue *createQueue()
{
    struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
    q->front = q->rear = NULL;
    return q;
}

void enQueue(struct Queue *q, int k, int l, int j)
{
    // Create a new LL node
    struct jobq *temp = newNode(k,l,j);

    // If queue is empty, then new node is front and rear both
    if (q->rear == NULL)
    {
    q->front = q->rear = temp;
       return;
    }

    // Add the new node at the end of queue and change rear
    q->rear->next = temp;
    q->rear = temp;
}

struct jobq *deQueue(struct Queue *q)
{
    // If queue is empty, return NULL.
    if (q->front == NULL)
       return NULL;

    // Store previous front and move front one node ahead
    struct jobq *temp = q->front;
    q->front = q->front->next;

    // If front becomes NULL, then change rear also as NULL
    if (q->front == NULL)
       q->rear = NULL;
    return temp;
}

void schedule(struct job rec[], struct job rec2[], int n1,int n2,int inst,int tq)
{
        struct Queue *q = createQueue();
        //Putting the jobs with AT=0 into jobq initially.
        for(int a=0 ; a<n1 ; a++)
        {
                enQueue(q , rec[a].pid, rec[a].at, rec[a].et);
        }

        struct jobq *j;
        j = q->front;

        struct ansjob{
                int apid;
                int aat;
                int start;
                int end ;
                int rem;
                int status;
                };

        struct ansjob instance[100];
      int start, end, rem, status,clock=0,i=0;
        while(1)
        {
                if(j->et < tq)
                {
                        start = clock;
                        end = start + j->et;
                        rem = 0 ;
                        clock = clock + j->et;
                        instance[i].apid = j->pid ;
                        instance[i].aat = j->at ;
                        instance[i].start = start ;
                        instance[i].end = end ;
                        instance[i].rem = rem ;
                        instance[i].status = 0 ; //COMPLETED
                        i++;
                        deQueue(q);
                        for(int k=0 ; k<n2 ; k++)
                        {       if(rec2[k].at <= clock)
                                {enQueue(q,rec2[k].pid,rec2[k].at,rec2[k].et);}
                        }

                        if(clock==(inst))
                        {break;}
                        else if(clock<(inst))
                        {
                          j = q->front;
                          continue;
                        }
                        else if(clock>(inst))
                        {break;}
                }
                        else if(j->et == tq)
                {
                          start = clock;
                          end = start + tq;
                          rem = 0 ;
                          clock = clock + tq ;
                          instance[i].apid = j->pid ;
                          instance[i].aat = j->at ;
                          instance[i].start = start ;
                          instance[i].end = end ;
                          instance[i].rem = rem ;
                          instance[i].status = 0 ;//COMPLETED
                          i++;
                          deQueue(q);
                          for(int k=0 ; k<n2 ; k++)
                        {     if(rec2[k].at <= clock)
                                {enQueue(q,rec2[k].pid,rec2[k].at,rec2[k].et);}
                          }
                          if(clock==(inst))
                          {break;}
                          else if(clock < (inst))
                          {
                          j = q->front;
                          continue;
                          }
                          else if(clock > (inst))
                          {break;}
                }

                else
                {
                        j->et = j->et -tq;
                        start = clock ;
                        end = -1 ;
                        rem = j->et ;
                        clock = clock + tq ;
                        instance[i].apid = j->pid ;
                        instance[i].aat = j->at ;
                        instance[i].start = start ;
                        instance[i].end = end ;
                        instance[i].rem = rem ;
                        instance[i].status = 1 ;//NOT COMPLETED
                        i++;
                        deQueue(q);
                        for(int k=0 ; k<n2 ; k++)
                        {       if(rec2[k].at <= clock)
                                {enQueue(q,rec2[k].pid,rec2[k].at,rec2[k].et);}
                        }
                        enQueue(q,j->pid,j->at,j->et);
                        if(clock==(inst))
                        {break;}
                        else if(clock < (inst))
                        {
                          j = q->front;
                          continue;
                        }
                        else if(clock > (inst))
                        {break; }


                }
        }
                                                                         printf("\n--------JOB STATUS------------\n");
        printf("PID\tAT\tSTART\tEND\tREM\tSTATUS\n");

        for(int s=0 ; s<i ; s++)
        {
                printf("%d\t%d\t%d\t%d\t%d\t",instance[s].apid ,instance[s].aat, instance[s].start, instance[s].end, instance[s].rem);
                if(instance[s].status == 0)
                {printf("COMPLETED");}
                else if(instance[s].status == 1)
                {printf("NOT COMPLETED");}
                printf("\n");
        }

}



void main()
{
        int tq,n,n1;
        printf("Enter the processor alloted time= ");
        scanf("%d" , &tq);
        printf("\nEnter the initial set of jobs at time = 0");
        printf("\nEnter number of jobs");
        scanf("%d" ,&n);
        struct job record[n] ;
        for(int i=0 ; i<n ; i++)
        {
                printf("PID = ");
                scanf("%d" , &record[i].pid);
                printf("Arrival Time = ");
                scanf("%d" , &record[i].at);
                printf("Execution Time = ");
                scanf("%d" , &record[i].et);
        }

        char flag[4];
        printf("Any more jobs at later time :");
        scanf("%s" , flag);
        struct job latrec[n1];
        if((strcmp(flag , "YES")) == 0)
        {       printf("\nEnter number of jobs");
                scanf("%d" ,&n1);

                for(int j=0 ; j<n1 ; j++)
                {
                printf("PID = ");
                                                                                        printf("PID = ");
                scanf("%d" , &latrec[j].pid);
                printf("Arrival Time = ");
                scanf("%d" , &latrec[j].at);
                printf("Execution Time = ");
                scanf("%d" , &latrec[j].et);
                }
        }

        else if((strcmp(flag , "NO")) == 0)
        {}
        int instance ;
        printf("Enter the time instance for displaying the job scheduling result:");
        scanf("%d" , &instance);
        schedule(record, latrec, n, n1,instance, tq);

}

现在,当我执行此代码时

Enter the processor alloted time= 3

Enter the initial set of jobs at time = 0
Enter number of jobs6
PID = 1
Arrival Time = 3
Execution Time = 24
PID = 2
Arrival Time = 6
Execution Time = 10
PID = 3
Arrival Time = 5
Execution Time = 12
PID = 4
Arrival Time = 8
Execution Time = 20
PID = 5
Arrival Time = 2
Execution Time = 40
PID = 6
Arrival Time = 1
Execution Time = 22
Any more jobs at later time :no
Enter the time instance for displaying the job scheduling result:7

--------JOB STATUS------------
PID     AT      START   END     REM     STATUS
1       3       0       -1      21      NOT COMPLETED
2       6       3       -1      7       NOT COMPLETED
3       5       6       -1      9       NOT COMPLETED

根据问题,如果这是正确的输出,我将无法验证结果。有人可以帮助我

1)如果输出错误,请验证结果,那么我想知道的是我实现中出了问题。 2)是否有更好的方法来解决该问题?我的意思是说,比实现中的算法更好的算法,如果是的话,那么如何降低此类问题的复杂性可能是除C之外的其他语言,某些内置函数可以实现相同的功能,我了解python的一些知识和Java的基础知识,但是我对C语言更满意。

0 个答案:

没有答案