免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
发表于 2009-03-11 20:35 |显示全部楼层
子进程是可以共享父进程打开的文件描述符的啊?

论坛徽章:
0
发表于 2009-03-11 20:54 |显示全部楼层
把socket打开成文件,然后传递文件id....我猜的

论坛徽章:
0
发表于 2009-03-11 21:23 |显示全部楼层
我建议你改成这样:
父进程创建监听socket,然后fork子进程,由操作系统决定由哪个子进程来进行处理,也就是说当有连接过来的时候由哪个子进程来处理.

现在你这种方式是由程序员来决定选择哪个子进程来处理.

论坛徽章:
0
发表于 2009-03-11 21:31 |显示全部楼层
原帖由 converse 于 2009-3-11 21:23 发表
我建议你改成这样:
父进程创建监听socket,然后fork子进程,由操作系统决定由哪个子进程来进行处理,也就是说当有连接过来的时候由哪个子进程来处理.

现在你这种方式是由程序员来决定选择哪个子进程来处理.

人家说用进程池了...

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

回复 #14 reiase 的帖子

这也是进程池...只不过由操作系统决定由哪个进程去处理.

论坛徽章:
0
发表于 2009-03-11 21:53 |显示全部楼层
原帖由 converse 于 2009-3-11 21:47 发表
这也是进程池...只不过由操作系统决定由哪个进程去处理.


。。。C++式进程池,接口上感觉不到创建进程而已....

论坛徽章:
0
发表于 2009-03-12 09:16 |显示全部楼层
原帖由 reiase 于 2009-3-11 21:53 发表


。。。C++式进程池,接口上感觉不到创建进程而已....

他是先创建子进程池,然后在子进程中accept

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
发表于 2009-03-12 09:38 |显示全部楼层
不太可能吧,不仅仅是一个文件fd问题

论坛徽章:
0
发表于 2009-03-12 09:48 |显示全部楼层
原帖由 wcw 于 2009-3-11 17:13 发表
我想写一个服务器端的程序,该程序使用进程池的方法,父进程预创建n个子进程,父进程使用PIPE与子进程进行通信。父进程接受来自网络请求(就是accept()),然后把这个网络请求交给预先创建好的子进程处理,现在 ...

父进程使用pipe与子进程通信,应该可以由子进程读写pipe,父进程接收pipe的数据写到connfd中。

论坛徽章:
0
发表于 2009-03-12 09:59 |显示全部楼层

  1. #include <time.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <arpa/inet.h>

  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>

  8. #include <dirent.h>
  9. #include <sys/stat.h>
  10. #include <signal.h>
  11. #include <sys/wait.h>
  12. #include <pthread.h>
  13. #include <errno.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>

  16. #include <stropts.h>
  17. int      fd[2];

  18. int
  19. s_pipe(int fd[2])
  20. {
  21.     return(pipe(fd));
  22. }

  23. void child()
  24. {
  25.         int sfd;
  26.         if ((sfd = open("test.c", O_RDONLY)) < 0) {
  27.                 perror("open failed");
  28.                 exit(-1);
  29.         }
  30.   if (ioctl(fd[1], I_SENDFD, sfd) < 0) {
  31.           perror("ioctl send failed");
  32.           exit(-1);
  33.   }
  34.         close(sfd);
  35.         exit(-1);
  36. }

  37. int main()
  38. {

  39.         pid_t           pid;
  40.         if (s_pipe(fd) < 0) {
  41.                 perror("pipe failed");
  42.                 exit(-1);
  43.         }
  44.         if ((pid = fork()) < 0) {
  45.                 perror("fork error");
  46.                 exit(-1);
  47.   }
  48.   else if (pid == 0) {      /* child */
  49.     close(fd[0]);
  50.     sleep(3);
  51.     child();
  52.         }
  53.   else
  54.           close(fd[1]);               /* parent */
  55.                   
  56.         struct strrecvfd    recvfd;
  57.         if (ioctl(fd[0], I_RECVFD, &recvfd) < 0) {
  58.                 perror("ioctl recv failed");
  59.     return(-1);
  60.   }
  61.         int rfd = recvfd.fd;
  62.         printf("rfd = %d\n", rfd);
  63.        
  64.         return 0;
  65. }
复制代码

我参考了UNIx环境高级编程里17.2,17.4,17.5,写出了上面的程序,但是运行时总是报:
ioctl recv failed: Invalid argument
ioctl send failed: Invalid argument

我看了半天也不知到是那个argument不对啊?麻烦各位帮我瞅瞅!
先谢过了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP