当前位置: 技术问答>linux和unix
两道简单的程序,那位帮忙在linux下运行一下结果,谢谢啦,给分20。
来源: 互联网 发布时间:2015-01-22
本文导语: 一: #include main() { int status; int tochild[2]; char *msg="what is this program doing"; char *argv[]={"/usr/bin/wc","-w",null}; pipe(tochild); if ( fork()==0 ) { close(tochild[1]); dup2(tochild[0],0); ...
一:
#include
main()
{
int status;
int tochild[2];
char *msg="what is this program doing";
char *argv[]={"/usr/bin/wc","-w",null};
pipe(tochild);
if ( fork()==0 )
{
close(tochild[1]);
dup2(tochild[0],0);
close(tochild[0]);
execv(argv[0],argv);
exit(1);
}
close(tochild[0]);
write(tochild[1],msg,strlen(msg));
close(tochild[1]);
wait(&status);
}
二:
#include
main()
{
int status;
char *argv[]={"/usr/bin/wc","-1","execWc.c",null};
if (fork()==0) {
execv(argv[0],argv);
exit(1);
}
wait(&status);
}
呵呵,有些地方搞不明白啊,
char *argv[]={"/usr/bin/wc","-1","execWc.c",null};
char *argv[]={"/usr/bin/wc","-w",null};
这两行代码的完整表达形式是什么呢,-l和-w这两个参数是什么意思?
第一个运行后标准输出答案是“1 5 22” 还是 “1 5 26”?
linux下和unix下的结果会不同吗?
第二个呢,运行结果是什么啊,呵呵。
谢谢啦。
#include
main()
{
int status;
int tochild[2];
char *msg="what is this program doing";
char *argv[]={"/usr/bin/wc","-w",null};
pipe(tochild);
if ( fork()==0 )
{
close(tochild[1]);
dup2(tochild[0],0);
close(tochild[0]);
execv(argv[0],argv);
exit(1);
}
close(tochild[0]);
write(tochild[1],msg,strlen(msg));
close(tochild[1]);
wait(&status);
}
二:
#include
main()
{
int status;
char *argv[]={"/usr/bin/wc","-1","execWc.c",null};
if (fork()==0) {
execv(argv[0],argv);
exit(1);
}
wait(&status);
}
呵呵,有些地方搞不明白啊,
char *argv[]={"/usr/bin/wc","-1","execWc.c",null};
char *argv[]={"/usr/bin/wc","-w",null};
这两行代码的完整表达形式是什么呢,-l和-w这两个参数是什么意思?
第一个运行后标准输出答案是“1 5 22” 还是 “1 5 26”?
linux下和unix下的结果会不同吗?
第二个呢,运行结果是什么啊,呵呵。
谢谢啦。
|
a.c: In function `main':
a.c:7: `null' undeclared (first use in this function)
a.c:7: (Each undeclared identifier is reported only once
a.c:7: for each function it appears in.)
a.c:7: `null' undeclared (first use this function)
a.c:7: (Each undeclared identifier is reported only once for each
function it appears in.)
a.c:9: `pipe' undeclared (first use this function)
a.c:11: `fork' undeclared (first use this function)
a.c:13: `close' undeclared (first use this function)
a.c:14: `dup2' undeclared (first use this function)
a.c:17: `execv' undeclared (first use this function)
a.c:18: `exit' undeclared (first use this function)
a.c:22: `strlen' undeclared (first use this function)
a.c:22: `write' undeclared (first use this function)
a.c:25: `wait' undeclared (first use this function)
a.c:7: `null' undeclared (first use in this function)
a.c:7: (Each undeclared identifier is reported only once
a.c:7: for each function it appears in.)
a.c:7: `null' undeclared (first use this function)
a.c:7: (Each undeclared identifier is reported only once for each
function it appears in.)
a.c:9: `pipe' undeclared (first use this function)
a.c:11: `fork' undeclared (first use this function)
a.c:13: `close' undeclared (first use this function)
a.c:14: `dup2' undeclared (first use this function)
a.c:17: `execv' undeclared (first use this function)
a.c:18: `exit' undeclared (first use this function)
a.c:22: `strlen' undeclared (first use this function)
a.c:22: `write' undeclared (first use this function)
a.c:25: `wait' undeclared (first use this function)
|
看清楚了, 我不得不佩服你的错字能力
#include
main()
{
int status;
char *argv[]={"/usr/bin/wc","-l","execWc.c", NULL};
if (fork()==0) {
execv(argv[0],argv);
exit(1);
}
wait(&status);
}
[la0wang@GateWay la0wang]$ gcc -o execWc execWc.c
[la0wang@GateWay la0wang]$ ./execWc
12 execWc.c
[la0wang@GateWay la0wang]$
说明:
wc 是 *nix下一个计算文件大小, 行数等等的一个应用程序, -l(注意不是-1) 参数的意思是计算execWc.c 文件的行数
argv[]的意思你既然明白了, 我也懒的说了, 不过写程序还是推荐要严谨一些
#include
main()
{
int status;
char *argv[]={"/usr/bin/wc","-l","execWc.c", NULL};
if (fork()==0) {
execv(argv[0],argv);
exit(1);
}
wait(&status);
}
[la0wang@GateWay la0wang]$ gcc -o execWc execWc.c
[la0wang@GateWay la0wang]$ ./execWc
12 execWc.c
[la0wang@GateWay la0wang]$
说明:
wc 是 *nix下一个计算文件大小, 行数等等的一个应用程序, -l(注意不是-1) 参数的意思是计算execWc.c 文件的行数
argv[]的意思你既然明白了, 我也懒的说了, 不过写程序还是推荐要严谨一些