为什么这里的子进程不打印任何东西?

发布于 2024-07-14 03:59:42 字数 1598 浏览 1 评论 0原文

假设所有变量都已在之前声明过...因为它们已经声明过。 子进程不会打印任何让我认为它没有被执行的内容。 父进程运行良好,尽管它没有获得共享内存。 对于这段代码的长度,我深表歉意......

// create 5 child process
for(int k=0;k<5;k++){

    // fork a child process
    pid = fork();

    // error occured on fork
    if (pid < 0) {
        fprintf(stderr, "Fork Failed");
        return 1;
    }
    // this is what the child process will run
    else if (pid == 0) {
        //create a shared mem segment
        segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);

        //attach the shared memory segment
        shared_memory = (char *) shmat(segment_id, NULL, 0);

        printf("this is child");

        double x = 0;
        double sum = 0;

        // Run process that sums the function
        for(int i=0; i<n; i++){
            // get random number in range of x1-x2
            x = rand()%(x2 - x1 + 1) + x1;
            sum = sum + f(x);
        }

        //write output to the shared memory segment
        sprintf(shared_memory, "%f", sum);
        execlp("/bin/ls", "ls", NULL);

     }

    // this is what the parent process will run
    else {

       //print output from shared memory
        printf("\n*%s", shared_memory);

        //detach shared memory
        shmdt(shared_memory);

        //Here we add the shared memory to the array
        // To add together at the end
        // but since I cant get the memory to share
        // the array can't be implemented

        //remove the shared memory segment
        shmctl(segment_id, IPC_RMID, NULL);

        wait(NULL);
    }
} // End of for statement

Assume all the variables have been declared previously... because they have been. The child process does not print anything which makes me think it's not being executed. The parent process runs fine, albeit it doesn't get the shared memory.
I apologize for the length of this code...

// create 5 child process
for(int k=0;k<5;k++){

    // fork a child process
    pid = fork();

    // error occured on fork
    if (pid < 0) {
        fprintf(stderr, "Fork Failed");
        return 1;
    }
    // this is what the child process will run
    else if (pid == 0) {
        //create a shared mem segment
        segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);

        //attach the shared memory segment
        shared_memory = (char *) shmat(segment_id, NULL, 0);

        printf("this is child");

        double x = 0;
        double sum = 0;

        // Run process that sums the function
        for(int i=0; i<n; i++){
            // get random number in range of x1-x2
            x = rand()%(x2 - x1 + 1) + x1;
            sum = sum + f(x);
        }

        //write output to the shared memory segment
        sprintf(shared_memory, "%f", sum);
        execlp("/bin/ls", "ls", NULL);

     }

    // this is what the parent process will run
    else {

       //print output from shared memory
        printf("\n*%s", shared_memory);

        //detach shared memory
        shmdt(shared_memory);

        //Here we add the shared memory to the array
        // To add together at the end
        // but since I cant get the memory to share
        // the array can't be implemented

        //remove the shared memory segment
        shmctl(segment_id, IPC_RMID, NULL);

        wait(NULL);
    }
} // End of for statement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尝蛊 2024-07-21 03:59:42

C stdout 流在内部缓冲数据。 很可能您的“这是子”消息正在被缓冲,并且缓冲区没有被 execlp 刷新,所以它就消失了。 在 printf 之后尝试 fflush(stdout); 。 顺便说一句,您也应该在 fork() 之前执行此操作,这样子进程和父进程就不会都尝试写入在 fork 之前缓冲的输出。

The C stdout stream internally buffers data. It's likely that your "this is child" message is being buffered, and the buffer isn't being flushed by execlp, so it just disappears. Try a fflush(stdout); after the printf. Incidentally, you should do this before the fork() as well, so that child and parent don't both try to write output buffered from before the fork.

魄砕の薆 2024-07-21 03:59:42

打印到 stderr 它没有被缓冲。

fprintf(stderr,"Plop\n");

此外,共享内存内容(segment_id、shared_memory)未在父级中初始化(或者如果是,则与子级不同)。

此外,当子进程仍在处理时,父进程可能会破坏共享内存。 父级应先等待,然后处理子级产生的数据。

Print to stderr it is not buffered.

fprintf(stderr,"Plop\n");

Also the shared memory stuff (segment_id, shared_memory) is not initialized in the parent (or if it is then it is not the same as the child).

Furthermore the parent is potentially destroying the shared memory stuff while the child is still processing. The parent should wait first then process the data produced by the child.

强者自强 2024-07-21 03:59:42

首先清除所有共享内存的东西,然后看看子进程是否可以成功 printf。

从该代码片段来看,您正在子进程中初始化共享内存,但不是在父进程中初始化。

Get rid of all the shared memory stuff first and see if the child process can printf successfully then.

By the look of that snippet, you are initializing shared_memory in the child process, but not in the parent process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文