2 UNIX interprocess communications

Size: px
Start display at page:

Download "2 UNIX interprocess communications"

Transcription

1 Parallel Programming Slide UNIX interprocess communications exchange of information between cooperating processes synchronization of cooperating processes communication mechanisms shared memory pipes (named / unnamed pipes, fifo files) messages (message queues, message passing interface library) mailbox systems sockets remote procedure call etc. synchronization mechanisms file locks (read-lock, write-lock) semaphores monitors etc.

2 Parallel Programming Slide Process creation and termination program: collection of statements and data generally stored on a hard disk marked as "executable" in the i-node the contents of the file fulfills the requirements of the operating system for executables (a.out format, elf format, etc.) executing a program 1. the kernel creates a process, i.e. it creates an environment which can be used to run the program 2. each process consists of three areas instruction segment user data segment system data segment 3. the program will be used to initialize the instruction and user data segment

3 Parallel Programming Slide when the initialization is finished there is no further connection between process and program 5. a process can request more resources (memory, files, etc.) several parallel processes can be initialized with the same program the operating system can save memory if such processes share the same instruction segment (the processes wouldn't discover the sharing because the instruction segment is read-only) among others the system data segment contains the following information current directory file descriptor table accumulated CPU time process and group ID of the parent process a process isn't allowed to manipulate its system data segment directly several system functions are needed for these tasks

4 Parallel Programming Slide 2-4 the operating system creates a process if this is requested by another process the requesting process is called parent process the new created process is called child process the child process inherits most of the environment of its parent process (e.g., open files, shared memory segments, etc.) each process identifies itself with a unique process ID (PID) each process (with the exception of the init process) has a parent (identified by a parent process ID, PPID) the init process is root of the process hierarchy (if a parent process terminates before its child processes terminate, init becomes parent of the orphan processes, i.e. the parent process ID of such processes is 1) each process belongs to a process group (identified by a process group ID, PGID) (e.g., someone can put all processes of a database management system into one group)

5 Parallel Programming Slide 2-5 each process group has a process group leader (a process is process group leader if its process ID and process group ID are identical) every process of a process group has a process group ID which is identical with the process ID of the process group leader some system calls have effects on all members of a process group (e.g., all processes of a process group can be killed in this way) each process can change its process group ID to become process group leader itself (e.g., you can create 20 processes in four different process groups) generally the output of a process appears on the screen which has been used to start the process group leader (i.e., the output of all members of a process group will interfere with each other) the device driver of the display sends all interrupt (<Ctrl-c>), quit (<Ctrl-\>), and hangup signals (<Ctrl-Break>) to all processes of a process group (possibly all processes will be terminated in this way if they haven't taken precautions (e.g., ignore hangups) and haven't changed their process group ID)

6 Parallel Programming Slide 2-6 creating a new process with fork ( ) the child process is mainly a copy of the parent process (look at the manual page if you want to know how the environment for the child process differs from that one of the parent) fork ( ) returns the process ID of the child process to the parent process fork ( ) returns 0 to the child process fork ( ) returns -1 to the parent process and no child process is created if an error has occurred (e.g., the limit on the total number of processes under execution by a single user would be exceeded) normally the child process overloads itself with another program using one of the exec ( ) functions the parent process waits until the child process terminates (wait () function) or performs other work fork ( ) is a very expensive system function because possibly large data segments must be copied

7 Parallel Programming Slide 2-7 waiting for the termination of a child process with wait ( ) wait ( ) returns the process ID of the terminated child process wait ( ) can store the exit-value of the child process if it is called with a pointer parameter if a child process terminates before its parent process has called wait ( ) it becomes a zombie process the instruction, user data, and system data segments are released the child process still occupies an entry in the process table the entry in the process table will be cleared if the parent process calls wait ( ) the zombie has left the system (rebooting a system always clears all zombies) terminating a process with exit ( ) terminate a process and return an exit-value to the parent process if the process has still active child processes the childs will not be terminated but they get a new parent process: the init process

8 Parallel Programming Slide 2-8 the following concurrent courses are possible 1) the parent process waits for its child process before the child process terminates (fork1.c, fork2.c) parent fork waitpid wait blocked child exit 2) the child process terminates before its parent process waits for its termination (fork3.c) parent fork waitpid wait child exit zombie 3) the parent process doesn't wait for its child process and terminates before the child process terminates (fork4.c) parent fork return in main exit child "init" becomes parent exit

9 Parallel Programming Slide 2-9 4) the parent process doesn't wait for its child process and terminates later than its child process (fork5.c) parent fork return in main exit child exit zombie the child process doesn't permanently remain a zombie process

10 Parallel Programming Slide 2-10 example to demonstrate the general structure of process creation and termination (fork1.c) #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <errno.h> int eval_wait_stat (int stat); int main (void) pid_t parent_pid, child_pid, /* process ID's */ fork_pid, wait_pid, parent_grp, child_grp; /* process groups */ int child_stat; /* return status of child */ parent_pid = getpid (); parent_grp = getpgrp (); printf ("\nparent process: process ID: %ld group ID: %ld\n", (long) parent_pid, (long) parent_grp); fork_pid = fork (); switch (fork_pid) case -1: /* error: no process created */ perror ("fork failed"); exit (EXIT_FAILURE); break; case 0: /* child process */ child_pid = getpid (); child_grp = getpgrp (); printf ("Child process: process ID: %ld group ID: %ld " "parent process ID: %ld\n", (long) child_pid, (long) child_grp, (long) getppid ()); printf ("Child process: terminate with \"exit\"-value: %d\n", EXIT_SUCCESS); exit (EXIT_SUCCESS); break; default: /* parent process */ printf ("Parent process: child process with ID %ld created.\n", (long) fork_pid); wait_pid = wait (&child_stat); if (wait_pid == -1) perror ("wait"); exit (EXIT_FAILURE); else printf ("Parent process: child process %ld has terminated.\n", (long) wait_pid); eval_wait_stat (child_stat); return EXIT_SUCCESS;

11 Parallel Programming Slide 2-11 int eval_wait_stat (int stat) int return_val; if (WIFEXITED (stat)!= 0) printf ("\t\tchild has terminated with status %d.\n", WEXITSTATUS (stat)); return_val = 0; if (WIFSIGNALED (stat)!= 0) printf ("\t\tchild has been terminated using signal %d.\n", WTERMSIG (stat)); return_val = 0; if (WIFSTOPPED (stat)!= 0) printf ("\t\tchild has been stopped using signal %d.\n", WSTOPSIG (stat)); return_val = 1; #if defined(sunos) &&!defined(_posix_c_source) if (WIFCONTINUED (stat)!= 0) printf ("\t\tchild has continued.\n"); return_val = 1; #endif return return_val; Exercise 2-1) Implement a program which creates three concurrent processes. Each child process terminates itself after displaying its PID, GID, and PPID and sleeping a short time. The parent process should wait for its child processes. 2-2) Implement a program which creates a process which creates a process and so on. Create a hierarchy of three processes. Each process should display its PID, GID, and PPID. Each parent should wait for its child process. 2-3) Modify excercise 2-1 in a way that each child process creates its own process group. Show the differences in solution 2-1 and 2-3 if you type <Ctrl-c>.

12 Parallel Programming Slide 2-12 overloading a process with an exec ( ) function the child process overloads itself with a new program process 1 fork process1 process 2 exec process 2* 1) create a new process (mainly a copy of the parent process) 2) when the child process overloads itself with a new program it has its individual instruction and data segments exec ( ) functions differ in the following areas: searching for the program absolute pathname searching all directories specified in the environment variable PATH

