| | |
Help with child counter
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 13
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
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); }
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.
What is done in this part of the code is correct:
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.
c Syntax (Toggle Plain Text)
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 }
If there is some problem it lies somewhere else in teh code. I'm unable to understand due to non-english language.
![]() |
Similar Threads
- ASP .NET hit counter? (ASP.NET)
- MDI Child Form Placement (C#)
- ASP .NET database hit counter (ASP.NET)
- Displaying a different bitmap in different child windows? - win32 in C (C)
- Counter Strike issue (Windows Software)
- Page counter print accounting (*nix Software)
- Scrolling MDI Child form (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: Help needed asap
- Next Thread: Recrusive Split on a linked list
| Thread Tools | Search this Thread |
* ansi append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi





