0

这段代码是为操作系统创建队列我使用结构来实现我的进程并使用 arr_processes 来处理所有这些进程,并使用 new_processes 数组根据其到达时间对这些进程进行排序

但是当我在 Visual Studio 2010 上运行此代码时,它会产生此运行时错误 Run-Time Check Failure #2 - 变量 arr_processes 周围的堆栈已损坏!

这是代码

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

typedef struct
{
    int id;
    int arr_time;
    int serv_time;
    int deadline;
} process;

void print_process(process n);
int main()
{
    process arr_processes[8];
    process new_processes[8];
    process real_processes[3];
    process ready_processes[5];
    process tmp_process[1];
    int length_ready;
    int i,length,j;
    int length_real;

    arr_processes[0].id=1;
    arr_processes[0].arr_time=12;
    arr_processes[0].serv_time=4;
    arr_processes[0].deadline=0;

    arr_processes[1].id=2;
    arr_processes[1].arr_time=10;
    arr_processes[1].serv_time=5;
    arr_processes[1].deadline=0;

    arr_processes[2].id=3;
    arr_processes[2].arr_time=9;
    arr_processes[2].serv_time=2;
    arr_processes[2].deadline=0;

    arr_processes[3].id=4;
    arr_processes[3].arr_time=8;
    arr_processes[3].serv_time=4;
    arr_processes[3].deadline=10;

    arr_processes[4].id=5;
    arr_processes[4].arr_time=5;
    arr_processes[4].serv_time=2;
    arr_processes[4].deadline=8;

    arr_processes[5].id=6;
    arr_processes[5].arr_time=3;
    arr_processes[5].serv_time=3;
    arr_processes[5].deadline=0;

    arr_processes[6].id=7;
    arr_processes[6].arr_time=2;
    arr_processes[6].serv_time=3;
    arr_processes[6].deadline=0;

    arr_processes[7].id=8;
    arr_processes[7].arr_time=1;
    arr_processes[7].serv_time=1;
    arr_processes[7].deadline=28;

    length=sizeof(arr_processes)/sizeof(arr_processes[0]);

    printf("\t length of the processes=%i\n\n",length);
    printf("\t The Original processes \n\n");
    for(i=0;i<8;i++)
        print_process(arr_processes[i]);

    // now we want to sort the processes according to their arrival time
    for(i=0;i<8;i++)
    {
        new_processes[i]=arr_processes[i];
    }

    for(i=0;i<length;i++)
    {
        for(j=0;j<length-i;j++)
        {
            if((new_processes[j].arr_time)>(new_processes[j+1].arr_time))
            {
                tmp_process[0]=new_processes[j];
                new_processes[j]=new_processes[j+1];
                new_processes[j+1]=tmp_process[0];
            }
        }
    }

    printf("\t The New processes \n\n");
    for(i=0;i<8;i++)
        print_process(new_processes[i]); // the new queue

    ready_processes[0]=arr_processes[0];
    ready_processes[1]=arr_processes[1];
    ready_processes[2]=arr_processes[2];
    ready_processes[3]=arr_processes[5];
    ready_processes[4]=arr_processes[6];

    length_ready=sizeof(ready_processes)/sizeof(ready_processes[0]);
    // now we want to design the ready queue
    for(i=0;i<length_ready;i++)
    {
        for(j=0;j<length_ready-i;j++)
        {
            if((ready_processes[j].arr_time)>ready_processes[j+1].arr_time)
            {
                tmp_process[0]=ready_processes[j];
                ready_processes[j]=ready_processes[j+1];
                ready_processes[j+1]=tmp_process[0];
            }
        }
    }

    printf("\t The ready processes \n\n");
    for(i=0;i<length_ready;i++)
    print_process(ready_processes[i]); // the ready  queue

    // now we want to design the ready real queue for the shortest deadline first
    // we donnot need to check for the new proesses at each instant of time
    //but we need to check for the service time from now

    real_processes[0]=arr_processes[3];
    real_processes[1]=arr_processes[4];
    real_processes[2]=arr_processes[7];

    length_real=sizeof(real_processes)/sizeof(real_processes[0]);
    for(i=0;i<length_real;i++)
    {
        for(j=0;j<length_real-i;j++)
        {
            if((real_processes[j].deadline)>real_processes[j+1].deadline)
            {
                tmp_process[0]=real_processes[j];
                real_processes[j]=real_processes[j+1];
                real_processes[j+1]=tmp_process[0];
            }
        }
    }

    printf("\t The real processes \n\n");
    for(i=0;i<length_real;i++)
    print_process(real_processes[i]); // the ready real queue

    // removed real process
    process removed_real;
    removed_real.id=0;
    removed_real.arr_time=0;
    removed_real.serv_time=0;
    removed_real.deadline=0;

    process running_process;
    running_process.id=0;
    running_process.arr_time=0;
    running_process.serv_time=0;
    running_process.deadline=0;

    int counter=0;
    int start_time;

    while(counter<=28)
    {
        printf("when time = %i\n\n",counter);
        // printf("\t The real processes when the counter=%i \n\n",counter);
        // for(i=0;i<length_real;i++)
        // print_process(real_processes[i]); // the ready real queue


        // first we must check for the real processes
        for(i=0;i<length_real;i++)
        {
            if((counter==real_processes[i].arr_time)
            &&((real_processes[i].deadline)-counter)>=(real_processes[i].serv_time))
            {
                running_process=real_processes[i];
                printf("The non zero deadline process is:%i\n",running_process.id);
                real_processes[i]=removed_real;
                start_time=counter;   // real process

                while(counter!=(start_time+running_process.serv_time))
                {
                    printf("At time = %i,The Running Process is...\n",counter);
                    print_process(running_process);
                    counter++;
                }
            }
        }

        counter++;
    }

    return 0;
}

