即使循环是无限的(但是孩子们),但在wait()ernain之前的陈述也被执行。这是为什么?

发布于 2025-01-22 07:02:04 字数 2000 浏览 1 评论 0原文

#include<stdlib.h>
#include<stdio.h>
  
  
int main()
{
    int j = 0;
    int jump = 1;
    while(1)
    {
        pid_t pid[4];
        for (int i = 0; i < 4; i++)
        {
            if (fork() == 0)
            {
                switch(i)
                {
                    case 0:
                        pid[i] = getpid(); 
                        while(1) {//printf("Parent Process: %d\nChild Process 1: %d\n", getppid(), getpid());
                         sleep(5);}
                        break;
                    case 1:
                        pid[i] = getpid(); 
                        while(1) {//printf("Child Process 2: %d\n", getpid());
                         sleep(5);}
                        break;
                    case 2:
                        pid[i] = getpid(); 
                        while(1) {//printf("Child Process 3: %d\n", getpid());
                         sleep(5);}
                        break;
                    case 3:
                        pid[i] = getpid(); 
                        while(1) {printf("Child Process 4: %d and pid[%d] is %d\n", getpid(), i, pid[i]);
                         sleep(5);}
                        break;
                    default:
                     printf("Something went wrong..");
                }
            }
        }
        printf("why doesn't this print");
        //pid_t killedPid =
        wait(NULL);
        //printf("Child %d Died", killedPid);
        //for (int i = 0; i < 4; i++)
        //{
            //printf("\nTesting against%d: %d\n", i, pid[i]);
            //if (killedPid == pid[i])
            //{
               // j = i;
               // printf("\n\nProcess %d (Child %d) Killed. Recreating Child.\n\n", pid[i], j);
            //}
        //}
        //sleep(4);
        //jump = 4;
    }
    return 0;
}

抱歉,如果代码或问题格式很混乱(我是C的新手,这是我第一次在此处发布)。

在wait()之前,就直接打印语句,如果我删除wait(),则可以。我知道孩子循环是无限的,但是从我收集的内容来看,父母应正常执行等待之前的语句。

#include<stdlib.h>
#include<stdio.h>
  
  
int main()
{
    int j = 0;
    int jump = 1;
    while(1)
    {
        pid_t pid[4];
        for (int i = 0; i < 4; i++)
        {
            if (fork() == 0)
            {
                switch(i)
                {
                    case 0:
                        pid[i] = getpid(); 
                        while(1) {//printf("Parent Process: %d\nChild Process 1: %d\n", getppid(), getpid());
                         sleep(5);}
                        break;
                    case 1:
                        pid[i] = getpid(); 
                        while(1) {//printf("Child Process 2: %d\n", getpid());
                         sleep(5);}
                        break;
                    case 2:
                        pid[i] = getpid(); 
                        while(1) {//printf("Child Process 3: %d\n", getpid());
                         sleep(5);}
                        break;
                    case 3:
                        pid[i] = getpid(); 
                        while(1) {printf("Child Process 4: %d and pid[%d] is %d\n", getpid(), i, pid[i]);
                         sleep(5);}
                        break;
                    default:
                     printf("Something went wrong..");
                }
            }
        }
        printf("why doesn't this print");
        //pid_t killedPid =
        wait(NULL);
        //printf("Child %d Died", killedPid);
        //for (int i = 0; i < 4; i++)
        //{
            //printf("\nTesting against%d: %d\n", i, pid[i]);
            //if (killedPid == pid[i])
            //{
               // j = i;
               // printf("\n\nProcess %d (Child %d) Killed. Recreating Child.\n\n", pid[i], j);
            //}
        //}
        //sleep(4);
        //jump = 4;
    }
    return 0;
}

Sorry if the code or question format is messy (I'm new to C and it's my first time posting here).

The print statement right before wait() does not print, if I remove wait() it does. I know the child loop is infinite, but from what I've gathered, statements before wait should be executed normally by the parent process.

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

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

发布评论

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

评论(1

提赋 2025-01-29 07:02:04

我本人不是专家,但我的印象是什么都没有印刷,因为文字没有被冲洗到stdout上。在某些编程语言中,当您打印某些内容时,文本不是直接打印的,而是保存在内存中(在某些情况下也称为缓冲区)。这是出于绩效原因。有时,要一次打印大量文本以比多次打印少量文本的表现要多。

当您“冲洗”文本时,您将其从缓冲区中删除并将其打印到Stdout。通常是在过程结束之前或缓冲区完整时自动完成的。如果我没记错的话,您也可以通过打印换行线“ \ n”来冲洗文本,因为(我不是100%确定这是实际原因,我在这里猜测)按线路和这样的方式,程序不必等待填充缓冲区或完成输入的过程。

问题在于,文本被存储在内存中,但由于没有被冲洗而未打印。尝试以下内容:

  1. 在文本
  2. 打印一个非常长的字符串之后,打印一个新线“ \ n”,该字符串不适合缓冲区。

如果我在这两种情况下都正确,则应在屏幕上显示文本。您还可以使用评论部分建议的fflush()函数@pmg,以明确刷新文本。

I'm not myself an expert, but I have the impression that nothing is being printed because the text is not being flushed to stdout. In some programming languages when you print something, the text is not directly printed but instead it is saved in memory (also known as buffer in some contexts like this one). This is for performance reasons. Sometimes it is more performant to print lots of text to stdout at once than it would be to print small amounts of texts many times.

When you "flush" the text you are removing it from the buffer and printing it to stdout. This is usually done automatically for instance before the process ends or when the buffer is full. If I recall correctly you can also flush the text by printing a line break "\n" because (I am not 100% sure if this is the actual reason, I'm speculating here) in some systems like linux some programs process input line by line and this way the program doesn't have to wait the buffer to be filled or the process finished to start working on the input.

The problem is then that the text is being stored in memory but not printed because it is not being flushed. Try the following:

  1. Printing a newline "\n" after the text
  2. Printing a really long string that wouldn't fit in the buffer.

If I'm right in both cases the text should be displayed in screen. You can also use the fflush() function @pmg suggested in the comment section to explicitly flush the text.

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