Hi,
I wanted to know that, is there any way to restrict the number of child processes getting created as in my program the child processes are keep on getting created and end up taking the complete cpu usage.
Thanks
Printable View
Hi,
I wanted to know that, is there any way to restrict the number of child processes getting created as in my program the child processes are keep on getting created and end up taking the complete cpu usage.
Thanks
Well you could just keep a counter of how many you create,
Might wanna upgrade that counter to a mutex/lock/semaphore
Can you be little more elaborative on this counter?
When you create a process, count up. When a process dies (SIG_CHILD) count down.
--
Mats
In the following code the parent process forks child processes to handle the incoming requests.
All the processes are getting terminated also but by the time they are getting terminated the parent forks more child processes which is resulting in utilizing the whole cpu usage.
ThanksCode:int InitServerSocket(int args, char *argv[])
{
int addrlen, result;
memset ((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
memset ((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
myaddr_in.sin_family = AF_INET;
myaddr_in.sin_addr.s_addr = INADDR_ANY;
sp = getservbyname ("bod", "tcp");
if (sp == NULL) {
fprintf(stderr, "%s: example not found in /etc/services\n",
argv[0]);
exit(1);
}
myaddr_in.sin_port = sp->s_port;
ls = socket (AF_INET, SOCK_STREAM, 0);
if (ls == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to create socket\n", argv[0]);
exit(1);
}
if (bind(ls, &myaddr_in, sizeof(struct sockaddr_in)) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to bind address\n", argv[0]);
exit(1);
}
if (listen(ls, 5) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to listen on socket\n", argv[0]);
exit(1);
}
setpgrp();
switch (fork()) {
case -1: /* Unable to fork, for some reason. */
perror(argv[0]);
fprintf(stderr, "%s: unable to fork daemon\n", argv[0]);
exit(1);
case 0:
fclose(stdin);
fclose(stderr);
signal(SIGCLD, SIG_IGN);
for(;;) {
addrlen = sizeof(struct sockaddr_in);
s = accept(ls, &peeraddr_in, &addrlen);
if ( s == -1) exit(1);
switch (fork()) {
case -1: /* Can't fork, just exit. */
exit(1);
case 0: /* Child process comes here. */
result = ProcessRequest(s);
SendReply(s, result);
exit(0);
default:
close(s);
}
}
default: /* Parent process comes here. */
exit(0);
}
}
Why do you fork YET AGAIN?Code:switch (fork()) {
case -1: /* Can't fork, just exit. */
exit(1);
case 0: /* Child process comes here. */
result = ProcessRequest(s);
SendReply(s, result);
exit(0);
--
Mats
The another fork is forking the child processes because there are so many requests in a day if i dont fork the child process then response time will increase.
I do confess I'm not very good with network programming, but creating a child for each request seems a bit weird - creating a child that handles a particular client session is fine, but creating one for every request does seem a bit excessive to me.
Does ProcessRequest() performs some serious work?
Anyway, if you have a SIG_CHILD signal handler, and you count up a counter in the main thread each time you do a fork, then count it down in the SIG_CHILD handler. Then if you have got "too many children", you simply don't create a new process until you have exited one of the children - you can probably use a semaphore to wait on the relevant sigchild.
--
Mats
Process request handles the databse connection & retrives the data from the database.
Can i have some lines of the code for the above statements which will help in moving further.Code:Anyway, if you have a SIG_CHILD signal handler, and you count up a counter in the main thread each time you do a fork, then count it down in the SIG_CHILD handler. Then if you have got "too many children", you simply don't create a new process until you have exited one of the children - you can probably use a semaphore to wait on the relevant sigchild.
Thanks
Actually, it would have to be the code that creates the process that you posted the code for that does the counting, since your current code does exit(0) when it has forked the process. So whatever creates that process would have to count how many it has created, and then throttle as necessary.
--
Mats