Saturday, November 8, 2014

Unix Prog: streams mechanism(1)

1. streams

STREAMS mechanism provide a general way to interface communication drivers into the kernel.A stream provides a full-duplex path between a user process and a device driver.


We can use ioctl command to push processing modules onto the stream. And for the process of stream messages, the processing in one direction is separate from the processing in the other direction. Data we write to a stream head is sent downstream, data read by the device driver is sent upstream.

The pathname we open for a stream normally lives beneath the /dev directory. All STREAMS devices are character special files.

2. streams messages
All input and ouptut under STREAMS is based on messages. Messages are passed up and down a stream between the stream head, the processing modules and the device driver

The control information and data are specified by strbuf structure:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/x86_64-linux-gnu/bits/stropts.h  
 ......  
 struct strbuf  
  {  
   int maxlen;     /* Maximum buffer length. */  
   int len;      /* Length of data. */  
   char *buf;     /* Pointer to buffer. */  
  };  
 ......  

When we send a message with putmsg or putpmsg, len specifies the number of bytes of data in the buffer. When we receive a message with getmsg or getpmsg, maxlen specifies the size of the buffer, and len is set by the kernel to the amount of data stored in the buffer.

Message include "control information" and "data information":
1) For sending network message, control information is destination address
2) For sending network message, control information is data content.

Message Type:
1) M_DATA: user data for I/O
2) M_PROTO: protocol control information
3) M_PCPROTO: high-priority protocol control information

Message queueing priority:
1) High-priority message(highest priority)
2) Priority band message(have a band 1-255, higher band, higher priority)
3) Ordinary message(lowest priority, just priority band message with band 0)
High-priority message is special, that only one message is queued by the stream head at a time, additional high-priority messages are discarded

Each processing module includes:
1) One input queue receives message from the module above(message moving downstream from the stream head toward the driver)
2) One input queue receives message from the module below(message moving upstream from the driver toward the stream head)

How kernel send the terminal generated signal to foreground processes:
stream head receives and M_SIG message from below, it generates a signal.

3. putmsg, putpmsg:
System Definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/stropts.h  
 ......  
 /* Send a message on a STREAM.  
   
   This function is a cancellation point and therefore not marked with  
   __THROW. */  
 extern int putmsg (int __fildes, const struct strbuf *__ctlptr,  
           const struct strbuf *__dataptr, int __flags);  
   
 /* Send a message on a STREAM to the BAND.  
   
   This function is a cancellation point and therefore not marked with  
   __THROW. */  
 extern int putpmsg (int __fildes, const struct strbuf *__ctlptr,  
           const struct strbuf *__dataptr, int __band, int __flags);  
 ......  

write system call can also be used to send a message to a stream. But using write can not specify the control information and flags is set to 0 by default.

1) With "write", no control, no band, no flag, message generated for stream is of type: M_DATA(ordinary)
2) With "putmsg", without control, with data, flag 0, message is of type M_DATA
   with control, with data, flag 0, message is of type M_PROTO(protocol control)
   with control, with data, flag RS_HIPRI, message is of type M_PCPROTO(high priority protocol control)
3) With "putpmsg", without control, with data, band 0, flag MSG_BAND, msg is of type M_DATA(ordinary, band 0)
   without control, with data, band 1-255, flag MSG_BAND, msg is of type M_DATA(priority band)
   with control, with data, band 0, flag MSG_BAND, msg is of type M_PROTO(ordinary)
   with control, with data, band 1-255, flag MSG_BAND, msg is of type M_PROTO(priority band)
   with control, with data, band 0, flag: MSG_HIPRI, msg is of type M-PCPROTO(high_priority)

No comments:

Post a Comment