Today’s topicsWhy other IPC mechanismsMessage queuesSlide 4PowerPoint PresentationSlide 6Shared MemoryCreating Shared MemoryAttach and Detach Shared MemoryDeleting Shared MemoryCommand-line IPC controlSemaphoresSlide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Realizing shell/make commandsFlorida State UniversityCOP5570 – Advanced Unix ProgrammingToday’s topics•System V Interprocess communication (IPC) mechanism–Message Queues–Semaphores–Shared Memory• Summary of processes related conceptsFlorida State UniversityCOP5570 – Advanced Unix ProgrammingWhy other IPC mechanisms• Pipes/sockets• FIFO semantics• Signals: sending flags• Sometimes, we want something beyond FIFO• FIFO with tags (message queue)• File semantics: the content is always there unless it is modified explicitly. (shared memory)•Once concurrency is allowed in shared data, we will need a way to protect (lock) the data. (semaphore)Florida State UniversityMessage queues•What are they?–Similar to the FIFO pipes, except that a tag (type) is matched when reading/writing.•Allowing cutting in line (I am only interested in a particular type of message)•Equivalent to merging of multiple FIFO pipes in one.COP5570 – Advanced Unix ProgrammingFlorida State UniversityMessage queues•Creating a message queue:–int msgget(key_t key, int msgflag);–Key can be any large number. But to avoiding using conflicting keys in different programs, use ftok() (the key master).•key_t ftok(const char *path, int id);–Path point to a file that the process can stat–Id: project ID, only the last 8 bits are usedCOP5570 – Advanced Unix ProgrammingFlorida State UniversityCOP5570 – Advanced Unix Programming•Message queue.–A linked list of messages stored within the kernel and identified by a message queue identifier.•Every message has a type field, and a nonnegative length, and the actual data bytes.•Msgsnd puts a message at the end of the queue•Msgrcv gets a message, may not follow FIFO order (can be based on type)•Has resource limits: MSGMAX, MSGMNB, etcFlorida State UniversityCOP5570 – Advanced Unix Programming•Message queue operationsInt msgget(key_t, int flag)Int msgctl(int msgid, int cmd, struct msgid_ds *buf)Int msgsnd(int msgid, const void *ptr, size nbytes, int flag);Int msgrcv(int msgid, void *ptr, size_t nbytes, long type, int flag);•Performance advantage is no longer there in newer systems (compared with pipe)Florida State UniversityCOP5570 – Advanced Unix ProgrammingShared MemoryCommon chunk of read/write memory among processesProc. 1Proc. 2ptrAttachProc. 3Proc. 4 Proc. 5ptrptrptrptrAttachCreateShared Memory(unique key)0MAXFlorida State UniversityCOP5570 – Advanced Unix ProgrammingCreating Shared Memoryint shmget(key_t key, size_t size, int shmflg);Example:key_t key; int shmid; key = ftok(“<somefile>", ‘A'); shmid = shmget(key, 1024, 0644 | IPC_CREAT); Here’s an example: shm_create.c.Florida State UniversityCOP5570 – Advanced Unix ProgrammingAttach and DetachShared Memoryvoid *shmat(int shmid, void *shmaddr, int shmflg);int shmdt(void *shmaddr);Example:key_t key; int shmid; char *data; key = ftok("<somefile>", ‘A'); shmid = shmget(key, 1024, 0644); data = shmat(shmid, (void *)0, 0);shmdt(data);Here’s an shm_attach.cFlorida State UniversityCOP5570 – Advanced Unix ProgrammingDeleting Shared Memoryint shmctl(int shmid, int cmd, struct shmid_ds *buf);shmctl(shmid, IPC_RMID, NULL); Example: Shm_delete.cFlorida State UniversityCOP5570 – Advanced Unix ProgrammingCommand-line IPC control•ipcs –Lists all IPC objects owned by the user•ipcrm–Removes specific IPC objectFlorida State UniversityCOP5570 – Advanced Unix ProgrammingSemaphores•Managing concurrent access to shared memory segment.•Using Semaphores–Creation: semget( … )–Incr/Decr/Test-and-set : semop(…)–Deletion: semctl(semid, 0, IPC_RMID, 0); Online tutorialhttp://www.ecst.csuchico.edu/~beej/guide/ipc/semaphores.htmlFlorida State University•Process environment–Command line arguments: argc, argv–environ and getenv()–getpid(), getuid(), getppid()–How does a program access the second (command line) argument?–How does a process access the variable you set in shell using commands such as “setenv TERM vt100”?–How does a process know its parent’s process id? How does a parent know its children’s process ids?Florida State University•Process management–fork, exit, wait, waitpid, execv–How can a parent process know whether its child has executed successfully?–How to determine whether execv runs a command successfully?Florida State University•File operations:–What are the related data structures for file operations?–open, close, read write, unlink, dup.–How to redirect the standard input/output/error?Florida State University•Inter-process communication:–Pipe–What kind of processes can communicate with pipes? –How to implement “ps | grep xyuan | more”?–Message queue–Shared memory–semaphoreFlorida State University•Inter-process communication:–Signal–What is the typical default action for a signal?–Blocking/unblocking a signal•sigset manipulation–sigfillset, sigemptyset, sigaddset, sigdelset, sigismember•the sigprocmask system call–Install signal handler (can ignore a signal or use default handler)•signal•sigaction–Sending signal: kill, alarmFlorida State University•Terminal I/O–canonical mode–noncanonical mode•tcgetattr, tcsetattr•termios data structureFlorida State University•Process group/session/control terminal–Related to job control •Who gets to access the keyboard? Who to send signal generated from the keyboard.–Foreground and background processes–Joining a group or creating a group: setpgid–Making a group foreground or background•tcgetpgrp/tcsetpgrpFlorida State UniversityRealizing shell/make commands•IO redirection•Pipes•Background execution•“arrow” functions•‘cd’ command–Why is this command different from other commands?•PATH environment variable.COP5570 – Advanced Unix
View Full Document