c - 为什么程序永远不会超过 open()?

标签 c multithreading

我正在使用多线程计算目录中所有文件的各个校验和,然后计算所有校验和的总和(即校验和之和)。 (是的,如果这个概念听起来很熟悉,我之前确实发布了一个关于使用 pthread_create 的问题,但我向你保证这不是重复的。)

我有代码计算给定 argv[1] 的一个文件的校验和。在该程序中,校验和计算正确。

但是,现在我正在使用多线程实现多个文件校验和计算,并且永远不会打印单个校验和。我已经调试并盯着看了几个小时,但我仍然对为什么从不打印单个校验和以及为什么我无法通过该行感到困惑: handle = open(filenames[b], O_RDONLY); .

下面是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>

#define BUFFER_SIZE (1<<16)

void cleanup();
void get_filenames();
void* get_checksum();

char **filenames;
int file_cnt;
DIR *dir;

int handle;
unsigned char* ptr;
int length;
int count;
unsigned char* buffer;
int* sum;
unsigned int total = 0;

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

        int i;
        pthread_t* file;

        atexit(cleanup);
        get_filenames();

        printf("There are %d files:\n", file_cnt);

        file = calloc(file_cnt, sizeof(pthread_t));
        sum = calloc(file_cnt, sizeof(int));
        for(i=0; i<file_cnt; i++){

                printf("%s\n", filenames[i]);

                pthread_create(&(file[i]), NULL, get_checksum, (void*)i);
        }
                for(i=0; i<file_cnt; i++){
                        total += sum[i];
                }
                printf("total is: %u\n", total);
        return EXIT_SUCCESS;
}

void* get_checksum(void* a){

    unsigned int checksum;
        int b = (int)a;

        printf("b=%d\n", b);

        handle = open(filenames[b], O_RDONLY);

         printf("handle=%d\n", handle);

        if( handle == -1 ){
                  printf( "Can't open file: %s\n", filenames[b]);
                  exit(1);
        }

        buffer = malloc(BUFFER_SIZE);
        if( buffer == NULL ){
                printf( "Can't get enough memory\n" );
                exit(1);
        }
        checksum = 0;

        printf("length1=%d\n", length);
                 do{
                        length = read(handle, buffer, BUFFER_SIZE);
            printf("length=%d\n", length);
                         if(length == -1){
                                printf( "Error reading file: %s\n", filenames[b]);
                exit(1);
                              }

                        ptr = buffer;
                        count = length;

                        while( count-- ){
                                checksum = checksum + (unsigned int)( *ptr++ );
                                sum[b] = checksum;
                        }
                }while(length);
                    printf("Checksum = %d\n", checksum);
}

void cleanup() {

        if(filenames && file_cnt > 0) {
                while(file_cnt-- > 0) {
                        if(filenames[file_cnt]) {
                                free(filenames[file_cnt]);
                        }
                }
                free(filenames);
        }

        if(dir) {
                closedir(dir);
        }

        return;
}


void get_filenames() {

        struct dirent *dir_entry;

        if((dir = opendir(".")) == NULL) {
                fprintf(stderr, "Couldn't open the directory entry for reading\n");
                exit(1);
        }

        errno = 0;
        file_cnt = 0;
        while((dir_entry = readdir(dir)) != NULL) {
                char **new_filenames = filenames;
                static int realative_dirs = 0;

                if(realative_dirs < 2 &&
                   (strcmp(".", dir_entry->d_name) == 0 || strcmp("..", dir_entry->d_name) == 0)
                  ) {
                        realative_dirs++;
                        continue;
                }

                new_filenames = (char **)realloc(filenames, sizeof(char **) * (file_cnt + 1));
                if(new_filenames == NULL) {
                        free(filenames[file_cnt]);
                        fprintf(stderr, "Could not allocate reference for filename[%d]\n", file_cnt);
                        exit(1);
                }

                filenames = new_filenames;
                filenames[file_cnt] = (char *)calloc(strlen(dir_entry->d_name) + 1, sizeof(char));
                if(filenames[file_cnt] == NULL) {
                        fprintf(stderr, "Could not allocate memory for filename[%d]'s string: \"%s\"\n",
                                file_cnt, dir_entry->d_name);
                        exit(1);
                }

                strcpy(filenames[file_cnt], dir_entry->d_name);
                file_cnt++;
        }

        if(errno != 0) {
                fprintf(stderr, "An error occured getting the filenam list\n");
                exit(1);
        }

        return;
}

我的输出:

    There are 7 files:
    test.c
    a.out
    test.c.save
    test2.c
    b=2
    test3.c
    checksum.c
    b=0
    b=1
    b=3
    checksum1.c
    b=5
    total is: 0

正如您从输出中看到的那样,我无法打印 handle 的值或 handle = open(filenames[b], O_RDONLY); 之后的任何内容。我是编程新手,所以非常感谢任何关于为什么我不能越过那条线的建议。 (请注意,我是通过虚拟机运行 Linux,所以目标是 Linux)

最佳答案

您的代码似乎还有其他问题,但我只会解决为什么它似乎在 open() 之后终止。

open 之后它实际上并没有终止:你永远不会等待你的线程被加入。创建线程后,主线程退出。您需要使用pthread_join 来等待通过pthread_create 创建的线程终止。

您似乎有一些线程在第一个 pthread_create 和实际循环(在 main 中)终止之间执行,但这只是偶然。

关于c - 为什么程序永远不会超过 open()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36267581/

相关文章:

Java:使所有字段成为最终字段或可变字段?

c++ - 为什么原子操作需要独占缓存访问?

python - 这是线程的正确操作流程吗?

c - eventbuffer 只能读取 8 个字节的输入?

c - 从函数返回的值与接收到的值不同。为什么?

c - 字符串存在,但未打印

c# - 等价于 C# 中的 yield

c - UDP客户端未收到消息

c# - 在 WebAPI 中保存后返回文件名

multithreading - C# : UDP Listener Thread