发生主电源前的段故障

问题描述 投票:-1回答:2

我对C还是有点陌生​​,但是我在main之前总是遇到这种分段错误,我不知道为什么,我会问我的教授,但是她正忙于ATM的其他事情来担心如此琐碎的事情。

这里是完整代码:

任何帮助将不胜感激。

编辑:我相信它是main之前的段错误,因为我已经在main的第一行上放置了一条打印语句,并且在该行之前出现了错误。

我正在使用gcc program.c -o prog -lpthread -DUNIX进行编译,并在Windows的Ubuntu子系统上运行它,但之前从未遇到过此问题。

/***** Includes *****/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Linux or MacOS
#ifdef UNIX
    #include <pthread.h>
    #include <unistd.h>
#endif

//Windows
#ifdef WINDOWS
    #include <windows.h>
#endif

/***** Defines: Constants in seconds *****/
#define PRODUCER_SLEEP_S    1
#define CONSUMER_SLEEP_S    1
#define QUEUESIZE           5
#define LOOP                10


/***** Function prototypes *****/
void *producer (void *args);
void *consumer (void *args);

/***** Queue struct *****/
typedef struct 
{
    int buf[QUEUESIZE];
    long head, tail;
    int full, empty; 
    pthread_mutex_t* mutex;
    pthread_cond_t *nextCon;
    pthread_cond_t *nextProd;

    //mutex 1= free
    //mutex 2= taken
    //you may need or may not
    /*TODO for students: Declare the locks here */
} queue;

/***** Queue function prototypes  *****/
queue *queueInit (void);
void queueDelete (queue *q);
void queueAdd (queue *q, int in);
void queueDel (queue *q, int *out);

/***** main *****/
int main ()
{
//does not reach this point

   printf("test");

    queue *fifo;
    int i;

    //for Consumer's random coming
    unsigned int iseed = (unsigned int)time(NULL);
    //seeds the random number generator
    srand(iseed);

    /******one producer and multiple consumer********/

    //an array of consumers and one producer
    #ifdef UNIX
    pthread_t pro, con[LOOP];
    #endif

    //an array of consumers and one producer
    #ifdef WINDOWS
    HANDLE pro, con[LOOP];
    #endif

    fifo = queueInit ();
    if (fifo ==  NULL) 
    {
        fprintf (stderr, "main: Queue Init failed.\n");
        exit (1);
    }

    #ifdef UNIX
        pthread_create (&pro, NULL, producer, fifo);
        for(i=0; i<LOOP; i++)
        {
            pthread_create (&con[i], NULL, consumer, fifo);
        }
    #endif

    #ifdef WINDOWS
        pro = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE) producer, fifo, 0, NULL);
        for(i=0; i<LOOP; i++)
        {
    #ifdef WINDOWS
            /* TODO for students: Simulate Consumers' random arrival */
            Sleep((rand()%2)*1000);
    #endif
            con[i] = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE) consumer, fifo, 0, NULL);        
        }
    #endif


    #ifdef UNIX
        pthread_join (pro, NULL);
        for(i=0; i<LOOP; i++)
        {
            pthread_join (con[i], NULL);    
        }
    #endif

    #ifdef WINDOWS
        WaitForSingleObject(pro, INFINITE);
        /*******Wait for all the threads complete**********/      
        WaitForMultipleObjects(LOOP, con, TRUE, INFINITE);  

    #endif

    queueDelete (fifo);

    #ifdef WINDOWS
    system("pause");
    #endif

    return 0;
}

