找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3794|回复: 0

父进程群发消息给子进程的方法

[复制链接]
发表于 2014-10-22 13:36:44 | 显示全部楼层 |阅读模式
最近在研究一些功能,父进程需要通知子进程一些消息。
于是写了一个例子,借助本地socketpair。
于是实现了一个例子,测试通过。
感觉这个目前还有缺陷,当管道破裂的时候如何重建?还需要自己想想。
  1. // ACETimer.cpp : 定义控制台应用程序的入口点。
  2. //
  3. /*
  4. #include "ace/ACE.h"
  5. #include "ace/SString.h"
  6. #include "ace/Malloc.h"
  7. #include "ace/Malloc_T.h"
  8. #include "ace/Task_T.h"
  9. #include "ace/Local_Memory_Pool.h"
  10. #include "ace/Time_Value.h"
  11. #include "ace/OS_main.h"
  12. #include "ace/OS_NS_sys_stat.h"
  13. #include "ace/OS_NS_sys_socket.h"
  14. #include "ace/OS_NS_unistd.h"
  15. #include "ace/High_Res_Timer.h"
  16. #include "ace/INET_Addr.h"
  17. */
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. using namespace std;
  24. //子线程个数
  25. #define CHILDREN_COUNT 2
  26. int main(int argc, char *argv[])
  27. {
  28. #ifndef WIN32
  29.        
  30.         int nTest = 1;
  31.         pid_t pid = 0;
  32.         //pipe(filedes);
  33.        
  34.         //子进程和父进程的fd通讯信息
  35.         int ayfd[CHILDREN_COUNT][2];
  36.        
  37.         for(int i = 0; i < CHILDREN_COUNT; i++)
  38.         {
  39.                 printf("[main]i=%d.\n", i);
  40.                
  41.                 socketpair(AF_UNIX, SOCK_STREAM, 0, ayfd[i]);
  42.                
  43.                 pid = fork();
  44.                 if(pid == 0)
  45.                 {
  46.                         //子进程
  47.                         printf("[Child]child process run(%d).\n", i);
  48.                         char szData[50] = {'\0'};
  49.                         while(true)
  50.                         {
  51.                                 //printf("[Child]child recv begin.\n");
  52.                                 int nLen = read(ayfd[i][0], szData, sizeof(szData));
  53.                                 if(nLen > 0)
  54.                                 {
  55.                                         printf("[Child]Recv Data[child %d](%s).\n", i, szData);
  56.                                         printf("[child]nTest=%d.\n", nTest);
  57.                       close(ayfd[i][1]); //关闭写
  58.                                 }
  59.                                 else
  60.                                 {
  61.                                         //ACE_Time_Value tvSleep(0, 10000);
  62.                                         //ACE_OS::sleep(tvSleep);
  63.                                         usleep(10000);
  64.                                 }
  65.                         }
  66.                 }
  67.         }
  68.        
  69.         printf("[Father]pid=%d.\n", pid);
  70.         //父进程
  71.         printf("[Father]Father process run.\n");
  72.         char szData[50] = {'\0'};
  73.         sprintf(szData, "Hello child.");
  74.        
  75.         //关闭所有读
  76.         for(int j = 0; j < CHILDREN_COUNT; j++)
  77.         {
  78.                 close(ayfd[j][0]);  
  79.         }
  80.         while(true)
  81.         {
  82.                 printf("[Father]Send Begin.\n");
  83.                 for(int j = 0; j < CHILDREN_COUNT; j++)
  84.                 {
  85.                         int nLen = write(ayfd[j][1], szData, sizeof(szData));
  86.                         printf("[Father]Send Data(%d).\n", nLen);
  87.                 }
  88.                
  89.                 nTest++;
  90.                 /*
  91.                 ACE_Time_Value tvSleep(10, 0);
  92.                 ACE_OS::sleep(tvSleep);               
  93.                 */
  94.                 sleep(10);
  95.   }
  96.   //close(filedes[1]);                
  97. #endif
  98.         return 0;
  99. }
复制代码
这部分代码只支持Linux,不考虑windows。


您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-28 02:42 , Processed in 0.010645 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表