944,184 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 847
  • C RSS
Mar 24th, 2007
0

Help with child counter

Expand Post »
  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.
Similar Threads
Reputation Points: 17
Solved Threads: 0
Newbie Poster
roby4eldiablo is offline Offline
13 posts
since Mar 2007
Mar 24th, 2007
0

Re: Help with child counter

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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help needed asap
Next Thread in C Forum Timeline: Recrusive Split on a linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC