16
Presented by: P.SIVA KUMAR (07U41A0542) 

NP-MQ & SHM

Embed Size (px)

Citation preview

Page 1: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 1/16

Presented by:

P.SIVA KUMAR (07U41A0542) 

Page 2: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 2/16

Message Queuesy Introduction

 A message queue works kind of like a FIFO, butsupports some additional functionality. Generally, messages aretaken off the queue in the order they are put on.

In terms of usage, a process can create a new messagequeue, or it can connect to an existing one. In this way twoprocesses can exchange information through the same messagequeue.

Use the ipcs command to check if any of your unusedmessage queues are just floating around out there. You can

destroy them with the ipcrm command.

Page 3: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 3/16

Topics to be discuss

y 1. Where's my queue?

y 2. Are you the Key Master?

y 3. Sending to the queue

y 4. Receiving from the queuey 5. Destroying a message queue

Page 4: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 4/16

Where's my queue?

First of all, you want to connect to a queue, or create it if it doesn'texist. The call to accomplish this is the msgget() system call.Syntax:int msgget(key_tkey, intmsgflg);

:returns the message queue ID on success, or -1 on failure (and itsets errno, of course.)

The first, k ey is a system-wide unique identifierdescribing the queue.

The other argument, msgflg tells msgget() what to do with queue inquestion. To create a queue, this field must be set equalto IPC_CREAT bit-wise OR'd with the permissions for this queue.

 A sample call is given in the next slide.

Page 5: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 5/16

Are you the Key Master?

y  We can create key using any number we want, as Key_t is a long type.

y The solution is to use the f tok() function which generates a key fromtwo arguments:

key_t f tok(const char * path, int id );y path just has to be a file that this process can read.

y The other argument, id is usually just set to some arbitrary char, like 'A'.

y

Sample Code:#include <sys/msg.h>

key = f tok("/home/beej/somef ile", 'b');

msqid = msgget(key , 0666 | IPC_CREAT);

Page 6: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 6/16

Sending to the queue

y In sending: each message is made up of two parts, which are defined inthe template structure struct msgbuf :

struct msgbuf { long mtype; char mtext[1]; };

y  we pass this information to a message queue by using msgsnd()

int msgsnd(intmsqid ,const void *msg p, 

size_tmsgsz,

intmsgflg);

msqid: is the message queue identifier returned by msgget().

*msg p: 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.

y To know the size of data send, we use sizeof() method

ex: int size = sizeof(struct msgbuf);

Page 7: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 7/16

Receiving from the queue

y There is a counterpart to msgsnd(): it is msgrcv(). It is used to receivemessages from the message queue.

int msgrcv(intmsqid , void *msg p, size_tmsgsz, long msgt y p, intmsgflg);

the behavior of msgrcv() can be modified drastically by choosing

a msgt y p :msgt y p Eff ect on msgrcv()

Zero Retrieve the next message on the queue, regardless of its mt y pe.

Positive Get the next message with an mt y pe equal to the specified msgt y p.

Negative Retrieve the first message on the queue whose mt y pe field is less than

or equal to the absolute value of the msgt y p argument.

Page 8: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 8/16

Destroying a message queue

y There are two ways:

1. Use the Unix command ipcs to get a list of defined message queues,then use the command ipcrm to delete the queue.

2. Write a program to do it for you.

y To clean up the queue at some time or another we use the functionmsgctl().

y int msgctl(intmsqid , int cmd , struct msqid_ds *bu f );

The important argument is cmd  which tells msgctl() how to behave.

It can be a IPC_RMID, which is used to remove the message queue.

The buf argument can be set to NULL for the purposes of IPC_RMID.

y #include <sys/msg.h>

. .

msgctl(msqid, IPC_RMID, NULL);

Page 9: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 9/16

SHARED MEMORY

y Introduction

y  A segment of memory that is shared between processes.

y Shared memory allows one or more processes to communicate via

memory that appears in all of their virtual address spaces.y The pages of the virtual memory is referenced by page table entries in

each of the sharing processes' page tables.

y  Access to shared memory areas is controlled via keys and access rightschecking.

Page 10: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 10/16

Topics to be discuss

y 1. Creating the segment and connecting

y 2. Attach megetting a pointer to the segment

y 3. Reading and Writingy 4. Detaching from and deleting segments

y 5. Concurrency 

Page 11: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 11/16

Creating the segment and connecting

y  A shared memory segment is created and connected to viathe shmget() call:

int shmget(key_tkey, size_t size, int shmflg);

size, is the size in bytes of the shared memory segment.The shmflg should be set to the permissions of the segment bitwise-OR d with IPC_CREAT if you want to create the segment, but canbe 0 otherwise.

y shmid = shmget(key , 1024, 0644 | IPC_CREAT);

Page 12: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 12/16

Attach Shared Memory

y  void *shmat(int shmid , void *shmaddr , int shmflg);

shmadd r , which you can use to tell shmat() which specific address touse but you should just set it to 0 and let the OS choose the address for

 you.

The shmflg can be set to SHM_RDONLY if you only want to read fromit, 0 otherwise.

y data = shmat(shmid, (void *)0, 0);

y if (data == (char *)(-1))

y perror("shmat");

y shmat() returns -1 on failure.

Page 13: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 13/16

Reading and Writing

y  you have the d atapointer from the above example. It is a char pointer,so we'll be reading and writing chars from it.

y  we can print it like this:

printf ("shared contents: %s\n", data);y  And we could store something in it as easily as this:

printf ("Enter a string: ");

gets(data);

Page 14: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 14/16

Detaching from and deleting segments

y  When you're done with the shared memory segment, your programshould detach itself from it using the shmdt() call:

int shmdt(void *shmaddr );

returns -1 on error, 0 on success.y shmctl(shmid, IPC_RMID, NULL);

The above call deletes the shared memory segment, assuming no oneelse is attached to it.

or

using ipcrm command you can delete shared memory segmentdirectly.

Page 15: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 15/16

Concu

rrency

y  What are concurrency issue?

 when you have multiple processes modifying the shred memory.

y Solution:

The way to get around this is to use Semaphores to lock the sharedmemory segment while a process is writing to it.

Page 16: NP-MQ & SHM

8/8/2019 NP-MQ & SHM

http://slidepdf.com/reader/full/np-mq-shm 16/16