void print_process(process n)
{
    if(n.deadline!=0)
    printf("ID=%i\narr_time=%i\nserv_time=%i\ndeadline=%i\n\n\n",n.id,n.arr_time,n.serv_time,n.deadline);
    else if(n.deadline==0)
    printf("ID=%i\narr_time=%i\nserv_time=%i\n\n\n",n.id,n.arr_time,n.serv_time);
}
4

2 回答 2

2

当您用完索引时,这是一个排序示例:

for(i=0; i<length - 1; i++)
{
     for(j=i + 1;j<length;j++)
     {
         if((new_processes[j].arr_time)>(new_processes[i].arr_time))
         {
              tmp_process[0]=new_processes[j];
              new_processes[j]=new_processes[i] ;
              new_processes[i]=tmp_process[0] ;
         }
    }
}

或者,您可以使用标准功能:

void qsort(void *base, size_t nmemb, size_t size,
       int (*compar)(const void *, const void *));

定义一个比较函数:

int compare_by_arr_time(const void* a, const void* b)
{
    int a_int = ((const process*)a)->arr_time;
    int b_int = ((const process*)b)->arr_time;

    return a_int - b_int;  // or b_int - a_int
}

并按如下方式使用它:

qsort(new_processes,
      sizeof(new_processes)/sizeof(new_processes[0]),
      sizeof(new_processes[0]),
      compare_by_arr_time);
于 2013-04-20T23:00:15.567 回答
1

当您超出数组的范围时,您会遇到这些类型的错误。

for(i=0;i<length;i++)
 {
       for(j=0;j<length-i;j++)
       {
             if((new_processes[j].arr_time)>(new_processes[j+1].arr_time))
             {
                   tmp_process[0]=new_processes[j];
                   new_processes[j]=new_processes[j+1] ;
                   new_processes[j+1]=tmp_process[0] ;
             }
       }
 }

在第一次迭代中i = 0j = 0j必须小于8 - i,即8。注意表达式j+1。此表达式将在[1 ... 9]最外层循环的第一次迭代期间返回范围内的值,因此,您将超出new_processes数组的范围。

有你的问题。

编辑:这个问题也可能出现在for第一个之后的循环中。

于 2013-04-20T22:53:18.367 回答