NCHU System & Network Lab Lab 10 Message Queue and Shared Memory.

Slides:



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

1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania Fall 2003 Lecture Note 2.6: Message-Based Communication.
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.
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.
NCHU System & Network Lab Lab 15 Record Locking. NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same.
NCHU System & Network Lab Lab 12 Page Replacement Algorithm.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
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.
University of Pennsylvania 10/3/00CSE 380 Interprocess Communication CSE 380 Lecture Note 8 Insup Lee.
CSE 451 Section 4 Project 2 Design Considerations.
NCHU System & Network Lab Lab 13 File I/O & Standard I/O.
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
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
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.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.
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.
NCHU System & Network Lab Lab 11 Memory Mapped File.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
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.
Slide 9-1 Copyright © 2004 Pearson Education, Inc. Inter process Communication Mechanisms  Allow arbitrary processes to exchange data and synchronize.
Interprocess Communication
NCHU System & Network Lab Lab 14 File and Directory.
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,
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Termination Process executes last statement and asks the operating.
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/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
Linux Interprocess Communication
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
Shared Memory Dr. Yingwu Zhu.
Interprocess Communication-1
Interprocess Communication and Synchronization using System V IPC
Message Queues.
Inter Process Communication
Lab 5 Process Control Operating System Lab.
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
Lab 10 Paging.
Shared Memory Dr. Yingwu Zhu Feb, 2007.
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

NCHU System & Network Lab Lab 10 Message Queue and Shared Memory

NCHU System & Network Lab Message Queue

NCHU System & Network Lab Message Queue provide a reasonably easy and efficient way of passing data between two unrelated processes Process 1 process2 Message queue 1 message

NCHU System & Network Lab Create and Access a Message Queue #include int msgget(key_t key, int msgflg); key : names a particular message queue. msgflg: IPC_PRIVATE only used by process itself IPC_CREAT create new message queue, if exist, ignore this flag permission flags rwxrwxrwx000 owner permission group permission others permission Return queue identifier on success, -1 on failure

NCHU System & Network Lab Send a Message to a message queue int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg); msqid : message queue identifier msg_ptr : a pointer to the message to be sent. msg_sz: size of the message pointed to by msg_ptr. This size must not include the long int message type msgflg: controls what happens if either the current message queue is full or the systemwide limit on queued messages has been reached IPC_NOWAITreturn -1 without sending message If no IPC_NOWAIT, it will be suspended and waiting for space to become available in the queue. Return 0 on success, -1 on error.

NCHU System & Network Lab Message The structure of the message must follow the following two rules. struct my_message { long int message_type; /* The data you wish to transfer */ } 1.smaller than the system limit. 2.start with a long int.

NCHU System & Network Lab retrieves messages from a message queue int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg); msqid : message queue identifier. msg_ptr: point to the message to be received. msg_sz: size of the message pointed to by msg_ptr. msgtype: a simple form of reception priority. 0retrieves first available message. >0retrieves first with the same type message. <0retrieves the same type or less than it message. msgflg: controls what happens when no message of the appropriate type is waiting to be received IPC_NOWAITreturn -1 without sending message If no IPC_NOWAIT, it will be suspended and waiting for an appropriate type of message arrived in queue. return number of bytes placed in the receive buffer, -1 on error.

NCHU System & Network Lab Control function int msgctl(int msqid, int command, struct msqid_ds *buf); msqid: message queue identifier. command: IPC_STAT Sets the data in the msqid_ds structure to reflect the values associated with the message queue. IPC_SET If the process has permission to do so, this sets the values associated with the message queue to those provided in the msqid_ds data structure. IPC_RMID Deletes the message queue. buf: save the information of msqid. return 0 on success, -1 on error.

NCHU System & Network Lab msqid_ds struct msqid_ds { uid_t msg_perm.uid; uid_t msg_perm.gid; mode_t msg_perm.mode; }

NCHU System & Network Lab Example: retrieve and show

