title: How to create a socket between 2 processes after fork-exec
body: <p>i'm learning C and this is my problem: i need to connect 2 processes with a socket AF_UNIX.
  My question is: how can i achieve that ? </p>
  
  <p>I've already tried something with poor results: </p>
  
  <ul>
  <li><p>Tried to pass the fd.</p></li>
  <li><p>Tried to dup but again failed and the 2 processes didn't take any messages from the parent process.</p></li>
  </ul>
  
  <p>Can i just open a socket in parent and then pass the file descriptor with execl ? or should i just try something more "complicated" ?</p>
  
  <p>EDIT: code
  this is P1.c</p>
  
  <pre><code>int main (){
  
    printf("Hello this is process 1\n");
  int fd=open("./foo",O_RDWR);
  int h=fork();
  if(h==0)
  {
      sleep(2);
      dup2(fd,0);//note we will be loosing standard input in p2
      execvp("./client",NULL);
  }
  else
  {
      printf("This is from p1 process\n");
      write(fd,"buf",4);
                  //do some process with p1
      printf("This is end of p1 process\n");
  }
  
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>this is P2.c</p>
  
  <pre><code>int main (int argc, char * argv[]){
  int fd=atoi(argv[1]);      
  char buf[1024];
  int n=read(fd,buf,1024);
  buf[n]='\0';
  printf("This is from p2\n");
  write(1,buf,strlen(buf));
      exit(EXIT_SUCCESS);
  </code></pre>
  
  <p>}</p>
  
  <p>note: i wasn't trying to use a socket.</p>