Message Queues.

Slides:



Advertisements
Similar presentations
Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory.
Advertisements

Inter-Process Communication: Message Passing Tore Larsen Slides by T. Plagemann, Pål Halvorsen, Kai Li, and Andrew S. Tanenbaum.
CS 450 Module R4. R4 Overview Due on March 11 th along with R3. R4 is a small yet critical part of the MPX system. In this module, you will add the functionality.
Unix IPC and Synchronization. Pipes and FIFOs Pipe: a circular buffer of fixed size written by one process and read by another int pipe(int fildes[2])
Shared Memory  Creating a Shared Memory Segment Allocated in byte amounts  Shared Memory Operations Create Attach Detach  Shared Memory Control Remove.
XSI IPC Message Queues Semaphores Shared Memory. XSI IPC Each XSI IPC structure has two ways to identify it An internal (within the Kernel) non negative.
Shared memory. Process A Process B Physical Memory Virtual address space A Virtual address space B Share Instruction memory Data Instruction memory Data.
System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
进程通信 中科大软件学院. 消息队列 消息队列提供了一种由一个进程向另一个进程发送块数据 的方法。另外,每一个数据块被看作有一个类型,而接收 进程可以独立接收具有不同类型的数据块。 消息队列定义如下: #include int msgget(key_t key, int msgflg); int msgrcv(int.
UNIX IPC CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of Concurrency2 Creating a UNIX process A process is created by making an exact.
NCHU System & Network Lab Lab 10 Message Queue and Shared Memory.
Inter Process Communication. Introduction Traditionally describe mechanism for message passing between different processes that are running on some operating.
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Inter-Process Communication: Message Passing
System Config IPC Iris Zhu
1 Chapter 6 Interprocess Communications. 2 Contents u Introduction u Universal IPC Facilities u System V IPC.
Lecture 6 Introduction to Distributed Programming System V IPC: Message Queues, Shared Memory, Semaphores.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Thread Synchronization with Semaphores
S -1 Shared Memory. S -2 Motivation Shared memory allows two or more processes to share a given region of memory -- this is the fastest form of IPC because.
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Lecture 7 Introduction to Distributed Programming System V IPC: Message Queues, Shared Memory, Semaphores.
Sogang University Advanced Operating Systems (Inter-Process Communication - Linux) Advanced Operating Systems (Inter-Process Communication - Linux) Sang.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
UNIX Files File organization and a few primitives.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
Semaphores Creating and Accessing Semaphore Sets Semaphore Operations
Interprocess Communication Bosky Agarwal CS 518. Presentation Layout Introduction Pipes FIFOs System V IPC 1.Using pipes 2.Working of pipes 3.Pipe Data.
Socket Programming Lab 1 1CS Computer Networks.
File descriptor table File descriptor (integer)File name 0stdin 1stdout 2stderr Use open(), read(), write() system calls to access files Think what happens.
Inter Process Comunication in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
© 2006 RightNow Technologies, Inc. Synchronization September 15, 2006 These people do not actually work at RightNow.
Advanced UNIX IPC Facilities After Haviland, et al.’s book.
Slide 9-1 Copyright © 2004 Pearson Education, Inc. Inter process Communication Mechanisms  Allow arbitrary processes to exchange data and synchronize.
Interprocess Communication
KUKUM Real Time System Module #3 POSIX programming- Shm Lecture 4.
IPC Message Queue. Data Structure msgque IP_NOUSED.
Linux/UNIX Programming APUE (Interprocess Communication) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
Message Queues. Unix IPC Package ● Unix System V IPC package consists of three things – Messages – allows processes to send formatted data streams to.
Signals & Message queue Inter process mechanism in Linux system 3/24/
Interprocess Communication. Resource Sharing –Kernel: Data structures, Buffers –Processes: Shared Memory, Files Synchronization Methods –Kernel: Wait.
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 7: POSIX Interprocess Communication (IPC)
Operating Systems: Inter-Process Communication Pål Halvorsen 13 March 2016 INF1060: Introduction to Operating Systems and Data Communication.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
Textbook: Advanced Programming in the UNIX Environment, 2 nd Edition, W. Richard Stevens and Stephen A. Rago 1 Chapter 15. Interprocess Communication System.
Operating Systems Inter-Process Communication Shared Memory & Semaphores Moti Geva
Distributed and Parallel Processing George Wells.
Linux Internals.
Linux/UNIX Programming APUE (Interprocess Communication)
Lecture 7 Introduction to Distributed Programming System V IPC:
Message queue Inter process communication primitive
Introduction to Distributed Programming System V IPC: Message Queues
Operating Systems: Inter-Process Communication
Socket Programming in C
Linux Interprocess Communication
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
Lock and IPC (Inter-Process Communication) (Chap 12, 14 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at.
Shared Memory Dr. Yingwu Zhu.
Interprocess Communication-1
Interprocess Communication
Interprocess Communication and Synchronization using System V IPC
Inter Process Communication
Unix programming Term: Unit-V I PPT Slides
Inter-Process Communication
Introduction to Distributed Programming System V IPC:
| Website for Students | VTU -NOTES -Question Papers
Shared Memory Dr. Yingwu Zhu Feb, 2007.
Presentation transcript:

Message Queues

Unix IPC Package Unix System V IPC package consists of three things Messages – allows processes to send formatted data streams to arbitrary processes Shared memory allows processes to share parts of their virtual address space. Semaphores allow processes to synchronise execution.

struct msqid_ds { struct ipc_perm msg_perm; struct msg *msg_first; /* first message on queue */ struct msg *msg_last; /* last message in queue */ time_t msg_stime; /* last msgsnd time */ time_t msg_rtime; /* last msgrcv time */ time_t msg_ctime; /* last change time */ ushort msg_cbytes; ushort msg_qnum; ushort msg_qbytes; /* max number of bytes on queue */ ushort msg_lspid; /* pid of last msgsnd */ ushort msg_lrpid; /* last receive pid */ };

struct ipc_perm { key_t key; ushort uid; /* owner euid and egid */ ushort gid; ushort cuid; /* creator euid and egid */ ushort cgid; ushort mode; /* access modes see mode flags below */ ushort seq; /* slot usage sequence number */ };

msg structure struct msg { struct msg *msg_next; /* pointer to next message on q */ long msg_type; /* message type */ ushort msg_ts; /* message text size */ short msg_spot; /* message text map address */ };

System imposed limits on the manipulation of message queues System imposed limits on the manipulation of message queues. The limits are defined in the <sys/msg.h>. MSGMNI :The maximum number of message queues that may exist any given time in a system MSGMAX : The maximum number of bytes of data allowed for a message MSGMNB : The maximum number of bytes of all messages allowed in a queue MSGTQL : The maximum number of messages in all queues allowed in a system

UNIX system V API for Message Queues msgget() : open and create if needed, a message queue for access msgsnd( ) :send a message to a queue msgrcv() : receive a message from message queue msgctl() : manipulate the control data of a message queue

To connect to a queue Arguments int msgget(key_t key, int msgflg); msgget() returns the message queue ID on success, or -1 on failure Arguments key is a system-wide unique identifier describing the queue you want to connect to (or create). Every other process that wants to connect to this queue will have to use the same key. msgflg tells msgget() what to do with queue in question. To create a queue, this field must be set equal to IPC_CREAT bit-wise OR'd with the permissions for this queue.

type key_t is actually just a long, you can use any number you want. use the ftok() function which generates a key from two arguments: key_t ftok(const char *path, int id); path is the file that this process can read, id is usually just set to some arbitrary char, like 'A'. The ftok() function uses information about the named file (like inode number, etc.) and the id to generate a probably- unique key for msgget().

To make the call: #include <sys/msg.h> key = ftok("/home/abc/somefile", 'b'); msqid = msgget(key, 0666 | IPC_CREAT);

Sending to the Queue Each message is made up of two parts, which are defined in the template structure struct msgbuf, as defined in sys/msg.h struct msgbuf { long mtype; char mtext[1]; }; mtype is used later when retrieving messages from the queue, set to any positive number. mtext is the data this will be added to the queue.

struct pirate_msgbuf { You can use any structure you want to put messages on the queue, as long as the first element is a long. struct pirate_msgbuf { long mtype; /* must be positive */ char name[30]; char ship_type; int notoriety; int cruelty; int booty_value; };

To send msg int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); Arguments msqid is the message queue identifier returned by msgget(). msgp is a pointer to the data you want to put on the queue. msgsz is the size in bytes of the data to add to the queue. msgflg allows you to set some optional flag parameters, which we'll ignore for now by setting it to 0.

Sending to the Queue key_t key; int msqid; struct pirate_msgbuf pmb = {2, "L'Olonais", 'S', 80, 10, 12035}; key = ftok("/home/abc/somefile", 'b'); msqid = msgget(key, 0666 | IPC_CREAT); msgsnd(msqid, &pmb, sizeof(pmb), 0);

Receiving from the Queue int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); Here msgtyp corresponds to the mtype we set in the msgsnd() msgtyp Effect on msgrcv() Zero - Retrieve the next message on the queue, regardless of its mtype. Positive - Get the next message with an mtype equal to the specified msgtyp. Negative - Retrieve the first message on the queue whose mtype field is less than or equal to the absolute value of the msgtyp argument.

Receiving from a Queue key_t key; int msqid; struct pirate_msgbuf pmb; /* where L'Olonais is to be kept */ key = ftok("/home/abc/somefile", 'b'); msqid = msgget(key, 0666); msgrcv(msqid, &pmb, sizeof(pmb), 2, 0);

Destroying a message queue There are two ways of destroying a queue: Use the Unix command ipcs to get a list of defined message queues, then use the command ipcrm to delete the queue. Write a program to do it for you.

int msgctl(int msqid, int cmd, struct msqid_ds *buf); Arguments msqid is the queue identifier obtained from msgget(). cmd tells msgctl() how to behave. IPC_RMID is used to remove the message queue. Buf argument can be set to NULL for the purposes of IPC_RMID. msgctl(msqid, IPC_RMID, NULL);

Demo Program