Thread: forking

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    14

    forking

    Any way to get the parent to do something first before going on to the child without shared memory?

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    Im trying to make 3 processes with fork but I keep getting segmentation fault and dont know why. I just started using fork() so Im not sure if this is correct.
    Code:
    #include <stdio.h> 
    int  main() 
    {
      FILE *Rfp,*Wfp;
      char s[40]; 
     
     
      if(fork()==0){ //10         
        
    
        if(fork()==0){         //Grandchild
          Rfp = fopen("File3.txt","r");
          if(!Rfp){exit(1);
          }
          else{
            while(fgets(s,20,Rfp)!=NULL){
          printf("Grandchild:  %s\n",s);  //20
            }
        fclose(Rfp);
          }
        }
        
       
         else{                   //Child
         Rfp = fopen("File2.txt","r");
          //Wfp = fopen("Fiel3.txt","w");  //30
         if(!Rfp){exit(1);
         }
          else{
            while(fgets(s,20,Rfp)!=NULL){
          printf("Child:  %s\n",s);
              // fputc();
        }
        //40
            fclose(Rfp);
        fclose(Wfp);
          }
          }
      }
      
      //-----------------------------------------------------------------        
      else{                     //Parent  //50
        Rfp = fopen("File1.txt","r");
        // Wfp = fopen("File2.txt","w");
        if(!Rfp)exit(1);
        else{
          while(fgets(s,20,Rfp)!=NULL){
        printf("Parent:  %s\n",s);
            //fputc();
          }
          fclose(Rfp);
          fclose(Wfp);
       }
      }
      return 0;
    }
    Last edited by Salem; 01-28-2005 at 03:40 PM. Reason: tagging the code

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Use code tags - refer to the sticky at the top of the forum titled, "<> Posting Code? Read this first <>".

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well you seem to be trying to implement pipes, by using files.
    Try using pipes for pipes, it's much better.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forking
    By sean in forum Linux Programming
    Replies: 5
    Last Post: 02-24-2009, 02:45 PM
  2. Forking Issue...
    By cookie in forum Linux Programming
    Replies: 1
    Last Post: 07-10-2008, 04:46 PM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. have I got forking problems, or what?
    By reptonite in forum C Programming
    Replies: 8
    Last Post: 02-23-2006, 06:11 PM
  5. Forking advice
    By zee in forum C Programming
    Replies: 4
    Last Post: 06-18-2004, 02:35 PM