0

我正在经历 Head First C。几乎完成了!但是,呃,我厌倦了这些练习不起作用。在“进程和系统调用”一章中,我们下载了一个 Python 程序,然后运行我们的代码,它将 fork 并执行 Python 三次,每次它都应该在 rss 提要中搜索我们在命令行中编写的短语。RSS 提要是环境变量。当然,我从 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;
            }
        }
    }
}

以下所有结果均来自该程序的相同编译。提要有时确实会发生变化,但我选择了在多个提要中看到的单词,但只有一次我得到了多个结果。

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

如何拯救被网缠住的鲸鱼 加拿大队从渔网中救出鲸鱼

所以它应该返回两行。

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

赛格温:

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
$
4

1 回答 1

1

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

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

如果它使用正确的参数从命令行运行,那么您就知道 python 程序不是问题所在。这应该可以让你专注于你的问题实际上来自哪里。

于 2013-09-25T13:40:25.113 回答