主叉后的间歇分段故障

问题描述 投票:3回答:2

我正在上一节课,学习如何在大学的Linux上学习编程多程序。我仍然非常环保并且尽力学习,所以你可能会看到任何错误的东西都会受到欢迎。

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

请查看代码,并告诉我它有什么问题,或者我是否缺少此类编程的关键方面。我的回答在评论//答案从这里开始后开始。

#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;
}
c linux system-calls
2个回答
5
投票

分段错误是由于n有时超出界限引起的:

   n = rand() % 10000;


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

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

也许你的意思是:

 n = rand() % 1000;

1
投票

这是您的代码的固定版本。初始化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;
}
© www.soinside.com 2019 - 2024. All rights reserved.