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! - int main(char argc, char *argv[])
-
{
-
pid_t pid;
-
pid_t process_id;
-
struct sigaction sa;
-
int error;
-
int sockfd;
-
int ss_fd;
-
char response[MAX_MSG_BUFF_SIZE];
-
-
-
sprintf(VersionString, "Daemon v %d.%d.%d", MajorVersion,
-
MinorVersion, PatchVersion);
-
-
/* Spawn the daemon */
-
pid = fork();
-
-
if (pid < 0) {
-
printf("Failed PID #0\n");
-
exit(EXIT_FAILURE);
-
}
-
else if (pid > 0) {
-
exit(EXIT_SUCCESS);
-
}
-
-
sid = setsid();
-
if (sid < 0) {
-
printf("Failed SID\n");
-
exit(EXIT_FAILURE);
-
}
-
-
/* Set up and install handler for signal SIGTERM */
-
memset(&sa, 0, sizeof(sa));
-
sa.sa_handler = &sigterm_handler;
-
sigemptyset(&sa.sa_mask);
-
sa.sa_flags = 0;
-
sigaction(SIGTERM, &sa, NULL);
-
-
/* Set up and install handler for signal SIGINT */
-
memset(&sa, 0, sizeof(sa));
-
sa.sa_handler = &sigint_handler;
-
sigemptyset(&sa.sa_mask);
-
sa.sa_flags = 0;
-
sigaction(SIGINT, &sa, NULL);
-
-
ss_fd = ds_open_server(DSTREAM_PORT);
-
if (ss_fd == -1) {
-
printf("ds_open_server socket error\n");
-
exit(1);
-
}
-
-
while (!quit_daemon)
-
{
-
sockfd = 0;
-
if ((sockfd = ds_listen_for_client(ss_fd, BACKLOG)) > 0)
-
{
-
if (client_cnt > 5)
-
{
-
strcpy(response, "MAX_CLIENTS");
-
ds_send(sockfd, response, strlen(response));
-
ds_close(sockfd);
-
sleep(SLEEP_TIME);
-
}
-
else
-
{
-
pid = fork();
-
if (pid > 0)
-
{
-
client_cnt++;
-
-
while (client_cnt)
-
{
-
process_id = wait(NULL);
-
/* This was causing zombie processes */
-
//process_id = waitpid((pid_t) -1, NULL, WNOHANG); /* non-blocking wait */
-
if (process_id < 0)
-
{ /* waitpid error */
-
perror("waitpid");
-
}
-
else if (process_id == 0)
-
{ /* no zombies to wait on */
-
break;
-
}
-
else
-
{
-
client_cnt--;
-
//printf("Stopped child process %d\n", process_id);
-
}
-
}
-
}
-
else
-
{
-
if ((error = StartDataStreamer(sockfd)))
-
{
-
printf("Exiting with error = %d\n", error);
-
StopDataStreamer();
-
-
exit(EXIT_FAILURE);
-
}
-
else
-
{
-
// printf("Datastreamer started successfully: %d\n", sockfd);
-
while (!quit_client)
-
{
-
// printf("sleeping client %d\n",sockfd);
-
sleep(SLEEP_TIME);
-
}
-
exit(EXIT_SUCCESS);
-
} // end else
-
} // end else
-
} // end else
-
} // end if
-
} // end while
-
-
ds_close_server(ss_fd);
-
exit(EXIT_SUCCESS);
-
} // 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 StartDataStream er is the actual code that does the work of transferring data back and forth from my other computer. StopDataStreame r 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
0 1309 Sign in to post your reply or Sign up for a free account.
Similar topics |
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 www.sysinternals.com, or even simply look at
the Handle count in the good old windows TaskManager.
To demonstrate the problem, all I did was created a basic Win Forms
application and with Main implemented as:
|
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 and display it on a datagrid that is on the form. The problem is that the thread will slowly take over all of the processor time. After about 8 events, the form will not even respond anymore. Here is the guts of my test code
// Class and event for...
|
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 threads. Iam using system threads
by using Thread objects and delegates. Threads are aborted
by making an explicit call to Thread.Abort().
Occurance of events E1 and E2 are not under the control of
my application. My application runs stable for a...
|
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 that the object will not be garbage collected while the thread is
running. Fair enough, the documented explanation is that the GC compresses
objects on the heap and needs to update references, the references will be
invalid for a couple of moments...
|
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, the main thread waits. At the
completion of each subthread, the mainthread checks all 12 thread objects to
see if they are done. If they are, raise an event that says we're done.
So, it's kinda like this:
ProcessThread
- Creates a ProcessObject
| |
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 starting and ending
processes. Below is what I have been doing. It looks like only the
first thread is starting. Can someone explain a basic example of how
threads work and are implemented, what is better to use threading or
thread module, and what am...
|
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? I've been leaning
towards threads, I'm going to say why.
Processes seem fairly expensive from my research so far. Each fork
copies the entire contents of memory into the new process. There's also
a more expensive context switch between...
|
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 borrowed from this article
(http://www.codeproject.com/useritems/LikeJavaThreads.asp)
|
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 object should run
in its own thread, it should implement this mix-in class. Does this
sound like plausible design decision?
I'm surprised that C++ doesn't have such functionality, say in its
STL. This absence of a thread/object relationship in...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |