c语言中锁的作用,C语言中的多线程死锁

我是C的新手,在下面的多线程场景中,N个线程从一个二进制文件中读取,并写入自己的单独文件,例如线程1写入文件1,线程2写入文件2,等等。

这对~2/3个线程有效,但对于其他线程,它似乎陷入了死锁,但我不确定问题的根源是什么。

干杯!:)

#include "tape.h"

#include

#include

#include

#include

#define BYTE unsigned char

int present_file = 0;

pthread_t THREADS[100];

int OFFSET[100];

FILE * file;

pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t finished_read_mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t file_read = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;

pthread_cond_t finished_read_cond = PTHREAD_COND_INITIALIZER;

int read = 0;

int finished_reading = 0;

int read_value;

size_t file_length;

int n_threads = 0;

void* tape_reader(void* args) {

int offset = *((int *) args);

FILE * file_ptr;

if(present_file == 1){

file_ptr = fopen("head1", "wb");

}

else if (present_file == 2){

file_ptr = fopen("head2", "wb");

}

else if (present_file == 3){

file_ptr = fopen("head3", "wb");

}

else if (present_file == 4){

file_ptr = fopen("head4", "wb");

}

else if (present_file == 5){

file_ptr = fopen("head5", "wb");

}

else if (present_file == 6){

file_ptr = fopen("head6", "wb");

}

else if (present_file == 7){

file_ptr = fopen("head7", "wb");

}

else if (present_file == 8){

file_ptr = fopen("head8", "wb");

}

else if (present_file == 9){

file_ptr = fopen("head9", "wb");

}

else if (present_file == 10){

file_ptr = fopen("head10", "wb");

}

else{

file_ptr = NULL;

}

while(read == 0){

pthread_cond_wait(&read_cond, &read_mutex);

}

//thread start reading

pthread_mutex_lock(&file_read);

BYTE buffer;

rewind(file);

fseek(file, offset, SEEK_SET);

for(int i = 0; i < read_value; i++){

if(i == file_length){

i = 0;

}

fread(&buffer, 1, sizeof(buffer), file);

fwrite(&buffer, 1, sizeof(buffer), file_ptr);

}

fclose(file_ptr);

pthread_mutex_unlock(&file_read);

return 0;

}

int main(int argc, char** argv) {

if(argc == 1){

printf("Tape Not Inserted\n");

return 1;

}

else if(argc == 2){

file = fopen(argv[1], "rb");

if(file == NULL){

printf("Cannot Read Tape\n");

return 1;

}

else{

fseek(file, 0, SEEK_END);

file_length = ftell(file);

rewind(file);

while(1){

char userInput[100];

int offset;

scanf("%s", userInput);

if(strcmp(userInput, "QUIT") == 0){

return 0;

}

//printf("%s\n", userInput);

if(strcmp(userInput, "HEAD") == 0){

scanf("%d", &offset);

pthread_create(THREADS + present_file, NULL, tape_reader, (void *) &offset);

n_threads++;

present_file++;

if(offset >= 0){

printf("HEAD %d at +%d\n", present_file, offset);

printf("\n");

}

else{

printf("HEAD %d at %d\n", present_file, offset);

printf("\n");

}

}

if(strcmp(userInput, "READ") == 0){

int read_for;

scanf("%d", &read_for);

read_value = read_for;

read = 1;

pthread_cond_signal(&read_cond);

//threads read

printf("Finished Reading\n\n");

for(int i = 0; i < n_threads; i++){

pthread_join(THREADS[i], NULL);

}

return 0;

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值