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

时间:2016-03-28 17:36:47

标签: 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)

1 个答案:

答案 0 :(得分:2)

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

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

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