前几天在博客里http://www.cnblogs.com/eavn/archive/2010/08/29/1812040.html提出了一个问题,就是pthread_create导致虚拟内存上涨到极限的问题,下面项目里的实际代码阉割版,其中调用pthread_create的条件是多路I/O转接集中的相应的socket描述符有变化,即监听到链接,此时if条件成立调用pthread_create
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
///
2 // include all header files needed
3 ///
4
5 void * Client_TS_handle_ss_local_data_cmd( void * arg)
6 {
7 printf( " enter local ss handler\n " );
8
9 ///
10 // forword the real-time data received from client-com-server to web-server
11 // through socket communication
12 ///
13
14 printf( " leave local ss handler\n " );
15 pthread_detach(pthread_self());
16 pthread_exit(NULL);
17 }
18
19 void * Client_TS_handle_up_local_data_cmd( void * arg)
20 {
21 printf( " enter local up handler\n " );
22
23 ///
24 // forword the historical data received from client-com-server to web-server
25 // through socket communication
26 ///
27
28 printf( " leave local up handler\n " );
29 pthread_detach(pthread_self());
30 pthread_exit(NULL);
31 }
32
33 int main()
34 {
35 ///
36 // initialize the server sockets
37 // Local_UP_Socket_fd
38 // Local_SS_Socket_fd
39 // Remote_Socket_fd
40 ///
41
42 while ( 1 )
43 {
44 FD_ZERO( & rset);
45 FD_SET(Local_UP_Socket_fd, & rset);
46 FD_SET(Local_SS_Socket_fd, & rset);
47 FD_SET(Remote_Socket_fd, & rset);
48 maxfd = max(Remote_Socket_fd,Local_SS_Socket_fd);
49 maxfd = max(maxfd,Local_UP_Socket_fd);
50
51 if (select(maxfd + 1 , & rset,NULL,NULL,NULL) < 0 )
52 {
53 if (errno == EINTR)
54 continue ;
55 else
56 {
57 fprintf(stderr, " Select error : %s\n " ,strerror(errno));
58 exit( 1 );
59 }
60 }
61
62 if (FD_ISSET(Remote_Socket_fd, & rset))
63 {
64 printf( " Remote Data/Cmd Recved\n " );
65 }
66
67 if (FD_ISSET(Local_UP_Socket_fd, & rset))
68 {
69 if (pthread_create( & _local_up_thread,NULL,Client_TS_handle_up_local_data_cmd,( void * ) & UPPara) != 0 )
70 {
71 perror( " Error Creating UP Thread " );
72 }
73 }
74
75 if (FD_ISSET(Local_SS_Socket_fd, & rset))
76 {
77 if (pthread_create( & _local_ss_thread,NULL,Client_TS_handle_ss_local_data_cmd,( void * ) & SSPara) != 0 )
78 {
79 perror( " Error Creating SS Thread " );
80 }
81 }
82 }
83
84 close(Remote_Socket_fd);
85 close(Local_SS_Socket_fd);
86 close(Local_UP_Socket_fd);
87 exit( 0 );
88 }
89 /* End of File */
90
2 // include all header files needed
3 ///
4
5 void * Client_TS_handle_ss_local_data_cmd( void * arg)
6 {
7 printf( " enter local ss handler\n " );
8
9 ///
10 // forword the real-time data received from client-com-server to web-server
11 // through socket communication
12 ///
13
14 printf( " leave local ss handler\n " );
15 pthread_detach(pthread_self());
16 pthread_exit(NULL);
17 }
18
19 void * Client_TS_handle_up_local_data_cmd( void * arg)
20 {
21 printf( " enter local up handler\n " );
22
23 ///
24 // forword the historical data received from client-com-server to web-server
25 // through socket communication
26 ///
27
28 printf( " leave local up handler\n " );
29 pthread_detach(pthread_self());
30 pthread_exit(NULL);
31 }
32
33 int main()
34 {
35 ///
36 // initialize the server sockets
37 // Local_UP_Socket_fd
38 // Local_SS_Socket_fd
39 // Remote_Socket_fd
40 ///
41
42 while ( 1 )
43 {
44 FD_ZERO( & rset);
45 FD_SET(Local_UP_Socket_fd, & rset);
46 FD_SET(Local_SS_Socket_fd, & rset);
47 FD_SET(Remote_Socket_fd, & rset);
48 maxfd = max(Remote_Socket_fd,Local_SS_Socket_fd);
49 maxfd = max(maxfd,Local_UP_Socket_fd);
50
51 if (select(maxfd + 1 , & rset,NULL,NULL,NULL) < 0 )
52 {
53 if (errno == EINTR)
54 continue ;
55 else
56 {
57 fprintf(stderr, " Select error : %s\n " ,strerror(errno));
58 exit( 1 );
59 }
60 }
61
62 if (FD_ISSET(Remote_Socket_fd, & rset))
63 {
64 printf( " Remote Data/Cmd Recved\n " );
65 }
66
67 if (FD_ISSET(Local_UP_Socket_fd, & rset))
68 {
69 if (pthread_create( & _local_up_thread,NULL,Client_TS_handle_up_local_data_cmd,( void * ) & UPPara) != 0 )
70 {
71 perror( " Error Creating UP Thread " );
72 }
73 }
74
75 if (FD_ISSET(Local_SS_Socket_fd, & rset))
76 {
77 if (pthread_create( & _local_ss_thread,NULL,Client_TS_handle_ss_local_data_cmd,( void * ) & SSPara) != 0 )
78 {
79 perror( " Error Creating SS Thread " );
80 }
81 }
82 }
83
84 close(Remote_Socket_fd);
85 close(Local_SS_Socket_fd);
86 close(Local_UP_Socket_fd);
87 exit( 0 );
88 }
89 /* End of File */
90
今天老师跟我提了个意见就是去找问题源,即向监听socket提出链接的客户端,查看该客户端是否也是在循环里无休止地提出链接请求,如果是这样的话,把源问题解决了就可以解决前文提出的问题
顺着老师的意见,查看客户端程序,主要代码如下
while
(
!
feof(fhandle))
{
fgets(fhandle);
if (feof(fhandle))
{
fseek(fhandle);
continue ;
}
senddata(); // socket communication
sleep(WAITTIME);
}
{
fgets(fhandle);
if (feof(fhandle))
{
fseek(fhandle);
continue ;
}
senddata(); // socket communication
sleep(WAITTIME);
}
从以上代码可以看出,客户端并不是无理取闹那样无休止地提出连接请求,而是有一定的sleep(),当然了这个WAITTIME可以人为修改,项目中设置为10s,想一下间隔10s发送数据并不是很过分,只不过每次发送12004bytes的数据容易造成拥挤,而且实际中发送的数据可能不止这些,因此pthread_create()导致虚拟内存上涨的问题很是程序设计的不合理,有待解决,如果有好的建议,欢迎指点