Presentation is loading. Please wait.

Presentation is loading. Please wait.

1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 7: POSIX Interprocess Communication (IPC)

Similar presentations


Presentation on theme: "1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 7: POSIX Interprocess Communication (IPC)"— Presentation transcript:

1 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 7: POSIX Interprocess Communication (IPC)

2 2GWU CS 259 Brad Taylor Spring 2004 Objectives IPC Overview IPC Overview Semaphore Sets Semaphore Sets Shared Memory Shared Memory Message Queues Message Queues Producer-Consumer Synchronization Producer-Consumer Synchronization Project Status Briefs (45% code review) Project Status Briefs (45% code review) Assignment 4: deferred to next week, finish project coding this week! Assignment 4: deferred to next week, finish project coding this week!

3 3GWU CS 259 Brad Taylor Spring 2004 POSIX Interprocess Communication IPC: What means exist? Why is it necessary? IPC: What means exist? Why is it necessary? What is the terminology? What is the terminology? How does it work? How does it work? How to deal with multiple processes? How to deal with multiple processes? Concurrency … in yet another form Concurrency … in yet another form

4 4GWU CS 259 Brad Taylor Spring 2004 Cooperating Processes An Independent process is and does not affect another process executionAn Independent process is and does not affect another process execution A Cooperating process can affect or be affected by execution of another processA Cooperating process can affect or be affected by execution of another process Process cooperation Benefits :Process cooperation Benefits : –Information sharing –Computation speed-up –Modularity –Convenience Cooperation Costs :Cooperation Costs : –Complexity –Overhead: Memory, Synchronization, Transmission Analogy: Team vs. Individual task accomplishmentAnalogy: Team vs. Individual task accomplishment

5 5GWU CS 259 Brad Taylor Spring 2004 IPC Overview Mechanisms allowing processes to communicate and synchronize actionsMechanisms allowing processes to communicate and synchronize actions Semaphore & Shared Memory methods extendedSemaphore & Shared Memory methods extended Message system: processes communicate with others without resorting to shared variablesMessage system: processes communicate with others without resorting to shared variables 3 Different, but similar means, to accomplish same goal3 Different, but similar means, to accomplish same goal Shared Memory fastest, but requires external synchronization; OS must maintain link countShared Memory fastest, but requires external synchronization; OS must maintain link count Semaphore sets fast; most useful as generic advisory locking mechanismsSemaphore sets fast; most useful as generic advisory locking mechanisms IPC message facility slowest, but most flexible, providing two operations:IPC message facility slowest, but most flexible, providing two operations: – send ( destination, message ) – message size fixed or variable – receive ( source, message )

6 6GWU CS 259 Brad Taylor Spring 2004 IPC Overview (con’t) If P and Q wish to communicate, they need to:If P and Q wish to communicate, they need to: –establish a communication link between them (uni-/multi-cast) –exchange messages via send/receive Implementation of communication linkImplementation of communication link –physical (shared memory, hardware bus) –logical (message queue) Other IPC mechanisms covered previously :Other IPC mechanisms covered previously : –shared variables with locks (2) –files (3) –pipes (3) –signals (5) –threads {semaphores & shared memory} (6) –network sockets remain

7 7GWU CS 259 Brad Taylor Spring 2004 Implementation Questions How are links established?How are links established? Can a link be associated with more than two processes?Can a link be associated with more than two processes? How many links can there be between every pair of communicating processes?How many links can there be between every pair of communicating processes? What is the capacity of a link?What is the capacity of a link? Is the size of a message that the link can accommodate fixed or variable?Is the size of a message that the link can accommodate fixed or variable? Is a link unidirectional or bi-directional?Is a link unidirectional or bi-directional?

8 8GWU CS 259 Brad Taylor Spring 2004 Direct Communication Processes must name each other explicitly:Processes must name each other explicitly: – send ( P, message ) – send a message to process P – receive ( Q, message ) – receive a message from process Q Properties of communication linkProperties of communication link –Links established automatically –Link associated with exactly one pair of communicating processes –Between each pair there exists exactly one link –Link may be unidirectional, but usually bi-directional

9 9GWU CS 259 Brad Taylor Spring 2004 Indirect Communication Messages directed to and received from mailboxes (aka ports or keys )Messages directed to and received from mailboxes (aka ports or keys ) –Each mailbox has a unique id –Processes communicate only through shared mailboxes Communication link propertiesCommunication link properties –Link established only if processes share a common mailbox –A link may be associated with many processes –Each pair of processes may share several communication links –Link may be unidirectional or bi-directional

