Using fork() to create two childs

This is a discussion on Using fork() to create two childs within the C Programming forums, part of the General Programming Boards category; Hello, I have the following code that is supposed a parent and two childs. It works, but it doesn't look ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Using fork() to create two childs

    Hello,
    I have the following code that is supposed a parent and two childs. It works, but it doesn't look right in terms of process creation. I am confused about the part in star comments. Thanks.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/socket.h>
    
    #define CHILD 1
    #define PARENT 0
    #define BUF_SIZE 512
    
    void perror_exit(const char *msg)
    {
       perror(msg);
       exit(1);
    }
    
    void socketpair2(int *sock_fd)
    {
      if(socketpair(AF_LOCAL, SOCK_STREAM, 0, sock_fd) < 0)
        perror_exit("socketpair");
    }
    
    /*
     * creates a new process and returns return value of fork()
     */ 
    int fork2()
    {  
      int pid;
    
      if((pid = fork()) < 0)
        perror("fork"), exit(1);
    
      return pid;
    }
    
    void app_layer(int sock_fd1[], int sock_fd2[])
    {
      char buf[BUF_SIZE];
      char buf2[BUF_SIZE];
    
      buf[0] = 0;
      buf2[0] = 0;
      close(sock_fd1[CHILD]);
      close(sock_fd2[CHILD]);
    
      strcpy(buf, "APP TO IP/ICMP 1 ");
      if(write(sock_fd1[PARENT], buf, BUF_SIZE) < 0)
        perror_exit("write app_layer");
    
      strcpy(buf2, "APP TO IP/ICMP 2 ");
      if(write(sock_fd2[PARENT], buf2, BUF_SIZE) < 0)
        perror_exit("write app_layer");
    
      if(read(sock_fd1[PARENT], buf, BUF_SIZE) < 0)
        perror_exit("read");
    
      if(read(sock_fd2[PARENT], buf2, BUF_SIZE) < 0)
        perror_exit("read");
    
      printf("%s\n%s\n", buf, buf2);
    }
    
    void ip_icmp_layer(int sock_fd[], char *str)
    {
      char buf[BUF_SIZE];
    
      close(sock_fd[PARENT]);
    
      if(read(sock_fd[CHILD], buf, BUF_SIZE) < 0)
        perror_exit("read");
    
      strcat(buf, str);
      strcat(buf, " IP/ICMP TO APP");
    
      if(write(sock_fd[CHILD], buf, BUF_SIZE) < 0)
        perror_exit("write");
    }
    
    int main()
    {
      int pid;
      int sock1[2], sock2[2];
    
      // create a parent with two childs NOT child and grandchild
      socketpair2(sock1);
      socketpair2(sock2);
      pid = fork2();
    
      if(pid)
      {
    //*****************
        pid = fork2();
    
        if(pid)
          app_layer(sock1, sock2);
        else
          ip_icmp_layer(sock2, "22");
    //*****************
      }
      else
        ip_icmp_layer(sock1, "11");
    
      return 0;
    }
    Last edited by Yasir_Malik; 03-05-2005 at 05:49 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533

    Unhappy

    No idea - your continual abuse of the language after being told (and having it explained to you) in previous threads makes me think that it's a waste of time even bothering to try.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Ok, I replaced the defines with functions. Sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  3. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 11:15 AM
  4. problems with fork
    By raja9911 in forum C Programming
    Replies: 2
    Last Post: 02-03-2006, 06:48 AM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 09:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21