tcl-mq

NAME
SYNOPSIS
DESCRIPTION
EXAMPLE
OUTPUT
AUTHOR
COPYRIGHT

NAME

tcl−mq − POSIX Message Queues for Tcl

SYNOPSIS

package require mq

mq create queue command qsize msize mode ?handler?

mq open queue command qsize msize mode ?handler?

mq forceopen queue command qsize msize mode ?handler?

mq delete queue command

queue send priority list

queue receive

queue queue

DESCRIPTION

This extension provides a Tcl interface to POSIX Message Queues. It allows scripts to create/open/close/unlink multiple parallel message queues, and to send/receive messages synchronously and asynchronously to/from them.

A POSIX message queue is an Inter−Process Communication mechanism available on Linux and some other POSIX−compliant operating systems. It allows to or more processes (or threads) to communicate under the same OS. The messages are buffered by the kernel, which gives them kernel persistency. A message queue can be thought of as a linked list of messages. Threads with adequate permission can put messages onto the queue, and threads with adequuate permission can remove messages from the queue. Each message is assigned a priority by the sender, and the oldest message of highest priority is always retrieved first. Unlike PIPES and FIFOS, no requirement exists that someone be waiting for a message to arrive on a queue, before some process writes a message to that queue. It’s not even a requirement for both processes to exist at the same time. Read mq_overview(7) for more details.

The following parameter are used in the commands: queue The name of the POSIX queue.

command The Tcl command that reflects the opened queue.

qsize The number of messages that the queue can hold.

msize The maximum size of each message in the queue in bytes.

mode The unix file mode for the queue, expressed as a string or number (e.g. 00777).

handler If this argument is provided then the handler procedure will be executed asynchronously whenever there are messages on the queue.

priority The priority to associate the message with. Higher priority messages are delivered first. Up to 32768 for linux, but 31 for portability.

list A Tcl list to send to the queue.

The difference between create, open and forceopen is that create requires that the queue does not exists, open requires that the queue pre−exists, and forceopen it creates the queue if it does not exist.

The "queue" command returns the number of messages in the queue.

The "receive" command returns all messages in the queue in a list.

EXAMPLE

package require mq

proc queue {} {}

catch {mq delete /queue1 queue}
mq create /queue1 queue 10 1000 644
puts [queue queue]
queue send 0 asdf

puts [queue queue]
puts [queue receive]
puts [queue queue]

rename queue {}
mq open  /queue1 queue 10 1000 644

queue send 1 [list first but low priority]
queue send 15 [list second but higher priority]
queue send 15 [list third but middle priority]
puts [queue queue]
puts [queue receive]
mq delete /queue1 queue

proc handler {queue command} {
    puts IN
    puts $queue:[$command queue]
    puts $queue:[$command receive]
}

mq create /queue1 queue 10 1000 644 ::handler
after 1000 queue send 0 message1
after 2000 queue send 0 message2
after 3000 puts [queue queue]
after 4000 set forever 1
set forever 0
vwait forever
mq delete /queue1 queue

OUTPUT

0
1
asdf
0
3
{second but higher priority} {third but middle priority} {first but low priority}
IN
/queue1:1
/queue1:message1
IN
/queue1:1
/queue1:message2
0

AUTHOR

Alexandros Stergiakis sterg@kth.se

COPYRIGHT

Copyright (C) 2008 Alexandros Stergiakis

This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.