2017-09-06 3 views
1

Ich benutze etwas namens rssgossip.py, um nach einer Phrase in RSS-Feeds meiner Wahl zu suchen.Mein Programm scheint nicht zu beenden; Warum?

Grundsätzlich bin ich durch eine Reihe von RSS-Feeds iterieren, die ich suchen möchte, und jedes Mal, wenn ich den Prozess Forking bin und execle() auf dem Kind-Prozess aufrufen. Ich bekomme die passende Ausgabe, aber es sieht komisch aus und dann sitzt mein Terminal einfach da und wartet, bis alles gedruckt ist.

-Code

#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; 
} 

Ausgabe

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. 

Wie Sie sehen können, gibt es keine Eingabeaufforderung am Ende, und mein Terminal wartet nur dort sitzen. Warum? Es scheint auch seltsam, dass die Eingabeaufforderung angezeigt wird, bevor Sie einen der übereinstimmenden Artikel drucken, nicht sicher, warum dies auch geschieht.

Antwort

2

Ihr Programm ist bereits beendet, aber es ist schwer zu erkennen, weil die gefork- ten Kindprozesse weiterhin die Ausgabe produzieren.

Blick auf der Mitte des Transkripts:

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

Sie können sehen, dass die Shell eine Aufforderung in der Mitte des Ausgangs an dem Punkt, angezeigt hat, wenn das ursprüngliche Programm beendet, aber während der Kindprozesse laufen noch.

Wenn Sie [ENTER] drücken, nachdem alle Ausgaben beendet sind, sehen Sie, dass Ihre Shell tatsächlich abhört und Sie sofort eine weitere Shell-Eingabeaufforderung erhalten.

Wenn Sie möchten, dass Ihr Programm nicht beendet, bis schließlich die Kindprozesse fertig sind, müssen Sie warten (2) verwenden, um zu warten, bis sie fertig: http://man7.org/linux/man-pages/man2/waitpid.2.html

Da Warte -1 zurück, wenn Ihr Prozess keine Kinder hat, können Sie es einfach in einer Schleife aufrufen, bis es so tut:

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

(I aus Make parent wait for all child processes, dass spezifische Formel bekam.)

+0

damit Probleme zu haben, heißt es warten ein int nimmt * kein Int? –

+0

@ A.Pizzle Siehe meine erweiterte Antwort für die Verwendung der Wartefunktion. Wenn ich "wait (2)" sage, ist das eine übliche Art, "die Funktion wait()" zu sagen, die in Abschnitt 2 der Hilfeseiten dokumentiert ist. " Ich bin mir nicht sicher, ob dieser spezifische Jargon als Standard für SO gilt oder nicht, Entschuldigung für die Verwirrung. Speziell das Argument int * ist ein Zeiger auf ein int, wo wait() den Exit-Status des Kindes, das beendet hat, setzen wird (und die PID des Kindes wird von wait() zurückgegeben werden.) –

+0

Ah ok; habe es funktioniert, vielen Dank! –

Verwandte Themen