免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2787 | 回复: 17
打印 上一主题 下一主题

本人写了一个线程池的程序,现在遇到一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-10-16 15:19 |只看该作者 |倒序浏览
这个星期,写了线程池实现socket服务器端的程序,在redhat环境下测试都能通过,移植到Aix unix的时候,正常通讯都是没有问题的。只是在按Ctrl+C的时候,却产生了core。
所以就有了疑问,在unix下的多线程程序,是否是随机由一个线程接收Ctrl+C产生的中断信号?我该怎么屏蔽掉子线程的信号接收?
产生core一般来说,肯定是程序本身的问题,但是我找不到错在哪里,下面是一个简单的程序模拟,也产生了core,请大家看看。

#include <stdio.h>;
#include <errno.h>;
#include <sys/types.h>;
#include <sys/socket.h>;
#include <sys/signal.h>;
#include <sys/select.h>;
#include <sys/sem.h>;
#include <netinet/in.h>;
#include <pthread.h>;

pthread_mutex_t mutex;
pthread_cond_t  cond;
pthread_t       tid, tid2, tid3;

void *thread_start(void *arg)
{

  while(1)
  {
   pthread_mutex_lock(&mutex);

    if( pthread_cond_wait(&cond,&mutex) == EINTR )
    {
      printf("eintr...............\n";
    }
    pthread_mutex_unlock(&mutex);
  }
  pthread_exit(0);
}

void killHandle(int signo)
{
  printf("get here.\n";
  pthread_mutex_destroy(&mutex);
  pthread_cond_destroy(&cond);
  exit(-2);
}
int createthread(void)
{

  pthread_mutex_init(&mutex,NULL);
  pthread_cond_init(&cond, NULL);
  
  pthread_create(&tid, NULL, thread_start, NULL);

  pthread_create(&tid2, NULL, thread_start, NULL);
  pthread_create(&tid3, NULL, thread_start, NULL);
  
}
int main(void)
{
  int sockfd   = -1;
  int ret      = 0;
  int listenfd = 0;
  struct sockaddr_in server_addr;
  

  signal(SIGTERM, killHandle);
  signal(SIGINT,  killHandle);
  signal(SIGPIPE, killHandle);
  
  createthread();
  
  sockfd = socket(AF_INET, SOCK_STREAM, 0 );
  if( sockfd == -1 )
  {
    printf("getSocket:socket create error.\n";
    return -1;
  }
  
  server_addr.sin_family      = AF_INET;
  server_addr.sin_port        = htons(50001);
  server_addr.sin_addr.s_addr = INADDR_ANY; /* 自动获取本机地址 */
  bzero(&(server_addr.sin_zero), ;

  ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
  if( ret == -1 )
  {
    printf("getSocket:socket bind error.\n";
    return -1;
  }
  listenfd = listen(sockfd, 120);
   

  for( ;; )
  {
    printf("main:get here 1\n";   
    ret = select(sockfd+1, NULL, NULL, NULL, NULL);
    if( ret == EINTR )
    {
      printf("main: eintr\n";
      return 0;
    }
    printf("main:get here 2\n";   
  }   
  return 0;
}

论坛徽章:
0
2 [报告]
发表于 2004-10-17 19:10 |只看该作者

本人写了一个线程池的程序,现在遇到一个问题

signal(SIGTERM, killHandle);

问题应该在这里!

论坛徽章:
0
3 [报告]
发表于 2004-10-18 12:41 |只看该作者

本人写了一个线程池的程序,现在遇到一个问题

呵呵,楼上的仁兄这话什么意思呢?
问题该在这里,那么是什么问题,该怎么解决呢?
不要放一炮就走哦。

论坛徽章:
0
4 [报告]
发表于 2004-10-18 12:43 |只看该作者

本人写了一个线程池的程序,现在遇到一个问题

Segmentation fault in spin_lock_global_ppc_up at 0xd0019828
0xd0019828 (spin_lock_global_ppc_up+0x 90c40154        stw   r6,0x154(r4)

论坛徽章:
0
5 [报告]
发表于 2004-10-18 12:59 |只看该作者

本人写了一个线程池的程序,现在遇到一个问题

信号SIGTERM在linux下能被捕捉到吗?
Aix unix呢?

AIX我没用过,请兄弟测试一下,应该能找到答案了。

论坛徽章:
0
6 [报告]
发表于 2004-10-18 13:44 |只看该作者

本人写了一个线程池的程序,现在遇到一个问题

给你一个能用的,希望对你有启发

  1. #include <stdio.h>;
  2. #include <errno.h>;
  3. #include <sys/types.h>;
  4. #include <sys/socket.h>;
  5. #include <sys/signal.h>;
  6. #include <sys/select.h>;
  7. #include <sys/sem.h>;
  8. #include <netinet/in.h>;
  9. #include <pthread.h>;

  10. pthread_mutex_t mutex;
  11. pthread_cond_t cond;
  12. pthread_t tid, tid2, tid3;

  13. void *thread_start(void *arg)
  14. {

  15. while(1)
  16. {
  17. pthread_mutex_lock(&mutex);

  18. if( pthread_cond_wait(&cond,&mutex) == EINTR )
  19. {
  20. printf("eintr...............\n");
  21. }
  22. pthread_mutex_unlock(&mutex);
  23. }
  24. pthread_exit(0);
  25. }

  26. int killHandle(int signo)
  27. {
  28. printf("get here.\n");
  29. pthread_mutex_destroy(&mutex);
  30. pthread_cond_destroy(&cond);
  31. pthread_exit(0);
  32. exit(0);
  33. }
  34. int createthread(void)
  35. {

  36. pthread_mutex_init(&mutex,NULL);
  37. pthread_cond_init(&cond, NULL);

  38. pthread_create(&tid, NULL, thread_start, NULL);

  39. pthread_create(&tid2, NULL, thread_start, NULL);
  40. pthread_create(&tid3, NULL, thread_start, NULL);

  41. }
  42. int main(void)
  43. {
  44. int sockfd = -1;
  45. int ret = 0;
  46. int listenfd = 0;
  47. struct sockaddr_in server_addr;


  48. signal(SIGTERM, (void *)killHandle);
  49. signal(SIGINT, (void *)killHandle);
  50. signal(SIGPIPE, (void *)killHandle);

  51. createthread();

  52. sockfd = socket(AF_INET, SOCK_STREAM, 0 );
  53. if( sockfd == -1 )
  54. {
  55. printf("getSocket:socket create error.\n");
  56. return -1;
  57. }

  58. server_addr.sin_family = AF_INET;
  59. server_addr.sin_port = htons(50001);
  60. server_addr.sin_addr.s_addr = INADDR_ANY;
  61. bzero(&(server_addr.sin_zero));

  62. ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
  63. if( ret == -1 )
  64. {
  65. printf("getSocket:socket bind error.\n");
  66. return -1;
  67. }
  68. listenfd = listen(sockfd, 120);


  69. for( ;; )
  70. {
  71. printf("main:get here 1\n");
  72. ret = select(sockfd+1, NULL, NULL, NULL, NULL);
  73. if( ret == EINTR )
  74. {
  75. printf("main: eintr\n");
  76. return 0;
  77. }
  78. printf("main:get here 2\n");
  79. }
  80. return 0;
  81. }
复制代码


楼上说的也不正确,代码问题出在signal(SIGINT, (void *)killHandle);

论坛徽章:
0
7 [报告]
发表于 2004-10-18 14:35 |只看该作者

本人写了一个线程池的程序,现在遇到一个问题

superdoctor兄的意思不是太明白,我试试!

信号都是一样的。

我现在把子线程的中断信号屏蔽了,这样只让父线程获得中断信号,就没有问题了。前几天可能是我在屏蔽信号的时候写错了代码,所以没有看到效果。
谢谢大家!

论坛徽章:
0
8 [报告]
发表于 2004-10-18 14:40 |只看该作者

本人写了一个线程池的程序,现在遇到一个问题

呵呵,signal(SIGINT, (void *)killHandle);编译不过去!

论坛徽章:
0
9 [报告]
发表于 2004-10-18 14:53 |只看该作者

本人写了一个线程池的程序,现在遇到一个问题

compile error?
signal(SIGINT, (void *)killHandle);


the code i tested on redhat(x86),this should not be a problem on Linux/BSD systems

论坛徽章:
0
10 [报告]
发表于 2004-10-25 12:32 |只看该作者

本人写了一个线程池的程序,现在遇到一个问题

呵呵!楼上的没有错了!
我是在AIX下编译不过,没有说清楚,sorry!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP