简体   繁体   English

主 function 中的 printf 在 C 中返回两次

[英]printf in main function returning twice in C

I am running such a weird problem in C.我在 C 中遇到了这样一个奇怪的问题。 Here is my code:这是我的代码:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdbool.h>
                                                                        
#define NUM_OF_CORES 1
#define MAX_PRIME 100000
                                                                        
unsigned long do_primes()
{
    unsigned long i, num, primes = 0;
    for (num = 1; num <= MAX_PRIME; ++num) {
        for (i = 2; (i <= num) && (num % i != 0); ++i);
        if (i == num)
            ++primes;
    }
    //printf("Calculated %ld primes.\n", primes);
    return primes;
}
                                                                        
int main(int argc, char ** argv)
{
    //printf ("TEST <CPUBURN_PRIME TEST>\n");
    time_t start, end;
    time_t run_time;
    int i;
    static unsigned long prime_count = 0;
    pid_t pids[NUM_OF_CORES];
                                                                        
    /* start of test */
    start = time(NULL);
    for (i = 0; i < NUM_OF_CORES; i++) {
        if (!(pids[i] = fork())) {
            prime_count = do_primes();
            printf("Calculated %ld primes.\n", prime_count);
            exit(0);
        }
        if (pids[i] < 0) {
            perror("Fork");
            exit(1);
        }
    }
    for (i = 0; i < NUM_OF_CORES; ++i) {
        waitpid(pids[i], NULL, 0);
    }
    end = time(NULL);
    run_time = (end - start);
    
    printf("<TEST:CPUBURN_PRIME TEST><%d--%ld--%ld>\n", MAX_PRIME, prime_count, run_time);
    
    return 0;
}

The output is: output 是:

Calculated 9592 primes.计算出 9592 个素数。 <TEST:CPUBURN_PRIME TEST><100000--0--3> <测试:CPUBURN_PRIME 测试><100000--0--3>

The problem is the last printf is returing twice.问题是最后一个 printf 正在返回两次。 Therefore I am loosing the value of the variable prime_count.因此我失去了变量 prime_count 的值。 What wrong that I am doing?我做错了什么?

prime_count would not be updated since it's not changed in the parent process. prime_count不会更新,因为它在父进程中没有更改。 Only the fork -d child process updates its own copy of prime_count .只有fork -d 子进程更新它自己的prime_count副本。 You would need to establish some form of Inter-Process communication between the two processes so that the parent can read back the value of prime_count from the child.您需要在两个进程之间建立某种形式的进程间通信,以便父进程可以从子进程中读回prime_count的值。 One of the (many) ways could be to use pipes as follows (this is not production level code ) (许多)方法之一可能是如下使用pipes (这不是生产级代码

if (!(pids[i] = fork())) {
    close(pipes[i][0]);
    prime_count = do_primes();
    printf("%d Calculated %ld primes.\n",getpid(), prime_count);
    char buff[100] = {0};
    sprintf(buff, "%ld", prime_count);
    write(pipes[i][1], buff, strlen(buff));
    close(pipes[i][1]);
    exit(0);
} else if(pids[i] > 0) {
    close(pipes[i][1]);
    char buff[100];
    read(pipes[i][0], buff, sizeof(buff));
    printf("Read num primes %s\n", buff);
    prime_count = strtol(buff, NULL, 10);
    close(pipes[i][0]);
} else {
    perror("Fork");
    exit(1);
}

This is just an example and might suffice when NUM_CORES is 1. You'll need to adapt it to read asynchronously from the pipe since read is a blocking call by default and the parent process won't get to proceed with the subsequent loop iterations in case the read is blocked.这只是一个示例,当NUM_CORES为 1 时可能就足够了。您需要调整它以从 pipe 异步读取,因为默认情况下read是一个阻塞调用,并且父进程将无法继续进行后续循环迭代如果读取被阻止。

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

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