C:pthread segfaults

时间:2016-12-26 14:36:25

标签: c pthreads

我正在使用pthread编写我的第一个C程序。我想为commadline(argv)上提供的所有(非选项)参数运行我的函数sha1sum

pthread_t t[256];

c = 0;
for (n = optind; n < argc; n++) {
    if(pthread_create(&t[c], NULL, sha1sum, argv[n])) {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }
    c++;
}

c = 0;
for (n = optind; n < argc; n++) {
    pthread_join(t[c]);
    c++;
}

使用2个命令行参数(file1file2)运行我的程序时。在成功执行sha1sum函数之后,有时会立即进行段错误,有时会在最后进行。

有人可以指出出了什么问题吗?

修改

事实证明,该程序有时仍然是segfaulting。有时候,有时在中间,有时从不:

我在下面发布了整个MCV示例:

#include <stdio.h>
#include <getopt.h>
#include <unistd.h>
#include <locale.h>
#include <pthread.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <openssl/evp.h>    

static unsigned char flag = 0;


int sha1sum(char *filename) {

    FILE *f;
    size_t len;
    unsigned char buffer[BUFSIZ]; 

    EVP_MD_CTX hashctx;
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    const EVP_MD *hashptr = EVP_get_digestbyname("SHA1");

        f = fopen(filename, "r");

        EVP_MD_CTX_init(&hashctx);
        EVP_DigestInit_ex(&hashctx, hashptr, NULL);

        do {
                len = fread(buffer, 1, BUFSIZ, f);
                EVP_DigestUpdate(&hashctx, buffer, len);
        } while (len == BUFSIZ);

        unsigned int outlen;
        EVP_DigestFinal_ex(&hashctx, buffer, &outlen);
        EVP_MD_CTX_cleanup(&hashctx);

        int i;
        for (i = 0; i < outlen; i++)
                printf("%02x", buffer[i]);

        printf("\n");
        fclose(f);

    pthread_exit(NULL);
}


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

    int c,n;
    pthread_t t[256];

    while ((c = getopt(argc, argv, "c")) != EOF) switch(c) {
        case 'c':
            flag |= 1;
            break;
    }

    c = 0;
    for (n = optind; n < argc; n++) {

        if (pthread_create(&t[c], NULL, &sha1sum, argv[n])) {
            fprintf(stderr, "Error creating thread\n");
            return 1;
        }
        c++;
    }

    c = 0;
    for (n = optind; n < argc; n++) {
        pthread_join(t[c], NULL);
        c++;
    }

    return 0;
}

编辑2

添加#include <pthread.h>后,我现在在编译时遇到以下错误/警告:

test.c: In function ‘main’:
test.c:67:9: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
In file included from test.c:9:0:
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘int (*)(char *)’

2 个答案:

答案 0 :(得分:3)

pthread_join(t[c])更改为pthread_join(t[c], NULL)

答案 1 :(得分:2)

我对您的代码做了一些小改动。现在它不会崩溃。

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

void *sha1sum(void) {
    char *a = malloc(10);
    strcpy(a, "hello world\n");
    pthread_exit((void *) a);
}

int main(int argc, char **argv) {
    pthread_t t[256];
    int n = 0;
    int c = 0;
    char *b;
    for (n = optind; n < argc; n++) {
        if (pthread_create(&t[c], NULL, &sha1sum, argv[n])) {
            fprintf(stderr, "Error creating thread\n");
            return 1;
        }
        c++;
    }
    c = 0;
    for (n = optind; n < argc; n++) {
        pthread_join(t[c], (void **) &b);
        printf("b is %s", b);
        c++;
    }
    return 0;
}

测试

./a.out foo bar baz bletch
b is hello world
b is hello world
b is hello world
b is hello world