LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 780|回复: 5

关于线程的奇怪问题,请大家帮忙分析一下原因。

[复制链接]
发表于 2006-7-30 01:26:47 | 显示全部楼层 |阅读模式

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <netdb.h>
  8. #include <pthread.h>
  9. #include "myproxy.h"
  10. #include "parse.h"

  11. void *check_proxy(void *p)
  12. {
  13.         int sockfd;
  14.         char *ip;
  15.         char *port;
  16.         struct sockaddr_in address;       
  17.         char request[256];
  18.         char response[12];
  19.         int ret;
  20.        
  21.         //get the required message in the struct
  22.         ip = ((proxy *)p)->ip;
  23.         port = ((proxy *)p)->port;
  24.        
  25.         /*create the socket*/
  26.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  27.         if(sockfd == -1)
  28.         {
  29.                 fprintf(stderr, "Failed in creating socket!\n");
  30.         }

  31.         /*connect to the server by socket*/
  32.         memset(&address, 0, sizeof(struct sockaddr_in));
  33.         address.sin_family = AF_INET;
  34.         address.sin_addr.s_addr = inet_addr(ip);
  35.         address.sin_port = htons(atoi(port));       
  36.         ret = connect(sockfd, (struct sockaddr *)&address, sizeof(struct sockaddr));
  37.         if(ret == -1)
  38.         {
  39.                 fprintf(stderr, "Failed in connecting to server!\n");
  40.         }

  41.         /*send the http request*/
  42.         sprintf(request,
  43.                 "%s", "GET / HTTP/1.0\nHost:www.google.com\n\n");
  44.         ret = write(sockfd, request, strlen(request));               
  45.         if(ret == -1)
  46.         {
  47.                 fprintf(stderr, "Failed in writing data to socket!\n");
  48.         }

  49.         /*read the response from server*/
  50.         /*here i donnot check the integrity of the data*/
  51.         ret = read(sockfd, response, sizeof(response));
  52.         if(response[9] == '2' || response[9] == '3')
  53.         {
  54.                 fprintf(stdout, "%s:%s OK!\n", ip, port);
  55.         }
  56.         else
  57.         {
  58.                 /*debug to display*/
  59.                 /*fprintf(stderr, "Proxy Failed!\n");*/
  60.         }
  61.        
  62.         /*close the socket*/
  63.         close(sockfd);
  64.        
  65.         return NULL;
  66. }

  67. int
  68. main()
  69. {
  70.         int ret;
  71.         int i;
  72.         pthread_t t_id[MAX_PROXY_NUM];        /*thread id*/
  73.        
  74.         ret = parse_proxy();
  75.         if(ret != 0)
  76.         {
  77.                 printf("Parsed proxy failed!\n");
  78.         }
  79.        
  80.         /*create a thread to test a proxy*/
  81.         i = 0;
  82.         while(i != proxy_num)
  83.         {
  84.                 pthread_create(&(t_id[i]), NULL, &check_proxy, &(parsed_proxy[i]));
  85.                 i++;
  86.         }
  87.        
  88.         /*wait the son thread*/
  89.         i = 0;
  90.         //while(i != proxy_num)
  91.         //{
  92.         //        pthread_join(t_id[i], (void **) &ret);
  93.         //        i++;
  94.         //}
  95.        
  96.         while(1)
  97.         {
  98.         };
  99. }
复制代码


  我用pthread_create创建了大约五十个子线程,然而即便我在MAIN函数里边加入了while(1)循环,每一个线程执行退出后整个程序就退出,而使用pthread_join问题同样。
  那么我的问题是,主线程创建了第一个线程后,就做什么去了,还是已经退出了。
  (单步执行时,程序的运行结果是正确的,我也检测出正确的代理。)
  谢谢大家!
 楼主| 发表于 2006-7-30 12:06:09 | 显示全部楼层

已经解决了,不过原因不是很清楚


  1.         while(i != proxy_num)
  2.         {
  3.                 pthread_create(&(t_id[i]), NULL, &check_proxy, &(parsed_proxy[i]));
  4.                 i++;
  5.         }
  6.        
  7.         /*wait the son thread*/
  8.         i = 0;
  9.         //while(i != proxy_num)
  10.         //{
  11.         //        pthread_join(t_id[i], (void **) &ret);
  12.         //        i++;
  13.         //}
  14. //改为:
  15.         while(i != proxy_num)
  16.         {
  17.                 pthread_create(&(t_id[i]), NULL, &check_proxy, &(parsed_proxy[i]));
  18.                 pthread_join(t_id[i], (void **) &ret);
  19.                 i++;
  20.         }
  21.        
  22.         /*wait the son thread*/
  23.         i = 0;
  24.         //while(i != proxy_num)
  25.         //{
  26.         //        pthread_join(t_id[i], (void **) &ret);
  27.         //        i++;
  28.         //}
复制代码

  或许是原来的办法进行join时,线程都已经返回了(尽管事实上每个线程应当持续一定时间,因为进行代理检测可能较为费时),所以主进程就会退出。
  但在原来的程序的基础上,主线程为什么会退出,我仍不是很很明白,请大家帮忙分析一下,谢谢。
回复 支持 反对

使用道具 举报

发表于 2006-7-31 09:55:59 | 显示全部楼层
while(i != proxy_num)
        {
                pthread_create(&(t_id), NULL, &check_proxy, &(parsed_proxy));
                pthread_join(t_id, (void **) &ret);
                i++;
        }
这样一定不行的,是不是创建完一个纯种之后一定要它工作完了之后,其它纯种才可以工作!!

这样流程不对!!

对于一段代码没有看出为什么主进程会退出,


while(1)
        {
        };

这种方法来等,也一定是不行的!!线程不会有时间片运行!!
回复 支持 反对

使用道具 举报

发表于 2006-7-31 12:33:59 | 显示全部楼层
3楼请说中国话

楼主:你create之后立刻join然后循环是有问题的,成线形了,那也不用thread了
回复 支持 反对

使用道具 举报

发表于 2006-7-31 16:49:20 | 显示全部楼层
楼主create线程后把它detach了就不用join了

至于现在楼主的解决方法是不对的,这样子各个线程是依次执行的, 如三楼兄弟所言
回复 支持 反对

使用道具 举报

发表于 2006-7-31 22:48:10 | 显示全部楼层
Post by x11
3楼请说中国话

楼主:你create之后立刻join然后循环是有问题的,成线形了,那也不用thread了


哦,不好意思,我用的是五笔,输入法是"极品五笔"在输入"线程"时它老是出"纯种"

以后注意!!
回复 支持 反对

使用道具 举报

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

本版积分规则

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