13 Parallel Programming Slide 2-13 passing the environment for the new program inheritance (automatically) explicitly passing a new one (manually) passing the "command line" arguments explicit list appearing in the exec ( ) call as a vector (array like argv) system call passing passing the searching for arguments environment the program execl list automatically absolute path execv vector automatically absolute path execle list manually absolute path execve vector manually absolute path execlp list automatically PATH execvp vector automatically PATH l v e p explicit list of command line arguments vector of command line arguments (necessary if the number of arguments is unknown at compile time) explicitly passing a new environment searching for the program using environment variable PATH

14 Parallel Programming Slide 2-14 most often the functions execlp ( ) and execvp ( ) are used it's not necessary to fork a program before using an exec ( ) function (Many large programs execute in sequential steps (e.g., a compiler). If one step has finished the process can overload itself with the program for the next step. Generally the steps must be independent of each other or they have to use temporary files to communicate with each other because the user data segment will be re-initialized with the new program.) the first argument (arg0) of an explicit list or vector of command line arguments is always the program name the last argument of an explicit list or vector of command line arguments is always NULL

15 Parallel Programming Slide 2-15 example to demonstrate the use of an exec ( ) function (forkexec.c)... int main (void) pid_t fork_pid, wait_pid; /* process ID's */ int child_stat, /* return status of child */ ret_val; /* return value of a function */ fork_pid = fork (); switch (fork_pid) case -1: /* error: no process created */ perror ("fork failed"); exit (EXIT_FAILURE); break; case 0: /* child process */ ret_val = execl ("/bin/ls", "ls", "-al", NULL); if (ret_val == -1) perror ("execl failed"); exit (EXIT_FAILURE); break; default: /* parent process */ printf ("Parent process: child process with ID %ld created.\n", (long) fork_pid); wait_pid = wait (&child_stat); if (wait_pid == -1) perror ("wait"); exit (EXIT_FAILURE); else printf ("Parent process: child process %ld has terminated.\n", (long) wait_pid); eval_wait_stat (child_stat); return EXIT_SUCCESS;... Exercise 2-4: Implement a very small shell. The program should allow the input of a new program name and its arguments. When the input is finished, it will run the program with an exec ( ) function where the program will be searched using the environment variable PATH. Afterwards your program should "ask" for new input.

16 Parallel Programming Slide Shared Memory efficient data exchange between parallel processes (normally the fastest possibility to exchange data between processes) processes must take care of the data consistency (e.g., access synchronization via semaphores) implemented in main memory different interfaces are available memory-mapped files (mmap) System V IPC shared memory POSIX IPC shared memory overview of application programming interfaces System V IPC UNIX mmap POSIX IPC create memory area in operating system table attach memory to virtual address space detach memory from virtual address space remove memory area from operating system table shmget () open () lseek () write () shm_open () lseek () write () shmat () mmap () mmap () shmdt () munmap () munmap () shmctl () close () unlink () close () shm_unlink () (you can create a new file with a specified size if you change the position of the file pointer with lseek () to the position for the required file size and use write () to write an arbitrary character at the current position of the file pointer.)

17 Parallel Programming Slide 2-17 shared memory using memory-mapped files overview virtual memory of process 1 file view 1 file view 2 virtual memory of process 2 physical memory file mapping object file on disk file view 2 basic operations obtain a file descriptor by opening a regular disk file call mmap ( ) with the file descriptor and further parameters to map the file to virtual memory examine or modify the contents of the file via normal memory references you can map different parts of the file to different locations in the virtual memory if you use several mmap-calls

18 Parallel Programming Slide 2-18 direct file access vs. mmap pseudocode for direct file access fd = open (...); lseek (fd, offset, SEEK_SET); read (fd, buf, len); use/modify data in buf pseudocode for file access via mmap ( ) struct xyz... *buf; fd = open (...); buf = (struct xyz *) mmap (..., len,..., fd, offset); use/modify data in structure buf mmap is better integrated into the concepts of UNIX than System V IPC because it uses files uses the same naming schemes as files use (i.e. pathnames) because the shared objects are files uses normal system calls to create or delete the shared objects (open () / shm_open (), unlink () / shm_unlink ()) uses normal system calls to change ownership or permissions (chown (), chmod ()) shared objects are nonvolatile, i.e. they survive a power cut or reboot of the computer (provided that the memory has been flushed to disk) slower than System V IPC because it depends on disk accesses

19 Parallel Programming Slide 2-19 POSIX IPC similar to mmap ( ), i.e., based on files in the file system supports two functions: shm_open ( ) and shm_unlink ( ) (they only provide an API to support the POSIX IPC name abstraction, i.e., they are mainly replacements for open ( ) and unlink ( ) the real mapping must still be done with mmap ( ))

20 Parallel Programming Slide 2-20 shared memory using System V IPC completely implemented in main memory allows efficient data transfers between concurrent processes requires a key value to fetch the proper identifier of the desired IPC resource several processes can attach a common memory area in main memory to their own data segments at the same time basic operations 1) create / open a shared memory segment (shmget) 2) attach the shared memory to the data segment (shmat) 3) communicate via shared memory with other processes 4) detach the shared memory (shmdt) 5) close / remove the shared memory segment (shmctl) shared memory accesses can be controlled (shmctl) (permissions, locking / unlocking,...) new shared memory segments don't occupy main memory (they only occupy an entry in the shared memory table of the operating system) main memory will be occupied when the first process attaches the shared memory segment to its own data segment accesses to shared memory must be synchronized (generally with semaphores)

21 Parallel Programming Slide 2-21 controlling shared memory accesses with shmctl ( ) int shmctl (int shmid, int cmd, struct shmid_ds *buf) shmid cmd handle for the shared memory segment (return value from shmget ( )) command to execute IPC_SET set permissions IPC_RMID remove shared memory entry etc. have a look at the manual page buf pointer to a data structure if you want to read / set members of the data structure associated with shmid operating system dependent data structure struct shmid_ds struct ipc_perm shm_perm; /* operation permission struct */ int shm_segsz; /* size of segment in bytes */ struct anon_map *shm_amp; /* segment anon_map pointer */ ushort shm_lkcnt; /* # of times it is being locked */ pid_t shm_lpid; /* pid of last shmop */ pid_t shm_cpid; /* pid of creator */ ulong shm_nattch; /* used only for shminfo */ ulong shm_cnattch; /* used only for shminfo */ time_t shm_atime; /* last shmat time */ long shm_pad1; /* reserved for time_t expansion */ time_t shm_dtime; /* last shmdt time */ long shm_pad2; /* reserved for time_t expansion */ time_t shm_ctime; /* last change time */ long shm_pad3; /* reserved for time_t expansion */ long shm_pad4[4]; /* reserve area */ ; each shared memory segment must be explicitly removed from the shared memory table (IPC_RMID)

