IPC-using fork() in a loop


 
Thread Tools Search this Thread
Top Forums Programming IPC-using fork() in a loop
# 1  
Old 09-01-2010
IPC-using fork() in a loop

I need to write a program which creates some n number of processes first and also creates a fifo for each of them, then it connects to the server and..

I tried creating these processes with fork() in a for loop but it doesn't print out what i write inside the child..
Code:
   for(int count = 0; count < nIntClientCount; count++)
   {
      pid_t pid;

      if((pid = fork()) < 0){
          perror("fork failed\n");
        exit(1);
      }else if(pid == 0)
      {   
         if((pid = fork()) < 0)
         {
                  perror("inner fork failed\n");
                    exit(1);
         }
         else if(pid > 0)
         exit(0);
          //second child body

            printf("I am CHILD %d", count);
      }
     if(waitpid(pid, NULL, 0) != pid) 
    perror("wait pid didn't match\n");
   }


   

  sleep(2);
  printf("parent exited..\n");

  return 0;

//--------------------------
i also tried the code without using 2 forks, but it also didnt print anything, plz help me



Moderator's Comments:
Mod Comment Please use code tags, thank you

Last edited by Franklin52; 09-01-2010 at 08:31 AM..
# 2  
Old 09-01-2010
Try to add
Code:
fflush(stdout);

After each call of printf()
# 3  
Old 09-01-2010
fflush(stdout) does not even help

Quote:
Originally Posted by dawwin
Try to add
Code:
fflush(stdout);

After each call of printf()
I did but again i get the same result, printf does not print anything and i don't know what happens to the childs...

[root@localhost Randomized Dissemination]# ./a.out
downloader peers randomly selected
parent exited..
# 4  
Old 09-01-2010
After add
Code:
exit(0);

after
Code:
printf("I am CHILD %d\n", count);

It prints this on my computer
Code:
I am CHILD 0
I am CHILD 1
I am CHILD 2
parent exited..

What system you've got?
# 5  
Old 09-01-2010
I tried it both on CentOS 5.2 and Ubuntu

plz have a look at my code again

Code:
   for(int count = 0; count < nIntClientCount; count++)
   {
      pid_t pid;

      if((pid = fork()) < 0){
          perror("fork failed\n");
            exit(1);
      }else if(pid == 0)
      {
         if((pid = fork()) < 0)
         {
                          perror("inner fork failed\n");
                                exit(1);
         }
         else if(pid > 0)
                exit(0);
          //second child body

            printf("I am CHILD %d\n", count);
            fflush(stdout);
      }
     if(waitpid(pid, NULL, 0) != pid)
        perror("wait pid didn't match\n");
   }

  printf("parent exited..\n");

  return 0;
}


Last edited by pludi; 09-01-2010 at 12:42 PM..
# 6  
Old 09-01-2010
Moderators have had to edit you for code tags so many times you've got negative bits. The formatting on your code is also all ruined because you didn't use code tags. PLEASE USE CODE TAGS. IT'S NOT HARD.

You haven't posted the complete program, either, I'm having to write a large part of it myself to make it work.

Your program, with nIntClientCount=3, gives this output:
Code:
I am CHILD 0
wait pid didn't match
: No child processes
I am CHILD 1
I am CHILD 1
wait pid didn't match
: No child processes
wait pid didn't match
: No child processes
I am CHILD 2
I am CHILD 2
wait pid didn't match
: No child processes
wait pid didn't match
: No child processes
parent exited..
parent exited..
parent exited..
parent exited..
I am CHILD 2
wait pid didn't match
: No child processes
parent exited..
I am CHILD 2
parent exited..
wait pid didn't match
: No child processes
parent exited..
parent exited..

# 7  
Old 09-01-2010
i cannot go inside child process

I just want to create n number of childs and after each child would start executing some instruction, but when i execute the program i don't receive anything, I wonder how you get the result.
my linux system is
Code:
CentOS 5.2

do u know how i can solve the problem?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

IPC settings on HP-UX

Hi Experts, Need your help for checking te interprocess communications settings on HP-UX box. Using ipcs command I am able to view Message queue,semapohores etc, but from that output I m not able to understand how to determine if there is any issue with ipc settings and how to resolve that? (1 Reply)
Discussion started by: sai_2507
1 Replies

2. Programming

C Socket Client/Server Fork Loop

Hello people. I'm trying to do something like a search engine. Server runs in the background by using ./server & which has data from a textfile stored in an array. Client then runs by using ./client It will then prompt "Search for:" For example, if I searched for Products called Instant... (0 Replies)
Discussion started by: andylbh
0 Replies

3. Shell Programming and Scripting

Inifinite Loop on fork

I am trying to ping through a list of devices in parallel. When the code below is executed, I enter a infinite loop. I want to used the number of lines in the file as my boundry. I am a perl rookie and just cant see whats wrong. would appreciate help ... use Net::Ping; # $filename... (0 Replies)
Discussion started by: popeye
0 Replies

4. Solaris

errors on Netra-440: "IPC Warning: ipc: tcp_protocol: bad magic number"

I was asked to look into a problem with a Sun Netra 440 in another department. On the server in question, the relevant 'uname -a' information is, "SunOS host1 5.9 Generic_118558-16 sun4u sparc SUNW,Netra-440". That information aside, while the other admin is logged into the ALOM, these errors are... (0 Replies)
Discussion started by: Borealis
0 Replies

5. Programming

[C] Problem with IPC

Hi! I'm trying to write this program: in my intentions it should get a message and send it to a second process (pid_upost), then to a third process (pid1, pid2, pid3, depending on the choice made when a new message is inserted). This program should write the message in a file (message1, message2 or... (1 Reply)
Discussion started by: Kaminski
1 Replies

6. UNIX for Dummies Questions & Answers

Ipc Details

hai, i am doing my masters degree in computers.please any one tell me about fork(),semaphores,mutex,messaging queues,messaging using pipes ,and msgget(),msgrecv() funtions in ipc programming . i have exam on that i have a book but in that they not given clearly. hope u will ... (2 Replies)
Discussion started by: G.Vishnuvardhan
2 Replies

7. UNIX for Dummies Questions & Answers

Ipc

I have a parent that is passing data to child A and then child A has to process it and pass to child B. I am able to pass the data to child A but am not able to pass it to child B. Child B seems to only be receiving the last data instead of the whole data. I saw one example in a book but it uses... (1 Reply)
Discussion started by: scmay
1 Replies

8. Programming

fork() in for loop

hello, Every time i use fork() in the for loop, my college network(which i work in) either gets slow or hangs up.Can any 1 explain why it is so? Of course there is no use of doing it though. But still i want to clear my doubt. Thanks (2 Replies)
Discussion started by: cyno
2 Replies

9. Shell Programming and Scripting

Endless loop - Fork function failed?

I need a quick script that will serve as a sort of "real time monitor" for watching some log files. I am using Bourne shell in HP-UX 10.20. I have basically created a script that never ends, unless of course I manually terminate it. Here's the script (it's called qhistory): clear echo "REAL... (3 Replies)
Discussion started by: cdunavent
3 Replies

10. Programming

fork() and IPC

Hey all. I recently ported a multithreaded program from WinNT to LINUX using pthreads. Everything works fine, except that it turns out the drivers for the hardware I'm working with aren't thread safe. So, now I have to find a way to make this multi-process instead of multi-threaded. I just... (1 Reply)
Discussion started by: TelePlayer
1 Replies
Login or Register to Ask a Question