不知道为什么fgets之后会立即执行子进程

卦星 2013-01-28 11:10:07
这是比较简单程序代码,但是就是感觉输出结果有问题,希望有人帮忙解决一下
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 #include<sys/types.h>
5 #include<unistd.h>
6 #include<sys/stat.h>
7 int main()
8 {
9 pid_t pid;
10 int pipe_fd[2];
11 char buf[100];
12 int num;
13 memset(buf,0,100);
14 if(pipe(pipe_fd)<0)
15 {
16 printf("creat pipe error;\n");
17 exit(0);
18 }
19 if((pid=fork())==0)
20 {
21 sleep(1);
22 waitpid(pid,NULL,0);
23 close(pipe_fd[1]);
24 printf("now we will read the file\n");
25 num=read(pipe_fd[0],buf,sizeof(buf));
26 if(num!=1)
27 {
28 printf("read file is :%s\n",buf);
29 }
30 else
31 {
32 printf("read error\n");
33 exit(0);
34 }
35 exit(0);
36 }
37 else
38 {
39 close(pipe_fd[0]);
40 printf("now we will write some files\n");
41 printf("please input:");
42 fgets(buf,100,stdin);
43 printf("\n");
44 num=write(pipe_fd[1],buf,100);
45 if(num!=-1)
46 printf("write good %s\n",buf);
47 else
48 {
49 printf("error");
50 exit(0);
51 }
52 exit(0);
53 }
54 }
下面是输出的结果
[……@localhost new]$ ./2
now we will write some files
please input:now we will read the file(这儿为什么直接跳到了子进程)
asd(这是输入的)

write good asd

[……@localhost new]$ read file is :asd(输出怎么直接在这一行输出了)
度娘没找到结果,不知道标准输入输出有什么具体的要求,请大神知道,是不是我的管道进程设置有错
新手没有积分,请见谅
...全文
94 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
卦星 2013-01-29
  • 打赏
  • 举报
回复
引用 7 楼 AnYidan 的回复:
C/C++ code?1234567891011if ((pid = fork()) < 0) {err_sys("fork error");} else if (pid == 0) { /* child */execlp(buf, buf, (char *)0);err_ret("couldn't execute: %s", buf);exit(127);}/* par……
这个函数让我见到了新的东西,应该学习,谢谢了
FancyMouse 2013-01-28
  • 打赏
  • 举报
回复
父进程已经退出了,所以命令提示符出来了。但是子进程还在运行(sleep中),所以会在命令提示符出来以后再打印输出。程序本身运行是正确的。
AnYidan 2013-01-28
  • 打赏
  • 举报
回复

if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* child */
execlp(buf, buf, (char *)0);
err_ret("couldn't execute: %s", buf);
exit(127);
}
/* parent */
if ((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
卦星 2013-01-28
  • 打赏
  • 举报
回复
引用 4 楼 FancyMouse 的回复:
引用 2 楼 zhy025907 的回复:引用 1 楼 FancyMouse 的回复:父进程已经退出了,所以命令提示符出来了。但是子进程还在运行(sleep中),所以会在命令提示符出来以后再打印输出。程序本身运行是正确的。 我把最后的一个exit(0)去掉,仍然运行不对,求解,关键是不明的是 please input:now we will read the file……
谢谢了,我刚刚学习一直半解,试试去获取父进程的pid然后,阻塞试试,慢慢一点点的实验
卦星 2013-01-28
  • 打赏
  • 举报
回复
引用 3 楼 looooooooop 的回复:
你这代码写的, 仔细看下吧 (1) fork 该怎么用? pid = fork() == 0 这个判断, 说明if 是子进程 else 分支里的代码才是父进程 (2) 你在子进程中 wait 一个pid = 0 的进程? 查下手册看看api怎么用, 不难吧?
谢谢了,刚刚学习总是一知半解
FancyMouse 2013-01-28
  • 打赏
  • 举报
回复
引用 2 楼 zhy025907 的回复:
引用 1 楼 FancyMouse 的回复:父进程已经退出了,所以命令提示符出来了。但是子进程还在运行(sleep中),所以会在命令提示符出来以后再打印输出。程序本身运行是正确的。 我把最后的一个exit(0)去掉,仍然运行不对,求解,关键是不明的是 please input:now we will read the file(这儿为什么直接跳到了子进程) fget……
不关exit(0)的事。看3L。你父进程是一条路执行到底,子进程先sleep然后waitpid(虽然没有wait任何东西)然后继续下面的。
looooooooop 2013-01-28
  • 打赏
  • 举报
回复
你这代码写的, 仔细看下吧 (1) fork 该怎么用? pid = fork() == 0 这个判断, 说明if 是子进程 else 分支里的代码才是父进程 (2) 你在子进程中 wait 一个pid = 0 的进程? 查下手册看看api怎么用, 不难吧?
卦星 2013-01-28
  • 打赏
  • 举报
回复
引用 1 楼 FancyMouse 的回复:
父进程已经退出了,所以命令提示符出来了。但是子进程还在运行(sleep中),所以会在命令提示符出来以后再打印输出。程序本身运行是正确的。
我把最后的一个exit(0)去掉,仍然运行不对,求解,关键是不明的是 please input:now we will read the file(这儿为什么直接跳到了子进程) fgets之后为什么直接跳到了子进程,我已经把子进程阻塞了

69,382

社区成员

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

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