简体   繁体   English

使用fork()后,父进程中的无限循环不会更新屏幕

[英]Infinite cycle in the parent process does not update the screen after using fork()

I need to write UNIX appplication that creates 2 processes using fork(), both of these processes prints current time in different positions on the screen. 我需要编写UNIX应用程序,该应用程序使用fork()创建2个进程,这两个进程都将当前时间打印在屏幕上的不同位置。 Parent process must stop its work after the child process is over. 子进程结束后,父进程必须停止其工作。 I've written this code: 我写了这段代码:

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

struct Point2
{
    int X;
    int Y;
};

int kbhit()
{
    //getch() implementation
}

void printTime(const struct Point2 pt, const char* st)
{
    char buf[255];
    time_t rawtime;
    struct tm * timeinfo;

    time (&rawtime);
    timeinfo = localtime(&rawtime);
    sprintf(buf, "%s: %02d:%02d:%02d", st, timeinfo->tm_hour,
            timeinfo->tm_min, timeinfo->tm_sec);
    mvaddstr(pt.Y, pt.X, buf);
    refresh();
}

void childp(pid_t pid, const struct Point2 pt)
{
    while(kbhit() != 27)
    {
        printTime(pt, "CHLD");
        usleep(1000);
    }
}

void parentp(pid_t pid, const struct Point2 pt)
{
    struct Point2 newp = {pt.X, pt.Y + 1};
    while(1)
    {
        int stat;
        waitpid(pid, &stat, WNOHANG);
        if(WIFEXITED(stat) != 0)
            break;

        printTime(newp, "PARN");
            usleep(1000);
    }
}

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        printf("unable to load XY position for clock.\n");
        return 1;
    }

    initscr();
    refresh(); // <-- this refresh

    struct Point2 opt;
    opt.X = atoi(argv[1]);
    opt.Y = atoi(argv[2]);

    pid_t pid;
    pid = fork();

    switch(pid)
    {
        case -1:
            printw("Error");
            _exit(0);
        case 0:
            childp(pid, opt);
            break;
        default:
        parentp(pid, opt);
            break;

    }
    endwin();
    return 0;
}

Once the program is started, it outputs once "CHLD" and "PARN" time, and then correctly updates "CHLD" time from the child proccess, but the output from the parent process does not change. 程序启动后,将输出一次“ CHLD”和“ PARN”时间,然后从子进程中正确更新“ CHLD”时间,但父进程的输出不会更改。 Moreover, if I comment the refresh() call in main() "PARN" time string does not show up at all. 此外,如果我评论main()“ PARN”时间字符串中的refresh()调用根本不显示。 So my question is: why the parent process does not updates the screen? 所以我的问题是:为什么父进程不更新屏幕?

upd. 更新。 I have deleted almost all code in parent function's cycle, now it looks like: 我已经删除了父函数周期中的几乎所有代码,现在看起来像:

void parentp(pid_t pid, const struct Point2 pt)
{
    struct Point2 newp = {pt.X, pt.Y + 1};
    while(1)
    {
        printTime(newp, "PARN");
    }
}

but it still not working 但它仍然无法正常工作

waitpid(pid, &stat, WNOHANG); worked correctly. 工作正常。 I don't know why, but the problem was in mvaddstr(); 我不知道为什么,但是问题出在mvaddstr(); I changed it to printw() and voila, it worked! 我将其更改为printw()和voila,它起作用了! Maybe that's happening because mvaddstr(); 可能是因为mvaddstr(); does not updates the whole screen. 不会更新整个屏幕。 Another way is to draw PARN and CHLD time before calling fork(), but I think this is neither simple nor elegant solution. 另一种方法是在调用fork()之前绘制PARN和CHLD时间,但是我认为这既不简单也不优雅。 Used printw(). 使用了printw().

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

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