- 论坛徽章:
- 1
|
请教蓝色键盘:关于name stream pipe的用法问题
可惜我没有sco的环境。
不过我看了一下说明,通过spx建立无名管道,需要打开这个设备两次,
并通过ioctl对这两个描述符建立对应的联系。之后就和通常的pipe操作一样了。希望以下的代码对你有所帮助,并且,也讲述到了如何通过spx建立有名管道,以及传递文件描述符。
http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?spx+HW
- Examples
- A routine to create an unnamed bi-directional stream pipe in the manner of pipe(S):
- #include <unistd.h>;
- #include <sys/types.h>;
- #include <sys/fcntl.h>;
- #include <sys/stream.h>;
- #include <sys/stropts.h>;
- #include <sys/stat.h>;
- #include <termio.h>;
- #include <signal.h>;
- #define SPX "/dev/spx"
- int spipe(fd)
- int *fd;
- {
- struct strfdinsert s;
- long p;
- if ( ( fd[0] = open(SPX, O_RDWR) ) < 0 )
- return(-1);
- if ( ( fd[1] = open(SPX, O_RDWR) ) < 0 ) /* open different minor */
- return(-1);
- s.ctlbuf.buf = (caddr_t) &p ;
- s.ctlbuf.maxlen = s.ctlbuf.len = sizeof(long);
- s.databuf.buf = (caddr_t) NULL;
- s.databuf.maxlen = s.databuf.len = -1;
- s.fildes = fd[1];
- s.offset = s.flags = 0;
- if ( ioctl(fd[0], I_FDINSERT, &s) < 0 ) /* join loop drivers */
- return(-1);
- return(0);
- }
- A routine to create a named stream pipe from a unnamed one (this requires root privilege):
- int nspipe(fd, pipename)
- int *fd;
- char *pipename;
- {
- struct stat status;
- int omask;
-
- if ( spipe(fd) < 0 )
- return(-1);
-
- if ( fstat(fd[0],&status) < 0 )
- return(-1);
-
- unlink(pipename);
- omask = umask(0);
- if ( mknod(pipename, S_IFCHR|0666, status.st_rdev) < 0 ) /* create node */
- return(-1);
- umask(omask);
-
- return(0);
- }
- A routine to pass a file descriptor to another process:
- int passfd(fd, sendfd)
- int fd, sendfd;
- {
- if ( ioctl(fd, I_SENDFD, sendfd) < 0 ) /* send descriptor sendfd */
- return(-1);
- }
- A routine to receive a file descriptor from a process:
- int recvfd(fd)
- int fd;
- {
- struct strrecvfd s;
-
- if ( ioctl(fd, I_RECVFD, (char *) &s) < 0 )
- return(-1);
-
- /* s.uid received effective user ID of sender
- s.gid received effective group ID of sender */
-
- return(s.fd); /* received file descriptor */
- }
复制代码 |
|