简体   繁体   English

C:pthread segfaults

[英]C: pthread segfaults

I am writing my first C program using pthread. 我正在使用pthread编写我的第一个C程序。 I want to run my function sha1sum for all (non-option) arguments provided on the commadline (argv). 我想为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++;
}

When run my program with 2 commandline arguments ( file1 , file2 ). 使用2个命令行参数( file1file2 )运行我的程序时。 it segfaults, sometimes right away, sometimes at the end after having successfully executed the sha1sum function. 在成功执行sha1sum函数之后,有时会立即进行段错误,有时会在最后进行。

Can somebody please point out what is wrong? 有人可以指出出了什么问题吗?

EDIT 编辑

Turns out, the program is still sometimes segfaulting. 事实证明,该计划有时仍然是segfaulting。 Sometimes right away, sometimes in the middle, sometimes never: 有时候,有时在中间,有时从不:

I am posting the whole MCV example below: 我在下面发布了整个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;
}

EDIT 2 编辑2

after adding #include <pthread.h> , I now get following errors/warnings when compiling: 在添加#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 *)’

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

I made some small changes to your code. 我对您的代码做了一些小改动。 Now it doesn't crash. 现在它不会崩溃。

#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;
}

Test 测试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM