简体   繁体   English

为什么在C中的多线程调度程序中出现分段错误(内核已转储)

[英]Why is there segmentation fault(core dumped) in my multithreaded scheduling program in C

I have to write a multi-threaded program in C in which one thread displays working of FCFS Scheduling and the other shows SJF scheduling. 我必须用C语言编写一个多线程程序,其中一个线程显示FCFS调度的工作,另一个线程显示SJF调度。 Now if I run the two types of scheduling as separate C programs, I get no errors and the programs run smoothly. 现在,如果我将两种类型的调度作为独立的C程序运行,则不会出现任何错误,并且程序可以平稳运行。 But when I put them in two different functions and use the concept of multi-threading, the terminal prints the error "Segmentation fault (core dumped)" Please help me out 但是当我将它们置于两个不同的函数中并使用多线程的概念时,终端会显示错误“分段错误(核心已转储)”,请帮帮我

#include <stdio.h>
#include <pthread.h>
void *fcfs(void *);
void *sjf(void *);
int pid[10],at[10],bt[10];
int pid1[10],at1[10],bt1[10];
void main()
{
    pthread_t fcfsT,sjfT;
    pthread_attr_t attr;
    int lower = 0, upper = 20, count = 10;
    int i;
    for(i=0;i<10;i++)
    {
        pid[i]=i+1;
    }
    for (i = 0; i < count; i++) {
        at[i] = (rand() % (upper - lower + 1)) + lower;
        bt[i] = (rand() % (upper - lower + 1)) + lower;
    }
    for(i=0;i<10;i++)
    {
        pid1[i]=pid[i];
        at1[i]=at[i];
        bt1[i]=bt[i];
    }
    pthread_attr_init(&attr);
    pthread_create(&fcfsT,&attr,fcfs, NULL);
    pthread_create(&sjfT,&attr,sjf,NULL);
    pthread_join(fcfsT,NULL);
    pthread_join(sjfT,NULL);
}

void *fcfs(void *p)
{
    int ct[10],a,wt[10],tat[10],i,j=0;
    for (i = 0; i < 10; ++i) 
    {
        for (j = i + 1; j < 10; ++j)
        {
            if (at[i] > at[j]) 
            {
                a =  at[i];
                at[i] = at[j];
                at[j] = a;
                a =  bt[i];
                bt[i] = bt[j];
                bt[j] = a;
                a =  pid[i];
                pid[i] = pid[j];
                pid[j] = a;
             }
         }
    }
    ct[0]=at[0]+bt[0];
    for(i=1;i<10;i++)
    {
        if(at[i]<ct[i-1])
            ct[i]=ct[i-1]+bt[i];
        else
            ct[i]=at[i]+bt[i];
    }

    for (i = 0; i < 10; ++i) 
    {
         for (j = i + 1; j < 10; ++j)
        {
            if (pid[i] > pid[j]) 
            {
                a =  pid[i];
                pid[i] = pid[j];
                pid[j] = a;
                a =  at[i];
                at[i] = at[j];
                at[j] = a;
                a =  bt[i];
                bt[i] = bt[j];
                bt[j] = a;
                a =  ct[i];
                ct[i] = ct[j];
                ct[j] = a;      
             }
         }
    }
    for(i=0;i<10;i++)
    {
        tat[i]=ct[i]-at[i];
        wt[i]=tat[i]-bt[i];
    }
    printf("PID\tAT\tBT\tCT\tTAT\tRT:\n\n");
    for(i=0;i<10;i++)
    {
        printf("P%d\t%d\t%d\t%d\t%d\t%d\n",pid[i],at[i],bt[i],ct[i],tat[i],wt[i]);
    }
    pthread_exit(0);
}

