The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parent child Relation !! using awk/sed ??? varungupta UNIX for Advanced & Expert Users 0 01-29-2008 10:24 AM
Parent/Child Processes yoi2hot4ya Shell Programming and Scripting 2 05-31-2006 10:27 AM
kill parent and child larry UNIX for Dummies Questions & Answers 4 01-11-2003 08:18 PM
How hard can it be? ps child/parent velde046 Filesystems, Disks and Memory 2 05-25-2002 01:36 PM
what are parent and child processes all about? xyyz UNIX for Dummies Questions & Answers 1 04-26-2002 12:53 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: May 2004
Location: Brazil
Posts: 40
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Problems with child comunicating with parent on fork()

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 pipe using
Code:
 int pipefd[2];
 if(pipe(pipefd)==-1){
    perror("pipe");
    exit(EXIT_FAILURE);
  }
2)all the socket(),bind(),listen() stuff
3)the main loop, now, here's the catch I think
Code:
int done = 0;
.
.
.
while(done!=1){
  clLen = sizeof(clAddr);
  if((clientsd=accept(serversd,(struct sockaddr *)&clAddr,&clLen)) == -1){
    perror("accept");
    close(serversd);
    exit(EXIT_FAILURE);
  }
  if((cpid = fork()) == -1){
      perror("fork");
      close(clientsd);
      close(serversd);
      exit(EXIT_FAILURE);
    }
    if(cpid==0){
     .
     .
     //It keeps reading for comands on a variable named cmd (this works)
     if(strncmp(cmd,"STOP",4)==0){
       write(pipefd[1],"1",1);
       _exit(0);
     }
     }else{
        close(clientsd);
        read(pipefd[0],&tmp,1);
        done=atoi(tmp);
     }
   }
}
Now, if I start this server and have only one connection, and this connection goes DIRECTLY sending a STOP signal, it works, but, if this connection does something else, or there's another connection (let's say a total of 9 connection for instance), then I have to send 9 diferent STOPs signals to actually kill the server. Where I want just to have to send 1 instead. Also, I want that if the client send some data, that the server acnowledges as being the last data it will ever recieve, to send a done to the main server and continue with all the rest (showing the data it recieved for instance). I thought that by using pipes and read/write on the pipe this would work, but as far as I could see, the read() function is a blocking function, and the main server keeps waiting for something to read, and this could be making some trouble?
I'm really out of ideias here, and really newbie when it comes to fork() and signals processing.
Sorry for the long post, does anyone knows what can possible be wrong? I can post the complete code somewhere over the net if somebody want's to help me.

Also, another question, is there any good way of debuging fork() programs?

Thanks a lot.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 356
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
It would be nice to see the code from the fork() function onwards. From the posted code it seems that the parent and child process are both reading from the pipe. That is incorrect as the parent should write to the pipe while the child reads from it. Owing to the atomicity of the parent writes it is guaranteed that the child process waits for data to be written to the pipe before pulling it out from its end.
Reply With Quote
  #3 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: May 2004
Location: Brazil
Posts: 40
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
I'll post in my website ASAP, but isn't the pipe used for this?
I mean, if I write on the pipe, shouldn't the parent be able to read it normally?
Reply With Quote
  #4 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 356
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Question Requirements unclear

Which process (parent / child) are you implying by this statement ... mean, if I write on the pipe
Reply With Quote
  #5 (permalink)  
Old 04-24-2008
Registered User
 

Join Date: May 2004
Location: Brazil
Posts: 40
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
I meant that if the child write's on the pipe, isn't the father allowed to read from it? (any child).

Anyway, here's the code, sorry for the portuguese comentaries, but it should be easy to follow what's it up to.
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 02:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102