简体   繁体   中英

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. Now if I run the two types of scheduling as separate C programs, I get no errors and the programs run smoothly. 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.

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. Everybody is willing to help, but you are expected to develop the skills to help out too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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