NCHU System & Network Lab Example : input and send

NCHU System & Network Lab Lab I Use message queue write a program that –Show the used message queue information creator id and permission mode number –Two processes can chat with each other –Just like… uid:1 Pmode:0666 P1:hello!! P2:Hi~! P2:how are you ? P1:so so. Input: _ uid:1 Pmode:0666 P1:hello!! P2:Hi~! P2:how are you ? P1:so so. Input: _ Process 1Process 2

NCHU System & Network Lab Shared Memory

NCHU System & Network Lab Shared Memory Two or multiple processes share the same memory region. The fastest form of IPC Process 1Process 2 Shared memory Two processes share the same memory region

NCHU System & Network Lab Obtain a shared memory id. #include int shmget(key_t key, size_t size, int flag); size : the size of the shared memory segment in bytes. flag : IPC_PRIVATE only used by process itself IPC_CREAT create new message queue, if exist, ignore this flag permission flags return shared memory on success, -1 on error. rwxrwxrwx000 owner permission group permission others permission

NCHU System & Network Lab attachment # include char *shmat ( int shmid, char *shmaddr, int shmflg ) shmid: shared memory identifier shmaddr: the address at which the shared memory is to be attached to the current process. This should almost always be a null pointer shmflg : SHM_RNDindicates that the address specified for the second parameter should be rounded down to a multiple of the page size. SHM_RDONLYindicates that the segment will be only read, not written. return -1 on error, the address of the attached shared memory segment for success

NCHU System & Network Lab detachment # include int shmdt ( char *shmaddr) return 0 on success, -1 on error.

NCHU System & Network Lab Controlling and Deallocating Shared Memory #include int shmctl(int shm_id, int command, struct shmid_ds *buf); shm_id : shared memory identifier command: IPC_STATcopy the information about the shared memory segment into the buffer buf IPC_SET apply the changes the user has made to the uid, gid, or mode members of the shm_perms field. IPC_RMIDmark the segment as destroyed. buf : save the information of shmid. Return 0 on success, -1 on error

NCHU System & Network Lab shmid_ds structure struct shmid_ds { struct ipc_perm shm_perm; /* operation perms */ int shm_segsz; /* size of segment(bytes) */ time_t shm_atime; /* last attach time */ time_t shm_dtime; /* last detach time */ time_t shm_ctime; /* last change time */ unsigned short shm_cpid; /* pid of creator */ unsigned short shm_lpid; /* pid of last operator */ short shm_nattch; /* no. of current attaches */ /* the following are private */ unsigned short shm_npages; /* size of segment (pages) */ unsigned long *shm_pages; struct shm_desc *attaches; /* descriptors for attaches */ };

NCHU System & Network Lab ipc_perm structure struct ipc_perm { key_t key; ushort uid; /* owner euid and egid */ ushort gid; ushort cuid; /* creator euid and egid */ ushort cgid; ushort mode; /* lower 9-bits of access modes */ ushort seq; /* sequence number */ };

NCHU System & Network Lab Example input.c

NCHU System & Network Lab Example output.c

NCHU System & Network Lab Lab I Use shared memory write a program that –Show the information of shared memory pid of creator and last attach time. –Two processes can chat with each other pid:2 last: xxxx P1:hello!! P2:Hi~! P2:how are you ? P1:so so. Input: _ pid:2 last: xxxx P1:hello!! P2:Hi~! P2:how are you ? P1:so so. Input: _ Process 1Process 2

NCHU System & Network Lab Reference Advanced Programming in the UNIX Environment 2nd Author : Richard Stevens, Stephen A.Rago, Publisher : Addison-Wesley Beginning Linux Programming Author : Richard Stones, Neil MatthewPublisher : Wrox Operating System Concepts 6th edition Author : Abraham Silberschatz, Peter Baer Galvin, Greg Gagne Publisher : John Wiley & Sons, inc. Google LINUX MAN PAGES ONLINE