22 Parallel Programming Slide 2-22 get a shared memory identifier with shmget ( ) create / open a shared memory segment and return a handle int shmget (key_t key, int size, int shmflg) key user defined arbitrary unique positive numerical key or IPC_PRIVATE (if you use IPC_PRIVATE other programs don't know the key so that they can't use the shared memory segment; a user defined key can be defined as symbolic constant, command line option,...) size size of the shared memory segment shmflg access permissions and flags (a shared memory segment is without any value if you don't set proper access permissions; the default is that not even the owner has any access permissions) flags IPC_CREAT IPC_EXCL create a new shared memory segment if it doesn't already exist create a new shared memory segment exclusively, i.e. return an error if a shmid already exists for key

23 Parallel Programming Slide 2-23 shared memory operations attach a shared memory segment to the data segment of the calling process void *shmat (int shmid, void *shmaddr, int shmflg) shmid identifier for the shared memory segment shmaddr (void *) 0 if the system is allowed to choose an address (have a look at the manual page) shmflg generally access permissions (have a look at the manual page) detach a shared memory segment int shmdt (void *shmaddr) shmaddr address of the shared memory a shared memory segment is available until you remove it you can check with ipcs -m which segments are available if necessary you must remove all shared memory segments manually which you have created before you logout (use "ipcrm -m <shmid>" or "ipcrm shm <shmid>" (Linux))

24 Parallel Programming Slide 2-24 example: shared memory access without synchronization One process displays a character n times on a display. Another one can change the character which should be displayed and the number of repetitions. If a context switch occurs between reading the character and the repetition counter, the last character will be displayed with a new repetition. common header file for both programs (shm_definitions.h) /* Common data structures and macros. This example is based on a * program in "Brown, Ch.: UNIX - Distributed Programming. Prentice * Hall, 1994." * * * File: shm_definitions.h Author: S. Gross * Date: * */ struct shared_seg char c; /* character to print */ int frq; /* frequency of character */ ; #define SEGSIZE (sizeof (struct shared_seg)) #define SHM_KEY ((key_t) 0x5678) /* arbitrary value */ #define SHM_PERM (0600) /* access permissions */

25 Parallel Programming Slide 2-25 program to display the character (shm_output_2.c) /* The program reads every five seconds a "char" and "frequency" * from a shared memory segment and displays the "char" "frequency" * times on a line. The contents of the shared memory segment can * be modified with the program "shm_change_contents". * * The shared memory segment will be removed from the operating * system table when this program terminates. * * Please perform the following actions: * * 1) Start this program with "shm_output_2" in one window. * * 2) Change the contents of the shared memory segment in another * window with the commands: * * shm_change_contents d 60 * shm_change_contents m 20 * shm_change_contents t 70 * etc. * * The old character will be displayed with the new frequency * if you change the contents between "reading the character" * and "reading the frequency". You get an inconsistent data * item because the access to the shared memory area isn't * synchronized. You will always display a character with the * correct frequency if you protect the modification of the * shared memory with semaphores. * * 5) Stop "shm_output_2" with "shm_change_contents q 0". * * 6) Check with "ipcs -m" that the shared memory segment has * been removed from the operating system table. * * The source code depends on the X/Open version.... /* starting with Linux 2.2.x the macro "_SVID_SOURCE" must be defined * for "System V IPC" */ #ifdef Linux #define _SVID_SOURCE #endif #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <unistd.h> #include <errno.h> #include "shm_definitions.h" /* definition of shm segment */ #define SLEEP_TIME 2 /* 2 seconds */ #define ESHMGET 1 /* error calling "shmget()" */ #define ESHMAT 2 /* error calling "shmat()" */ #define ESHMDT 3 /* error calling "shmdt()" */ #define ESHMCTL 4 /* error calling "shmctl()" */

26 Parallel Programming Slide 2-26 int main (int argc, char *argv[]) struct shared_seg *shm_adr; /* address of shm segment */ struct shmid_ds shmbuf; /* for shmctl */ int shm_id, /* ID of shm segment */ shm_frq, /* temporary value */ i; /* loop variable */ char shm_c; /* temporary value */ shm_id = shmget (SHM_KEY, SEGSIZE, IPC_CREAT SHM_PERM); if (shm_id < 0) fprintf (stderr, "%s: shmget failed: %s\n", argv[0], strerror (errno)); exit (ESHMGET); /* let the system choose an appropriate address */ shm_adr = (struct shared_seg *) shmat (shm_id, NULL, SHM_PERM); if (shm_adr < (struct shared_seg *) 0) fprintf (stderr, "%s: shmat failed: %s\n", argv[0], strerror (errno)); exit (ESHMAT); /* initialize the shared memory segment */ shm_adr->c = 'x'; shm_adr->frq = 70; /* display a new line every "2 * SLEEP_TIME" seconds */ while ((shm_adr->c!= 'q') && (shm_adr->frq > 0)) shm_c = shm_adr->c; printf ("Got the character. I'll read the frequency in %d s\n", SLEEP_TIME); sleep (SLEEP_TIME); /* allow shm inconsistency */ shm_frq = shm_adr->frq; for (i = 0; i < shm_frq; ++i) putchar (shm_c); putchar ('\n'); sleep (SLEEP_TIME); /* release the shared memory segment */ #ifdef XPG3... #else if (shmdt (shm_adr) < 0) fprintf (stderr, "%s: shmdt failed: %s\n", argv[0], strerror (errno)); exit (ESHMDT); #endif if (shmctl (shm_id, IPC_RMID, &shmbuf) < 0) fprintf (stderr, "%s: shmctl failed: %s\n", argv[0], strerror (errno)); exit (ESHMCTL); return EXIT_SUCCESS;

27 Parallel Programming Slide 2-27 program to change the values (shm_change_contents.c)... int main (int argc, char *argv[]) struct shared_seg *shm_adr; /* address of shm segment */ int shm_id; /* ID of shm segment */ if (argc!= 3) fprintf (stderr, "\nthis program changes the \"char\" and " "\"frequency\" in a shared memory segment.\n" "The \"char\" will be displayed \"frequency\" times " "on a line.\n\n"); fprintf (stderr, "Usage: %s <char> <frequency>\n\n", argv[0]); exit (EXIT_FAILURE); shm_id = shmget (SHM_KEY, SEGSIZE, IPC_CREAT SHM_PERM); if (shm_id < 0) fprintf (stderr, "%s: shmget failed: %s\n", argv[0], strerror (errno)); exit (ESHMGET); /* let the system choose an appropriate address */ shm_adr = (struct shared_seg *) shmat (shm_id, NULL, SHM_PERM); if (shm_adr < (struct shared_seg *) 0) fprintf (stderr, "%s: shmat failed: %s\n", argv[0], strerror (errno)); exit (ESHMAT); /* change the contents of the shared memory segment */ shm_adr->c = argv[1][0]; shm_adr->frq = atoi (argv[2]); /* detach the shared memory segment */ #ifdef XPG3 if (shmdt ((char *) shm_adr) < 0) fprintf (stderr, "%s: shmdt failed: %s\n", argv[0], strerror (errno)); exit (ESHMDT); #else if (shmdt (shm_adr) < 0) fprintf (stderr, "%s: shmdt failed: %s\n", argv[0], strerror (errno)); exit (ESHMDT); #endif return EXIT_SUCCESS; every character will always be displayed with the correct repetition if all accesses to the shared memory are synchronized with semaphores

28 Parallel Programming Slide Semaphores different interfaces are available System V IPC semaphores completely implemented in main memory require a key value to fetch the proper identifier of the desired IPC resource POSIX IPC semaphores unnamed semaphores completely implemented in main memory correspond to Dijkstra's semaphores #define SEM_INITVAL 1... sem_t s /* semaphore */... sem_init (&s, TRUE, SEM_INITVAL);... sem_wait (&s); /* one-to-one correspondence to P(s) */... sem_post (&s); /* one-to-one correspondence to V(s) */... named semaphores: based on files in the file system

29 Parallel Programming Slide 2-29 overview of application programming interfaces POSIX named semaphores POSIX unnamed semaphores System V semaphores create semaphore sem_open () sem_init () semget () initialize at creation time at creation time semctl () P ( ) sem_wait () sem_timedwait () sem_trywait () sem_wait () sem_timedwait () sem_trywait () semop () V ( ) sem_post () sem_post () semop () read value sem_getvalue () sem_getvalue () semctl () remove semaphore sem_close () sem_unlink () sem_destroy () semctl ()

30 Parallel Programming Slide 2-30 System V semaphores are very powerful the semaphore value can be incremented / decremented with arbitrary values a decrement operation takes place if the semaphore value is at least as high as the subtrahend the process blocks until the subtraction can be performed it's allowed to perform operations on several semaphores within one function call the operation is indivisible, i.e., for decrement operations the semaphore values of all semaphores must be at least as high as the subtrahends the process blocks until all operations can take place at the same time it's allowed to perform some operations on semaphore values and additionally wait for other semaphore values to become zero the operation is indivisible, i.e., for decrement operations the semaphore values of all semaphores must be at least as high as the subtrahends the process blocks until all operations can be performed at the same time and the requested semaphore values are zero a process can request that it won't be blocked if the semaphore operation can't be performed immediately

31 Parallel Programming Slide 2-31 a process can request that the system keeps track of all semaphore operations and undoes all operations if the process exits representation of semaphores each semaphore is represented by the following data structure struct sem ushort semval; /* current value, nonnegative */ pid_t sempid; /* PID of last operation */ /* perhaps more operating system dependent variables */ ; semaphores are combined in semaphore sets (even for a single semaphore a semaphore set must be created / initialized) a semaphore set is represented by the following data structure (operating system dependent) struct semid_ds struct ipc_perm sem_perm; /* operation permission struct */ struct sem *sem_base; /* ptr to first semaphore in set */ ushort sem_nsems; /* # of semaphores in set */ time_t sem_otime; /* last semop time */ long sem_pad1; /* reserved for time_t expansion */ time_t sem_ctime; /* last change time */ long sem_pad2; /* time_t expansion */ long sem_pad3[4]; /* reserve area */ ; such a structure will be identified with a semaphore ID (semid)

32 Parallel Programming Slide 2-32 entire data structure for semaphores pool of semaphore sets (struct semid_ds *) semaphore set (struct semid_ds) array of semaphores (struct sem) sem_perm sem_base... sem_perm sem_base... (struct semid_ds *) NULL (struct semid_ds *) NULL semval sempid semncnt semzcnt semval sempid semncnt semzcnt semval sempid semncnt semzcnt semval sempid semncnt semzcnt semaphore 0 semaphore n semaphore 0 semaphore k semid will be used as index into the array of semaphore sets very memory efficient as long as there are very few or no semaphores in use basic operations 1) create / initialize a semaphore set with semaphores (semget, semctl) 2) use the semaphore(s) (semop, semctl) 3) remove the semaphore set (semctl)

