Operating Systems: Inter-Process Communication Pål Halvorsen 13 March 2016 INF1060: Introduction to Operating Systems and Data Communication.

Slides:



Advertisements
Similar presentations
Inter-Process Communication: Message Passing Tore Larsen Slides by T. Plagemann, Pål Halvorsen, Kai Li, and Andrew S. Tanenbaum.
Advertisements

IPC (Interprocess Communication)
Operating Systems Lecture 7.
Deadlocks, Message Passing Brief refresh from last week Tore Larsen Oct
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])
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS Lecture 17 Outline Named pipes Signals Lecture 17
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
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.
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
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.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows 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.
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.
Interprocess Communication. Process Concepts Last class.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Inter-Process Communication: Message Passing
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 3: Processes 170 UCSB T. Yang Some of slides are from the Chapter.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
1 CSI Module 2: Processes Reading: Chapter 3 (Silberchatz) Objective: To understand the nature of processes including the following concepts:  Process.
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.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 3: Processes.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
System calls for Process management
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
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.
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
File descriptor table File descriptor (integer)File name 0stdin 1stdout 2stderr Use open(), read(), write() system calls to access files Think what happens.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
UNIX signals & pipes. UNIX Signals A UNIX signal corresponds to an event –It is raised by one process (or hardware) to call another process’s attention.
Interprocess Communication Mechanisms. IPC Signals Pipes System V IPC.
Operating Systems Recitation 4, April th, 2002 Signals.
Chapter 3 – Processes (Pgs 101 – 141). Processes  Pretty much identical to a “job”  A task that a computer is performing  Consists of: 1. Text Section.
Interprocess Communication
KUKUM Real Time System Module #3 POSIX programming- Shm Lecture 4.
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. 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,
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
System calls for Process management Process creation, termination, waiting.
Distributed and Parallel Processing George Wells.
Signals What is a Signal?
Message queue Inter process communication primitive
Operating Systems: Inter-Process Communication
Threads and Cooperation
Applied Operating System Concepts
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.
Message Queues.
Processes in Unix and Windows
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Shared Memory Dr. Yingwu Zhu Feb, 2007.
System Programming: Process Management
Presentation transcript:

Operating Systems: Inter-Process Communication Pål Halvorsen 13 March 2016 INF1060: Introduction to Operating Systems and Data Communication

INF1060, Pål Halvorsen University of Oslo Big Picture communication? inter-process communication machine process Aprocess B shared memory message passing signals pipes

INF1060, Pål Halvorsen University of Oslo Message Passing  What is message-passing for? −communication across address spaces and protection domains −synchronization  Generic API − send( dest, &msg ) − recv( src, &msg )  What should the “ dest ” and “ src ” be? −pid −file: e.g., a pipe −port: network address, etc −no src: receive any message  What should “ msg ” be? −need both buffer and size for a variable sized message

INF1060, Pål Halvorsen University of Oslo Direct Communication  Must explicitly name the sender/receiver (“ dest ” and “ src ”) processes  Requires buffers… −… at the receiver more than one process may send messages to the receiver to receive from a specific sender, it requires searching through the whole buffer −… at each sender a sender may send messages to multiple receivers

INF1060, Pål Halvorsen University of Oslo Indirect Communication  “ dest ” and “ src ” are a shared (unique) queue  Use a shared queue to allow many-to-many communication  Where should the buffer be? −a buffer and its mutex and conditions should be at the mailbox

INF1060, Pål Halvorsen University of Oslo Mailboxes  Mailboxes are implemented as message queues sorting messages according to FIFO −messages are stored as a sequence of bytes −system V IPC messages also have a type: −get/create a message queue identifier: Qid = msgget(key, flags) −sending messages: msgsnd(Qid, *mymsg, size, flags) −receiving messages: msgrcv(Qid, *mymsg, size, type, flags) −control a shared segment: msgctl( … ) struct mymsg { long mtype; char mtext[..]; }

INF1060, Pål Halvorsen University of Oslo OS-kernel  Example: A B C D... Mailboxes msgsnd(A,,...) msgrcv(B,,...)

