一:
#include
1<stdio.h>
2main()
3{
4int status;
5int tochild[2];
6char *msg="what is this program doing";
7char *argv[]={"/usr/bin/wc","-w",null};
8
9pipe(tochild);
10
11if ( fork()==0 )
12{
13close(tochild[1]);
14dup2(tochild[0],0);
15close(tochild[0]);
16
17execv(argv[0],argv);
18exit(1);
19}
20
21close(tochild[0]);
22write(tochild[1],msg,strlen(msg));
23close(tochild[1]);
24
25wait(&status);
26}
27
28二:
29
30#include <stodio.h>
31main()
32{
33int status;
34char *argv[]={"/usr/bin/wc","-1","execWc.c",null};
35if (fork()==0) {
36execv(argv[0],argv);
37exit(1);
38}
39wait(&status);
40}
41
42呵呵,有些地方搞不明白啊,
43char *argv[]={"/usr/bin/wc","-1","execWc.c",null};
44char *argv[]={"/usr/bin/wc","-w",null};
45这两行代码的完整表达形式是什么呢,-l和-w这两个参数是什么意思?
46
47第一个运行后标准输出答案是“1 5 22” 还是 “1 5 26”?
48linux下和unix下的结果会不同吗?
49
50第二个呢,运行结果是什么啊,呵呵。
51
52谢谢啦。
53
54\---------------------------------------------------------------
55
56a.c: In function `main':
57a.c:7: `null' undeclared (first use in this function)
58a.c:7: (Each undeclared identifier is reported only once
59a.c:7: for each function it appears in.)
60
61
62
63a.c:7: `null' undeclared (first use this function)
64a.c:7: (Each undeclared identifier is reported only once for each
65function it appears in.)
66a.c:9: `pipe' undeclared (first use this function)
67a.c:11: `fork' undeclared (first use this function)
68a.c:13: `close' undeclared (first use this function)
69a.c:14: `dup2' undeclared (first use this function)
70a.c:17: `execv' undeclared (first use this function)
71a.c:18: `exit' undeclared (first use this function)
72a.c:22: `strlen' undeclared (first use this function)
73a.c:22: `write' undeclared (first use this function)
74a.c:25: `wait' undeclared (first use this function)
75\---------------------------------------------------------------
76
77看清楚了, 我不得不佩服你的错字能力
78
79#include <stdio.h>
80main()
81{
82int status;
83char *argv[]={"/usr/bin/wc","-l","execWc.c", NULL};
84if (fork()==0) {
85execv(argv[0],argv);
86exit(1);
87}
88wait(&status);
89}
90
91[la0wang@GateWay la0wang]$ gcc -o execWc execWc.c
92[la0wang@GateWay la0wang]$ ./execWc
9312 execWc.c
94[la0wang@GateWay la0wang]$
95
96说明:
97
98wc 是 *nix下一个计算文件大小, 行数等等的一个应用程序, -l(注意不是-1) 参数的意思是计算execWc.c 文件的行数
99argv[]的意思你既然明白了, 我也懒的说了, 不过写程序还是推荐要严谨一些</stdio.h></stodio.h></stdio.h>