网站打开是建设中,网络舆情系统,做网站最烂公司,南京市雨花区建设局网站这几天一直在调试一个系统#xff0c;系统的功能就是定时发送数据、接收数据然后解析收到的数据#xff0c;转换成一定的格式存入数据库中。我为了并发操作#xff0c;所以每接收到一个数据包#xff0c;就调用pthread_create函数创建一个默认属性的线程进行处理。 系统…这几天一直在调试一个系统系统的功能就是定时发送数据、接收数据然后解析收到的数据转换成一定的格式存入数据库中。我为了并发操作所以每接收到一个数据包就调用pthread_create函数创建一个默认属性的线程进行处理。 系统一开始运行很正常但是当接收到第299个数据包时就发生异常查看程序日志得知原来自接收到299个数据包后就不再解析接收到的数据。我本以为是网络的问题于是重启下程序结果异常发生在了同样的位置。这时我猜想可能是代码的问题找到相关代码如下while (1) { len recvfrom(sock, buf, MAXPACKETSIZE, 0,(struct sockaddr *)c_addr, addr_len); ....... targ (struct threadarg *)malloc(sizeof(struct threadarg)); memset(targ, 0, sizeof(struct threadarg)); targ-len len; targ-ip (int)c_addr.sin_addr.s_addr; memcpy(targ-buffer, buf, len); printf(received\n); //注:targ在线程中会被free掉。 pthread_create(tid, NULL, insertToList, (void *)targ); } 从代码看不出什么异常由于解析数据是调用pthread_create函数创建一个默认属性的线程进行处理如果没有解析那么应该是pthread_create函数没有创建成功。而pthread_create函数创建失败最可能的原因应该就是系统资源不足根据经验线程的默认堆栈大小是1MB就是说系统每创建一个线程就要至少提供1MB的内存那么创建线程失败极有可能就是内存不够用了。从代码中看不出有内存泄露的现象有malloc的地方就会有free对应。而仍然出现问题那么唯一的解释就是pthread_create会导致内存泄露 pthread_create创建的线程结束后系统并未回收其资源从而导致了泄露。 然后从网上查了相关资料如下线程的分离状态决定一个线程以什么样的方式来终止自己。线程的默认属性是非分离状态这种情况下原有的线程等待创建的线程结束。只有当pthread_join函数返回时创建的线程才算终止才能释放自己占用的系统资源。而分离线程不是这样子的它没有被其他的线程所等待自己运行结束了线程也就终止了马上释放系统资源。程序员应该根据自己的需要选择适当的分离状态。从上面的描述中可以得知如果调用pthread_create函数创建一个默认非分离状态的线程如果不用pthread_join()函数线程结束时并不算终止所以仍然会占用系统资源。这里有如下几种方法解决这个问题1.使用pthread_join()函数回收相关内存区域。pthread_t tid; void* state; pthread_create(tid, NULL, test, NULL); pthread_join(tid, state); 2.可以调用 pthread_detach() 函数分离线程。pthread_t tid; pthread_create(tid, NULL, test, NULL); pthread_detach(tid); 当然也可以在 thread function 中调用。void* test(void* arg) { ..... pthread_detach(pthread_self()); return NULL; } 3.使用线程属性。pthread_attr_t attr; pthread_t tid; pthread_attr_init(attr); pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED); pthread_create(tid, attr, test, NULL); sleep(3);//等待线程结束 pthread_attr_destroy(attr); 根据实际需要任选其一即可。 ps最后我写了个测试程序然后用valgrind检查了一下。 测试程序#include pthread.h #include stdio.h void* test() { printf(ok\n); return; } int main(int argc, char** argv) { pthread_t tid; pthread_create(tid, NULL, test, NULL); //pthread_join(tid, NULL); return 1; }
编译链接[rootlocalhost ~]# gcc -g b.c -o b -lpthread然后用valgrind进行内存检查[rootlocalhost ~]# valgrind --toolmemcheck --leak-checkfull ./b20980 Memcheck, a memory error detector20980 Copyright (C) 2002-2009, and GNU GPLd, by Julian Seward et al.20980 Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info20980 Command: ./b20980 ok20980 20980 HEAP SUMMARY:20980 in use at exit: 272 bytes in 1 blocks20980 total heap usage: 1 allocs, 0 frees, 272 bytes allocated20980 20980 272 bytes in 1 blocks are possibly lost in loss record 1 of 120980 at 0x4C1F1A0: calloc (vg_replace_malloc.c:418)20980 by 0x4010422: _dl_allocate_tls (in /lib64/ld-2.7.so)20980
by 0x4E2AB52: pthread_createGLIBC_2.2.5 (in /lib64/libpthread-2.7.so)20980 by 0x40059E: main (b.c:13)20980 20980 LEAK SUMMARY:20980 definitely lost: 0 bytes in 0 blocks20980 indirectly lost: 0 bytes in 0 blocks20980 possibly lost: 272 bytes in 1 blocks20980 still reachable: 0 bytes in 0 blocks20980 suppressed: 0 bytes in 0 blocks20980 20980 For counts of detected and suppressed errors, rerun with: -v20980 ERROR SUMMARY:
1 errors from 1 contexts(suppressed: 4 from 4)确实有内存泄露。 修改测试程序#include pthread.h #include stdio.h void* test() { printf(ok\n); return; } int main(int argc, char** argv) { pthread_t tid; pthread_create(tid, NULL, test, NULL); pthread_join(tid, NULL); return 1; }
编译链接[rootlocalhost ~]# gcc -g b.c -o b -lpthread然后用valgrind进行内存检查[rootlocalhost ~]# valgrind --toolmemcheck --leak-checkfull ./b21013 Memcheck, a memory error detector21013 Copyright (C) 2002-2009, and GNU GPLd, by Julian Seward et al.21013 Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info21013 Command: ./b21013 ok21013 21013 HEAP SUMMARY:21013 in use at exit: 0 bytes in 0 blocks21013
total heap usage: 1 allocs, 1 frees, 272 bytes allocated21013 21013 All heap blocks were freed -- no leaks are possible21013 21013 For counts of detected and suppressed errors, rerun with: -v21013 ERROR SUMMARY:
0 errors from 0 contexts(suppressed: 4 from 4)问题解决。