10 10GWU CS 259 Brad Taylor Spring 2004 Indirect Communication OperationsOperations –create a new mailbox –send and receive messages through mailbox –destroy a mailbox Primitives are defined as:Primitives are defined as: send ( A, message ) – send a message to mailbox A send ( A, message ) – send a message to mailbox A receive ( A, message ) – receive a message from mailbox A receive ( A, message ) – receive a message from mailbox A

11 11GWU CS 259 Brad Taylor Spring 2004 Indirect Communication Mailbox sharingMailbox sharing – P 1, P 2, and P 3 share mailbox A – P 1, sends; P 2 and P 3 receive –Who gets the message? SolutionsSolutions –Allow a link to be associated with at most two processes –Allow only one process at a time to execute a receive operation –Allow the system to select receiver arbitrarily (e.g., first available); sender notified who receiver was –Example: Queue for multiple printer pool

12 12GWU CS 259 Brad Taylor Spring 2004 Synchronization Message passing may be either blocking or non-blockingMessage passing may be either blocking or non-blocking Blocking is considered synchronous Blocking is considered synchronous Non-blocking is considered asynchronous Non-blocking is considered asynchronous send and receive primitives may be either blocking or non-blocking send and receive primitives may be either blocking or non-blocking

13 13GWU CS 259 Brad Taylor Spring 2004 Buffering Queue of messages attached to the link; implemented in one of three ways:Queue of messages attached to the link; implemented in one of three ways: –1. Zero capacity – 0 messages: Sender must wait for receiver (rendezvous) –2. Bounded capacity – finite length of n messages: Sender must wait if link full –3. Unbounded capacity – infinite length: Sender never waits

14 14GWU CS 259 Brad Taylor Spring 2004 Remember … Bounded-Buffer Shared data: #define BUFFER_SIZE 10 typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; int counter = 0;

15 15GWU CS 259 Brad Taylor Spring 2004 Bounded-Buffer (con’t) Producer process item nextProduced; while (1) { while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++;}

16 16GWU CS 259 Brad Taylor Spring 2004 Bounded-Buffer (con’t) Consumer process item nextConsumed; while (1) { while (counter == 0) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--;}

17 17GWU CS 259 Brad Taylor Spring 2004 Bounded-Buffer (con’t) The statements counter++; counter--; must be performed atomically.The statements counter++; counter--; must be performed atomically. Atomic operation means an operation that completes in its entirety without interruptionAtomic operation means an operation that completes in its entirety without interruption If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleavedIf both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved Interleaving depends upon how producer and consumer processes scheduledInterleaving depends upon how producer and consumer processes scheduled Race condition : The situation where several processes access – and manipulate shared data concurrently: the final value of the shared data depends upon which process finishes last Race condition : The situation where several processes access – and manipulate shared data concurrently: the final value of the shared data depends upon which process finishes last To prevent race conditions, concurrent processes must be synchronizedTo prevent race conditions, concurrent processes must be synchronized

18 18GWU CS 259 Brad Taylor Spring 2004 Semaphore Sets Motivation: Similar to file locking used as generic advisory locking mechanisms to control: Motivation: Similar to file locking used as generic advisory locking mechanisms to control: –access to files –shared memory –other resources atomically Operations: Operations: –set it –check it –wait until it clears then set it ("test-n-set") –That’s it!

19 19GWU CS 259 Brad Taylor Spring 2004 Semaphore Set Creation semget() adds new semaphore to current process; if successful, created semaphore ID semid returnssemget() adds new semaphore to current process; if successful, created semaphore ID semid returns int semget(key_t key, int nsems, int semflg); Common to the 3 POSIX IPC methods is the key means of identifying the mailbox; ftok() creates a unique one:Common to the 3 POSIX IPC methods is the key means of identifying the mailbox; ftok() creates a unique one: key_t ftok(const char *path, int id); –path is a file that this process can read –id is usually just set to some arbitrary char, like 'A' –ftok() generates a probably-unique key for semget() –processes using the same semaphore must generate the same key, so the same parameters should be passed nsems is the number of semaphores in this semaphore setnsems is the number of semaphores in this semaphore set semflg tells semget() new semaphore set permissions, if a new set is being created or connecting to an existing one (creating permissions for a new set may be done bit-wise or the accessed with IPC_CREAT)semflg tells semget() new semaphore set permissions, if a new set is being created or connecting to an existing one (creating permissions for a new set may be done bit-wise or the accessed with IPC_CREAT)

