I'm trying to write multithreading TCP Daemon which executes external program when new network connection arrives on a socket.
After accept() I'm doing fork() for initiating of new child process, in which will be executed external program. After child creation I'm doing fork() again, and substitute context of new child process using execv() with context of external program. In the body of parent code I'm doing waitpid() for expectation of finishing of child process. Waitpid() always returns -1 with error code errno=ECHILD (No child processes).
Testing this code without _first_ fork(), everything works perfectly.
May be somebody knows how it should be done? Is it possible to use fork() few times to create child processes and wait finish them into parents by waitpid() ?
Here is code of external program excecution:
This code perfectly works if execute() called within first fork(). But doing another fork() causes waipid retun -1 and errno set to ECHILD.
Nowhere SIGCHLD handler is redefined.
P.S Operational System is FreeBSD 5.3
Last edited by Perderabo; 07-13-2005 at 01:40 PM..
Reason: Add code tags for readability
Process2 is a child for Process1. Process3 is a child for Process2. I do waitpid in Process2 that waits for Process3 finishing. I do not try to wait for grandchild. I try to waitpid in parent process and wait for child finishing.
This can be caused by setting SA_NOCLDWAIT for SIGCHLD or explicitly ignoring SIGCHLD. Do you do either? I am strictly a structured progrommer so I don't have much experience with goto's or longjmp. Consequently, I don't know what happens when you longjmp out of a signal handler. I thought that the rule was longjmp within a handler, but siglongjmp out.
I'm attempting to write a daemon that will start, stop, and monitor processes across a network of servers, meaning that a daemon would start on each server, attempt to connect to siblings at regular intervals (if there are unconnected siblings), and start services as remote dependencies are... (3 Replies)
Hi folks, I am writing a simple program to understand how fork() and waitpid works, but it doesn't seem that is working like I wanted.
if(fork()==0){
//el hijo
pid1=getpid();
printf("\nSoy el hijo %d",pid1);
}else {
//el padre
if (fork()==0) {
//el hijo
pid2=getpid();... (2 Replies)
Hi gurus, I would like to fork more children and then write their return values: so far I tried:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int rv=0, i;
... (5 Replies)
Hello, I'm trying to implement a version of a bucketSort (kinda) server/client, but I'm having a VERY hard time on making the server behave correctly, when talking to the children, after it forks.
The server is kinda big (300+ lines), so I won't post it here, but here's what I'm doing.
1)create a... (8 Replies)
Hi,
thanks for any ideas...
in main() i wrote this code:
...
for(i=0;i<10;i++){
pid=fork();
switch(pid){
case -1 printf("error");
case 0:child();
default:waitpid(pid,NULL,0);
}
}
...
void child(){
SOMETHING;
} (4 Replies)
hi all
About this code
for (i = 1; i < n; i++)
if ((childpid = fork()) <= 0)
break;
I really can't understand the output .
and the way fork () return the value .
how about the process Id ,the child process Id and the parent ID
in this case
so please answer me soon (5 Replies)
Hello all.
I'm developing a filetransfer application, which is supposed to work sort of like dcc, with multiple transfers etc. Now i wonder what the best way to manage the transfers is. Should i fork() for each new transfer, hogging loads of memory or use pthreads? Maybe I can use select to see... (0 Replies)
I run this code, actually I want to both processes print the message from "data". But only one does. What happens? Anyone can help?
#include <stdio.h>
main(){
int fd, pid;
char x;
fd = open("data",0); /* open file "data" */
pid = fork();
if(pid != 0){
wait(0);
... (2 Replies)