当前位置: 技术问答>linux和unix
为什么书中的参考程序老编译不过?
来源: 互联网 发布时间:2015-11-25
本文导语: 一个管道通信的程序,参考程序如下: #include #include #include int p1,p2; main() { int fd[2]; char OutPipe[100],InPipe[100]; pipe(fd); while ((p1==fork())==-1);/*创建子进程P1*/ if (p1==0) { lockf(fd[1],1,...
一个管道通信的程序,参考程序如下:
#include
#include
#include
int p1,p2;
main()
{
int fd[2];
char OutPipe[100],InPipe[100];
pipe(fd);
while ((p1==fork())==-1);/*创建子进程P1*/
if (p1==0)
{
lockf(fd[1],1,0);
sprintf(OutPipe,”child 1 process is sending message!”);
write(fd[1],OutPipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else
{
while ((p2==fork())==-1);/*创建另一个子进程P2*/
if (p2==0)
{
lockf(fd[1],1,0);
sprintf(OutPipe,”child 2 process is sending message!”);
write(fd[1],OutPipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else
{
wait(0);
read(fd[0],InPipe,50);
printf(“%sn”,InPipe);
wait(0);
read(fd[0],InPipe,50);
printf(“%sn”,InPipe);
xit(0);
}
}
}
在RED hat下用GCC编译确出现多处错误,真的烦躁!错误如下:
[root@linux 实验]# gcc -o file file.c
file.c:5: stray '302' in program
file.c:5: stray '240' in program
file.c: In function `main':
file.c:11: stray '357' in program
file.c:11: stray '274' in program
file.c:11: stray '233' in program
file.c:15: stray '342' in program
file.c:15: stray '200' in program
file.c:15: stray '235' in program
file.c:15: `child' undeclared (first use in this function)
file.c:15: (Each undeclared identifier is reported only once
file.c:15: for each function it appears in.)
file.c:15: parse error before numeric constant
file.c:15: stray '342' in program
file.c:15: stray '200' in program
file.c:15: stray '235' in program
file.c:23: stray '357' in program
file.c:23: stray '274' in program
file.c:23: stray '233' in program
file.c:27: stray '342' in program
file.c:27: stray '200' in program
file.c:27: stray '235' in program
file.c:27: parse error before numeric constant
file.c:27: stray '342' in program
file.c:27: stray '200' in program
file.c:27: stray '235' in program
file.c:37: stray '342' in program
file.c:37: stray '200' in program
file.c:37: stray '234' in program
file.c:37: parse error before '%' token
file.c:37: stray '' in program
file.c:37: stray '342' in program
file.c:37: stray '200' in program
file.c:37: stray '235' in program
file.c:40: stray '342' in program
file.c:40: stray '200' in program
file.c:40: stray '234' in program
file.c:40: parse error before '%' token
file.c:40: stray '' in program
file.c:40: stray '342' in program
file.c:40: stray '200' in program
file.c:40: stray '235' in program
请问file.c:40: stray '235' in program是什么意思?老编译不过就没信心啦!麻烦高手给个答案阿!小弟先谢了!悬分50!
#include
#include
#include
int p1,p2;
main()
{
int fd[2];
char OutPipe[100],InPipe[100];
pipe(fd);
while ((p1==fork())==-1);/*创建子进程P1*/
if (p1==0)
{
lockf(fd[1],1,0);
sprintf(OutPipe,”child 1 process is sending message!”);
write(fd[1],OutPipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else
{
while ((p2==fork())==-1);/*创建另一个子进程P2*/
if (p2==0)
{
lockf(fd[1],1,0);
sprintf(OutPipe,”child 2 process is sending message!”);
write(fd[1],OutPipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else
{
wait(0);
read(fd[0],InPipe,50);
printf(“%sn”,InPipe);
wait(0);
read(fd[0],InPipe,50);
printf(“%sn”,InPipe);
xit(0);
}
}
}
在RED hat下用GCC编译确出现多处错误,真的烦躁!错误如下:
[root@linux 实验]# gcc -o file file.c
file.c:5: stray '302' in program
file.c:5: stray '240' in program
file.c: In function `main':
file.c:11: stray '357' in program
file.c:11: stray '274' in program
file.c:11: stray '233' in program
file.c:15: stray '342' in program
file.c:15: stray '200' in program
file.c:15: stray '235' in program
file.c:15: `child' undeclared (first use in this function)
file.c:15: (Each undeclared identifier is reported only once
file.c:15: for each function it appears in.)
file.c:15: parse error before numeric constant
file.c:15: stray '342' in program
file.c:15: stray '200' in program
file.c:15: stray '235' in program
file.c:23: stray '357' in program
file.c:23: stray '274' in program
file.c:23: stray '233' in program
file.c:27: stray '342' in program
file.c:27: stray '200' in program
file.c:27: stray '235' in program
file.c:27: parse error before numeric constant
file.c:27: stray '342' in program
file.c:27: stray '200' in program
file.c:27: stray '235' in program
file.c:37: stray '342' in program
file.c:37: stray '200' in program
file.c:37: stray '234' in program
file.c:37: parse error before '%' token
file.c:37: stray '' in program
file.c:37: stray '342' in program
file.c:37: stray '200' in program
file.c:37: stray '235' in program
file.c:40: stray '342' in program
file.c:40: stray '200' in program
file.c:40: stray '234' in program
file.c:40: parse error before '%' token
file.c:40: stray '' in program
file.c:40: stray '342' in program
file.c:40: stray '200' in program
file.c:40: stray '235' in program
请问file.c:40: stray '235' in program是什么意思?老编译不过就没信心啦!麻烦高手给个答案阿!小弟先谢了!悬分50!
|
最大的问题是你用了中文输入时的引号逗号分号之类的
|
还有main()要改成int main(),在程序的最后加return 0;
|
另外有的地方漏掉了某个字符,比如xit(0)应该是exit(0),