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

lecture 20
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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?

slide-2
SLIDE 2

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 2

Outline

 Finish exercise from last class  System V message queues  POSIX message queues

slide-3
SLIDE 3

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-4
SLIDE 4

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-5
SLIDE 5

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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

slide-6
SLIDE 6

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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);

slide-7
SLIDE 7

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-8
SLIDE 8

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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]; };

slide-9
SLIDE 9

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-10
SLIDE 10

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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( )).

slide-11
SLIDE 11

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.)

slide-12
SLIDE 12

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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).

slide-13
SLIDE 13

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-14
SLIDE 14

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-15
SLIDE 15

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 15

Creating a Message Queue (POSIX)

 The flags argument must contain O_CREAT

  • r'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( )

  • r mq_receive( ) to block.
slide-16
SLIDE 16

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.
slide-17
SLIDE 17

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-18
SLIDE 18

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-19
SLIDE 19

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-20
SLIDE 20

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-21
SLIDE 21

Thursday, November 4 CS 375 UNIX System Programming - Lecture 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.

slide-22
SLIDE 22

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.

slide-23
SLIDE 23

Thursday, November 4 CS 375 UNIX System Programming - Lecture 20 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.