33 Parallel Programming Slide 2-33 controlling semaphores with semctl ( ) read / modify semaphore values modify access permissions remove a semaphore set etc. (have a look at the manual page for more details) int semctl (int semid, int semnum, int cmd, union semun arg) semid handle for semphore set (return value from semget ( )) semnum number of semaphore within the set cmd command to execute GETVAL return semaphore value SETVAL set semaphore value GETALL return all semaphore values SETALL set all semaphore values IPC_SET set permissions IPC_RMID remove semaphore set etc. have a look at the manual page arg depending on cmd union semun int val; /* e.g., for SETVAL */ struct semid_ds *buf; /* e.g., for IPC_SET */ ushort *array; /* e.g., for SETALL */ ; return value depends on cmd (have a look at the manual page)

34 Parallel Programming Slide 2-34 the data structures for semaphores must be explicitly removed (IPC_RMID) a semaphore set is available until you remove it you can check with ipcs -s which semaphore sets are available if necessary you must remove all semaphore sets manually which you have created before you logout (use "ipcrm -s <semid>" or "ipcrm sem <semid>" (Linux)) get an identifier for a semaphore set with semget ( ) int semget (key_t key, int nsems, int semflg) key user defined arbitrary unique positive numerical key or IPC_PRIVATE (if you use IPC_PRIVATE other programs don't know the key so that they can't use the semaphore set; a user defined key can be defined as symbolic constant, command line option,...) nsems number of semaphores in set semflg access permissions and flags flags IPC_CREAT IPC_EXCL create a new semaphore set if it doesn't already exist create a new semaphore set exclusively, i.e. return an error if a semid already exists for key

35 Parallel Programming Slide 2-35 performing semaphore operations with semop ( ) all operations are indivisible, i.e. all operations are performed in one step int semop (int semid, struct sembuf *sops, size_t nsops) struct sembuf ushort sem_num; /* number of semaphore in set */ short sem_op; /* operation */ short sem_flg; /* flags */ semop (int semid, struct sembuf *sops, size_t nsops) handle for semaphore set array of semaphore operations number of operations in the array sem_num sem_op sem_flg number of semaphore to operate on operations: < 0: corresponds to P() operation > 0: corresponds to V() operation = 0: wait until count equals 0 IPC_NOWAIT SEM_UNDO 0

36 Parallel Programming Slide 2-36 it's operating system dependent how many operations are allowed in a single semop ( ) call sem_op > 0 increment semaphore value by sem_op sem_op < 0 sem_op = 0 decrement semaphore value by sem_op if possible (possibly the process will be blocked until operation is possible) wait until the semaphore value equals zero allowed values for sem_flg: IPC_NOWAIT can be used for every single operation (if any operation with IPC_NOWAIT cannot be performed, semop ( ) performs no operation at all and returns -1) SEM_UNDO the operating system keeps track of this operation and is able to undo all operations if the process exits Attention: Don't use this flag if processes are only running for some time and performing a different number of P ( ) and V ( ) operations (e.g., the P ( ) operation is in one process and the corresponding V ( ) operation in another one) 0 in most cases the default (if a process terminates due to a failure it can possibly block resources for other processes which use the same semaphore set)

37 Parallel Programming Slide 2-37 example: implementation und usage of Dijkstra's P ( ) and V ( ) operation with System V semaphores (sem_implement_p_v.c)... #define SEM_KEY IPC_PRIVATE /* create private semaphore */ #define SEM_PERM 0600 /* access permissions */ typedef int semaphore; /* semaphore (handle) type */ #if (_SEM_SEMUN_UNDEFINED == 1) defined(sunos) defined(cygwin) typedef union semun int val; /* cmd: SETVAL */ struct semid_ds *buf; /* cmd: IPC_STAT, IPC_SET */ ushort *array; /* cmd: GETALL, SETALL */ semunion; #endif semaphore init_sem (int value); /* create/initialize semaphore */ void rel_sem (semaphore sem); /* release semaphore */ void P (semaphore sem); /* semaphore operations */ void V (semaphore sem); int main (int argc, char *argv[]) semaphore s;... srand ((unsigned int) time ((time_t) NULL)); s = init_sem (1); /* create sem with value 1 */ for (i = 0; i < MAX_CHILD; ++i) child_pid[i] = fork (); switch (child_pid[i]) case -1: /* error: no process created */... case 0: /* child process */ alarm (LIFETIME); /* force process termination */ while (1) /* simulate some normal work */ P (s); /* simulate some critical work */ V (s); break; default: /* parent process */ if (i == (MAX_CHILD - 1)) /* all childs created? */ /* yes -> wait for termination */... printf ("All child processes have terminated.\n"); rel_sem (s); /* end switch */ /* end for */ return EXIT_SUCCESS;

38 Parallel Programming Slide 2-38 semaphore init_sem (int value) union semun arg; /* parameter for semctl */ int semid; /* semaphore handle */ semid = semget (SEM_KEY, 1, IPC_CREAT SEM_PERM); if (semid == -1) fprintf (stderr, "init_sem: semget failed: %s\n", strerror (errno)); exit (ESEMGET); arg.val = value; if (semctl (semid, 0, SETVAL, arg) == -1) fprintf (stderr, "init_sem: semctl failed: %s\n", strerror (errno)); exit (ESEMCTL); return semid; void rel_sem (semaphore sem) union semun tmp; /* for Linux up to 2.0.x */ if (semctl (sem, 0, IPC_RMID, tmp) == -1) fprintf (stderr, "rel_sem: semctl failed: %s\n", strerror (errno)); exit (ESEMCTL); void P (semaphore sem) struct sembuf tmp; tmp.sem_num = 0; tmp.sem_op = -1; tmp.sem_flg = 0; if (semop (sem, &tmp, (size_t) 1) == -1) fprintf (stderr, "P: semop failed: %s\n", strerror (errno)); exit (ESEMOP); void V (semaphore sem) struct sembuf tmp; tmp.sem_num = 0; tmp.sem_op = 1; tmp.sem_flg = 0; if (semop (sem, &tmp, (size_t) 1) == -1) fprintf (stderr, "V: semop failed: %s\n", strerror (errno)); exit (ESEMOP);

