lecture 20

Lecture 20 Log into Linux. Copy files from - PowerPoint PPT Presentation

Lecture 20 Log into Linux. Copy files from /home/hwang/cs375/lecture20/ into a subdirectory. Reminder: Project 6 due today. Project 7 has been posted to the course webpage. Questions? Thursday, November 4 CS 375 UNIX System


  1. Lecture 20  Log into Linux. Copy files from /home/hwang/cs375/lecture20/ into a subdirectory.  Reminder: Project 6 due today. Project 7 has been posted to the course webpage.  Questions? Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 1

  2. Outline  Finish exercise from last class  System V message queues  POSIX message queues Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 2

  3. Last Lecture In-Class Exercise  The programs in the posix subdirectory are versions of the example programs modified to use POSIX shared memory. Complete the unfinished client program.  Further modify the programs to use a pair of POSIX semaphores for synchronization, instead of polling the written_by_you flag. The semaphores are initialized to 1 and 0. The producer acquires the first before writing data and releases the second when it is done, while the consumer does the opposite. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 3

  4. Introduction to Message Queues  Message queues allow messages (data packets) to be passed from one process to another. There can be multiple writers to the queue as well as multiple readers.  Message queues are often used for passing small messages between processes. They also can be used for process synchronization. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 4

  5. System V Message Queues  System V message queues are defined in the <sys/msg.h> library.  The System V creation and control routines are similar in format to the semaphore and shared memory routines.  See examples in msg_server.cpp and msg_client.cpp Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 5

  6. Creating a Message Queue (SysV)  The msgget( ) routine is used to create a message queue: int msgget(key_t key, int flags);  The routine returns a queue id. Examples: // Create a named queue id=msgget(19,IPC_CREAT|IPC_EXCL|0660); // Access existing queue by name id=msgget(19, 0); // Create unnamed queue id=msgget(IPC_PRIVATE, 0660); Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 6

  7. Deleting a Message Queue (SysV)  The msgctl( ) routine is used to change permissions on a queue, get information about the queue, and to delete the queue: // Remove the queue ret = msgctl(id, IPC_RMID, 0);  Unlike semaphores and shared memory, the queue is removed immediately. Any processes that are waiting on the queue are awakened and receive an error return. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 7

  8. Sending Messages (SysV)  msgsnd( ) adds a message to the queue: msgsnd(int id, void *msg, size_t len, int flg);  id is the queue identifier. msg must be a pointer to an area of memory that starts with a long int. The integer is the message type . msg is usually a pointer to a struct: struct msg { long type; char data[len]; }; Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 8

  9. Sending Messages (SysV)  The data field of the struct may be an array (or struct) of any plain type. The len parameter is the size of the data field of the msg struct in bytes. Note that the len parameter does not include the size of the integer type field.  flg is usually 0 or IPC_NOWAIT. By default msgsnd( ) will block if the queue is full unless the IPC_NOWAIT flag is set. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 9

  10. Receiving Messages (SysV)  msgrcv( ) is used to get a message: msgrcv(int id, void *msg, size_t len, size_t type,int flg);  id is the queue identifier. msg must be a pointer to an area of memory just as for msgsnd( ) . The len parameter is the size of the data area in the receiving struct. If the message is longer than len , the message is removed from the queue and the call fails. (This can be modified via msgctl( ) ). Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 10

  11. Receiving Messages (SysV)  The type parameter allows us to read the next message of a particular type.  If type is 0, the first message is read.  If type is greater than 0, the first message in the queue of that type is read. (Unless flg contains MSG_EXCEPT, then the first message NOT of that type is read.) Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 11

  12. Receiving Messages (SysV)  If type is less than 0, the first message in the queue with the lowest type less than or equal to the absolute value of type is read.  The flg parameter is usually either 0 or IPC_NOWAIT (if you do not want msgrcv( ) to block until a message becomes available). Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 12

  13. POSIX Message Queues  POSIX message queues are defined in the <mqueue.h> library  As with the semaphores and shared memory, POSIX message queues are named by strings rather than integers. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 13

  14. Creating a Message Queue (POSIX)  To create a POSIX message queue: mqd_t mq_open(const char *name, int flags, mode_t perms, struct mq_attr *attr);  For portability, the name should begin with a slash and contain no other slashes as was the case for POSIX semaphores and shared memory. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 14

  15. Creating a Message Queue (POSIX)  The flags argument must contain O_CREAT or'd (|) with one of O_RDONLY, O_WRONLY or O_RDWR, depending on whether the creating process wants to receive or send or both.  flags can include O_EXCL if you want the call to fail when the queue already exists. Use O_NONBLOCK if you do not want mq_send( ) or mq_receive( ) to block. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 15

  16. Creating a Message Queue (POSIX)  perms is similar to the permissions on files. Read and write permission mean the ability to receive and send messages, and execute permission is meaningless.  The mq_maxmsg and mq_msgsize fields of the attr structure set the maximum number of messages and the maximum message size respectively. See the man page for details. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 16

  17. Creating a Message Queue (POSIX)  To open an existing queue, mq_open( ) is called with only two arguments: mqd_t mq_open(const char *name, int flags);  A message queue descriptor is returned on success. On error, -1 is returned and errno is set appropriately. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 17

  18. Deleting a Message Queue (POSIX)  To close a queue use mq_close( ) : int mq_close(mgq_t mqd);  To remove a queue use mq_unlink( ) : int mq_unlink(const char *name);  The name disappears immediately, but the queue is not removed until all open queue descriptors have been closed. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 18

  19. Sending Messages (POSIX)  To send messages use mq_send( ) int mq_send(mqd_t mqd, const char *msg, size_t msgsize, unsigned priority);  The priority must be greater than zero. Messages are placed in the queue in decreasing priority order. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 19

  20. Receiving Messages (POSIX)  To receive a message: ssize_t mq_receive(mqd_t mqd, char *msg, size_t msgsize, unsigned *priorityp);  msgsize is the size of the msg buffer. It must be at least as big as the queue mq_msgsize attribute or the call will fail. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 20

  21. Receiving Messages (POSIX)  The oldest message with the highest priority is received. If the priorityp pointer is non-NULL then the message priority is returned at that address.  There are also mq_timedsend( ) and mq_timedreceive( ) routines as well as mq_getattr( ) and mq_setattr( ) routines. You can use mq_notify( ) to request a signal when a message arrives in an empty queue. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 21

  22. POSIX Message Queues  On Linux, message queues are created in a virtual file system. You can (optionally) create and mount this system using: $ mkdir /tmp/mqueue $ mount -t mqueue none /tmp/mqueue This allows message queues to be opened and read using file routines. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 22

  23. In-class Exercise  Make copies of the System V example program files in a new subdirectory. Modify the copies to use POSIX message queues instead. (You will need to create two message queues for the required bidirectional communication, since there is no message type in the receive routine.) Recall that the -lrt option for g++ is needed to compile POSIX IPC programs. Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 23

Recommend


More recommend