fork和exec没有产生正确的结果

时间:2013-09-24 21:06:09

标签: python c rss exec fork

我正在经历Head First C.几乎完成了!但是,我很厌倦这些练习不起作用。在关于进程和系统调用的章节中,我们下载了一个python程序,然后运行我们的代码,它将分叉和执行python三次,每次它应该在rss feed中搜索我们在命令行上写的短语。 rss feed是环境变量。当然,我从BBC选择了2,从StackOverflow选择了1。

这本书显示我们应该可以使用我们想要搜索的短语调用该程序,并在下一行显示所有匹配项。我一直没有得到任何结果或奇怪的结果和不可重复的结果。抱歉,包含不正确。

我从https://github.com/dogriffiths/rssgossip/zipball/master下载的python代码。

问题是代码,或Cygwin,或python还是只是我????

C程序:

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

int main(int argc, char *argv[]) {
    char *feeds[] = {"http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml",
                    "http://stackoverflow.com/feeds",
                    "http://feeds.bbci.co.uk/news/technology/rss.xml"};
    int times = 3;
    char *phrase = argv[1];
    int i;

    for (i=0; i<times; i++) {
        char var[255];
        sprintf(var, "RSS_FEED=%s", feeds[i]);
        char *vars[] = {var, NULL};

        //FORK() AND EXEC()
        pid_t pid = fork(); 
        if (pid == -1) {    
            fprintf(stderr, "Could not fork process: %s\n", strerror(errno));
            return 1;
        }
        if (!pid) {         // pid == 0 for child process
            if (execle("/usr/bin/python", "/usr/bin/python", "./rssgossip.py", phrase, NULL, vars) == -1) {
                fprintf(stderr, "Can't run script: %s\n", strerror(errno));
                return 1;
            }
        }
    }
}

以下所有结果均来自此程序的相同编译。饲料确实有时会改变,但我选择了在多个饲料中看到的单词,但只有一次我得到的结果不止一次。

网中的鲸鱼来自2行的这个rss条目,但它不是我的格式:

如何营救缠在蚊帐中的鲸鱼 加拿大队用渔网救出鲸鱼

所以它应该返回两行。

结果在$之后的那个是这样的:我在上面的行中输入了命令,它在一秒钟内没有做任何事情,然后在下一行给了我一个新的提示和结果。然后就会停止。我等了一会儿,看看它还在搜索。我没有意识到我可以输入一个新的命令,所以我ctrl-C'd。

Cygwin的:

XPS410 ~/c-bin
$ gcc newshound.c -o newshound

XPS410 ~/c-bin
$ ./newshound 'website'

XPS410 ~/c-bin
$ ./newshound 'mobile'

XPS410 ~/c-bin
$ jquery mobile button icons-36-white.png shows wrong icon
^C

XPS410 ~/c-bin
$ ./newshound 'mobile'

XPS410 ~/c-bin
$ ./newshound 'website'

XPS410 ~/c-bin
$ ./newshound 'the'

XPS410 ~/c-bin
$ AUDIO: Gunman's mother: 'My heart is broken'
China 'unblocks' Twitter and others
VIDEO: Exploring the violent world of GTA5
Make-It-Yourself: The rise of the micro-manufacturers
Digital Indians: The final Hangout
Could high-end camping gear save lives around the world?
"Unrecognized selector sent to instance", even though the instance is the right type
ASPNET MVC : Getting rid of apple-touch-icon errors : The controller for path Controller1/apple-touch-icon-precomposed.png was not found
Error deleting files with FineUploader when using more than one uploader in the same jsp
Javascript on click not showing the elements it should
Sum a column that corresponds a aggregate from a column of another table
Trying To Find The Cause Of Very Slow MySQL Query
How can I quickly find the first element in a list that matches a conditional?
Meteor.js: how to pass the data context of one helper to another helper?
Iam using javamail IAP but I dont know how to get Sent or another folder except inbox
^C

XPS410 ~/c-bin
$ ./newshound 'net'

XPS410 ~/c-bin
$ How to rescue whales tangled in nets
^C

XPS410 ~/c-bin
$ ^C

XPS410 ~/c-bin
$ ps
      PID    PPID    PGID     WINPID   TTY     UID    STIME COMMAND
      416       1     416        416  ?       1005   Sep 20 /usr/bin/mintty
     5852     416    5852       5664  pty0    1005   Sep 20 /usr/bin/bash
     5564    5852    5564        800  pty0    1005 13:31:44 /usr/bin/ps

XPS410 ~/c-bin
XPS410 ~/c-bin
$ ps
      PID    PPID    PGID     WINPID   TTY     UID    STIME COMMAND
     4348    5852    4348       5192  pty0    1005 13:31:52 /usr/bin/ps
      416       1     416        416  ?       1005   Sep 20 /usr/bin/mintty
     5852     416    5852       5664  pty0    1005   Sep 20 /usr/bin/bash
     2644       1    5856       1288  pty0    1005 13:31:51 /usr/bin/python2.6
     1384       1    5856        872  pty0    1005 13:31:51 /usr/bin/python2.6
     5864       1    5856       5344  pty0    1005 13:31:51 /usr/bin/python2.6

XPS410 ~/c-bin
$ How to rescue whales tangled in nets
ps
      PID    PPID    PGID     WINPID   TTY     UID    STIME COMMAND
     3980    5852    3980       4348  pty0    1005 13:32:56 /usr/bin/ps
      416       1     416        416  ?       1005   Sep 20 /usr/bin/mintty
     5852     416    5852       5664  pty0    1005   Sep 20 /usr/bin/bash

XPS410 ~/c-bin
$

1 个答案:

答案 0 :(得分:1)

我建议做的第一件事是检查python代码是否在外部工作。

这也应该让您了解程序的输入应该是什么。如果你迷路了,Python源代码也应该很容易理解。

如果它使用带有正确参数的命令行,那么你知道python程序不是问题。这应该可以让你专注于你实际来自问题的地方。