Thread: Restricting the no of child processses getting created

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Restricting the no of child processses getting created

    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

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Well you could just keep a counter of how many you create,
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Might wanna upgrade that counter to a mutex/lock/semaphore
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Can you be little more elaborative on this counter?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you create a process, count up. When a process dies (SIG_CHILD) count down.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    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.

    Code:
    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);
    	}
    }
    Thanks

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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);
    Why do you fork YET AGAIN?


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ss_ss View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Process request handles the databse connection & retrives the data from the database.

    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.
    Can i have some lines of the code for the above statements which will help in moving further.

    Thanks

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. custom created child windows
    By alandrums in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2002, 06:04 PM