20 20GWU CS 259 Brad Taylor Spring 2004 Semaphore Set Creation Example #include #include key_t key; int semid; key = ftok("/brad/assignments/hw4", ‘M'); semid = semget(key, 10, 0666 | IPC_CREAT); Creates a 10 semaphore set, generates the key with ftok() and, with 666 (rw-rw-rw-) permissionsCreates a 10 semaphore set, generates the key with ftok() and, with 666 (rw-rw-rw-) permissions

21 21GWU CS 259 Brad Taylor Spring 2004 Semaphore Set Operations All operations that set, get, or test-n-set a semaphore use the semop() system call and structure sembuf All operations that set, get, or test-n-set a semaphore use the semop() system call and structure sembuf int semop(int semid,struct sembuf *sops, unsigned int nsops); struct sembuf { ushort sem_num; ushort sem_num; short sem_op; short sem_op; short sem_flg; short sem_flg; }; }; semid : key of semaphore set semid : key of semaphore set sops : pointer to the struct sembuf sops : pointer to the struct sembuf nsop : As an array of struct sembuf s to do many semaphore operations at the same time, the nsop argument addresses this one; if only using one, this argument is 1 nsop : As an array of struct sembuf s to do many semaphore operations at the same time, the nsop argument addresses this one; if only using one, this argument is 1 sem_num : semaphore number (in the set) being manipulated sem_num : semaphore number (in the set) being manipulated sem_op : sem_op : –Positive: The value of sem_op is added to the semaphore's value: this is how a process uses a semaphore to mark a resource as allocated –Negative: While absolute value of sem_op > semaphore value, calling process blocks until semaphore value reaches sem_op absolute value; then, absolute value of sem_op is subtracted from semaphore's value: this is how a process releases semaphore-guarded resource –Zero: This process will wait until the semaphore in question reaches 0 sem_flg : allows specifying flags to further modify semop() call effectssem_flg : allows specifying flags to further modify semop() call effects

22 22GWU CS 259 Brad Taylor Spring 2004 Semaphore Set Sample Programs Common includes, all 3 programs: #include #include Run seminit.c to create the semaphoreRun seminit.c to create the semaphore Try using ipcs from the command line to verify it existsTry using ipcs from the command line to verify it exists Them run semdemo.c in a couple of windows to see how they interactThem run semdemo.c in a couple of windows to see how they interact Finally, use semrm.c to remove the semaphoreFinally, use semrm.c to remove the semaphore Might also try removing the semaphore while running semdemo.c just to see what kinds of errors are generatedMight also try removing the semaphore while running semdemo.c just to see what kinds of errors are generated

