KUKUM Real Time System Module #3 POSIX programming- Shm Lecture 4.

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.
Operating Systems Lecture 7.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CSCC69: Operating Systems
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
Inter-process communication (IPC) using Shared Memory & Named Pipes CSE 5520/4520 Wireless Networks.
System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
3.5 Interprocess Communication
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Linux Operating System
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Phones OFF Please Processes Parminder Singh Kang Home:
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
Interprocess Communication. Process Concepts Last class.
Inter-Process Communication: Message Passing
Chapter 8 Windows Outline Programming Windows 2000 System structure Processes and threads in Windows 2000 Memory management The Windows 2000 file.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Introduction to Processes CS Intoduction to Operating Systems.
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.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Chapter 6 UNIX Special Files Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
Inter-process Communication:
CE Operating Systems Lecture 10 Processes and process management in Linux.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /* */
Operating Systems Yasir Kiani. 13-Sep Agenda for Today Review of previous lecture Interprocess communication (IPC) and process synchronization UNIX/Linux.
File descriptor table File descriptor (integer)File name 0stdin 1stdout 2stderr Use open(), read(), write() system calls to access files Think what happens.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
UNIX IPC CSC345.
Advanced UNIX IPC Facilities After Haviland, et al.’s book.
Interprocess Communication
 Wind River Systems, Inc Chapter - 7 Intertask Communication.
Message Queues. Unix IPC Package ● Unix System V IPC package consists of three things – Messages – allows processes to send formatted data streams to.
InterProcess Communication. Interprocess Communication Processes within a system may be independent or cooperating Cooperating process can affect or be.
CSE Operating System Principles
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally INTERPROCESS COMMUNICATION AND SYNCHRONIZATION SYNCHRONIZATION.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Termination Process executes last statement and asks the operating.
Distributed and Parallel Processing George Wells.
Message queue Inter process communication primitive
Processes in Unix, Linux, and Windows
INTER-PROCESS COMMUNICATION
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
Processes in Unix, Linux, and Windows
Shared Memory Dr. Yingwu Zhu.
Interprocess Communication (IPC)
Interprocess Communication
Processes in Unix, Linux, and Windows
Message Queues.
Unix programming Term: Unit-V I PPT Slides
Processes in Unix, Linux, and Windows
Presentation transcript:

KUKUM Real Time System Module #3 POSIX programming- Shm Lecture 4

KUKUM Real Time System 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)

KUKUM Real Time System 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: 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

KUKUM Real Time System ipcs command staff]$ ipcs Shared Memory segments Keyshmidownerpermsbytes nattchstatus 0x staff dest 0x staff dest ……Semaphore Arraya………………. Keysemidownerpermsnsems …….message queues Keymsqidownerpermsused-bytesmessages

KUKUM Real Time System IPC mechanisms 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

KUKUM Real Time System 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.

KUKUM Real Time System 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 #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

KUKUM Real Time System 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)

KUKUM Real Time System Shared Memory ( shm) The fastest form of inter- process communication involving data Process 1Process 2 memory Shared Region

KUKUM Real Time System …/ 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

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

KUKUM Real Time System Shm-reader process #include #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]); } }

KUKUM Real Time System Shm –writer process #include #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;} }

KUKUM Real Time System Output It’s time to think Explain how does the code behave??

KUKUM Real Time System Exercise 1.Modify the program so that you can accommodate a different data strucuture for the shared memory region, fro 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.

KUKUM Real Time System 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)

KUKUM Real Time System 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 mailsfromthe box. Just like all messges have receipient address, each message has an integer mesae type assigned by the sender, so tht receipient process can selectively recive 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.

KUKUM Real Time System Problems with pipe and FIFO No mechanism for selective communication between processes, eg: A,B are writers, C, D are readers to a pipe. If A’s data is only meant for C and B’s data for D, then C an D cannot selectively read data relevant to each o them only. With the un-named pipe, problem of transience ( exist only temporarily) is also true.

KUKUM Real Time System Kernel data structure for message queue Message table msg3msg1msg2 record

KUKUM Real Time System 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.

