gpt4 book ai didi

c - 运行程序时出现段错误和变化的输出

转载 作者:行者123 更新时间:2023-11-30 15:02:38 26 4
gpt4 key购买 nike

我有一个程序,应该使用多个线程将一个文件的内容精确复制到另一个文件。读取器线程从文件中读取一行并将其存储在循环缓冲区中。然后写入器线程从缓冲区读取并写入文件。但是我遇到了段错误,并且它没有写入文件。知道为什么我会遇到段错误吗?或者有什么方法可以找出导致错误的原因吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

FILE *inputFile;
FILE *outputFile;

pthread_mutex_t mutex;

int endOfFile = 0;

typedef struct bufferStruct{
int capacity;
int size;
int head;
int tail;
char **data;
}buffer;

buffer * bufferInit(int maxElements){
buffer *buf;
buf = (buffer *)malloc(sizeof(buffer));

buf->data = (char**)malloc(sizeof(char*)*maxElements);
buf->size = 0;
buf->capacity = maxElements;
buf->head = 0;
buf->tail = -1;

return buf;
}

void popFront(buffer *buf){
if(buf->size != 0){
free(buf->data);
buf->size--;
buf->head++;
if(buf->head == buf->capacity){
buf->head = 0;
}
}
return;
}

char* front(buffer *buf){
if(buf->size != 0){
return buf->data[buf->head];
}

return NULL;
}

void pushBack(buffer *buf, char *data){
if(buf->size == buf->capacity){
printf("Queue is Full\n");
}

else{
buf->size++;
buf->tail = buf->tail + 1;

if(buf->tail == buf->capacity){
buf->tail = 0;
}

buf->data[buf->tail] = (char *) malloc((sizeof data + 1)* sizeof(char));

strcpy(buf->data[buf->tail], data);
}
return;
}

buffer *buf;

void* reader(void* arg){
char line[1024];
while(endOfFile != 1){
fgets(line, sizeof(line), inputFile);
printf("Line read: %s", line);

pushBack(buf, line);

if(feof(inputFile)){
endOfFile = 1;
}
}
pthread_exit(0);
}

void* writer(void* arg){
char *line;
while(endOfFile != 1){
pthread_mutex_lock(&mutex);
line = front(buf);
fputs(line, outputFile);
popFront(buf);
pthread_mutex_unlock(&mutex);
}
pthread_exit(0);
}

int main(int argc, char **argv){
if (argc < 4) {
printf("Usage: %s <input file> <output file> <number>\n", argv[0]);
exit(-1);
}

inputFile = fopen(argv[1], "r");
outputFile = fopen(argv[2], "w");
int numOfThreads = atoi(argv[3]);

buf = bufferInit(16);

pthread_t readerTids[numOfThreads];
pthread_t writerTids[numOfThreads];

pthread_mutex_init(&mutex, NULL);

for(int i = 0; i < numOfThreads; i++){
if(endOfFile != 1){
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&readerTids[i], &attr, reader, NULL);
pthread_create(&writerTids[i], &attr, writer, NULL);

printf("Thread %d created\n", i);
}
}

for (int i = 0; i < numOfThreads; i++) {
pthread_join(readerTids[i], NULL);
pthread_join(writerTids[i], NULL);
}

fclose(inputFile);
fclose(outputFile);
}

最佳答案

考虑您的读取器线程可能比写入器线程慢。编写者线程独自持有锁,进行锁定和解锁,而不关心读者。如果当读取器尚未更新缓冲区时写入器尝试使用缓冲区怎么办?使用线程同步,例如信号量,它不存在任何所有权问题。

void* reader(void* arg){
char line[1024];
while(endOfFile != 1){
fgets(line, sizeof(line), inputFile);
printf("Line read: %s", line);

pushBack(buf, line);

--- Lock semaphore here---

if(feof(inputFile)){
endOfFile = 1;
}
}
pthread_exit(0);
}


void* writer(void* arg){
char *line;
while(endOfFile != 1){

-- Unlock semaphore here---

line = front(buf);
fputs(line, outputFile);
popFront(buf);
}
pthread_exit(0);
}

与互斥锁不同,两个线程之间可以使用相同的信号量。这可以帮助您同步两个线程。

关于c - 运行程序时出现段错误和变化的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com