/***** producer *****/
void *producer (void *q)
{

    queue *fifo;
    int i;

    fifo = (queue *)q;

    //TODO for students: obtain the lock and release the lock somewhere

    for (i = 0; i < LOOP; i++) 
    {

        // TODO for students: Obtain the locks somewhere here

        #ifdef UNIX
        pthread_mutex_lock(fifo->mutex);
        while (fifo->full) 
        {
            pthread_cond_wait(fifo->nextProd, fifo->mutex);
        }
        #endif

        #ifdef WINDOWS

        #endif


        queueAdd (fifo, i+1);
        printf ("producer: produced %d th.\n",i+1);
        /* sleep */
        #ifdef UNIX
            usleep ( PRODUCER_SLEEP_S * 1000000); 
        #endif

        #ifdef WINDOWS
            Sleep ( PRODUCER_SLEEP_S * 1000);   
        #endif

        /*******Release the locks**********/
        #ifdef UNIX
         pthread_cond_signal(fifo->nextCon);
            pthread_mutex_unlock(fifo->mutex);
        #endif


        #ifdef WINDOWS

        #endif
    }

    return (NULL);
}

/***** consumer *****/
void *consumer (void *q)
{
    queue *fifo;
    int d;

    fifo = (queue *)q;

    /* Simulate Consumers' random arrival */
    #ifdef UNIX
        usleep ( (rand()%LOOP) * 1000000); 
    #endif


    // TODO for students: obtain the lock and release the lock somewhere

    #ifdef UNIX
    pthread_mutex_lock(fifo->mutex);
    while (fifo->empty) 
    {
        pthread_cond_wait(fifo->nextCon, fifo->mutex);
    }
    #endif


    #ifdef WINDOWS

    #endif

    /* sleep */
    #ifdef UNIX
        usleep ( CONSUMER_SLEEP_S * 1000000); 
    #endif

    #ifdef WINDOWS
        Sleep ( CONSUMER_SLEEP_S * 1000);   
    #endif


    queueDel (fifo, &d);
    printf ("------------------------------------>consumer: recieved %d.\n", d);        

    #ifdef UNIX
        pthread_cond_signal(fifo->nextProd);
        pthread_mutex_unlock(fifo->mutex);
    #endif


    #ifdef WINDOWS

    #endif


    return (NULL);
}


/***** queueInit *****/
queue *queueInit (void)
{
    queue *q;
    int i;

    q = (queue *)malloc (sizeof (queue));
    if (q == NULL) return (NULL);

    for(i=0;i<QUEUESIZE;i++)
    {
        q->buf[i]=0;
    }
    q->empty = 1;
    q->full = 0;
    q->head = 0;
    q->tail = 0;
    //TODO for students: Initialize the locks here
    pthread_mutex_init(q->mutex,NULL);
    pthread_cond_init(q->nextCon,NULL);
    pthread_cond_init(q->nextProd,NULL);
    return (q);
}


/***** queueDelete *****/
void queueDelete (queue *q)
{

    //TODO for students: free the locks here

    pthread_mutex_destroy(q->mutex);
    /* free memory used for queue */
    free (q);
}

/***** queueAdd *****/
void queueAdd (queue *q, int in)
{
    q->buf[q->tail] = in;
    q->tail++;
    if (q->tail == QUEUESIZE)
        q->tail = 0;
    if (q->tail == q->head)
        q->full = 1;
    q->empty = 0;

    return;
}

/***** queueDel *****/
void queueDel (queue *q, int *out)
{
    *out = q->buf[q->head];
    q->buf[q->head]=0;

    q->head++;

    if (q->head == QUEUESIZE)
        q->head = 0;
    if (q->head == q->tail)
        q->empty = 1;
    q->full = 0;

    return;
}



c linux segmentation-fault pthreads
2个回答
0
投票

这是gdb的回溯,它没有说分段错误发生在main()之前,而是在main()之后,在queueInit()被调用之后不久:

#1  0x0000555555555726 in queueInit () at seg.c:259
#2  0x00005555555553d1 in main () at seg.c:77

1
投票

编辑:我相信它是main之前的段错误,因为我已经在main的第一行上放置了一条打印语句,并且在该行之前出现了错误。

© www.soinside.com 2019 - 2024. All rights reserved.