2017-09-06 10 views
1

私は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. 

あなたが見ることができるように、最後に何のコマンドプロンプトが存在しない、と私の端末はただ待ってそこに座っています。どうして?一致する記事のいずれかを印刷する前にコマンドプロンプトが表示されるのは奇妙に思えますが、その理由もわかりません。

答えて

2

あなたのプログラムはすでに終了していますが、フォークされた子プロセスがその後に出力を生成し続けるため、プログラムは終了していません。

あなたの転写産物の真ん中を見てみましょう。

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

あなたは、元のプログラムが終了したときに、あなたのシェルがポイントで、あなたの出力の途中でプロンプトを表示していることを確認することができますが、子プロセス間まだ実行中です。

すべての出力が完了した後に[ENTER]を押すだけで、実際にシェルがリスニングしていることがわかり、すぐに別のシェルプロンプトが表示されます。

あなたはすべての子プロセスが完了した後、あなたのプログラムではありません出口が、あなたは彼らが終了するのを(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からその特定の式を得た。)

+0

ことに問題がある、それは待ち時間がintを取ると言う*:プロセスが子を持たない、それはそうするまで、あなただけのループでそれを呼び出すことができますintではない? –

+0

@ A.Pizzle wait関数の使い方については、私の拡張答えを見てください。私が "wait(2)"と言うとき、それは "function wait()"と言う標準的な方法です。これはマンページのセクション2に記載されています。私はその特定の専門用語がSOの標準と見なされるかどうかは分かりません。混乱して申し訳ありません。 int *引数は、wait()が終了した子の終了ステータスを格納するintへのポインタです(子のpidはwait()によって返されます)。 –

+0

ああ、それは働いて、ありがとう! –

関連する問題