【发布时间】:2020-08-20 10:22:57
【问题描述】:
我对 C 有点陌生,但我在 main 之前一直遇到这个分段错误,我不知道为什么,我会问我的教授,但她正忙于 ATM 的其他事情,担心像这样微不足道的事情。
这里是完整代码:
任何帮助将不胜感激。
编辑:我相信它在 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;
}
【问题讨论】:
-
为什么你认为在调用
main之前就发生了分段错误? -
我很确定如果发生任何段错误,那是在
main()开始之后。如果您认为情况并非如此,请说明您是如何得出该结论的。 -
我打印了一个简单的 printf 语句,它甚至没有在 main 的第一行运行,它已经出现了段错误。
-
你必须
fflush输出。printf("test"); fflush(stdout);流输出被缓冲,崩溃的程序不会刷新它的缓冲区。一个正常退出的程序会。正在发生的事情是你没有得到提示,而是得出了错误的结论。更好的是使用适当的调试器。 -
少数段错误可以通过在 queue_Init() 中为队列的 mutex、nextCon、nextProd 成员分配内存来修复,因为它们是指针。
标签: c linux segmentation-fault pthreads