KUKUM Real Time System …/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.

KUKUM Real Time System 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

KUKUM Real Time System Message queue program – q.h /* q.h– header for message facility example*/ #include extern int errno; #define QKEY(key_t)0105/*identifying key for queue*/ #define QPERM 0660/*permission bits for queue*/ #define MAXOBN 50/*max len of object name*/ #define MAXPRIOR 10/*max priority level*/ struct q_entry{/*structure used for message*/ longmtype; char mtext[MAXOBN+1]; };

KUKUM Real Time System Message queue prog- init_queue (create queue) /*init_queue – get queue identifier */ #include “q.h” init_queue() { intqueue_id; /* attempt to create message queue */ if((queue_id = msgge(QKEY, IPC_CREAT|QPERM)==- 1) perror(“init_queue: msggetfaied”); return queue_id; }

KUKUM Real Time System Message queue program – client.c /*enter – places an object into a queue*/ #include “q.h” static int s_qid = -1; /*message queue identifier*/ char *objname; /*objname*/ int priority; /* priority level */ { int len; char *strncpy(); strcut q_entry s_entry; /* strcut to hold msg */ /* 1 st validate length and priority level */ if((len =strlen(objname))>MAXOBN){ printf(“name too lon \n”); return(-1); }

KUKUM Real Time System …/ client if(priorit >MAXPRIOR || priority<0){ printf(“client:invalid priority\n”); return(-1); } If(s_qid== -1 && ( s_qid = init_queue())==-1) return(-1); /*initialize s_entry*/ s_entry.mtype=(long)priority; strncpy(s_entry.mtext,objname, MAXOBN); /*send message, wait if necessary */ if(msgsnd(s_qid,&s_entry,len,0)==-1){ perror(“client: msgsnd failed”); return(-1);} else return(0); }

KUKUM Real Time System Message queue program- server.c /* server – server object with highest priority on queue */ #include “q.h” static int r_qid = -1;/*msg queue identifier*/ serve() { intmlen; struct q_entry, r_entry;/*strcut to hldmsg */ /*initialize queue */ if(r_qid== -1 && ( r_qid = init_queue()) ==-1) return(-1); for(;;) { if(mlen=msgrcv(r_qid, &r_entry, MAXOBN,(long)-1*MAXPRIOR, MSG_NOERROR)) == -1){ perror(“msgrcv failed”); return(-1); }else{ /*make sure we have string */ r_entry.mtext[mlem] =‘\0’; /*process object name */ proc_obj(&r_ntry); }

KUKUM Real Time System Test Program- client_test.c /* client_test- client enters object name into a queue*/ #include #include “q.h” main(argc, argv) { int argc; char *argv[]; int priority; if(argc != 3){ fprintf(stderr, “usage: %s objnme priority\n”,argv[0]); exit(1);} if((priority=atoi(argv[2])) MXPRIOR){ printf(“Invalid priority \n”); exit(2);} if(enter(argv[1],priority)<0){ printf(“enter fails\n”); exit(3);} exit(0); }

KUKUM Real Time System Test prog- server_test.c /* server_test –simple server for queue*/ #include #include “q.h” main() { intpid; pid=fok(); switch(pid){ case 0://child setpgrp();//break link with teminal, i.e with parent //and become a new proces leader printf(“Sever pocess pid is %d \n”,getpgrp()); serve(); break;//in real serve never exits

KUKUM Real Time System …/ server_test.c case -1:printf(“fork to start seve failed \n”); break; default: ; } exit(pid != -1 ? 0:1); // not good in SE proc_obj(msg); struct q_entry *msg; { printf(“\npriority: %ld name:%s\n”, msg->mtype, msg->mtext); }

KUKUM Real Time System Execution output power0:12/user/test>client_test kerja1 3 power0:13/user/test>client_test kerja2 5 power0:14/user/test>client_test kerja3 8 power0:15/user/test>client_test kerja4 1 power0:20/user/test>server_test server process pid is priority: 1 name: kerja4 priority: 2 name: kerja1 priority: 3 name: kerja2 priority: 3 name: kerja3

KUKUM Real Time System The END