39 Parallel Programming Slide 2-39 System V IPC can be configured via operating system parameters SHMMNI maximum number of identifiers for shared memory segments SHMMAX maximum size of a shared memory segment in bytes SEMMNI SEMMSL SEMOPM MSGMNI maximum number of identifiers for semaphore sets maximum number of semaphores per set maximum number of operations per semop-call maximum number of identifiers for message queues MSGMNB maximum number of bytes of messages on message queue MSGTQL maximum number of messages on a message queue Linux 2.6.x specifies these values in the following files /usr/include/linux/shm.h /usr/include/linux/sem.h /usr/include/linux/msg.h it's necessary to create a new kernel if you change any value use "/sbin/sysctl -a grep -e shm -e sem" to read current values presumably Solaris defines these values in the following module files shmsys semsys msgsys (these files are stored in directory "/kernel/sys" (32-bit operating system) and "/kernel/sys/sparcv9" or "/kernel/sys/amd64" (64-bit operating systems))

40 Parallel Programming Slide 2-40 up to Solaris 9 all values can be changed via /etc/system (try "man -s 4 system") e.g.: set shmsys:shminfo_shmmni = <value> set semsys:seminfo_semmni = <value> it's necessary to reboot the system if you change any value the following command displays all current values (try "man sysdef") /usr/sbin/sysdef /usr/xpg4/bin/grep -e SHM -e SEM (Everything is different for Solaris 10 or newer which introduced "containers" and "process specific resources". Try "man prctl", "man getrctl", or "man rctladm". You can display the current values with "rctladm -l" instead of "sysdef" in older Solaris versions. The advantage of the new concept is that you can change the values on a per process basis and that you don t have to reboot if you change the values.)

41 Parallel Programming Slide 2-41 Exercise 2-5: Implement a solution for the dining philosophers problem. Don't use the above simulation! Use all of the power of System V semaphores, i.e. take all necessary forks in one operation #define N 5 semaphore fork [N]; int main (void) int i; for (i = 0; i < N; ++i) fork [i] = init_sem (1); /* all forks are free */ COBEGIN philosopher (0);...; philosopher (N-1); COEND; void philosopher (int i) while (1) philosopher is thinking; P (fork [i]); /* right fork */ P (fork [(i + 1) % N]); /* left fork */ philosopher is eating; V (fork [i]); V (fork [(i + 1) % N]);

COP 4604 UNIX System Programming IPC. Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University

COP 4604 UNIX System Programming IPC. Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University COP 4604 UNIX System Programming IPC Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University Interprocess Communication Interprocess communication (IPC) provides two major functions/services:

More information

Shared Memory Semaphores. Goals of this Lecture

Shared Memory Semaphores. Goals of this Lecture Shared Memory Semaphores 12 Shared Memory / Semaphores Hannes Lubich, 2003 2005 Page 1 Goals of this Lecture Understand the design and programming of the System V Unix shared memory interprocess communication

More information

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist Interprocess Communication Originally multiple approaches Today more standard some differences between distributions still exist Pipes Oldest form of IPC provided by all distros Limitations Historically

More information

CSPP System V IPC 1. System V IPC. Unix Systems Programming CSPP 51081

