Find the answer to your Linux question:
Results 1 to 2 of 2
0 down vote favorite I want to write a program, one module is: I have two processes, process A is responsible for receiving packets using recvfrom() or recv and another ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2012
    Posts
    85

    reader-writer and inter-process communication designing


    0 down vote favorite


    I want to write a program, one module is:

    I have two processes, process A is responsible for receiving packets using recvfrom() or recv and another process B keep on some other things.

    whenever process A receives a packet, it should immediately extract some information from the packet and tell the information to process B. And B needs to immediately stop what it is doing and read the information.

    This is like a tcp stack, let me say, process A is responsible for receiving data packets or ack packets, and process B is responsible for sending packets. when Process A receives a data packet, process B should read the data packet, update seq number and ack_seq number for its next outgoing packet.

    Anyway, my idea is process A and B should share a structure variable, and there is a lock on the varialbe. B keeps on reading the varialbe, when A receives a packet, it lock the variable, thus B is blocked. After A modify the variable, A unlock it and B get to read the variable and then continue to do its own job. Pseudo code is like

    Code:
    struct SHARE shared_variable;
    pid=fork();
    if(pid==0) {  // process B
            while(1){
             lock(shared_variable);
             k=read(shared_variable);
             unlock(shared_variable);
             do_something(k);  
             }
           }
    
     if(pid>0){ // process A
           while(1){
           recv();
           lock(shared_variable);
           modify(shared_variable);
           unlock(shared_variable);
           }
       }
    I'm not familiar with inter-process communication and network programming my question is:
    1 is the structure of my pseudo code ok? are there better solutions? 2 what kind of IPC style should I use for the shared variable 3 I only knows some theory, are there any concrete sample source codes?

    thanks!

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,159
    Why not use threads instead of processes? Threads A and B can share buffer queues and a data access lock.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •