0
servaddr.sin_port = htons(ADD_PORT);

  Bind (listenfd, (SA*) &servaddr, sizeof(servaddr));

  Listen (listenfd, LISTENQ);

#ifdef TRACE
  err_msg ("(%s) info - socket created, port number= %ld, child number= %ld, waiting for connections ...\n", prog, ADD_PORT, child_n);
#endif

/* viene detto a signal come reagire se arriva il segnale SIGCHLD, cioè avvia sigchld_h*/
  signal (SIGCHLD, sigchld_h);

  while (1)
  {
    cliaddrlen = sizeof(cliaddr);
    /**/    
    /**/
    /* qua controlla il numero di figli attualmente generati e sceglie se eseguire l'accept o meno*/
    /* se il num di figli non è il massimo, allora invia 0 al client, altrimenti invia 255 e fai la close*/
    /**/
    connfd = Accept (listenfd, (SA*) &cliaddr, &cliaddrlen);

    /*fa la accept x inizializzare connfd, poi controlla se va bene e se num max figli chiude il canale*/
    if ((n_child<=child_n) && (n_child<=MAX_CHILD))
    {
        memset(buf,0,sizeof(uint8_t));    
        Writen (connfd, buf, 1);    
    
    }
    else
    {    memset(buf,1,sizeof(uint8_t));    
        Writen (connfd, buf, 1);
        Close (connfd);
        exit (0);
    }
    
    /**/
    /**/
    /**/
    /**/
    /**/

#ifdef TRACE
    err_msg ("(%s) info - new connection from client %s:%u", prog, inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
#endif

    
/* una volta accettata la connessione, viene generato un figlio e viene passato a lui il comando di eseguire add_server*/
    if ((pid = fork()) == 0) // this is the child
    {
      n_child+=1; /*incremento contatore figli*/
      Close (listenfd);
/* avvia add server*/
      add_server (connfd);
      calcola (connfd);    
/* chiude la connessione*/
      Close (connfd);
      n_child-=1; /*decremento contatore dei figli*/ 
    }

    else // this is the parent
    { 
      Close (connfd);

#ifdef TRACE
      err_msg ("(%s) info - child %d spawned", prog, pid);
#endif

    }
  }
  exit (0);
}

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?

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by thekashyap
0

What is done in this part of the code is correct:

if ((pid = fork()) == 0) // this is the child
{
    n_child+=1; /*incremento contatore figli*/
    Close (listenfd);
    /* avvia add server*/
    add_server (connfd);
    calcola (connfd);    
    /* chiude la connessione*/
    Close (connfd);
    n_child-=1; /*decremento contatore dei figli*/ 
}
else // this is the parent
{ 
    Close (connfd);

    #ifdef TRACE
    err_msg ("(%s) info - child %d spawned", prog, pid);
    #endif

}

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.