CSPP System V IPC 1. System V IPC. Unix Systems Programming CSPP 51081 System V IPC 1 System V IPC System V IPC 2 System V IPC Overview System V IPC 3 System V InterProcess Communication (IPC) Provides three mechanisms for sharing data between processes message queues (similar

More information

Lecture 18. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture18/ $ cp r /home/hwang/cs375/lecture18/*.

Lecture 18. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture18/ $ cp r /home/hwang/cs375/lecture18/*. Lecture 18 Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture18/ $ cp r /home/hwang/cs375/lecture18/*. Both subdirectories have makefiles. The "sysv" subdirectory has an example/exercise

More information

경희대학교컴퓨터공학과 조진성. UNIX System Programming

경희대학교컴퓨터공학과 조진성. UNIX System Programming Inter-Process Communication 경희대학교컴퓨터공학과 조진성 UNIX System Programming Inter-Process Communication n Mechanisms for processes to communicate with each other n UNIX IPC ü pipes ü FIFOs ü message queue ü shared

More information

struct ipc_perm sem_perm; ushort sem_nsems; /* count of sems in set */ time_t sem_otime; /* last operation time */

struct ipc_perm sem_perm; ushort sem_nsems; /* count of sems in set */ time_t sem_otime; /* last operation time */ IPC(5) Linux Programmer s Manual IPC(5) ipc System V interprocess communication mechanisms #include #include #include The manual page refers to the Linux implementation

More information

INTER-PROCESS COMMUNICATION. UNIX Programming 2015 Fall by Euiseong Seo

INTER-PROCESS COMMUNICATION. UNIX Programming 2015 Fall by Euiseong Seo INTER-PROCESS COMMUNICATION UNIX Programming 2015 Fall by Euiseong Seo Named Pipes Anonymous pipes can be used only between related processes Processes not from the same ancestor sometimes need to communicate

More information

UNIX IPC. Unix Semaphore Unix Message queue

UNIX IPC. Unix Semaphore Unix Message queue UNIX IPC Unix Semaphore Unix Message queue 1 UNIX SEMAPHORE: Unix semaphore is not a single variable but an array of non-negative integer variables. Number of non-negative values: 1 to some system defined

More information

OS Lab Tutorial 1. Spawning processes Shared memory

OS Lab Tutorial 1. Spawning processes Shared memory OS Lab Tutorial 1 Spawning processes Shared memory The Spawn exec() family fork() The exec() Functions: Out with the old, in with the new The exec() functions all replace the current program running within

More information

Shared Memory (8A) Shared Memory

Shared Memory (8A) Shared Memory Shared Memory (8A) Shared Memory Copyright (c) 2012 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2

More information

Shared Memory Memory mapped files

Shared Memory Memory mapped files Shared Memory Memory mapped files 1 Shared Memory Introduction Creating a Shared Memory Segment Shared Memory Control Shared Memory Operations Using a File as Shared Memory 2 Introduction Shared memory

More information

CSE 380: Homework 2: Synchronization

CSE 380: Homework 2: Synchronization CSE 380 Homework 2 1 CSE 380: Homework 2: Synchronization Due : Thursday, October 2, 2003 Submit a hardcopy solution of the problems in class on Oct 2, and submit code and documentation for the programs

More information

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes COSC4740-01 Operating Systems Design, Fall 2001 Lecture Note: Unnamed Pipe and Shared Memory Unnamed Pipes Pipes are a form of Inter-Process Communication (IPC) implemented on Unix and Linux variants.

More information

Using IPC: semaphores Interprocess communication using semaphores. Lecturer: Erick Fredj

Using IPC: semaphores Interprocess communication using semaphores. Lecturer: Erick Fredj Using IPC: semaphores Interprocess communication using semaphores Lecturer: Erick Fredj What is a 'semaphore'? A classical approach for restricting access to shared resources in multi-process environments

More information

Inter-process communication (IPC)

Inter-process communication (IPC) Inter-process communication (IPC) Operating Systems Kartik Gopalan References Chapter 5 of OSTEP book. Unix man pages Advanced Programming in Unix Environment by Richard Stevens http://www.kohala.com/start/apue.html

More information

UNIT 7 INTERPROCESS COMMUNICATION

UNIT 7 INTERPROCESS COMMUNICATION Gechstudentszonewordpresscom Prajwal K R UNIT 7 INTERPROCESS COMMUNICATION INTRODUCTION IPC enables one application to control another application, and for several applications to share the same data without

More information

Shared Memory. By Oren Kalinsky

Shared Memory. By Oren Kalinsky Shared Memory By Oren Kalinsky 1 Overview Shared memory (SHM) - two or more processes can share a given region of memory A form of Inter Process Communication (IPC) Other IPC methods pipes, message queues

More information

UNIT III- INTERPROCESS COMMUNICATION

UNIT III- INTERPROCESS COMMUNICATION UNIT III- INTERPROCESS COMMUNICATION OBJECTIVE Inter-process Communication o Pipes o Signals o Message Queues o Semaphores o Shared Memory INTER-PROCESS COMMUNICATION Inter-process communication (IPC)

More information

CS 385 Operating Systems Fall 2013 Homework Assignment 2 Inter-Process Communications and Synchronization

CS 385 Operating Systems Fall 2013 Homework Assignment 2 Inter-Process Communications and Synchronization CS 385 Operating Systems Fall 2013 Homework Assignment 2 Inter-Process Communications and Synchronization Due: Tuesday October15 th at 3:00 P.M. via Blackboard. Optional hard copy may be submitted to the

More information

CS 385 Operating Systems Spring 2013 Homework Assignment 2 Third Draft Inter-Process Communications and Synchronization

CS 385 Operating Systems Spring 2013 Homework Assignment 2 Third Draft Inter-Process Communications and Synchronization CS 385 Operating Systems Spring 2013 Homework Assignment 2 Third Draft Inter-Process Communications and Synchronization Due: Thursday March 14 th at 3:00 P.M. via Blackboard. Optional hard copy may be

More information

Part II Processes and Threads Process Basics

Part II Processes and Threads Process Basics Part II Processes and Threads Process Basics Fall 2017 Program testing can be used to show the presence of bugs, but never to show their absence 1 Edsger W. Dijkstra From Compilation to Execution A compiler

More information

CS 550 Operating Systems Spring Inter Process Communication

CS 550 Operating Systems Spring Inter Process Communication CS 550 Operating Systems Spring 2019 Inter Process Communication 1 Question? How processes communicate with each other? 2 Some simple forms of IPC Parent-child Command-line arguments, wait( ), waitpid(

More information

컴퓨터특강 (UNIX System Programming) APUE(Interprocess Communication) [Ch. 14]

컴퓨터특강 (UNIX System Programming) APUE(Interprocess Communication) [Ch. 14] 컴퓨터특강 () APUE(Interprocess Communication) [Ch. 14] 2006 년봄학기 문양세강원대학교컴퓨터과학과 Contents Pipes FIFOs System V IPC Message Queues Shared Memory Semaphores Page 2 1 IPC using Pipes IPC using regular files unrelated

More information

Interprocess Communication. Bosky Agarwal CS 518

Interprocess Communication. Bosky Agarwal CS 518 Interprocess Communication Bosky Agarwal CS 518 Presentation Layout Review Introduction Pipes 1. Using pipes 2. Working of pipes 3. Pipe Data Structure 4. Special pipefs File System 5. Creating and destroying

More information

Pipes. FIFOs. System V IPC. Message Queues. Shared Memory. Semaphores. APUE (Interprocess Communication. Page 2

Pipes. FIFOs. System V IPC. Message Queues. Shared Memory. Semaphores. APUE (Interprocess Communication. Page 2 Linux/UNIX Programming APUE (Interprocess Communication) 문양세강원대학교 IT특성화대학컴퓨터과학전공 Contents Pipes FIFOs System V IPC Message Queues Shared Memory Semaphores Page 2 IPC using Pipes IPC using regular files

More information

PRACTICAL NO : 1. AIM: To study various file management system calls in UNIX.

PRACTICAL NO : 1. AIM: To study various file management system calls in UNIX. PRACTICAL NO : 1 AIM: To study various file management system calls in UNIX. Write a program to implement 1. Create a file 2. Delete a file 3. Link a file 4. Copy one file to another file 5. Read contents

More information

UNIT III- INTER PROCESS COMMUNICATIONS Part A

UNIT III- INTER PROCESS COMMUNICATIONS Part A UNIT III- INTER PROCESS COMMUNICATIONS Part A 1 What are the different communications supported by UNIX? Inter process communication and network communication 2 What do you mean by Inter process communication?

More information

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes. Unix-Linux 2 fork( ) system call is successful parent suspended child created fork( ) returns child pid to parent fork( ) returns zero value to child; zero is the pid of the swapper/scheduler process both

More information

Systems Programming/ C and UNIX

Systems Programming/ C and UNIX Systems Programming/ C and UNIX Alice E. Fischer November 22, 2013 Alice E. Fischer () Systems Programming Lecture 12... 1/27 November 22, 2013 1 / 27 Outline 1 Jobs and Job Control 2 Shared Memory Concepts

More information

alarm(2) - Linux man page

alarm(2) - Linux man page alarm(2): set alarm clock - Linux man page http://www.die.net/doc/linux/man/man2/alarm.2.html Page 1 of 1 10/20/2003 alarm(2) - Linux man page NAME alarm - set an alarm clock for delivery of a signal SYNOPSIS

More information

Discussion of Assignments 2. Line buffered vs. full buffered I/O. Some often encountered issues in the submissions.

Discussion of Assignments 2. Line buffered vs. full buffered I/O. Some often encountered issues in the submissions. 3 4 Discussion of Assignment 1 Discussion of Assignments 1 and 2 Accompanying Tutorial to Operating Systems Course Alexander Holupirek, Stefan Klinger Database and Information Systems Group Department

More information

CS 385 Operating Systems Fall 2011 Homework Assignment 5 Process Synchronization and Communications

CS 385 Operating Systems Fall 2011 Homework Assignment 5 Process Synchronization and Communications CS 385 Operating Systems Fall 2011 Homework Assignment 5 Process Synchronization and Communications Due: Friday December 2 at 8:00 P.M. via Blackboard Overall Assignment Man Pages For this assignment,

More information

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017 Linux/UNIX IPC Programming POSIX Shared Memory Michael Kerrisk, man7.org c 2017 mtk@man7.org November 2017 Outline 10 POSIX Shared Memory 10-1 10.1 Overview 10-3 10.2 Creating and opening shared memory

More information

Computer Science & Engineering Department I. I. T. Kharagpur. Operating System: CS rd Year CSE: 5th Semester (Autumn ) Lecture XI

Computer Science & Engineering Department I. I. T. Kharagpur. Operating System: CS rd Year CSE: 5th Semester (Autumn ) Lecture XI Computer Science & Engineering Department I. I. T. Kharagpur Operating System: CS33007 3rd Year CSE: 5th Semester (Autumn 2006-2007) Lecture XI Goutam Biswas Date: 29th August - 4th September, 2006 1 Semaphore

More information

Signal Example 1. Signal Example 2

Signal Example 1. Signal Example 2 Signal Example 1 #include #include void ctrl_c_handler(int tmp) { printf("you typed CTL-C, but I don't want to die!\n"); int main(int argc, char* argv[]) { long i; signal(sigint, ctrl_c_handler);

More information

Message Queues, Semaphores, Shared Memory

Message Queues, Semaphores, Shared Memory Message Queues, Semaphores, Shared Memory Message Queues Basic idea of a message queue 1. Two processes can exchange information via access to a common system message queue. 2. A process places a message

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 16: Process and Signals Cristina Nita-Rotaru Lecture 16/ Fall 2013 1 Processes in UNIX UNIX identifies processes via a unique Process ID Each process also knows its parent

More information

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models Operating Systems VI. Threads Ludovic Apvrille ludovic.apvrille@telecom-paristech.fr Eurecom, office 470 http://soc.eurecom.fr/os/ @OS Eurecom Outline 2/36 Fall 2017 Institut Mines-Telecom Operating Systems

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Processes A process is an instance of a running program. Programs do not have to

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 17: Processes, Pipes, and Signals Cristina Nita-Rotaru Lecture 17/ Fall 2013 1 Processes in UNIX UNIX identifies processes via a unique Process ID Each process also knows

More information

Lecture 8: Inter-process Communication. Lecturer: Prof. Zichen Xu

Lecture 8: Inter-process Communication. Lecturer: Prof. Zichen Xu Lecture 8: Inter-process Communication Lecturer: Prof. Zichen Xu 1 Outline Unix IPC and Synchronization Pipe Message Semaphore Shared Memory Signals 2 Pipes and FIFOs Pipe: a circular buffer of fixed size

More information

COMP 3100 Operating Systems

COMP 3100 Operating Systems Programming Interface» A process is an instance of a running program. COMP 3100 Operating Systems» Functionality that an OS provides to applications» Process Management» Input/Output Week 3 Processes and

More information

CSC 1600 Unix Processes. Goals of This Lecture

CSC 1600 Unix Processes. Goals of This Lecture CSC 1600 Unix Processes q Processes Goals of This Lecture q Process vs. program q Context switching q Creating a new process q fork: process creates a new child process q wait: parent waits for child process

More information

Programmation Système Cours 10 System V IPC

Programmation Système Cours 10 System V IPC Programmation Système Cours 10 System V IPC Stefano Zacchiroli zack@pps.univ-paris-diderot.fr Laboratoire PPS, Université Paris Diderot 2014 2015 URL http://upsilon.cc/zack/teaching/1415/progsyst/ Copyright

More information

Inter Process Communication (IPC) Giorgio Richelli

Inter Process Communication (IPC) Giorgio Richelli Inter Process Communication (IPC) Contents Introduction Universal IPC Facilities System V IPC Introduction The purposes of IPC: Data transfer Sharing data Event notification Resource sharing Process control

More information

ECE 650 Systems Programming & Engineering. Spring 2018

ECE 650 Systems Programming & Engineering. Spring 2018 ECE 650 Systems Programming & Engineering Spring 2018 Inter-process Communication (IPC) Tyler Bletsch Duke University Slides are adapted from Brian Rogers (Duke) Recall Process vs. Thread A process is

More information

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University EECS3221.3 Operating System Fundamentals No.2 Process Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University How OS manages CPU usage? How CPU is used? Users use CPU to run

More information

Concurrent Servers. Overview. In our current assignment we have the following changes:

Concurrent Servers. Overview. In our current assignment we have the following changes: Concurrent Servers Overview In our current assignment we have the following changes: Concurrent server Session command with an argument of the session name Shutdown command 2 Concurrent Server When a client

More information

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

CSci 4061 Introduction to Operating Systems. Processes in C/Unix CSci 4061 Introduction to Operating Systems Processes in C/Unix Process as Abstraction Talked about C programs a bit Program is a static entity Process is an abstraction of a running program provided by

More information

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No. EECS3221.3 Operating System Fundamentals No.2 Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University How OS manages CPU usage? How CPU is used? Users use CPU to run programs

More information

INTER-PROCESS COMMUNICATION Tanzir Ahmed CSCE 313 Fall 2018

INTER-PROCESS COMMUNICATION Tanzir Ahmed CSCE 313 Fall 2018 INTER-PROCESS COMMUNICATION Tanzir Ahmed CSCE 313 Fall 2018 Inter-Process Communication IPC Methos Pipes and FIFO Message Passing Shared Memory Semaphore Sets Signals References: Beej s guide to Inter

More information

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu CPSC 341 OS & Networks Processes Dr. Yingwu Zhu Process Concept Process a program in execution What is not a process? -- program on a disk A process is an active object, but a program is just a file It

More information

Operating System Structure

Operating System Structure Operating System Structure CSCI 4061 Introduction to Operating Systems Applications Instructor: Abhishek Chandra Operating System Hardware 2 Questions Operating System Structure How does the OS manage

More information

Lesson 2. process id = 1000 text data i = 5 pid = 1200

Lesson 2. process id = 1000 text data i = 5 pid = 1200 Lesson 2 fork: create a new process. The new process (child process) is almost an exact copy of the calling process (parent process). In this method we create an hierarchy structure for the processes,

More information

Prepared by Prof. Hui Jiang (COSC3221) 2/9/2007

Prepared by Prof. Hui Jiang (COSC3221) 2/9/2007 1 * * ' &% $ # " "! 4 ' Prepared by Prof Hui Jiang COSC1 /9/007 / 0 How CPU is used? Users run programs in CPU In a multiprogramming system a CPU always has several jobs to run How to define a CPU job?

More information

Operating Systems. No. 5 ศร ณย อ นทโกส ม Sarun Intakosum

Operating Systems. No. 5 ศร ณย อ นทโกส ม Sarun Intakosum Operating Systems No. 5 ศร ณย อ นทโกส ม Sarun Intakosum 1 Interprocess Communication (IPC) 2 Interprocess Communication Processes within a system may be independent or cooperating Cooperating process can

More information

Processes. CS3026 Operating Systems Lecture 05

Processes. CS3026 Operating Systems Lecture 05 Processes CS3026 Operating Systems Lecture 05 Dispatcher Admit Ready Queue Dispatch Processor Release Timeout or Yield Event Occurs Blocked Queue Event Wait Implementation: Using one Ready and one Blocked

More information

Babu Madhav Institute of Information Technology, UTU

Babu Madhav Institute of Information Technology, UTU 5 Years Integrated M.Sc.(IT) Semester 6 Question Bank 060010601 UNIX Internals Unit 1: Introduction and Overview of UNIX Answer following in short 1. What is system call? 2. Who are principal designer

More information

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication I

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication I CS631 - Advanced Programming in the UNIX Environment Slide 1 CS631 - Advanced Programming in the UNIX Environment Interprocess Communication I Department of Computer Science Stevens Institute of Technology

More information

Announcement (1) sys.skku.edu is now available

Announcement (1) sys.skku.edu is now available Processes Prof. Jin-Soo Kim( jinsookim@skku.edu) TA JinHong Kim( jinhong.kim@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Announcement (1) sys.skku.edu is now available

More information

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1 Reading Assignment 4 Chapter 4 Threads, due 2/7 1/31/13 CSE325 - Processes 1 What s Next? 1. Process Concept 2. Process Manager Responsibilities 3. Operations on Processes 4. Process Scheduling 5. Cooperating

More information

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX.

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX. This document gives a general overview of the work done by an operating system and gives specific examples from UNIX. 1 Manages Resources: I/O devices (disk, keyboard, mouse, terminal) Memory Manages Processes:

More information

A Lightweight Semaphore for Linux

A Lightweight Semaphore for Linux Jojumon Kavalan Joy Menon Samveen Gulati Department of Computer Science and Engineering, IIT Mumbai. 31 October 2004 What are Semaphores? What are Semaphores? Sets of Semaphores Semaphore Performance Definition:

More information

#include <sys/types.h> #include <sys/wait.h> pid_t wait(int *stat_loc); pid_t waitpid(pid_t pid, int *stat_loc, int options);

#include <sys/types.h> #include <sys/wait.h> pid_t wait(int *stat_loc); pid_t waitpid(pid_t pid, int *stat_loc, int options); pid_t fork(void); #include pid_t wait(int *stat_loc); pid_t waitpid(pid_t pid, int *stat_loc, int options); char **environ; int execl(const char *path, const char *arg0,..., (char *)0); int

More information

UNIX System Calls. Sys Calls versus Library Func

UNIX System Calls. Sys Calls versus Library Func UNIX System Calls Entry points to the kernel Provide services to the processes One feature that cannot be changed Definitions are in C For most system calls a function with the same name exists in the

More information

Workshop on Inter Process Communication Solutions

Workshop on Inter Process Communication Solutions Solutions 1 Background Threads can share information with each other quite easily (if they belong to the same process), since they share the same memory space. But processes have totally isolated memory

More information

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430 Week 2 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430 1 Why is the Shell Important? Shells provide us with a way to interact with the core system Executes programs on

More information

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls System Calls Library Functions Vs. System Calls A library function: Ordinary function that resides in a library external to the calling program. A call to a library function is just like any other function

More information

518 Lecture Notes Week 3

518 Lecture Notes Week 3 518 Lecture Notes Week 3 (Sept. 15, 2014) 1/8 518 Lecture Notes Week 3 1 Topics Process management Process creation with fork() Overlaying an existing process with exec Notes on Lab 3 2 Process management

More information

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization CS 345 Operating Systems Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization Assignment 2 We have a treasure room, Team A and Team B. Treasure room has N coins inside. Each team

More information

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes CSci 4061 Introduction to Operating Systems IPC: Basics, Pipes Communication IPC in Unix Pipes: most basic form of IPC in Unix process-process ps u jon grep tcsh // what happens? Pipe has a read-end (receive)

More information

Gabrielle Evaristo CSE 460. Lab Shared Memory

Gabrielle Evaristo CSE 460. Lab Shared Memory Gabrielle Evaristo CSE 460 Lab 7 1. Shared Memory Use man to study each of the shared memory functions and write a brief description on the usage of each of them. o shmget (shared memory get): Allocated

More information

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes CSci 4061 Introduction to Operating Systems IPC: Basics, Pipes Communication IPC in Unix Pipes: most basic form of IPC in Unix process-process ps u jon grep tcsh // what happens? Pipe has a read-end (receive)

More information

W4118 Operating Systems. Junfeng Yang

W4118 Operating Systems. Junfeng Yang W4118 Operating Systems Junfeng Yang What is a process? Outline Process dispatching Common process operations Inter-process Communication What is a process Program in execution virtual CPU Process: an

More information

PROCESSES. Jo, Heeseung

PROCESSES. Jo, Heeseung PROCESSES Jo, Heeseung TODAY'S TOPICS What is the process? How to implement processes? Inter-Process Communication (IPC) 2 WHAT IS THE PROCESS? Program? vs. Process? vs. Processor? 3 PROCESS CONCEPT (1)

More information

Processes. Jo, Heeseung

Processes. Jo, Heeseung Processes Jo, Heeseung Today's Topics What is the process? How to implement processes? Inter-Process Communication (IPC) 2 What Is The Process? Program? vs. Process? vs. Processor? 3 Process Concept (1)

More information

Synchronization. Semaphores implementation

Synchronization. Semaphores implementation Synchronization Semaphores implementation Possible implementations There are seeral possible implementations (standard and non standard)of a semaphore Semaphores through pipe POSIX semaphores Linux semaphores

More information

CS 361 Computer Systems Fall 2017 Homework Assignment 4 - Inter-Process Communications & I/O

CS 361 Computer Systems Fall 2017 Homework Assignment 4 - Inter-Process Communications & I/O CS 361 Computer Systems Fall 2017 Homework Assignment 4 - Inter-Process Communications & I/O Overall Assignment For this assignment, you are to write three programs that will work together using inter-process

More information

Operating Systems 2010/2011

Operating Systems 2010/2011 Operating Systems 2010/2011 Processes Johan Lukkien 1 Agenda Processes, threads API for creation and termination POSIX Processes Implementation aspects Communication and Synchronization Shared Memory Message

More information

Operating systems and concurrency - B03

Operating systems and concurrency - B03 Operating systems and concurrency - B03 David Kendall Northumbria University David Kendall (Northumbria University) Operating systems and concurrency - B03 1 / 15 Introduction This lecture gives a more

More information

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J Processes & Threads Today! Process concept! Process model! Implementing processes! Multiprocessing once again Next Time! More of the same J The process model! Most computers can do more than one thing

More information

CSC209 Fall Karen Reid 1

CSC209 Fall Karen Reid 1 ' & ) ) #$ "! How user programs interact with the Operating System. Somehow we need to convert a program into machine code (object code). A compiler passes over a whole program before translating it into

More information

CSC 252: Computer Organization Spring 2018: Lecture 19

CSC 252: Computer Organization Spring 2018: Lecture 19 CSC 252: Computer Organization Spring 2018: Lecture 19 Instructor: Yuhao Zhu Department of Computer Science University of Rochester Action Items: Programming Assignment 3 grades are out Programming Assignment

More information

POSIX Semaphores. Operations on semaphores (taken from the Linux man page)

POSIX Semaphores. Operations on semaphores (taken from the Linux man page) POSIX Semaphores A variable of type sem_t Example Declaration of a semaphore sem_t sem; Operations on semaphores (taken from the Linux man page) int sem_init(sem_t *sem, int pshared, unsigned int value);

More information

Unix Processes 1 / 31

Unix Processes 1 / 31 Unix Processes 1/31 A Unix Process Instance of a program in execution. OS loads the executable in main-memory (core) and starts execution by accessing the first command. Each process has a unique identifier,

More information

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD GROUP - C EXPERIMENT NO : 12 1. Title: Implement UNIX system calls like ps, fork, join, exec family, and wait for process management (use shell script/ Java/ C programming) 2. Objectives : - To understand

More information

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line Operating Systems Lecture 06 System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line March 04, 2013 exec() Typically the exec system call is

More information

Processes and Threads

Processes and Threads Process Processes and Threads A process is an abstraction that represent an executing program A program in execution An instance of a program running on a computer The entity that can be assigned to and

More information

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system. Chp1 Objective Briefly describe services provided by various versions of the UNIX operating system. Logging In /etc/passwd local machine or NIS DB root:x:0:1:super-user:/root:/bin/tcsh Login-name, encrypted

More information

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes CSci 4061 Introduction to Operating Systems IPC: Basics, Pipes Today Directory wrap-up Communication/IPC Test in one week Communication Abstraction: conduit for data exchange between two or more processes

More information

Process management 1

Process management 1 Process management 1 The kernel The core set of service that the OS provides 2 User Mode & kernel mode User mode apps delegate to system APIs in order to access hardware User space Kernel space User Utilities

More information

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D) MANAGEMENT OF APPLICATION EXECUTION PROCESS CONTROL BLOCK Resources (processor, I/O devices, etc.) are made available to multiple applications The processor in particular is switched among multiple applications

More information

Interprocess Communication

Interprocess Communication Interprocess Communication Processes within a system may be independent or cooperating Cooperating process can affect or be affected by other processes, including sharing data Reasons for cooperating processes:

More information

Process Management! Goals of this Lecture!

Process Management! Goals of this Lecture! Process Management! 1 Goals of this Lecture! Help you learn about:" Creating new processes" Programmatically redirecting stdin, stdout, and stderr" Unix system-level functions for I/O" The Unix stream

More information

Fall 2015 COMP Operating Systems. Lab #3

Fall 2015 COMP Operating Systems. Lab #3 Fall 2015 COMP 3511 Operating Systems Lab #3 Outline n Operating System Debugging, Generation and System Boot n Review Questions n Process Control n UNIX fork() and Examples on fork() n exec family: execute

More information

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo PROCESS MANAGEMENT Operating Systems 2015 Spring by Euiseong Seo Today s Topics Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication

More information

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed Process Management CS 537 Lecture 3: Processes Michael Swift This lecture begins a series of topics on processes, threads, and synchronization Today: processes and process management what are the OS units

More information

Operating Systems, laboratory exercises. List 2.

Operating Systems, laboratory exercises. List 2. Operating Systems, laboratory exercises. List 2. Subject: Creating processes and threads with UNIX/Linux API functions. 1. Creating a process with UNIX API function. To create a new process from running

More information

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Mahmoud El-Gayyar / Introduction to OS 1 Processes in Unix, Linux, and Windows Unix pre-empted

More information