|

楼主 |
发表于 2004-12-31 14:07:05
|
显示全部楼层
我做了个实验,试着传文件描述符。
- int main()
- {
- int fd,pid;
- int pd[2];
- socketpair(AF_UNIX,SOCK_STREAM,0,pd);
- char buf[30];
- //printf("fd = %d\n",fd);
- pid = fork();
- if(pid == 0) //child
- {
- close(pd[0]);
- read(pd[1],&fd,sizeof(int));
- if(read(fd,buf,30) == -1)
- printf("In child,fd=%d\n",fd);
- else{
- printf("In child: %s\n",buf);
- }
- }
- if(pid >0) //parent
- {
- close(pd[1]);
- fd = open("select_serv.c",O_RDONLY,00700);
- [color=Magenta]//int tmp_fd = dup(fd);[/color]
- [color=Lime]int tmp_fd = fd;[/color]
- printf("In parent,tmp_fd=%d\n",tmp_fd);
- write(pd[0],&fd,sizeof(int));
- }
- return 0;
- }
复制代码
注意彩色两行,当用“int tmp_fd = dup(fd);”时,子进程里显示坏的描述符;
当用“int tmp_fd = fd”时,子进程里的read可以执行,但是读到的总是一个ascii码 值为01。
哪位解释一下,谢谢 |
|