void *sjf(void *g)
{
    int ct1[10],b,wt1[10],tat1[10],z,q=0,minimum,location,temp[3]={0,0,0};
    for (z = 0; z < 10; ++z) 
    {
         for (q = z + 1; q < 10; ++q)
        {
            if (bt1[z] > bt1[q]) 
            {
                b =  bt1[z];
                bt1[z] = bt1[q];
                bt1[q] = b;
                b =  at1[z];
                at1[z] = at1[q];
                at1[q] = b;
                b =  pid1[z];
                pid1[z] = pid1[q];
                pid1[q] = b;
             }
         }
    }

    for (z = 0; z < 10; ++z) 
    {
        for (q = z + 1; q < 10; ++q)
        {
            if (bt1[z] == bt1[q]) 
            {
                if(at1[q]<at1[z])
                {
                    b =  bt1[z];
                    bt1[z] = bt1[q];
                    bt1[q] = b;
                    b =  at1[z];
                    at1[z] = at1[q];
                    at1[q] = b;
                    b =  pid1[z];
                    pid1[z] = pid1[q];
                    pid1[q] = b;
                }
            }
        }
    }
    minimum = at1[0];
    for ( z = 1 ; z < 10 ; z++ ) 
    {
        if ( at1[z] < minimum ) 
        {
           minimum = at1[z];
           location = z;
        }
    }
    temp[0] = at1[location];
    temp[1] = bt1[location];
    temp[2] = pid1[location];
    for(z=location;z>0;z--)
    {
        at1[z]=at1[z-1];
        bt1[z]=bt1[z-1];
        pid1[z]=pid1[z-1];
    }
    at1[0]=temp[0];
    bt1[0]=temp[1];
    pid1[0]=temp[2];
    ct1[0]= at1[0]+bt1[0];
    for(z=1;z<10;z++)
    {
        if(at1[z]>ct1[z-1])
            ct1[z] = at1[z]+bt1[z];
        else
            ct1[z] = bt1[z]+ ct1[z-1];
    }
    for (z = 0; z < 10; ++z) 
    {
         for (q = z + 1; q < 10; ++q)
        {
            if (pid1[z] > pid1[q]) 
            {
                b =  pid1[z];
                pid1[z] = pid1[q];
                pid1[q] = b;
                b =  at1[z];
                at1[z] = at1[q];
                at1[q] = b;
                b =  bt1[z];
                bt1[z] = bt1[q];
                bt1[q] = b;
                b =  ct1[z];
                ct1[z] = ct1[q];
                ct1[q] = b;

             }
         }
    }
    for(z=0;z<10;z++)
    {
        tat1[z]=ct1[z]-at1[z];
        wt1[z]=tat1[z]-bt1[z];
    }
    printf("pid1\tAT\tBT\tCT\tTAT\tRT:\n\n");
    for(z=0;z<10;z++)
    {
        printf("P%d\t%d\t%d\t%d\t%d\t%d\n",pid1[z],at1[z],bt1[z],ct1[z],tat1[z],wt1[z]);
    }
    pthread_exit(0);
}

In sjf(), if at1[0] is the minimum, location will never be initialized, thus the loop below could readily generate invalid addresses. 在sjf()中,如果at1 [0]是最小值,则位置将永远不会初始化,因此下面的循环很容易生成无效地址。

for(z=location;z>0;z--)
    {
        at1[z]=at1[z-1];
        bt1[z]=bt1[z-1];
        pid1[z]=pid1[z-1];
    }

When you run in “process mode”, it is very likely that your initial stack is zero-filled, thus the flaws in your program were hidden. 当您在“进程模式”下运行时,很有可能初始堆栈为零填充,因此隐藏了程序中的缺陷。 The initial thread stack may have other stale data in it. 初始线程堆栈中可能包含其他过时的数据。

As the comments point out, you should try and use some level of compiler diagnostic (and initiative) before SO. 正如评论所指出的那样,您应该在SO之前尝试使用某种级别的编译器诊断(和主动性)。 Everybody is willing to help, but you are expected to develop the skills to help out too. 每个人都愿意提供帮助,但是希望您也能开发帮助的技能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM