Distributed and Parallel Processing George Wells.

Slides:



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

Operating Systems Lecture 7.
CSCC69: Operating Systems
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
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.
System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Understand Process concept Process scheduling Creating.
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.
CMPT 300: Operating Systems I Ch 3: Processes Dr. Mohamed Hefeeda
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
NCHU System & Network Lab Lab 10 Message Queue and Shared Memory.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
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
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program.
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.
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.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
2.3 InterProcess Communication (IPC)
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
1 Semaphores Chapter 7 from Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date: January.
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
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
Operating Systems Yasir Kiani. 13-Sep Agenda for Today Review of previous lecture Interprocess communication (IPC) and process synchronization UNIX/Linux.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
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
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
Semaphores Chapter 7 from Inter-process Communications in Linux:
© 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.
IPC Message Queue. Data Structure msgque IP_NOUSED.
Message Queues. Unix IPC Package ● Unix System V IPC package consists of three things – Messages – allows processes to send formatted data streams to.
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
Architecture of a Proactive Security Tool Vivek Ramachandran.
Distributed and Parallel Processing George Wells.
OPERATING SYSTEM CONCEPT AND PRACTISE
JCSP Tutorial & Homework Hints
Operating Systems Review ENCE 360.
Chapter 3: Process Concept
Message queue Inter process communication primitive
Threads Threads.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Operating System (013022) Dr. H. Iwidat
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
Chapter 4: Threads.
Shared Memory Dr. Yingwu Zhu.
Fork and Exec Unix Model
Chapter 4: Threads.
Chapter 2: The Linux System Part 2
Interprocess Communication
Message Queues.
PTHREADS AND SEMAPHORES
Unix System Calls and Posix Threads
Operating Systems Lecture 8.
Prof. Leonardo Mostarda University of Camerino
| Website for Students | VTU -NOTES -Question Papers
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 3: Process Concept
Shared Memory Dr. Yingwu Zhu Feb, 2007.
Presentation transcript:

Distributed and Parallel Processing George Wells

JCSP: Producer/Consumer Problem channel Producer Consumer One2OneChannel

Integer Producer/Consumer Integer object Producer Consumer One2OneChannelInt channel.write(item) One2OneChannelInt int item = channel.read() Channel_of_Integer

Producer Overview class Producer implements CSProcess { private One2OneChannelInt channel; public Producer (final One2OneChannelInt out) { channel = out; } // constructor public void run() { int item = 100; channel.write(item); } // run } // class Producer

Consumer Overview class Consumer implements CSProcess { private One2OneChannelInt channel; public Consumer (final One2OneChannelInt in) { channel = in; } // constructor public void run() { int item = channel.read(); System.out.println(item); } // run } // class Consumer

JCSP Classes Needed import jcsp.lang.*;

To Execute public final class PCMain { public static void main(String[] args) { new PCMain(); } // main public PCMain() { // create channel object final One2OneChannelInt channel = new One2OneChannelInt(); // create and run parallel construct with list of procs new Parallel ( new CSProcess[] { new Producer(channel), new Consumer(channel) } ).run (); } // constructor } // class PCMain

Practical Assignment 2 On the course web site follow the practical assignment link – Example program to get the feel of the JCSP library

Formal Analysis and JCSP Formal Analysis of Concurrent Java Systems, Peter Welch and Jeremy Martin, Communicating Process Architectures 2000 – Formal model of Java concurrency mechanisms – Formal proof of equivalence between JCSP and CSP communication also CTJ communication – Formal verification of JCSP ALT mechanism incorrect and correct!

Multiprocessing in UNIX UNIX OSes: – Threads (pthreads) – Processes – Interprocess communication Processes – Use the fork/join model

Multiprocessing in UNIX The fork() system call – Creates two copies of calling process int pid; if ((pid = fork()) == 0) { /* Execute child process code */... } else { /* Execute parent process code */... }

Exercise Write a “Hello World” multiprocessing program for Linux – Use fork() – Child and parent write different messages

Interprocess Communication (IPC) in UNIX Unix operating systems have a rich and powerful set of mechanisms – Message queues Asynchronous – Semaphore sets – Shared memory Known as System V IPC

System V IPC Complex APIs All forms require a numeric “key” – Unique to application – Shared by all cooperating processes The ftok system call generates a key from: – a filename – a single character “project” ID Use the key to “get” a numeric identifier

Example: Message Queue int msgKey = ftok(“/home/csgw/somefile.txt”, 'a'); int msgqID = msgget(msgKey, IPC_CREAT | 0660);

Sending a Message Messages have – a type ( long ) – a message (arbitrary block of data) struct msgbuf { long mtype; /* type */ char mtext[1]; /* message text */ }; struct my_msgbuf { long mtype; /* Type */ long request_id; /* Request ID */ struct client info; /* Data */ };

To Send struct msgbuf { long mtype; /* type */ char mtext[1000]; /* message text */ } myMsg; int result, length; /* The length is essentially the size of the structure minus sizeof(mtype) */ length = sizeof(struct msgbuf) – sizeof(long); myMsg.mtype = 1L; if((result = msgsnd( msgqID, &myMsg, length, 0)) == -1) /* ERROR... */ Or IPC_NOWAIT

To Receive struct msgbuf rdBuf; int length, len; length = sizeof(struct msgbuf) – sizeof(long); len = msgrcv(msgqID, &rdBuf, length, 1L, 0); if (len == -1) /* ERROR... */ Or IPC_NOWAIT Type parameter – =0: first message on queue – >0: first message matching type (exactly) – <0: first message with type ≤ |type|

Message Queue Control The msgctl() system call: – Query internal data structures – Set permissions – Delete message queue msgctl( msgqID, IPC_RMID, 0);

Exercise Modify the “Hello World” program – Use fork to create two processes – Parent sends “Hello World” message to child – Child displays message on screen

Semaphores System V IPC Semaphores – Actually sets of semaphores – Multiple, atomic operations int sid, numSems;... sid = semget(myKey, numSems, IPC_CREAT | 0660);

Working with Semaphores The semop() system call int semop (int sid, struct sembuf *sops, unsigned nops); struct sembuf { ushort sem_num; /* semaphore index in array */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */ }; The sem_op field – >0: add to the semaphore value – <0: subtract from the semaphore value Wait, if necessary – =0: Wait for semaphore to reach zero

Example: Simple Binary Semaphore struct sembuf semLock = { 0, -1, 0 }; struct sembuf semUnlock = { 0, 1, IPC_NOWAIT }; if (semop(sid, &semLock, 1) == -1) /* ERROR */ /* Do critical region stuff... */ if (semop(sid, &semUnlock, 1) == -1) /* ERROR */

Semaphore Control The semctl() system call: – Query internal data structures – Set permissions – Delete semaphore set – Get the value(s) of one/all semaphores in a set – Get the number of processes waiting – Get the number of processes waiting for 0 – Get the process ID of the last process to access the semaphore set – Set one/all semaphore value(s)

Shared Memory Use key to “get” shared memory segment – specify desired size int shmID; shmID = shmget(key, segSize, IPC_CREAT | 0640);

Shared Memory Step Two: “attach” the segment char * ptr = shmat(shmID, 0, 0); Desired address (best left as 0) Flags: address rounding, read-only.

When finished... Use shmdt() to detach shared memory segment – Doesn't necessarily delete segment shmdt(ptr);