INF1060, Pål Halvorsen University of Oslo Mailboxes Example – command line send #include … /* More includes in the real example files */ #define MSGLEN 100 struct text_message { long mtype; char mtext[MSGLEN]; }; int main(int argc, char *argv[]) { int msqID, len; struct text_message mesg; if (argc != 4) { printf("Usage: msgsnd \n"); exit(1); } len = strlen(argv[3]); if (len > MSGLEN-1) { printf("String too long\n"); exit(1); } /* get the message queue, which may need to be created */ msqID = msgget((key_t) atoi(argv[1]), IPC_CREAT | 0666); if (msqID == -1) { perror("msgget"); exit(1); } /* build message */ mesg.mtype = atoi(argv[2]); strcpy(mesg.mtext, argv[3]); /* place message on the queue */ if (msgsnd(msqID, (struct msgbuf *) &mesg, len+1, 0) == -1) { perror("msgsnd"); exit(1); } >./msgsnd “What’s up” >./msgsnd “Nothing” >./msgsnd “Going home” >./msgsnd “Hungry?” >./msgsnd “Going to sleep” What’s up 2 Nothing 1 Going home 9 Hungry 1 Going to bed

INF1060, Pål Halvorsen University of Oslo Mailboxes Example – command line rcv #include … /* More includes in the real example files */ #define MSGLEN 100 struct text_message { long mtype; char mtext[MSGLEN]; }; int main(int argc, char *argv[]) { int msqID; struct text_message mesg; if (argc != 3) { printf("Usage: msgrcv \n"); exit(1); } /* get the existing message queue */ msqID = msgget((key_t)atoi(argv[1]), 0); if (msqID == -1) { perror("msgget"); exit(1); } /* read message of the specified type; do not block */ if (msgrcv(msqID, (struct msgbuf *) &mesg, MSGLEN, atoi(argv[2]), IPC_NOWAIT) == -1) { if (errno == ENOMSG) printf("No suitable message\n"); else printf("msgrcv() error\n"); } else printf("[%ld] %s\n", mesg.mtype, mesg.mtext); } >./msgrcv [1] What’s up >./msgrcv [9] Hungry >./msgrcv [2] Nothing What’s up 2 Nothing 1 Going home 9 Hungry 1 Going to bed

INF1060, Pål Halvorsen University of Oslo Mailboxes Example – command line ctl #include … /* More includes in the real example files */ int main(int argc, char *argv[]) { key_t mkey; int msqID; struct msqid_ds mstatus; if (argc != 2) { printf("Usage: show_Q_stat \n"); exit(1); } /* access existing queue */ mkey = (key_t) atoi(argv[1]); if ((msqID = msgget(mkey, 0)) == -1){ perror("msgget"); exit(2); } /* get status information */ if (msgctl(msqID, IPC_STAT, &mstatus) == -1) { perror("msgctl"); exit(3); } /* print status info */ printf("\nKey %ld, queue ID %d, ", (long int) mkey, msqID); printf("%d msgs on queue\n\n", mstatus.msg_qnum); printf("Last send by pid %d at %s\n", mstatus.msg_lspid, ctime(&(mstatus.msg_stime))); printf("Last rcv by pid %d at %s\n", mstatus.msg_lrpid, ctime(&(mstatus.msg_rtime))); } >./show_Q_stat 100 Key 100, queue ID 0, 2 msgs on queue Last send by pid at Mon Oct 6 10:37: Last rcv by pid at Mon Oct 6 10:39: Going home 1 Going to bed

INF1060, Pål Halvorsen University of Oslo  Classic IPC method under UNIX: > ls -l | more −shell runs two processes ls and more which are linked via a pipe −the first process ( ls ) writes data (e.g., using write ) to the pipe and the second ( more ) reads data (e.g., using read ) from the pipe  the system call pipe( fd[2] ) creates one file descriptor for reading ( fd[0] ) and one for writing ( fd[1] ) - allocates a temporary inode and a memory page to hold data (Named) Pipes ls more struct pipe_inode_info { wait_queue_head_t wait; char *base; unsigned int len; unsigned int start; unsigned int readers, writers; unsigned int waiting_readers, waiting_writers; unsigned int r_counter, w_counter; } 1 52

INF1060, Pål Halvorsen University of Oslo Pipe Example – fork,child writing to parent #include char *msg = "hello"; main() { char inbuf[MSGSIZE]; int p[2]; pid_t pid; /* open pipe */ if (pipe(p) == -1) { perror("pipe call error"); exit(1); } switch( pid = fork() ) { case -1: perror("error: fork call"); exit(2); case 0: close(p[0]); /* CHILD: close the read end of the pipe */ write(p[1], msg, MSGSIZE); printf(“Child: %s\n", msg); break; default: close(p[1]); /* PARENT: close the write end of the pipe */ read(p[0], inbuf, MSGSIZE); printf("Parent: %s\n", inbuf); wait(0); } exit(0); }

