1 / 30

Module #3 POSIX programming- Shm

Module #3 POSIX programming- Shm. Lecture 4. Unix Interprocess Comm. Unix is rich in inter process communication mechanism. These are: Signal Pipe FIFO (named pipe) IPC Message queues Semaphore Shared memory (fastest). IPC.

kenny
Download Presentation

Module #3 POSIX programming- Shm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module #3POSIX programming- Shm Lecture 4

  2. Unix Interprocess Comm • Unix is rich in inter process communication mechanism. These are: • Signal • Pipe • FIFO (named pipe) • IPC • Message queues • Semaphore • Shared memory (fastest)

  3. IPC • An elegant means of interprocess communication mechanisms provided by the group of techniques called IPC IPCs are also directly managed by kernel • Using IPCs, concurrent processes can interact in a client-server manner (eg: database server) or peer-to-peer manner( eg: email server) • Within a machine each of these resources is identified by an integer called “KEY”, ie the key is used as the sharing anchor. • Each resource belongs to an owner and has access rights • There are two shell commands to check on them: • ipcs (similar to ls) –list down all the IPC resources currently available • Ipcrm (similar to rm) – remove the IPC resource given as argument in the command

  4. ipcs command [staff@localhost staff]$ ipcs -------Shared Memory segments--------- Key shmid owner perms bytes nattch status 0x0000 163841 staff 600 339216 2 dest 0x0000 196610 staff 600 393216 2 dest ……Semaphore Arraya………………. Key semid owner perms nsems …….message queues-------------------- Key msqid owner perms used-bytes messages

  5. IPC mechanisms <ipc.h> • Messages - a tech. which allows processes on the same machine to exchange formatted message • Semaphore – a set of wide variables which can be modified and used by processes on the same machine to synch execution. • Shared memory - a common region of virtual memory( part of RAM), shared by multiple processes on the same machine, such that the data in the shared memory can be directly read and modified by other processes

  6. IPC generic operations • All three IPCs share a set of generic operations: • get operation : to create or gain access to resource. The sys call returns a facility identifier to be used in calls to other IPC routines, • Control oper: to get status or set ctrl values • Specific oper: to do more specific task based on the type of resource.

  7. Common data structure • Note the common definitions for constants across these mechanisms used in their respective sys calls. • The values of IPC_CREAT and others like it are defined in <sys/ipc.h> • #define IPC_CREAT 0x1000 • #define IPC_EXCL 0x2000 • These constant are usually bit-wise operated, eg: IPC_CREAT | “0600” is bitwise OR-ed to achieve the required access rights

  8. Unix Interprocess Comm • Unix is rich in inter process communication mechanism. These are: • Signal • Pipe • FIFO (named pipe) • IPC • Message queues • Semaphore • Shared memory (fastest)

  9. Shared Memory ( shm) • The fastest form of inter- process communication involving data Shared Region memory Process 1 Process 2

  10. …/ shm • The shm (segment) can be shared by many processes, and a process may access many shared segments • Semaphore/messages are used by the kernel to manage write/read to the segment • When a shm is identified, it is initially outside the address space of a process. The process has to execute sys calls to include it into the space

  11. …/shm • System calls involved are: • shmget to translate a shm KEY to a segment-ID. • shmat to amap/attach a segment to the process address space • shmdt to detach a segment from the process address space. • shmctl to destroy a segment when it is no longer required.

  12. Shm-reader process #include <stdio.h> #include “sys/ipc.h” #include “sys/shm.h” #define KEY 1000 /* key for the shm */ main() { int descr, *p, i; printf((“shm: Start of main of “ readerprocess…\n”); //request shared memory of size 10 x integer size descr= shmget(KEY,10*sizeof(int), IPC_CREAT|0660); p = (int *)shmat(descr, NULL,0); while(1){ sleep(2); for(i=0; i<10; i++) { printf(“shm: value of p[%d]= %d\n”,I,p[i]); } } }

  13. Shm –writer process # include <stdio.h> # include “sys/ipc.h” # include “sys/shm.h” # define KEY 1000 /* key for the shm */ main() { int descr, i,g ; int *p; printf(“shm: Start of main of “ writer process…\n”); //request shared memory of size 10 x integer size descr = shmget (KEY,10*sizeof ( int ), IPC_CREAT|0660); p = (int *) shmat(descr, NULL,0); g=0; while(1) { sleep(1); for(i=0; i<10; i++) { p[i] =g; g=g+1; } } }

  14. Shm-reader process #include <stdio.h> #include “sys/ipc.h” #include “sys/shm.h” #define KEY 1000 /* key for the shm */ main() { intdescr, *p, i; printf((“shm: Start of main of “ readerprocess…\n”); //request shared memory of size 10 x integer size //request shared memory of size 10 x integer size descr = shmget(KEY,10*sizeof ( int ), IPC_CREAT|0660); p = (int *) shmat(descr, NULL,0); while(1){ sleep(2); for(i=0; i<10; i++) { printf(“shm: value of p[%d]= %d\n”,I,p[i]); } } } Real Time System

  15. Output • It’s time to think • Explain how does the code behave??

  16. Exercise • Modify the program so that you can accommodate a different data strucuture for the shared memory region, for example which consist of: An array of integers of size 10, followed by an array of characters of size 20 and followed by an array of double of size 5.

  17. UNIX IPC mechanism • Unix is rich in inter process communication mechanism. These are: • Signal • Pipe • FIFO (named pipe) • IPC • Message queues • Semaphore • Shared memory (fastest)

  18. Concept of ‘messages’ • Allow sending and receiving of messages among multiple processes, analogy: having a central mailbox in a building where many people can deposit and receive mails from the box. • Just like all messages have receipient address, each message has an integer mesage type assigned by the sender, so that receipient process can selectively receive messages based on the type. • Messages stored in queue are permanent, even when no proces is referencing the queue. A message is remove only when a process explicitly removes it from the queue. • There can be many message queues, which are referred and used by many processes, and this reflects the dynamism for interprocess comm using this technique.

  19. Kernel data structure for message queue msg3 msg2 msg1 record Message table

  20. Sending and Retrieving messages • When a process sends a message to the queue, the kernel creates a new message record and puts it at the end of the list. • When process retrieve a message, the kernel copies the content to his virtual address, then discard the message from the queue. Retrieval can be: • Retrieval the oldest regardless of message type • Retrieve the message with ID matches the one requested by the process. If there are many take the oldest. • Retrieve a message whose type is lowest among those which are less than or equal.

  21. …/Sending and retrieving messages • If there is no message to be read which satisfies the retrieval criteria, the process will be blocked, unless specified otherwise( IPC_NOWAIT flag is set, and returns a failure). • There are also system imposed limits on manipulation of messages ,eg: MSGMNI = max no. of queues that can exist, MSGMAX = max size of a message etc. • Any system call that attempts to trangress these limits will fail.

  22. Message APIs • msgget – open and create if needed, a message queue for acces • msgsend – send a message toamessage queue • msgrcv – retrieve a message • msgctrl – manipulate the control data of a message queue • These are analogous to : open, read, write,stat,unlink, chmod etc for files

  23. Message Q Buf The structure at the basis of the system describing a message is called msgbuf ;it is declared in linux/msg.h /* message buffer for msgsnd and msgrcv calls */ struct msgbuf { long mtype; /* type of message */ char mtext[1]; /* message text */ };

  24. struct message { long mtype; /* message type */ long sender; /* sender id */ long receiver; /* receiver id */ struct info data; /* message content */ ... };

  25. Sampel Code #include <stdio.h> #include <stdlib.h> #include <linux/ipc.h> #include <linux/msg.h> /* Redefines the struct msgbuf */ typedef struct mymsgbuf { long mtype; int int_num; float float_num; char ch; } mess_t;

  26. …cont int main() { int qid; key_t msgkey; mess_t sent; mess_t received; int length; /* Initializes the seed of the pseudo-random number generator */ srand (time (0)); /* Length of the message */ length = sizeof(mess_t) - sizeof(long); msgkey = ftok(".",'m');

  27. /* Creates the queue*/ qid = msgget(msgkey, IPC_CREAT | 0660); printf("QID = %d\n", qid); /* Builds a message */ sent.mtype = 1; sent.int_num = rand(); sent.float_num = (float)(rand())/3; sent.ch = 'f'; /* Sends the message */ msgsnd(qid, &sent, length, 0); printf("MESSAGE SENT...\n");

  28. /* Receives the message */ msgrcv(qid, &received, length, sent.mtype, 0); printf("MESSAGE RECEIVED...\n"); /* Controls that received and sent messages are equal */ printf("Integer number = %d (sent %d) -- ", received.int_num, sent.int_num); if(received.int_num == sent.int_num) printf(" OK\n"); else printf("ERROR\n"); printf("Float number = %f sent %f -- ", received.float_num, sent.float_num); if(received.float_num == sent.float_num) printf(" OK\n"); else printf("ERROR\n"); printf("Char = %c (sent %c) -- ", received.ch, sent.ch); if(received.ch == sent.ch) printf(" OK\n"); else printf("ERROR\n");

  29. /* Destroys the queue */ msgctl(qid, IPC_RMID, 0);

  30. ENDs here

More Related