previous | start | next

Thread Version

The remote login problem recast as two threads (see bottom of page 42) and each thread only takes a few lines and is easy to understand (each thread is sequential):

  1. One thread just reads from the remote user and writes to the local application:

           void incoming(int r_in, int l_out) {
              int eof = 0;
              char buf[BSIZE];
              int size;
    
              while (!eof) {
                  size = read(r_in, buf, BSIZE);
                  if (size <= 0)
                    eof = 1;
                  if (write(l_out, buf, size) <= 0)
                    eof = 1;
              }
           }
           
    
  2. The other thread just reads the response from the local application and writes back to the remote user:

           void outgoing(int l_in, int r_out) {
              int eof = 0;
              char buf[BSIZE];
              int size;
    
              while (!eof) {
                size = read(l_in, buf, BSIZE);
                if (size <= 0)
                  eof = 1;
                if (write(r_out, buf, size) <= 0)
                  eof = 1;
                }
           }
           
    


previous | start | next