INF1060, Pål Halvorsen University of Oslo Mailboxes vs. Pipes  Are there any differences between a mailbox and a pipe? −Message types mailboxes may have messages of different types pipes do not have different types −Buffer pipes – one or more pages storing messages contiguously mailboxes – linked list of messages of different types −More than two processes a pipe often (not in Linux) implies one sender and one receiver many can use a mailbox

INF1060, Pål Halvorsen University of Oslo Shared Memory  Shared memory is an efficient and fast way for processes to communicate −multiple processes can attach a segment of physical memory to their virtual address space −create a shared segment: shmid = shmget(key, size, flags) −attach a shared segment: shmat(shmid, *addr, flags) −detach a shared segment: shmdt(*shmaddr) −control a shared segment: shmctl( … ) −if more than one process can access segment, an outside protocol or mechanism (like semaphores) should enforce consistency/avoid collisions

INF1060, Pål Halvorsen University of Oslo Shared Memory Example – read/write alphabet #include #define SHMSZ 27 main() { int shmid; key_t key; char c, *shm, *s; key = 5678; /* selected key */ /* Create the segment.*/ if ((shmid = shmget(key,SHMSZ,IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } /* Now we attach the segment to our data space.*/ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* put some things into the memory */ for (s = shm, c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; /* wait until first character is changed to '*' */ while (*shm != '*') sleep(1); exit(0); } #include #define SHMSZ 27 main() { int shmid; key_t key; char *shm, *s; key = 5678; /* selected key by server */ /* Locate the segment. */ if ((shmid = shmget(key,SHMSZ,0666)) < 0) { perror("shmget"); exit(1); } /* Now we attach the segment to our data space. */ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* read what the server put in the memory. */ for (s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); /* change the first character in segment to '*' */ *shm = '*'; exit(0); }

INF1060, Pål Halvorsen University of Oslo Signals  Signals are software generated interrupts sent to a process −hardware conditions −software conditions −input/output notification −process control −resource control  Sending signals − kill( pid, signal ) – system call to send any signal to pid − raise( signal ) – call to send signal to current process kill (getpid(), signal) pthread_kill (pthread_self(), signal)  Signal handling −a signal handler can be invoked when a specific signal is received −a process can deal with a signal in one of the following ways: default action block the signal (some signals cannot be ignored)  to ignore a signal (not KILL/STOP), use signal( sig_nr, SIG_IGN ) catch the signal with a handler: signal( sig_nr, void (*func)())  write a function yourself - void func() {}

INF1060, Pål Halvorsen University of Oslo Signal Example – disable Ctrl-C #include void sigproc() { signal(SIGINT, sigproc); /* NOTE some versions of UNIX will reset * signal to default after each call. So for * portability reset signal each time */ printf(“you have pressed ctrl-c - disabled \n”); } void quitproc() { printf(“ctrl-\\ pressed to quit\n”); /* this is “ctrl” & “\” */ exit(0); /* normal exit status */ } main() { signal(SIGINT, sigproc); /* ctrl-c : DEFAULT ACTION: term */ signal(SIGQUIT, quitproc); /* ctrl-\ : DEFAULT ACTION: term */ printf(“ctrl-c disabled use ctrl-\\ to quit\n”); for(;;); }

INF1060, Pål Halvorsen University of Oslo Signal Example – parent terminating child void sighup() { signal(SIGHUP,sighup); /* reset signal */ printf("CHILD: I received a SIGHUP\n"); } void sigint() { signal(SIGINT,sigint); /* reset signal */ printf("CHILD: I received a SIGINT\n"); } void sigquit() { printf("My DADDY has Killed me!!!\n"); exit(0); } #include void sighup(); void sigint(); void sigquit(); main() { int pid; /* get child process */ if ((pid=fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { /* child */ signal(SIGHUP, sighup); signal(SIGINT, sigint); signal(SIGQUIT, sigquit); for(;;); } else { /* parent */ printf("\nPARENT: sending SIGHUP\n\n"); kill(pid,SIGHUP); sleep(3); printf("\nPARENT: sending SIGINT\n\n"); kill(pid,SIGINT); sleep(3); printf("\nPARENT: sending SIGQUIT\n\n"); kill(pid,SIGQUIT); sleep(3); }

INF1060, Pål Halvorsen University of Oslo Summary  Many ways to perform send messages or perform IPC on a machine −mailboxes - FIFO, messages has types −pipes – FIFO, no type −shared memory – shared memory mapped into virtual space −signals – send a signal which can invoke a special handler networks  Next, communication between processes on different machines using networks