簡體   English   中英

我的程序似乎沒有終止; 為什么?

[英]My program doesn't seem to terminate; why?

我正在使用一個叫做rssgossip.py東西在我選擇的rss feed中搜索一個短語。

基本上,我要遍歷要搜索的rss feed數組,每次我分叉該進程並在子進程上調用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.

如您所見,最后沒有命令提示符,而我的終端只是坐在那里等待。 為什么? 在打印任何匹配的文章之前顯示命令提示符也很奇怪,不知道為什么會這樣。

您的程序已經終止,但是很難看到,因為分叉的子進程此后繼續產生輸出。

看一下筆錄的中間部分:

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

由於如果您的進程沒有子進程,由於wait返回-1,因此您可以循環調用它,直到這樣做為止:

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

(我從讓父級等待所有子進程中獲得了特定的公式。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM