【问题标题】:My program doesn't seem to terminate; why?我的程序似乎没有终止;为什么?
【发布时间】:2018-02-15 10:42:45
【问题描述】:

我正在使用名为 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;
}

输出

aarons-MacBook-Pro:ch9 aaronparisi$ ./news hi
we are going to search for "hi"
we are going to search this feed: RSS_FEED=http://feeds.washingtonpost.com/rss/lifestyle
pid: 68853
we are going to search this feed: RSS_FEED=http://feeds.washingtonpost.com/rss/world
pid: 68854
we are going to search this feed: RSS_FEED=http://feeds.washingtonpost.com/rss/opinions
pid: 0
running a child process now
pid: 0
running a child process now
pid: 68855
pid: 0
running a child process now
aarons-MacBook-Pro:ch9 aaronparisi$ U.S. general in Afghanistan apologizes for highly offensive leaflets
How Burma's Rohingya crisis went from bad to worse
Sir Richard Branson is riding out Hurricane Irma in the wine cellar on his private island
Britains royal family announces third pregnancy for Duke and Duchess of Cambridge
Fashion is finally figuring out diversity  in ways that actually matter
The Salt Line is an instant hit, with superb seafood and a view to match
The 2017 Washington Post Travel photo contest winners and finalists
Ask Amy: New hire struggles with workplace racism
Hints From Heloise: Kitchen creativity
Miss Manners: Helping a young child deflect questions
A laughing matter
History shows us how calamitous the North Korea crisis could become
These Washington players didnt just stick to their college major
The only thing less fair than the electoral college is the scoring in tennis
With DACA, Jeff Sessions bent Trump to his will - again
Washington Post's Scott Wilson is out as national editor
Who first said, 'The best government is that which governs least'? Not Thoreau.

如您所见,最后没有命令提示符,我的终端只是坐在那里等待。为什么?在打印任何匹配的文章之前出现命令提示符似乎也很奇怪,也不知道为什么会发生这种情况。

【问题讨论】:

    标签: c terminal rss fork


    【解决方案1】:

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

    看看你的成绩单中间:

    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) 等待它们完成:http://man7.org/linux/man-pages/man2/waitpid.2.html

    如果您的进程没有子进程,则等待返回 -1,因此您可以在循环中调用它,直到它这样做为止:

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

    (我从Make parent wait for all child processes 得到了那个特定的公式。)

    【讨论】:

    • 遇到问题,它说等待需要一个 int* 而不是 int?
    • @A.Pizzle 请参阅我的扩展答案,了解如何使用等待功能。当我说“wait(2)”时,这是一种标准的说法,即“函数 wait(),它记录在手册页的第 2 节中。”我不确定那个特定的术语是否被认为是 SO 的标准,很抱歉造成混淆。具体来说,int* 参数是一个指向 int 的指针,wait() 将放置已退出的子进程的退出状态(子进程的 pid 将由 wait() 返回。)
    • 不客气!请点击我的回答上的“接受”,以便网站知道这是正确的! :-)
    • wait() 是最简单的操作;您也可以使用waitpid()waitid(),尽管后者并未在任何地方实现(例如,macOS Sierra 10.12.6 中没有waitid())。
    猜你喜欢
    • 2016-07-09
    • 1970-01-01
    • 2023-03-07
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    相关资源
    最近更新 更多