473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Threads don't seem to be cleaned up

jlm699
314 100+
Greetings all,

I have this server (running on Linux 2.6.17-gentoo-r9) that I've been tinkering with and I attempted to do a multi-threaded model to accept multiple connections, however I found that it was leaving zombie processes and not cleaning up after itself at all. The main thread was doing a waitpid((pid_t)-1, Null, WNOHANG) and keeping track of the number of clients it created/cleaned up. Since that did not work as I had expected I decided to try and allow only a single connection (ie, wait(NULL) causing the main thread to not listen for client connections until it cleaned up its child). While this works great and appears to clean up children when monitoring ps, when I watch 'top' it seems that used memory just constantly increases once the first client thread is spawned. After it is closed the used memory goes down a little, but then ever-so-slowly increases. If I remove the client threading all together the used memory does not fluctuate. Could somebody please peruse this threading model code I have posted below and tell me if I'm doing something blatently dumb?! I would greatly appreciate it!

Expand|Select|Wrap|Line Numbers
  1. int main(char argc, char *argv[])
  2. {
  3.     pid_t    pid;
  4.     pid_t    process_id;
  5.     struct    sigaction sa;
  6.     int    error;
  7.     int    sockfd;
  8.     int    ss_fd;
  9.     char    response[MAX_MSG_BUFF_SIZE];
  10.  
  11.  
  12.     sprintf(VersionString, "Daemon v %d.%d.%d", MajorVersion, 
  13.         MinorVersion, PatchVersion);                
  14.  
  15.     /* Spawn the daemon */
  16.     pid = fork();
  17.  
  18.     if (pid < 0) {
  19.         printf("Failed PID #0\n");
  20.         exit(EXIT_FAILURE);
  21.     }
  22.     else if (pid > 0) {
  23.         exit(EXIT_SUCCESS);
  24.     }
  25.  
  26.     sid = setsid();
  27.     if (sid < 0) {
  28.         printf("Failed SID\n");
  29.         exit(EXIT_FAILURE);
  30.     }
  31.  
  32.     /* Set up and install handler for signal SIGTERM */
  33.     memset(&sa, 0, sizeof(sa));
  34.     sa.sa_handler = &sigterm_handler;
  35.     sigemptyset(&sa.sa_mask);
  36.     sa.sa_flags = 0;
  37.     sigaction(SIGTERM, &sa, NULL);
  38.  
  39.     /* Set up and install handler for signal SIGINT */
  40.     memset(&sa, 0, sizeof(sa));
  41.     sa.sa_handler = &sigint_handler;
  42.     sigemptyset(&sa.sa_mask);
  43.     sa.sa_flags = 0;
  44.     sigaction(SIGINT, &sa, NULL);
  45.  
  46.     ss_fd = ds_open_server(DSTREAM_PORT);
  47.     if (ss_fd == -1) {
  48.         printf("ds_open_server socket error\n");
  49.         exit(1);
  50.     }
  51.  
  52.     while (!quit_daemon)
  53.     {
  54.         sockfd = 0;
  55.         if ((sockfd = ds_listen_for_client(ss_fd, BACKLOG)) > 0)
  56.         {
  57.             if (client_cnt > 5)
  58.             {
  59.                 strcpy(response, "MAX_CLIENTS");
  60.                 ds_send(sockfd, response, strlen(response));
  61.                 ds_close(sockfd);
  62.                 sleep(SLEEP_TIME);
  63.             }
  64.             else
  65.             {
  66.                 pid = fork();
  67.                 if (pid > 0)
  68.                 {
  69.                     client_cnt++;
  70.  
  71.                     while (client_cnt)
  72.                     {
  73.                         process_id = wait(NULL);
  74.                         /* This was causing zombie processes */
  75.                         //process_id = waitpid((pid_t) -1, NULL, WNOHANG); /* non-blocking wait */
  76.                         if (process_id < 0)
  77.                         {    /* waitpid error */
  78.                             perror("waitpid");
  79.                         }
  80.                         else if (process_id == 0)
  81.                         {    /* no zombies to wait on */
  82.                             break;
  83.                         }
  84.                         else
  85.                         {
  86.                             client_cnt--;
  87.                             //printf("Stopped child process %d\n", process_id);
  88.                         }
  89.                     }
  90.                 }
  91.                 else
  92.                 {
  93.                     if ((error = StartDataStreamer(sockfd)))
  94.                     {
  95.                         printf("Exiting with error = %d\n", error);
  96.                         StopDataStreamer();
  97.  
  98.                         exit(EXIT_FAILURE);
  99.                     }
  100.                     else
  101.                     {
  102.                         // printf("Datastreamer started successfully:  %d\n", sockfd);
  103.                         while (!quit_client)
  104.                         {
  105.                             // printf("sleeping client %d\n",sockfd);
  106.                             sleep(SLEEP_TIME);
  107.                         }
  108.                         exit(EXIT_SUCCESS);
  109.                     }    // end else
  110.                 }    // end else    
  111.             }    // end else
  112.         }    // end if
  113.     }    // end while
  114.  
  115.     ds_close_server(ss_fd);
  116.     exit(EXIT_SUCCESS);
  117. }    // end main
And by the way, the ds_* functions are just wrappers I made to simplify the amount of typing for sending/receiving/opening connections etc. And then StartDataStreamer is the actual code that does the work of transferring data back and forth from my other computer. StopDataStreamer just shuts everything down (raising quit_daemon). I don't see why this would cause a memory leak, and also if anybody has any idea why the original waitpid method was leaving zombies I'd love to hear your thoughts.

Thanks in advance!

James
Aug 9 '07 #1
0 1289

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Rohit | last post by:
Hi, Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak "Event" handles, by Event handles I mean Win32 Event handles which can be monitored using the ProcessExplorer from...
28
by: Dennis Owens | last post by:
I am trying to run a thread off of a form, and every once in a while the thread will raise an event for the form to read. When the form gets the event, the form will place the event into a dataset...
2
by: Jagadish | last post by:
Hi, My application requires to create a few threads on the occurance of an event E1 and the same threads need to be aborted on the occurance of another event E2. Iam not using .Net thread pool...
8
by: Martin Maat | last post by:
I am puzzled. I have this object that uses a thread. The thread is encapsulated by the object, the object has Start and Stop methods to enable the client to start or stop the thread. I found...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
7
by: gel | last post by:
Hi all I am attempting to understand threads to use in a network app which I am writing. The part that I am going to use threads on is run on the clients/workstations. I will monitor all...
35
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? ...
1
by: sharekhan | last post by:
Hello, I have written a simple relay server. using pthreads. To handle multiple clients. Design wise its like this.. (my humble apologies if the post is too long) The thread design was...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.