我正在用c中的两个线程做一个生产者-一个消费者问题。我使用一个共享链表来做到这一点,其中生产者放置一些东西,消费者从同一列表中取出它。我必须用2个值N和B运行我的代码(N是要传输的数据的大小,B是共享链表的最大大小)。(./thread-a1 N B)这段代码对于小值运行良好。但是,当N = 20,000, B = 16时,给出分割错误:11我不明白为什么会这样。请帮助
#include<time.h>
#include<sys/time.h>
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
struct job
{
int data;
struct job* next;
};
struct job* head;
int maxSize;
int n;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
int getCount()
{
int count = 0;
struct job* p = head;
while(p != NULL)
{
p = p -> next;
count++;
}
return count;
}
void Insert(int x)
{
struct job* temp = (struct job*)malloc(sizeof(struct job));
temp -> data = x;
if (head == NULL)
{
temp -> next = NULL;
}
else
{
temp -> next = head;
}
head = temp;
}
void Delete()
{
struct job *toDelete, *secondLastNode;
if(head == NULL)
{
}
else
{
toDelete = head;
secondLastNode = head;
while(toDelete->next != NULL)
{
secondLastNode = toDelete;
toDelete = toDelete->next;
}
printf("%d, ", toDelete -> data);
if(toDelete == head)
{
free(toDelete);
head = NULL;
}
else
{
secondLastNode->next = NULL;
free(toDelete);
}
}
}
void* producer(void* arg)
{
pthread_mutex_lock(&m);
head = NULL;
int i;
for (i = 0; i<n; i++)
{
while(getCount() >= maxSize)
{
pthread_mutex_unlock(&m);
}
Insert(i);
}
return NULL;
}
void* consumer(void* arg)
{
pthread_mutex_lock(&m);
int i;
for (i=0; i<n; i++)
{
while(getCount() <= 0)
{
pthread_mutex_unlock(&m);
}
Delete();
}
return NULL;
}
int main(int argc, char *argv[])
{
struct timeval start_init, end_init;
gettimeofday(&start_init, NULL);
n = atoi(argv[1]);
maxSize = atoi(argv[2]);
pthread_t thread1;
pthread_t thread2;
gettimeofday(&end_init, NULL);
printf("Time elapsed for initialization is: %ldn",
(long)(end_init.tv_sec*1000000 + end_init.tv_usec) - (start_init.tv_sec*1000000 + start_init.tv_usec));
struct timeval start_trans, end_trans;
gettimeofday(&start_trans, NULL);
pthread_create(&thread1, NULL, &producer, NULL);
pthread_create(&thread2, NULL, &consumer, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
gettimeofday(&end_trans, NULL);
printf("Time elapsed for transmission is: %ldn",
(long)(end_trans.tv_sec*1000000 + end_trans.tv_usec) - (start_trans.tv_sec*1000000 + start_trans.tv_usec));
return 0;
}
您需要重新访问您的锁定;你只能在每个制作人开始时获得锁。从那时起,只能解锁它们(重复!),这意味着不会发生进一步的同步。程序运行的时间越长,缺乏同步就越有可能使程序出错。
我认为你的锁应该是:
void* producer(void* arg)
{
int i;
for (i = 0; i<n; i++)
{
pthread_mutex_lock(&m);
while(getCount() >= maxSize)
{
pthread_mutex_unlock(&m); // allow consumer to acquire lock
pthread_mutex_lock(&m); // re-acquire lock
}
Insert(i);
pthread_mutex_lock(&m);
}
return NULL;
}
void* consumer(void* arg)
{
int i;
for (i=0; i<n; i++)
{
pthread_mutex_lock(&m);
while(getCount() <= 0)
{
pthread_mutex_unlock(&m); // allow producer to acquire lock
pthread_mutex_lock(&m); // re-acquire lock
}
Delete();
pthread_mutex_lock(&m);
}
return NULL;
}
请注意,所有操作,包括getCount
,都必须在获得锁的情况下完成。(还可以考虑用全局变量替换getCount
;