我对C有点陌生,但我在main之前一直遇到这个分段错误,我不知道为什么,我会问我的教授,但她正忙于ATM的其他事情,担心像这样琐碎的事情。
这里的完整代码:
如有任何帮助,我们将不胜感激。
编辑:我认为这是seg在main之前出错,因为我在main的第一行上放了一个print语句,它在那行之前出错了。
我正在使用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;
}
EDIT:我认为这是在main之前的seg错误,因为我在main的第一行上放了一个print语句,而它在该行之前出现了错误。
正如其他人所解释的,这不是一种有效的故障排除技术,崩溃发生的时间要晚得多。
pthread_mutex_t* mutex;
好的,所以mutex
是一个指向没有特定值的互斥对象的指针。
pthread_mutex_init(q->mutex,NULL);
然后将这个不特定的值传递给pthread_mutex_init
。难怪它会崩溃。您应该传递一个指向要初始化的互斥对象的指针,而不是一个不指向任何对象的指针。
这是gdb的回溯,它没有说分段错误发生在main((之前,而是在main(((之后,稍微在调用queueInit((之后:
#1 0x0000555555555726 in queueInit () at seg.c:259
#2 0x00005555555553d1 in main () at seg.c:77
事实上,作业中有一些注释标有"TODO",我想提醒您注意这一部分,还有许多其他类似的注释:
//TODO for students: Initialize the locks here