Thread: Process sending file descriptors to another process

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

    Process sending file descriptors to another process

    Hello,
    I'm simulating a bunch of routers communicating with each other. I have multiple process running, and each is a child of another process. The great-parent of all the processes is the master process. The master process communicates with the other process with a pair of sockets created from socketpair(2). The other processes themselves are commununicating with each other through sockets created from socketpair(2) as well, but the master process does not know about that. How can the master process create a new pair of sockets through socketpair(2), send it to two processes, and then have those two processes communicate with each other with the two new sockets created? Here's what I have, but it doesn't work:

    definition for Link struct:
    Code:
    typedef struct Link
    {
      int link[2];
      int comm_end;
    } Link;
    some master process code:
    Code:
        Link link;
    ...
        socketpair2(link.link);
        task = 3;
        router = 6;
        write(master_conns[0][MASTER_TO_ROUTER], &task, 4);
        write(master_conns[0][MASTER_TO_ROUTER], &router, 4);
        link.comm_end = 0;
        write(master_conns[0][MASTER_TO_ROUTER], &link, sizeof(link));
    
        router = 0;
        write(master_conns[6][MASTER_TO_ROUTER], &task, 4);
        write(master_conns[6][MASTER_TO_ROUTER], &router, 4);
        link.comm_end = 1;
        write(master_conns[6][MASTER_TO_ROUTER], &link, sizeof(link));
        distance_vector_routing(master_conns, 7);
        print_routing_table(master_conns, "After adding link between "
                                          "router 0 and 6", 7);
    And the code for the sub processes:
    Code:
              read(master_conn[ROUTER_TO_MASTER], &task, 4);
    ...
              switch(task)
              {
                case 3:
                  read(master_conn[ROUTER_TO_MASTER], &router, 4);
                  read(master_conn[ROUTER_TO_MASTER], &link, sizeof(link));
    
                  for(i = 0; i < 7; i++)
                    // shorten distance to router being connected to
                    if(my_table[i].destination == router)
                    {
                      my_table[i].next_hop = router;
                      my_table[i].hop_count = 1;
                      break;
                    }
    
                  links.conns[router] = 1;
                  links.links[ links.num_links ].comm_end = link.comm_end;
                  memcpy(links.links[ links.num_links ].link, link.link, 8);
                  
                  FD_SET(link.link[link.comm_end], &master);
                  set_max_fd(max_fd, link.link[link.comm_end]);
    Thanks,
    Yasir

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    One way to do that would be using IPC message queues. Create two message queues in Master process (one for sending messages to the child process (write) and other one to receive messages from child process(read) ) and when child is spawned , acquire this message queues for in child process (attach to them). Send a message to master process over its read queue that child is ready. Master process will then create the socket pair and will send the socket id to child process over its write queue. So basically, you are cross connecting the queues in master and child processes.

    Masters write queue is childs read queue
    Masters read queue is childs write queue

    Read more on IPC message queues here

    Thanks,

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Well, I finished the assignment, but it wasn't elegant.

    I already have the routers talking to each other through sockets created from socketpair; communication is not the problem. What I want is to add an extra link (represented by a socketpair) between two routers. The master process is creating the new socketpair, and is sending the newly created array to two router processes, as the per the second code fragment. This socketpair() is what the processes will use to communicate with each other, not the master process. I do not want to use message queues because I want to be consistent in way the routers are communicating.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Ok, more abstractly, is it possible for a process to create and send a file descriptor to two other processes and have the two process communicate over the file descriptor?

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    if the child processes are spawned after generating the sockets, the child processes will make the copy of the sockets created by parent process. They can use them to send/receive data. For e.g.

    When accept() function call returns, it returns the new socket descriptor for the received connection. Usually server program spawns a new child process to handle the received connections. Child process automatically receives a copy of the new socket descriptor. Child can use this socket descriptor to send/receive data over the new connection, while parent can safely use close() to delete its copy of the new socket descriptor.

    Code:
    int newfd,pid;
    for (;;)
    {
      int newfd = accept(sockfd,..../*additional args*/);  /*Blocking system call */
     
     /* accept returns with newfd when new connection is received*/
    
     /* Spwan child process */
     
      if ((pid=fork()) == 0) 
        {
           /*In Child Process */
           /* Send/Receive using newfd*/
           recv(newfd,../*Additional args*/);
           send(newfd,../*Additional args*/);
           close(newfd);
        } else {
         /* In Parent Process */
           close(newfd);   /*Parent dont need this. So close it.*/   
        }
         
     }
    Hope this helps.

    Thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM