Chinaunix

标题: 求教aix上面的一个pthread socket server是否有内存泄露 [打印本页]

作者: sinosinux    时间: 2012-08-11 18:35
标题: 求教aix上面的一个pthread socket server是否有内存泄露
在aix上用pthread开发多线程socket服务器,实验程序如下,发现长时间运行,进程的内存占用量会缓慢上涨,求指教程序是否有内存泄露,谢谢!!
  1. # include <iostream>
  2. # include <string>
  3. # include <sstream>
  4. # include <list>
  5. # include <map>
  6. # include <vector>
  7. # include <pthread.h>
  8. # include <semaphore.h>
  9. # include <errno.h>
  10. # include <stdio.h>
  11. # include <stdlib.h>
  12. # include <signal.h>
  13. # include <unistd.h>
  14. # include <algorithm>
  15. # include <netdb.h>
  16. # include <netinet/in.h>
  17. # include <sys/types.h>
  18. # include <sys/socket.h>
  19. # include <sys/socketvar.h>
  20. # include <arpa/inet.h>
  21. # include <sys/time.h>
  22. # include <sys/limits.h>
  23. # include <time.h>


  24. using namespace std;

  25. pthread_mutex_t pinmutex = PTHREAD_MUTEX_INITIALIZER;
  26. pthread_attr_t tattr;


  27. class Pin
  28. {
  29. public:
  30.     Pin() {};
  31.     Pin (int id, string nm)
  32.     {
  33.         fd = id;
  34.         name = nm;
  35.     }
  36.     Pin(int id, char* nm)
  37.     {
  38.         fd = id;
  39.         name = nm;
  40.     }
  41.     int fd;
  42.     string name;
  43. };
  44. map<int, Pin> pins;

  45. class PEMutex
  46. {
  47.     private:
  48.     pthread_mutex_t& _mutex;

  49.     public:
  50.     PEMutex(pthread_mutex_t& mutex):_mutex(mutex)
  51.     {
  52.         pthread_mutex_lock(&mutex);
  53.     }
  54.     ~PEMutex()
  55.     {
  56.         pthread_mutex_unlock(&_mutex);
  57.     }
  58. };


  59. void* threadfunc(void* apin)
  60. {
  61.     pthread_detach(pthread_self());
  62.     Pin* pin = (Pin*)apin;
  63.     int fd = pin->fd;

  64.     char buf[] = "hello client";
  65.     send(fd, buf, strlen(buf), 0);
  66.     cout << buf << ":" << pthread_self() << endl;
  67.     close(fd);
  68.     {
  69.         PEMutex mutex(pinmutex);
  70.         pins.erase(fd);
  71.     }
  72.     pthread_exit(0);
  73.     return NULL;
  74. }


  75. int main(int argc, char** argv)
  76. {
  77.     int    clientsocket;
  78.     int    serversocket;
  79.     int port = 14000;
  80.     socklen_t client_len;
  81.     struct sockaddr_in client, LSocket;
  82.     int ret;

  83.     pthread_attr_init (&tattr);
  84.     pthread_attr_setstacksize (&tattr, PTHREAD_STACK_MIN * 1024);

  85.     memset(&LSocket, 0, sizeof(LSocket));
  86.     LSocket.sin_family = AF_INET;
  87.     LSocket.sin_port = htons(port);
  88.     serversocket = socket (AF_INET, SOCK_STREAM, 0);
  89.     if (serversocket < 0)
  90.     {
  91.         cout << "socket failed:"<< errno << endl;
  92.         exit(1);
  93.     }
  94.     int option = 1;
  95.     if (setsockopt (serversocket, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option) ) < 0 )
  96.     {
  97.         cout << "setsockopt failed:"<< errno << endl;
  98.         exit(1);
  99.     }
  100.     ret = bind (serversocket, (struct sockaddr *)&LSocket, sizeof(struct sockaddr));
  101.     if (ret < 0) {
  102.         cout << "bind failed:"<< errno << endl;
  103.         exit(1);
  104.     }
  105.     listen(serversocket, 5);
  106.     client_len = sizeof(struct sockaddr_in);
  107.     cout << "start listen at:" << port << endl;

  108.     while(1) {
  109.         clientsocket = accept(serversocket, (struct sockaddr *)&client, &client_len);
  110.         if (clientsocket > 0)
  111.         {
  112.             stringstream portSS;
  113.             portSS << ntohs (client.sin_port);
  114.             string cliName = string(inet_ntoa (client.sin_addr)) + ":" + portSS.str();
  115.             cout << cliName << endl;
  116.             {
  117.                 PEMutex mutex(pinmutex);
  118.                 Pin pin(clientsocket, cliName.c_str());
  119.                 pins[clientsocket] = pin;
  120.                 pthread_t hdl;
  121.                 int ret = pthread_create(&hdl, &tattr, threadfunc, &pins[clientsocket]);
  122.             }
  123.         }
  124.         else
  125.         {
  126.             cout << "accept failed:" << errno << endl;
  127.             sleep (1);
  128.         }
  129.     }

  130.     return 0;
  131. }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2