23 23GWU CS 259 Brad Taylor Spring 2004 seminit.c int main(void) { key_t key; key_t key; int semid; int semid; union semun arg; union semun arg; if ((key = ftok("semdemo.c", 'J')) == -1) { if ((key = ftok("semdemo.c", 'J')) == -1) { perror("ftok"); perror("ftok"); exit(1); exit(1); } /* create a semaphore set with 1 semaphore: */ /* create a semaphore set with 1 semaphore: */ if ((semid = semget(key, 1, 0666 | IPC_CREAT)) == -1) { if ((semid = semget(key, 1, 0666 | IPC_CREAT)) == -1) { perror("semget"); perror("semget"); exit(1); exit(1); } /* initialize semaphore #0 to 1: */ /* initialize semaphore #0 to 1: */ arg.val = 1; arg.val = 1; if (semctl(semid, 0, SETVAL, arg) == -1) { if (semctl(semid, 0, SETVAL, arg) == -1) { perror("semctl"); perror("semctl"); exit(1); exit(1); } return 0; return 0; }

24 24GWU CS 259 Brad Taylor Spring 2004 semdemo.c int main(void) int main(void) { key_t key; key_t key; int semid; int semid; struct sembuf sb = {0, -1, 0}; /* set to allocate resource */ struct sembuf sb = {0, -1, 0}; /* set to allocate resource */ if ((key = ftok("semdemo.c", 'J')) == -1) { if ((key = ftok("semdemo.c", 'J')) == -1) { perror("ftok"); perror("ftok"); exit(1); exit(1); } /* grab the semaphore set created by seminit.c: */ /* grab the semaphore set created by seminit.c: */ if ((semid = semget(key, 1, 0)) == -1) { if ((semid = semget(key, 1, 0)) == -1) { perror("semget"); perror("semget"); exit(1); exit(1); } printf("Press return to lock: "); printf("Press return to lock: "); getchar(); getchar(); printf("Trying to lock...\n"); printf("Trying to lock...\n"); if (semop(semid, &sb, 1) == -1) { if (semop(semid, &sb, 1) == -1) { perror("semop"); perror("semop"); exit(1); exit(1); } printf("Locked.\n"); printf("Locked.\n"); printf("Press return to unlock: "); printf("Press return to unlock: "); getchar(); getchar(); sb.sem_op = 1; /* free resource */ sb.sem_op = 1; /* free resource */ if (semop(semid, &sb, 1) == -1) { if (semop(semid, &sb, 1) == -1) { perror("semop"); perror("semop"); exit(1); exit(1); } printf("Unlocked\n"); printf("Unlocked\n"); return 0; return 0; }

25 25GWU CS 259 Brad Taylor Spring 2004 semrm.c int main(void) { key_t key; key_t key; int semid; int semid; union semun arg; union semun arg; if ((key = ftok("semdemo.c", 'J')) == -1) { if ((key = ftok("semdemo.c", 'J')) == -1) { perror("ftok"); perror("ftok"); exit(1); exit(1); } /* grab the semaphore set created by seminit.c: */ /* grab the semaphore set created by seminit.c: */ if ((semid = semget(key, 1, 0)) == -1) { if ((semid = semget(key, 1, 0)) == -1) { perror("semget"); perror("semget"); exit(1); exit(1); } /* remove it: */ /* remove it: */ if (semctl(semid, 0, IPC_RMID, arg) == -1) { if (semctl(semid, 0, IPC_RMID, arg) == -1) { perror("semctl"); perror("semctl"); exit(1); exit(1); } return 0; return 0; }

26 26GWU CS 259 Brad Taylor Spring 2004 Shared Memory Motivation: shared memory is what it sounds like: a segment of memory shared between processes: think of the potential! Motivation: shared memory is what it sounds like: a segment of memory shared between processes: think of the potential! –Example: allocate a block a player information for a multi- player game; each process access it at will: fun, fun, fun! –Fastest form of IPC between processes –Once shared memory region is mapped into process address space, no further kernel involvement in passing data between processes, contrary to others –However, synchronization required Operations: Operations: –Creating –Attaching –Using –Destroying –Synchronizing

27 27GWU CS 259 Brad Taylor Spring 2004 Creating Shared Memory shmget() shared memory segment is created and connected to current process; if successful, returns a shared memory segment identifiershmget() shared memory segment is created and connected to current process; if successful, returns a shared memory segment identifier int shmget(key_t key, size_t size, int shmflg); As in Semaphore sets, ftok() generates a probably- unique key for shmget()As in Semaphore sets, ftok() generates a probably- unique key for shmget() size is the shared memory segment size (bytes)size is the shared memory segment size (bytes) shmflg should be set to the permissions of the segment bitwise-ORd with IPC_CREAT if you want to create the segment (always allowable), but can be 0 otherwiseshmflg should be set to the permissions of the segment bitwise-ORd with IPC_CREAT if you want to create the segment (always allowable), but can be 0 otherwise

28 28GWU CS 259 Brad Taylor Spring 2004 Shared Memory Creation Example #include #include key_t key; key_t key; int shmid; int shmid; key = ftok("/brad/travel/hawaii", 'R'); key = ftok("/brad/travel/hawaii", 'R'); shmid = shmget(key, 1024, 0644 | IPC_CREAT); shmid = shmget(key, 1024, 0644 | IPC_CREAT); Creates a 1K segment with 644 (rw-r--r--) permissionsCreates a 1K segment with 644 (rw-r--r--) permissions

29 29GWU CS 259 Brad Taylor Spring 2004 Attaching Shared Memory shmat() Before using shared memory, the process must attach to it using this callshmat() Before using shared memory, the process must attach to it using this call void *shmat(int shmid, void *shmaddr, int shmflg); shmid is shared memory ID from shmget() callshmid is shared memory ID from shmget() call shmaddr may be used to tell shmat() which specific address to use, but setting it to 0 (strongly preferred) allows the OS choose the address for youshmaddr may be used to tell shmat() which specific address to use, but setting it to 0 (strongly preferred) allows the OS choose the address for you shmflg can be set to SHM_RDONLY if you only want to read from it, 0 otherwiseshmflg can be set to SHM_RDONLY if you only want to read from it, 0 otherwise

30 30GWU CS 259 Brad Taylor Spring 2004 Shared Memory Attaching Example #include #include key_t key; key_t key; int shmid; int shmid; key = ftok("/brad/travel/hawaii", 'R'); key = ftok("/brad/travel/hawaii", 'R'); shmid = shmget(key, 1024, 0644 | IPC_CREAT); shmid = shmget(key, 1024, 0644 | IPC_CREAT); data = shmat(shmid, (void *)0, 0); data = shmat(shmid, (void *)0, 0); This returns a pointer to the shared memory segmentThis returns a pointer to the shared memory segment Notice that shmat() returns a void pointer; in this case, a char pointerNotice that shmat() returns a void pointer; in this case, a char pointer It may be treated as anything, depending on what kind of data is thereIt may be treated as anything, depending on what kind of data is there Pointers to arrays of structures are just as acceptable as anything elsePointers to arrays of structures are just as acceptable as anything else

31 31GWU CS 259 Brad Taylor Spring 2004 Using Shared Memory Reading and Writing: Reading and Writing: Continuing with the data pointer from the previous example: as a char pointer, reading and writing chars from it; also, for simplicity, presume it contains a null-terminated string Continuing with the data pointer from the previous example: as a char pointer, reading and writing chars from it; also, for simplicity, presume it contains a null-terminated string It couldn't be easier: as it's just a string in there, print is like this: It couldn't be easier: as it's just a string in there, print is like this: printf("shared contents: %s\n", data); Storing something in it as easily as this:Storing something in it as easily as this: printf("Enter a string: "); gets(data); Of course, other data can be handled in there besides just charsOf course, other data can be handled in there besides just chars

32 32GWU CS 259 Brad Taylor Spring 2004 Using Shared Memory (con’t) Destroying: Destroying: – When done with the shared memory segment, the process should detach itself from it using the shmdt() call: int shmdt(void *shmaddr); shmaddr is the address you got from shmat() – When no longer used by any processes it must be removed: shmctl(shmid, IPC_RMID, NULL); Concurrency: Concurrency: –As mentioned previously, Semaphores provide good controls for shared memory access

33 33GWU CS 259 Brad Taylor Spring 2004 Shared Memory Sample Program Note: Semaphores omitted for simplicity, but any use of shared memory should include them! #include #include #define SHM_SIZE 1024 /* make it a 1K shared memory segment */ #define SHM_SIZE 1024 /* make it a 1K shared memory segment */ int main(int argc, char *argv[]) int main(int argc, char *argv[]) { key_t key; key_t key; int shmid; int shmid; char *data; char *data; int mode; int mode; if (argc > 2) { if (argc > 2) { fprintf(stderr, "usage: shmdemo [data_to_write]\n"); fprintf(stderr, "usage: shmdemo [data_to_write]\n"); exit(1); exit(1); }

34 34GWU CS 259 Brad Taylor Spring 2004 Shared Memory Sample Program (con’t) /* make the key: */ /* make the key: */ if ((key = ftok("shmdemo.c", 'R')) == -1) { if ((key = ftok("shmdemo.c", 'R')) == -1) { perror("ftok"); perror("ftok"); exit(1); exit(1); } /* connect to (and possibly create) the segment: */ /* connect to (and possibly create) the segment: */ if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) { if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) { perror("shmget"); perror("shmget"); exit(1); exit(1); } /* attach to the segment to get a pointer to it: */ /* attach to the segment to get a pointer to it: */ data = shmat(shmid, (void *)0, 0); data = shmat(shmid, (void *)0, 0); if (data == (char *)(-1)) { if (data == (char *)(-1)) { perror("shmat"); perror("shmat"); exit(1); exit(1); }

35 35GWU CS 259 Brad Taylor Spring 2004 Shared Memory Sample Program (con’t) /* read or modify the segment, based on the command line: */ /* read or modify the segment, based on the command line: */ if (argc == 2) { if (argc == 2) { printf("writing to segment: \"%s\"\n", argv[1]); printf("writing to segment: \"%s\"\n", argv[1]); strncpy(data, argv[1], SHM_SIZE); strncpy(data, argv[1], SHM_SIZE); } else } else printf("segment contains: \"%s\"\n", data); printf("segment contains: \"%s\"\n", data); /* detach from the segment: */ /* detach from the segment: */ if (shmdt(data) == -1) { if (shmdt(data) == -1) { perror("shmdt"); perror("shmdt"); exit(1); exit(1); } return 0; return 0; }

36 36GWU CS 259 Brad Taylor Spring 2004 Message Queues Motivation: Message Queues provide flexibility Motivation: Message Queues provide flexibility –Although not as fast as semaphores allows cooperating tasks to communicate with each other –Allows messages to be queued and sent to & received from processes & interrupt service routines –Multiple processes can sent to & received from a common message queue –Work similar to a FIFO, but support some additional functionality as, generally, messages are taken off the queue in order put on; however, there are ways to pull certain messages from the queue before reaching front Operations: Operations: –Creating –Sending –Receiving –Destroying

37 37GWU CS 259 Brad Taylor Spring 2004 Message Queue Creation msgget() adds new message queue to current process; if successful, created message queue returns ID semid (or if fails, returns -1)msgget() adds new message queue to current process; if successful, created message queue returns ID semid (or if fails, returns -1) int msgget(key_t key, int msgflg); As in the other IPC methods, ftok() generates a probably-unique key for msgget()As in the other IPC methods, ftok() generates a probably-unique key for msgget() msgflg tells msgget() how to handle this new queue:msgflg tells msgget() how to handle this new queue: –For creation, this field must be set equal to IPC_CREAT bit- wise OR'd with the permissions for this queue –The queue permissions are same as standard file permissions--queues take on the user-id and group-id of creating process

38 38GWU CS 259 Brad Taylor Spring 2004 Sending to Message Queues Passing information to a message queue uses msgsnd(): Passing information to a message queue uses msgsnd(): int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); Each message is made up of two parts, which are defined in the template structure struct msgbuf, as defined in sys/msg.h : Each message is made up of two parts, which are defined in the template structure struct msgbuf, as defined in sys/msg.h : struct msgbuf { ***OR***struct pirate_msgbuf { struct msgbuf { ***OR***struct pirate_msgbuf { long mtype;long mtype; /* must be positive */ long mtype;long mtype; /* must be positive */ char mtext[1];char name[30]; char mtext[1];char name[30]; };char ship_type; };char ship_type; int notoriety; int cruelty; int cruelty; int booty_value; }; msgid : key of message queue msgid : key of message queue msgp : pointer to data being put on the queue msgp : pointer to data being put on the queue msgsz : data size (bytes) being added to the queue msgsz : data size (bytes) being added to the queue msgflg : allows setting some optional flag parameters, ignored for by setting to 0 msgflg : allows setting some optional flag parameters, ignored for by setting to 0

39 39GWU CS 259 Brad Taylor Spring 2004 Message Queues Sending Example #include #include key_t key; key_t key; int msqid; int msqid; struct pirate_msgbuf pmb = {2, "L'Olonais", 'S', 80, 10, 12035}; struct pirate_msgbuf pmb = {2, "L'Olonais", 'S', 80, 10, 12035}; key = ftok("/brad/atsea/pirates", 'b'); key = ftok("/brad/atsea/pirates", 'b'); msqid = msgget(key, 0666 | IPC_CREAT); msqid = msgget(key, 0666 | IPC_CREAT); msgsnd(msqid, &pmb, sizeof(pmb), 0); /* stick him on the queue */ msgsnd(msqid, &pmb, sizeof(pmb), 0); /* stick him on the queue */ The mtype field is arbitrarily set to 2: that’s important for receivingThe mtype field is arbitrarily set to 2: that’s important for receiving

40 40GWU CS 259 Brad Taylor Spring 2004 Receiving from Message Queues Receiving information from a message queue uses msgrcv(): Receiving information from a message queue uses msgrcv(): int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); Others defined before; mtype within msgbuf affects msgrcv based on msgtyp value: Others defined before; mtype within msgbuf affects msgrcv based on msgtyp value: –Positive: Get the next message with an mtype equal to the specified msgtyp –Negative: Retrieve the first message on the queue whose mtype field is less than or equal to the absolute value of the msgtyp argument –Zero: Retrieve the next message on the queue, regardless of its mtype Example: Example: #include #include key_t key; key_t key; int msqid; int msqid; struct pirate_msgbuf pmb; /* where L'Olonais is to be kept */ struct pirate_msgbuf pmb; /* where L'Olonais is to be kept */ key = ftok("/home/beej/somefile", 'b'); key = ftok("/home/beej/somefile", 'b'); msqid = msgget(key, 0666 | IPC_CREAT); msqid = msgget(key, 0666 | IPC_CREAT); msgrcv(msqid, &pmb, sizeof(pmb), 2, 0); /* get him off the queue! */ msgrcv(msqid, &pmb, sizeof(pmb), 2, 0); /* get him off the queue! */

41 41GWU CS 259 Brad Taylor Spring 2004 Message Queue Sample Program 2 Programs: kirk.c adds messages to the message queue, and spock.c retrieves them kirk.c: #include #include struct my_msgbuf { struct my_msgbuf { long mtype; long mtype; char mtext[200]; char mtext[200]; }; }; int main(void) int main(void) { struct my_msgbuf buf; struct my_msgbuf buf; int msqid; int msqid; key_t key; key_t key; if ((key = ftok("kirk.c", 'B')) == -1) { if ((key = ftok("kirk.c", 'B')) == -1) { perror("ftok"); perror("ftok"); exit(1); exit(1); }

42 42GWU CS 259 Brad Taylor Spring 2004 Message Queue Sample Program (con’t) if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) { if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) { perror("msgget"); perror("msgget"); exit(1); exit(1); } printf("Enter lines of text, ^D to quit:\n"); printf("Enter lines of text, ^D to quit:\n"); buf.mtype = 1; /* we don't really care in this case */ buf.mtype = 1; /* we don't really care in this case */ while(gets(buf.mtext), !feof(stdin)) { while(gets(buf.mtext), !feof(stdin)) { if (msgsnd(msqid, (struct msgbuf *)&buf, sizeof(buf), 0) == -1) if (msgsnd(msqid, (struct msgbuf *)&buf, sizeof(buf), 0) == -1) perror("msgsnd"); perror("msgsnd"); } if (msgctl(msqid, IPC_RMID, NULL) == -1) { if (msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl"); perror("msgctl"); exit(1); exit(1); } return 0; return 0; }

43 43GWU CS 259 Brad Taylor Spring 2004 Message Queue Sample Program (con’t) spock.c #include #include struct my_msgbuf { struct my_msgbuf { long mtype; long mtype; char mtext[200]; char mtext[200]; }; }; int main(void) int main(void) { struct my_msgbuf buf; struct my_msgbuf buf; int msqid; int msqid; key_t key; key_t key; if ((key = ftok("kirk.c", 'B')) == -1) { /* same key as kirk.c */ if ((key = ftok("kirk.c", 'B')) == -1) { /* same key as kirk.c */ perror("ftok"); perror("ftok"); exit(1); exit(1); }

44 44GWU CS 259 Brad Taylor Spring 2004 Message Queue Sample Program (con’t) if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */ if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */ perror("msgget"); perror("msgget"); exit(1); exit(1); } printf("spock: ready to receive messages, captain.\n"); printf("spock: ready to receive messages, captain.\n"); for(;;) { /* Spock never quits! */ for(;;) { /* Spock never quits! */ if (msgrcv(msqid, (struct msgbuf *)&buf, sizeof(buf), 0, 0) == -1) { if (msgrcv(msqid, (struct msgbuf *)&buf, sizeof(buf), 0, 0) == -1) { perror("msgrcv"); perror("msgrcv"); exit(1); exit(1); } printf("spock: \"%s\"\n", buf.mtext); printf("spock: \"%s\"\n", buf.mtext); } return 0; return 0; }

45 45GWU CS 259 Brad Taylor Spring 2004 The Critical-Section Problem n processes all competing to use some shared data n processes all competing to use some shared data Each process has a code segment, called critical section, in which the shared data is accessedEach process has a code segment, called critical section, in which the shared data is accessed Problem – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical sectionProblem – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section

46 46GWU CS 259 Brad Taylor Spring 2004 Solution to Critical-Section Problem 1. Mutual Exclusion : If process P i is executing in its critical section, then no other processes can be executing in their critical sections{remember yellow submarine} 2. Progress : If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting : A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed Assume that each process executes at a nonzero speed No assumption concerning relative speed of the n processes No assumption concerning relative speed of the n processes

47 47GWU CS 259 Brad Taylor Spring 2004 Initial Attempts to Solve Problem Only 2 processes, P 0 and P 1 General structure of process P i (other process P j ) do { entry section critical section exit section reminder section } while (1) ; Processes may share some common variables to synchronize their actions

48 48GWU CS 259 Brad Taylor Spring 2004 Algorithm 1 Shared variables:Shared variables: – int turn ; initially turn = 0 – turn - i  P i can enter its critical section Process P iProcess P i do { while (turn != i) ; critical section turn = j ; reminder section } while (1) ; Satisfies mutual exclusion, but not progressSatisfies mutual exclusion, but not progress

49 49GWU CS 259 Brad Taylor Spring 2004 Algorithm 2 Shared variablesShared variables – boolean flag[2] ; initially flag [0] = flag [1] = false. – flag [i] = true  P i ready to enter its critical section Process P iProcess P i do { flag[i] := true; while (flag[j]) ; critical section flag [i] = false; remainder section } while (1); Satisfies mutual exclusion, but not progress requirementSatisfies mutual exclusion, but not progress requirement

50 50GWU CS 259 Brad Taylor Spring 2004 Algorithm 3 Combined shared variables of algorithms 1 and 2: Process P i do { flag [i]:= true; turn = j; while (flag [j] and turn = j) ; critical section flag [i] = false; remainder section } while (1); Meets all three requirements; solves the critical- section problem for two processes

51 51GWU CS 259 Brad Taylor Spring 2004 Bakery Algorithm Before entering its critical section, thread receives a number. Holder of the smallest number enters the critical section.Before entering its critical section, thread receives a number. Holder of the smallest number enters the critical section. If processes P i and P j receive the same number, if i < j, then P i is served first; else P j is served first.If processes P i and P j receive the same number, if i < j, then P i is served first; else P j is served first. The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5... Notation <  lexicographical order (ticket #, process id #)Notation <  lexicographical order (ticket #, process id #) –( a,b ) < c,d ) if a < c or if a = c and b < d –max ( a 0,…, a n -1 ) is a number, k, such that k  a i for i - 0, …, n – 1 Shared dataShared data boolean choosing[n]; int number[n]; Data structures are initialized to false and 0 respectively Data structures are initialized to false and 0 respectively Critical section for n processes

52 52GWU CS 259 Brad Taylor Spring 2004 Bakery Algorithm do { choosing[i] = true; number[i] = max(number[0], number[1], …, number [n – 1])+1; choosing[i] = false; for (j = 0; j < n; j++) { while (choosing[j]) ; while ((number[j] != 0) && (number[j,j] < number[i,i])) ; } critical section number[i] = 0; remainder section } while (1);

53 53GWU CS 259 Brad Taylor Spring 2004 Semaphores Synchronization tool not requiring busy waitingSynchronization tool not requiring busy waiting Semaphore S an integer variable, accessed only by two atomic operationsSemaphore S an integer variable, accessed only by two atomic operations wait (S): while S  0 do no-op; S--; signal (S): S++; Each Semaphore has an associated queue Each Semaphore has an associated queue Can define a non-blocking version of wait( S )Can define a non-blocking version of wait( S )

54 54GWU CS 259 Brad Taylor Spring 2004 Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P 0 P 1 wait ( S ); wait ( Q ); wait ( Q ); wait ( S );     signal ( S ); signal ( Q ); signal ( Q ) signal ( S ); Starvation – indefinite blocking: a process may never be removed from the semaphore queue in which it is suspended

55 55GWU CS 259 Brad Taylor Spring 2004 Producer-Consumer Synchronization Semaphore Sets Semaphore Sets Shared Memory Shared Memory Message Queues Message Queues Producer-Consumer Synchronization Producer-Consumer Synchronization Project Status Briefs (45% code review) Project Status Briefs (45% code review) Assignment 4: deferred to next week, finish project coding this week! Assignment 4: deferred to next week, finish project coding this week!

56 56GWU CS 259 Brad Taylor Spring 2004 Project Status Report Group I: Dan, Ricardo & Kamal / Wire Socket Group II: Clayton, Jason / Named Pipes Group III: Brooke, Nush, Ram / Shared Memory, Semaphores & Futexs Integration: Nate & Brad Issues to discuss: Code Review for Each Group Error checking Rough draft code submission for integration review Further Guidance, Questions Good discussion tonight; concepts & code converging! Remember to bring code next Monday (4/5/04)


Download ppt "1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 7: POSIX Interprocess Communication (IPC)"

Similar presentations


Ads by Google