- 论坛徽章:
- 0
|
#include <unistd.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
int field[1];
char buf[50];
pid_t pidc;
if (pipe(field) == -1)
{
printf("pipe fail\n");
return -1;
}
if ((pidc = fork()) == -1) {
printf("fork fail\n");
return -2;
}
if(pidc == 0) {
printf("in child process\n");
sleep(2);
printf("begin read\n");
read(field[0], buf, sizeof(buf));
printf("buf is %s\n", buf);
close(field[0]);
}
else
{
int b;
printf("in father process\n");
printf("field[1]: is %d\n", field[1]);
if(b=write(field[1], "hello pipe", 11)==-1)
printf("write pipe fail: no is: %d\n", b);
printf("wait child\n");
waitpid(pidc, NULL, 0);
}
return 0;
}
代码如上
如果我把变量声明的顺序改变,程序就会出错。
例如,变量申明改成
pid_t pidc;
int field[1];
char buf[50];
子进程的field就会变成很大的数 |
|