I'm not sure that I even bark on the right tree here ... but here it goes.
I am trying to pass data from my parent process to all children. This is a simple server program that basically saves a list of connected clients, and then sends a routing table of connected clients to each client. Ultimately, this will include a structure of information about each client ... but now I just want to get every forked process to get the same information from the parent.
In the parent process, I first configured my pipes and set them to non-blocking (if there is no new data in the pipe). After a connection is established with the client, the number of variable elements increases to reflect this new connection. Then I split the child process into a new function and update the array of arrays with the new number of records in the table (I have 10 pipes at the moment to check if I need to maintain a separate channel for each child).
pid_t pid; int numchildren; int i, n; /* Create the pipes. */ for(i = 0; i < 10; i++) { if (pipe (mypipe[i])) { fprintf (stderr, "Pipe failed.\n"); return EXIT_FAILURE; } } for(i = 0; i < 10; i++) { for(n=0; n<2; n++) { // Get previous flags int f = fcntl(mypipe[i][n], F_GETFL, 0); // Set bit for non-blocking flag f |= O_NONBLOCK; // Change flags on fd fcntl(mypipe[i][n], F_SETFL, f); } //close(mypipe[i][0]); } pid = fork(); if (pid == (pid_t) 0) { close (mypipe[numentries-1][1]); recievecmds(new_fd, mypipe[numentries-1][0]); close(new_fd); return EXIT_SUCCESS; } else if (pid < (pid_t) 0) { fprintf (stderr, "Fork failed.\n"); return EXIT_FAILURE; } else { sprintf (buf,"%d",numentries); for(i = 0; i < 10; i++) write(mypipe[i][1], buf, strlen(buf)); memset(&buf, 0, sizeof buf); }
And then I try to read Whats in the pipe in the recievecmds () function:
nbytes = read(mypipe[childindex][0], buf, sizeof(buf));
The first connected client tells me numentries = 1, the second client tells me numentries = 2, and so on. I mean, I donβt even see the point in the pipe, because it seems that everything I put into the pipe can simply transfer it to the function that I called on the plug. Am I really wrong? It was very unpleasant, trying to figure it out. How can I control all my child processes from my parent process at the same time?
Thank you so much in advance.
edit - My main problem was that I updated the channel every time in an infinite while loop. A very stupid mistake, I immediately realized that this is probably the root of my problem. However, although now the first child / pipe combination contains the correct data ... the second is not. I'll see if I can figure it out on my own, thanks for the tip!
Of course, now I am facing problems, because I manually select the option to output data from the channel. I am going to come up with a way, perhaps either to get data for all pipes every time it is updated, or to get only the latest data (maybe only one char at a time).
Thanks for sharing with me guys! And I apologize for not publishing the whole program ... but there is very little. I definitely should have mentioned that I have this in an infinite loop.