免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345
最近访问板块 发新帖
楼主: wcw

[C] 进程间通信的疑惑? [复制链接]

论坛徽章:
0
发表于 2009-03-16 14:16 |显示全部楼层
进来学习。。。

论坛徽章:
0
发表于 2009-03-16 17:42 |显示全部楼层
套接字在主进程派生子进程的时候,子进程也获得了该套接字。
在unp的第1卷有说到。

论坛徽章:
0
发表于 2009-03-21 18:04 |显示全部楼层
原帖由 wcw 于 2009-3-12 09:59 发表

#include
#include
#include
#include

#include
#include
#include

#include
#include
#include
#include
#include
#include
#include
#include

#include
int      fd ...


后来我用unix-socket解决了,不过上面的用ioctl的方法还是不知为什么不行,apue里说的是2中都可以的,下面是尝试成功的:
#include <sys/socket.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <dirent.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/wait.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#include <stropts.h>
#define CONTROLLEN sizeof (struct cmsghdr) + sizeof (int)

int fd[2];

void child()
{
        int file;
        if ((file = open("1", O_RDONLY)) < 0) {
                perror("open file failed");
                exit(-2);
        }
        printf("fd = %d\n", file);
       
        char tmpbuf[CONTROLLEN];
        struct cmsghdr *cmptr = (struct cmsghdr *) tmpbuf;
        struct iovec iov[1];
        struct msghdr msg;
        char buf[1];
        iov[0].iov_base = buf;
        iov[0].iov_len = 1;
        msg.msg_iov = iov;
        msg.msg_iovlen = 1;
        msg.msg_name = NULL;
        msg.msg_namelen = 0;
        msg.msg_control = cmptr;
        msg.msg_controllen = CONTROLLEN;
        cmptr->cmsg_level = SOL_SOCKET;
        cmptr->cmsg_type = SCM_RIGHTS;
        cmptr->cmsg_len = CONTROLLEN;
        *(int *)CMSG_DATA (cmptr) = file;

          if (sendmsg(fd[1], &msg, 0) != 1) {
                   perror("sendmsg failed");
                exit(-1);
        }
        exit(0);
}

int main()
{
        //int socketpair(int domain, int type, int protocol, int sockfd[2]);
        if (socketpair(AF_UNIX/* or AF_LOCAL*/, SOCK_STREAM, 0, fd) != 0) {
                perror("socketpair failed");
                return(-1);
        }
        /*if (pipe(fd) < 0) {
                perror("pipe failed");
                return -2;
        }*/

        pid_t pid;
        if ((pid = fork()) == 0 ){
                child();
        }

        char tmpbuf[CONTROLLEN];
        struct cmsghdr *cmptr = (struct cmsghdr *) tmpbuf;
        struct iovec iov[1];
        struct msghdr msg;
        char buf[1];
        iov[0].iov_base = buf;
        iov[0].iov_len = sizeof (buf);
        msg.msg_iov = iov;
        msg.msg_iovlen = 1;
        msg.msg_name = NULL;
        msg.msg_namelen = 0;
        msg.msg_control = cmptr;
        msg.msg_controllen = CONTROLLEN;

        wait(NULL);
          if (recvmsg(fd[0], &msg, 0) <= 0) {
                perror("recvmsg failed");
                exit(-2);
        }
        int recvfd = *(int *) CMSG_DATA (cmptr);
        printf("recvfd = %d\n", recvfd);
        char tmp[256];
        u_int count;
        if ((count = read(recvfd, tmp, sizeof(tmp))) < 0) {
                perror("read failed\n");
                return -2;
        }
        tmp[count] = 0;
        printf("have read: %s\n", tmp);
        return 0;
}

[ 本帖最后由 wcw 于 2009-3-21 18:05 编辑 ]

论坛徽章:
0
发表于 2009-03-21 18:10 |显示全部楼层

回复 #1 wcw 的帖子

如何结贴?
连标题都不能改?

论坛徽章:
0
发表于 2009-03-23 09:27 |显示全部楼层
子进程在读管道的时候默认是阻塞的,
添加了
fcntl(fd[0], F_SETFL, O_NONBLOCK);
fcntl(fd[1], F_SETFL, O_NONBLOCK);
以后,读就出错了,返回-1,
怎么把读管道弄成非阻塞的?

论坛徽章:
0
发表于 2009-03-23 11:53 |显示全部楼层
原帖由 marco_hxj 于 2009-3-23 09:27 发表
子进程在读管道的时候默认是阻塞的,
添加了
fcntl(fd[0], F_SETFL, O_NONBLOCK);
fcntl(fd[1], F_SETFL, O_NONBLOCK);
以后,读就出错了,返回-1,
怎么把读管道弄成非阻塞的?

你把管道弄成非阻塞了,read的时候,如果没有数据,当然就马上返回-1了
可以检查errno,应该是EAGAIN
[EAGAIN]           The file was marked for non-blocking I/O, and no data
                        were ready to be read.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP