Help with child counter

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 13
Reputation: roby4eldiablo is an unknown quantity at this point 
Solved Threads: 0
roby4eldiablo roby4eldiablo is offline Offline
Newbie Poster

Help with child counter

 
0
  #1
Mar 24th, 2007
  1. servaddr.sin_port = htons(ADD_PORT);
  2.  
  3. Bind (listenfd, (SA*) &servaddr, sizeof(servaddr));
  4.  
  5. Listen (listenfd, LISTENQ);
  6.  
  7. #ifdef TRACE
  8. err_msg ("(%s) info - socket created, port number= %ld, child number= %ld, waiting for connections ...\n", prog, ADD_PORT, child_n);
  9. #endif
  10.  
  11. /* viene detto a signal come reagire se arriva il segnale SIGCHLD, cioè avvia sigchld_h*/
  12. signal (SIGCHLD, sigchld_h);
  13.  
  14. while (1)
  15. {
  16. cliaddrlen = sizeof(cliaddr);
  17. /**/
  18. /**/
  19. /* qua controlla il numero di figli attualmente generati e sceglie se eseguire l'accept o meno*/
  20. /* se il num di figli non è il massimo, allora invia 0 al client, altrimenti invia 255 e fai la close*/
  21. /**/
  22. connfd = Accept (listenfd, (SA*) &cliaddr, &cliaddrlen);
  23.  
  24. /*fa la accept x inizializzare connfd, poi controlla se va bene e se num max figli chiude il canale*/
  25. if ((n_child<=child_n) && (n_child<=MAX_CHILD))
  26. {
  27. memset(buf,0,sizeof(uint8_t));
  28. Writen (connfd, buf, 1);
  29.  
  30. }
  31. else
  32. { memset(buf,1,sizeof(uint8_t));
  33. Writen (connfd, buf, 1);
  34. Close (connfd);
  35. exit (0);
  36. }
  37.  
  38. /**/
  39. /**/
  40. /**/
  41. /**/
  42. /**/
  43.  
  44. #ifdef TRACE
  45. err_msg ("(%s) info - new connection from client %s:%u", prog, inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
  46. #endif
  47.  
  48.  
  49. /* una volta accettata la connessione, viene generato un figlio e viene passato a lui il comando di eseguire add_server*/
  50. if ((pid = fork()) == 0) // this is the child
  51. {
  52. n_child+=1; /*incremento contatore figli*/
  53. Close (listenfd);
  54. /* avvia add server*/
  55. add_server (connfd);
  56. calcola (connfd);
  57. /* chiude la connessione*/
  58. Close (connfd);
  59. n_child-=1; /*decremento contatore dei figli*/
  60. }
  61.  
  62. else // this is the parent
  63. {
  64. Close (connfd);
  65.  
  66. #ifdef TRACE
  67. err_msg ("(%s) info - child %d spawned", prog, pid);
  68. #endif
  69.  
  70. }
  71. }
  72. exit (0);
  73. }
This is a part of main into a server.
I want control the spawn of new child.
I want use n_child increment and decrement to limit it.
In theory when a child is spawned the program must increment the nchild, when the child finish his work, there's the decrementation.

Why don't run?
where i must put n_child+=1 and n_child-=1???

before the fork, at the exit of the fork? where?
Last edited by roby4eldiablo; Mar 24th, 2007 at 5:25 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Help with child counter

 
0
  #2
Mar 24th, 2007
What is done in this part of the code is correct:
  1. if ((pid = fork()) == 0) // this is the child
  2. {
  3. n_child+=1; /*incremento contatore figli*/
  4. Close (listenfd);
  5. /* avvia add server*/
  6. add_server (connfd);
  7. calcola (connfd);
  8. /* chiude la connessione*/
  9. Close (connfd);
  10. n_child-=1; /*decremento contatore dei figli*/
  11. }
  12. else // this is the parent
  13. {
  14. Close (connfd);
  15.  
  16. #ifdef TRACE
  17. err_msg ("(%s) info - child %d spawned", prog, pid);
  18. #endif
  19.  
  20. }
What is done is n_child is incremented IN CHILD as first thing and decremented IN CHILD as teh last thing.
If there is some problem it lies somewhere else in teh code. I'm unable to understand due to non-english language.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC