473,406 Members | 2,336 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,406 software developers and data experts.

Daemon Server, Forking, Defunct Processes

I am writing a server daemon that forks a child for every incoming
request. The child process terminates when the client closes the
connection. My problem is that I am unsure how to prevent the child
process from becoming defunct. Here is an over-simplified main
function...

int main(void) {

// Daemonize
int pid;
if ((pid = fork()) != 0) {
exit(0);
}
setsid();
if ((pid = fork()) != 0) {
exit(0);
}

// Assume this gets the socket descriptor, binds
// the socket and listens on given port.
int server_socket = create_server_socket(1234);

int client_socket;

while (1) {
// Assume this accepts a request to the server socket
client_socket = accept_socket_request(server_socket);

if (!fork()) {
// Child

// Assume this initiates a text interaction
// with the client, looping until the client
// enters 'exit'. Then it returns.
session();

// Close the client connection
shutdown(client_socket, 2);

// The child exits
exit(0);

} else {

// Parent
waitpid(-1, NULL, WNOHANG);
}
}

// Close the server socket
shutdown(server_socket, 2);
return 0;
}

What is happening is that if a client terminates a child process the
parent isn't aware until a new connection/process is made. I think
this is because of my placement of waitpid(). The result is that
there is a defunct child sitting around until a new connection is
made, then the defunct process is cleaned up. There is always at
least 1 defunct process lying around. How can I remedy this?

Thanks,
Scott
Sep 11 '08 #1
3 5175
>I am writing a server daemon that forks a child for every incoming
>request. The child process terminates when the client closes the
connection. My problem is that I am unsure how to prevent the child
process from becoming defunct.
This problem is more appropriate for comp.unix.programmer.

A child process *ALWAYS* becomes defunct, at least briefly.
>Here is an over-simplified main
function...

int main(void) {

// Daemonize
int pid;
if ((pid = fork()) != 0) {
exit(0);
}
setsid();
if ((pid = fork()) != 0) {
exit(0);
}

// Assume this gets the socket descriptor, binds
// the socket and listens on given port.
int server_socket = create_server_socket(1234);

int client_socket;

while (1) {
// Assume this accepts a request to the server socket
client_socket = accept_socket_request(server_socket);

if (!fork()) {
// Child

// Assume this initiates a text interaction
// with the client, looping until the client
// enters 'exit'. Then it returns.
session();

// Close the client connection
shutdown(client_socket, 2);

// The child exits
exit(0);

} else {

// Parent
waitpid(-1, NULL, WNOHANG);
You might want to loop here until waitpid returns something other than
a process ID, to clean up multiple children at one time.
> }
}

// Close the server socket
shutdown(server_socket, 2);
return 0;
}

What is happening is that if a client terminates a child process the
parent isn't aware until a new connection/process is made. I think
this is because of my placement of waitpid(). The result is that
there is a defunct child sitting around until a new connection is
made, then the defunct process is cleaned up.
>There is always at
least 1 defunct process lying around.
Please explain why this is a problem. (Ever-increasing numbers of
defunct children are a problem, but that's not the issue here).
>How can I remedy this?
If you do not care about getting the status of a terminated child,
nor do you want to keep track of which children have terminated,
something like:

signal(SIGCHLD, SIG_IGN);
OR
signal(SIGCLD, SIG_IGN);

in the parent may be appropriate (chances are your system has only
one of these signals). Beware that passing this state on to a child
can make it malfunction if it has children and doesn't expect this,
so set the signal handling back to SIG_DFL after the fork() in the
child process.

Sep 11 '08 #2
On 11 Sep 2008 at 4:40, Gordon Burditt wrote:
>>There is always at least 1 defunct process lying around. How can I
remedy this?

If you do not care about getting the status of a terminated child,
nor do you want to keep track of which children have terminated,
something like:

signal(SIGCHLD, SIG_IGN);
OR
signal(SIGCLD, SIG_IGN);

in the parent may be appropriate (chances are your system has only
one of these signals).
It may be worth pointing out that pre-2001 versions of POSIX allowed
terminated child processes to become zombies even if the disposition of
SIGCHLD is set to SIG_IGN - I seem to recall that Linux up to 2.4.* had
this behavior.

Sep 11 '08 #3
Scottman wrote:
>
I am writing a server daemon that forks a child for every incoming
request. The child process terminates when the client closes the
connection. My problem is that I am unsure how to prevent the
child process from becoming defunct. Here is an over-simplified
main function...
The C language doesn't contain forks, child processes, etc. That
makes this off-topic on c.l.c. Try comp.unix.programmer.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 11 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rob Hunter | last post by:
Hi all. I want to run a Python daemon that manages a "to-do" queue. I want it so that the daemon is always running, where its running consists of looking at the queue to see if it has any jobs,...
1
by: Bob Swerdlow | last post by:
I've created a Python daemon that starts a bunch of BitTorrent downloader process. Everything is working fine when I start the daemon by hand (logged on as root). I can quit the session and see...
1
by: Matt Stevens | last post by:
Hello, I'm having some trouble trying to get this simple forking http daemon to work, when I run it everything seems to run ok and then when I try to connect to it with a browser on another...
6
by: bcanavan | last post by:
Given a small number(maybe 4-5) of simulataneous remote users, can an app be run to provide "live" snapshot reports to snapviewer? If so, 1. Does the app have to run continuously? (I would...
3
by: felixfix | last post by:
Hi all, I am just wondering if something is wrong with my program. What it bascially does is to output a fibonacci sequence base on the command-line output. If I give a 5, it will generate the...
3
by: czajnik | last post by:
Hi! I'm quite new to Python development. Can someone advise me a framework useful for building (pre-)forking or threaded TCP servers, other than SocketServer ? I've seen source code of a few...
2
by: Reid Priedhorsky | last post by:
Hi folks, I am implementing a forking SocketServer daemon that maintains significant internal state (a graph that takes ~30s to build by fetching from a SQL database, and eventually further...
10
by: qwertycat | last post by:
I'm new to multi-process programming, should one avoid forking children from children of a parent? I'd like to spawn 10 children from the parent and each of those children spawns another 5...
6
by: Johny | last post by:
Is it possible to run a Python program as daemon? Thanks
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.