Linux下C语言问题 pipe问题(急)

hopepark 2009-11-09 06:21:43
小弟最近才开始学Linux下的C语言变成,还是只菜鸟。请问我下面的代码有什么问题啊?
小弟想写一个在主函数先输入N个单词,然后主函数生成N个子进程。然后父函数通过pipe将单词传给子进程,是子进程输出单词,具体想这么输出:
./a.out aa bbb cccc d
然后输入上面命令后,电脑输出:
2345 : aa
2356 : bbb
2378 : cccc
2379 : d
但是下面的代码运行后却是这样输出的:
root@localhost usp_ch06]# gcc parentwritepipe.c
[root@localhost usp_ch06]# .a.out aaa bb vv
[3076] aaa
[3076] bb
[3076] vv
[root@localhost usp_ch06]# [3077] aaa
q
bash q command not found
[root@localhost usp_ch06]# .a.out aaa bb vv
[3079] aaa
[3079] bb
[3079] vv
[root@localhost usp_ch06]# [3080] aaa
q
bash q command not found
[root@localhost usp_ch06]#
请问这是哪里有问题啊,下面是代码,这个代码是我从书上一个例题改编的,问题应该很多,请大家指教
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#define BUFSIZE 10

int main(int argc,char *argv[])
{
int i=1;
int bytesin;
pid_t childpid;
int fd[2];
if (argc < 2)
{
fprintf(stderr,"Usage: %s command arg1 arg2 ...\n",argv[0]);
return 1;
}
if (pipe(fd) == -1)
{
perror("Failed to create the pipe");
return 1;
}
bytesin = strlen(argv[1]);
childpid = fork();
if (childpid == -1)
{
perror("Failed to fork");
return 1;
}
while(argv[i] != NULL)
{
if (childpid) /* parent code */
write(fd[1], argv[i], strlen(argv[i])+1);
else /* child code */
bytesin = read(fd[0], argv[i], BUFSIZE);
fprintf(stderr, "[%ld]: %.*s\n",
(long)getpid(), bytesin, argv[i]);
i=i++;
}
return 0;
}
...全文
201 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hopepark 2009-11-09
  • 打赏
  • 举报
回复
哦,谢谢了,又学到了很多
do_fork 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hopepark 的回复:]
引用 4 楼 do_fork 的回复:
是不是要类似功能?

C/C++ code#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>#define MAX_PROC 10#define BUFSIZE 10#define E_EXIT(msg) do{fprintf(stderr, msg); return 1;}while(0)int main(int argc,char*argv[])
{if (argc-- <2)
        E_EXIT("Usage: command arg1 arg2 ...\n");

    pid_t childpid[MAX_PROC];int w, i, fd[MAX_PROC][2];char buf[BUFSIZE];for (i=0; i <argc; i++)if (pipe(fd[i])==-1)
            E_EXIT("Failed to create the pipe");for (i=0; i <argc; i++) {
        childpid[i]= fork();if (childpid[i]==-1)
            E_EXIT("Failed to fork");if (childpid[i]>0)
            close(fd[i][0]);else {
            close(fd[i][1]);
            read(fd[i][0], buf, BUFSIZE);
            printf("%d : %s\n", getpid(), buf);
            exit(0);
        }
    }for (i=0; i <argc; i++)
        write(fd[i][1], argv[i+1], strlen(argv[i+1])+1);for (i=0; i <argc; i++)
        wait(&w);return0;
}

恩,对的,就是这功能,真是感激不尽啊
请问你最下面这里是什么意思啊?
for (i=0; i <argc; i++)
        wait(&w);
这句怎么理解呢?
[/Quote]

父进程等待子进程结束
hopepark 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 do_fork 的回复:]
是不是要类似功能?

C/C++ code#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>#define MAX_PROC 10#define BUFSIZE 10#define E_EXIT(msg) do{fprintf(stderr, msg); return 1;}while(0)int main(int argc,char*argv[])
{if (argc--<2)
E_EXIT("Usage: command arg1 arg2 ...\n");

pid_t childpid[MAX_PROC];int w, i, fd[MAX_PROC][2];char buf[BUFSIZE];for (i=0; i<argc; i++)if (pipe(fd[i])==-1)
E_EXIT("Failed to create the pipe");for (i=0; i<argc; i++) {
childpid[i]= fork();if (childpid[i]==-1)
E_EXIT("Failed to fork");if (childpid[i]>0)
close(fd[i][0]);else {
close(fd[i][1]);
read(fd[i][0], buf, BUFSIZE);
printf("%d : %s\n", getpid(), buf);
exit(0);
}
}for (i=0; i<argc; i++)
write(fd[i][1], argv[i+1], strlen(argv[i+1])+1);for (i=0; i<argc; i++)
wait(&w);return0;
}
[/Quote]
恩,对的,就是这功能,真是感激不尽啊
请问你最下面这里是什么意思啊?
for (i=0; i<argc; i++)
wait(&w);
这句怎么理解呢?
do_fork 2009-11-09
  • 打赏
  • 举报
回复
是不是要类似功能?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

#define MAX_PROC 10
#define BUFSIZE 10

#define E_EXIT(msg) do{fprintf(stderr, msg); return 1;}while(0)

int main(int argc,char *argv[])
{
if (argc-- < 2)
E_EXIT("Usage: command arg1 arg2 ...\n");

pid_t childpid[MAX_PROC];
int w, i, fd[MAX_PROC][2];
char buf[BUFSIZE];

for (i=0; i<argc; i++)
if (pipe(fd[i]) == -1)
E_EXIT("Failed to create the pipe");

for (i=0; i<argc; i++) {
childpid[i] = fork();
if (childpid[i] == -1)
E_EXIT("Failed to fork");
if (childpid[i]>0)
close(fd[i][0]);
else {
close(fd[i][1]);
read(fd[i][0], buf, BUFSIZE);
printf("%d : %s\n", getpid(), buf);
exit(0);
}
}

for (i=0; i<argc; i++)
write(fd[i][1], argv[i+1], strlen(argv[i+1])+1);

for (i=0; i<argc; i++)
wait(&w);

return 0;
}
hopepark 2009-11-09
  • 打赏
  • 举报
回复
恩,但是视乎问题还不是出在这个子进程上
wangqingshui531 2009-11-09
  • 打赏
  • 举报
回复
你只fork了一个子进程,不是要fork出N个吗?
do_fork 2009-11-09
  • 打赏
  • 举报
回复
An application that uses pipe(2) and fork(2) should use suitable close(2) calls to close
unnecessary duplicate file descriptors; this ensures that end-of-file and SIGPIPE/EPIPE
are delivered when appropriate.

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