fork 后 main 中的间歇性分段错误

Intermittent segmentation faults in main after fork

我正在 class 上大学 linux 学习如何学习多进程程序编程。 我仍然很新,正在尽我最大的努力学习,所以任何你可能看到的错误都会受到欢迎。

我有一个问题要求我迭代一个数组,主进程的一半, 另一半在子进程上。 我写了这样做的代码,但问题是,我注意到如果我 运行 是二进制文件的几倍,主(父)进程有时会出现分段错误。

请查看代码,并告诉我它有什么问题,或者我是否遗漏了此类编程的一个关键方面。 我的回答在评论之后开始 //answer starts here.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>

#define ARRAY_SIZE 1000
int main()
{
    int numbers[ARRAY_SIZE]; /* array to lookup */
    int n;                   /* the number to find */
    time_t t;                /* needed to initialize random number generator (RNG) */
    int i;

    /* intializes RNG (srand():stdlib.h; time(): time.h) */
    srand((unsigned)time(&t));

    /* initialize array with random numbers (rand(): stdlib.h) */
    for (i = 0; i < ARRAY_SIZE; i++)
        numbers[i] = rand() % 10000;

    /* initialize n */
    n = rand() % 10000;


    //answer starts here
    int half = n / 2;
    int count = 0;
    int pid_status = 0;
    pid_t pid_f = fork();
    if (pid_f == -1)
    {
        return EXIT_FAILURE;
    }
    else
    {
        if (pid_f == 0) // child process iterates half end of the array
        {
            for (i = half; i < ARRAY_SIZE; ++i)
            {
                if (numbers[i] == n)
                {
                    count++;
                }
            }
            printf("Sons counter:%d\n", count);
            exit(count);
        } //else it's the father process
        else
        {
            for (i = 0; i < half; ++i)
            {
                if (numbers[i] == n)
                {
                    count++;
                }
            }
            waitpid(pid_f, &pid_status, 0);
            printf("Father counter:%d\n", count);
            if (WIFEXITED(pid_status))
            {
                count += WEXITSTATUS(pid_status);
            }
            printf("Sum is=%d\n", count);
        }
    }
    return 0;
}

分段错误是由于n有时越界造成的:

   n = rand() % 10000;


    //answer starts here
    int half = n / 2;

一半可以是5000个,但是数字只有1000个元素。

也许你的意思是:

 n = rand() % 1000;

这是您的代码的固定版本。初始化一半到一半ARRAY_SIZE,而不是n.

正如您在代码中所说:int n; /* the number to find */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>

#define ARRAY_SIZE 1000

int main() {

    int numbers[ARRAY_SIZE]; /* array to lookup */
    int n;                   /* the number to find */
    time_t t;                /* needed to initialize random number generator (RNG) */
    int i;

    srand((unsigned)time(&t));

    for (i = 0; i < ARRAY_SIZE; i++) {
        numbers[i] = rand() % 10000;
    }

    /* initialize n */
    n = rand() % 10000;

    int half = ARRAY_SIZE / 2;
    int count = 0;
    int pid_status = 0;
    pid_t pid_f = fork();

    if (pid_f == -1) {
        return EXIT_FAILURE;
    } else {
        if (pid_f == 0) {
            for (i = half; i < ARRAY_SIZE; ++i) {
                if (numbers[i] == n) {
                    count++;
                }
            }
            printf("Sons counter:%d\n", count);
            exit(count);
        } else {
            for (i = 0; i < half; ++i) {
                if (numbers[i] == n) {
                    count++;
                }
            }
            waitpid(pid_f, &pid_status, 0);
            printf("Father counter:%d\n", count);
            if (WIFEXITED(pid_status)) {
                count += WEXITSTATUS(pid_status);
            }
            printf("Sum is=%d\n", count);
        }
    }
    return 0;
}