C 我的程序没有';似乎没有终止;为什么?

C 我的程序没有';似乎没有终止;为什么?,c,terminal,rss,fork,C,Terminal,Rss,Fork,我正在使用名为rssgossip.py的东西在我选择的rss提要中搜索短语 基本上,我是在迭代一系列我想要搜索的rss提要,每次我都在分岔进程并对子进程调用execle()。我确实得到了适当的输出,但它看起来很奇怪,然后我的终端就在那里等待所有的东西都打印出来 代码 #include <stdio.h> #include <errno.h> #include <unistd.h> #include <string.h>

我正在使用名为
rssgossip.py
的东西在我选择的rss提要中搜索短语

基本上,我是在迭代一系列我想要搜索的rss提要,每次我都在分岔进程并对子进程调用
execle()
。我确实得到了适当的输出,但它看起来很奇怪,然后我的终端就在那里等待所有的东西都打印出来

代码

    #include <stdio.h>
    #include <errno.h>
    #include <unistd.h>
    #include <string.h>

    int main(int argc, char *argv[]) {
        char *feeds[] = {"http://feeds.washingtonpost.com/rss/lifestyle",
                     "http://feeds.washingtonpost.com/rss/world",
                     "http://feeds.washingtonpost.com/rss/opinions"};

        int num_feeds = 3;
        if(argc < 2) {
            fprintf(stderr, "You need to tell me a search phrase.\n");
            return 1;
        }
        char *phrase = argv[1];  // this will be the phrase you to search for in the rss feed, passed as an argument
    printf("we are going to search for \"%s\"\n", phrase);
    int i;
    for(i = 0; i < num_feeds; i ++) {
        char var[255];
        sprintf(var, "RSS_FEED=%s", feeds[i]);
        printf("we are going to search this feed: %s\n", var);
        char *my_env[] = {var, NULL};
        // I believe that once we call execle, the while loop stops because we've totally replaced the process! (we need fork...)
        pid_t pid = fork();
        printf("pid: %d\n", pid);
        if(pid == -1) {  // -1 indicates that fork() had a problem cloning the process
            fprintf(stderr, "Can't fork process: %s\n", strerror(errno));
            return 1;
        }
        if(pid == 0) {  // isn't a non-zero number for the parent process?? NO, this is like pid==0, i.e. child process
            printf("running a child process now\n");
            if(execle("/usr/bin/python", "/usr/bin/python",
                    "./rssgossip/rssgossip.py", phrase, NULL, my_env) == -1) {
                fprintf(stderr, "Can't run script: %s\n", strerror(errno));
                return 1;
            }
        }
    }

    return 0;
}

正如您所看到的,末尾没有命令提示符,而我的终端只是坐在那里等待。为什么?在打印任何匹配的文章之前出现命令提示符似乎也很奇怪,但也不确定为什么会出现这种情况。

您的程序已经终止,但很难看到这一点,因为分叉的子进程随后继续生成输出

看看你成绩单的中间部分:

running a child process now
aarons-MacBook-Pro:ch9 aaronparisi$ U.S. general in Afghanistan apologizes for highly offensive leaflets

您可以看到,您的shell在输出的中间显示了一个提示,在原始程序结束时,但子进程仍在运行。 如果在所有输出完成后点击[ENTER],您将看到您的shell实际上正在侦听,并且您将立即得到另一个shell提示

如果希望程序在所有子进程完成后才退出,则需要使用wait(2)等待它们完成:

因为如果进程没有子进程,wait返回-1,所以您可以在循环中调用它,直到它这样做:

int status = 0;
while ((wpid = wait(&status)) > 0);

(我从中得到了具体的公式。)

有问题,它说等待需要整数*而不是整数?@A.Pizzle查看我的扩展答案,了解如何使用等待函数。当我说“wait(2)”时,这是说“函数wait()”的标准方式,这在手册的第2节中有记录。我不确定这个特定的术语是否被认为是标准的,很抱歉混淆了。具体来说,int*参数是指向int的指针,其中wait()将放置已退出的子级的退出状态(子级的pid将由wait()返回)。不客气!请在我的答案上单击“接受”,以便网站知道它是正确的答案!:-)
wait()
是最简单的操作;您也可以使用
waitpid()
waitid()
,但后者并不是到处都可以实现的(例如,在macOS Sierra 10.12.6中没有
waitid()
)。
int status = 0;
while ((wpid = wait(&status)) > 0);