27 Ipc
27 Ipc
27 Ipc
CS 241
April 4, 2014
University of Illinois
1
Shared Memory
Private
Private
address OS address address space
space space
Shared
Process A segment Process B
Processes request the segment
OS maintains the segment
Processes can attach/detach the segment
2
Shared Memory
Private
Private Private
addressspace
address OS address address space
space space
Shared
Process A segment Process B
3
Shared Memory example
/* make the key: */
if ((key = ftok(”shmdemo.c", 'R')) == -1) {
perror("ftok");
exit(1);
}
/* connect to (and possibly create) the segment: */
if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}
/* attach to the segment to get a pointer to it: */
data = shmat(shmid, (void *)0, 0);
if (data == (char *)(-1)) {
perror("shmat");
exit(1);
}
4
Shared Memory example
/* read or modify the segment, based on the command line: */
if (argc == 2) {
printf("writing to segment: \"%s\"\n", argv[1]);
strncpy(data, argv[1], SHM_SIZE);
} else
printf("segment contains: \"%s\"\n", data);
return 0;
}
Run demo
5
Memory Mapped Files
6
Memory Mapped Files
Memory Mapping
• Eliminate intermediary buffering
• Significantly improve performance
• Random-access interface
7
Memory Mapped Files
VM of User 1
File
Memory Mapped File Blocks of data
In Blocks From file mapped
To VM
mmap requests
Disk VM of User 2
Blocks of data
From file mapped
To VM
8
Memory Mapped Files: Benefits
Dynamic loading
#
" Map executable files and shared libraries into address space
" Programs can load and unload executable code sections dynamically
9
Memory Mapped Files: Benefits
Memory persistence
#
Enables IPC!
10
POSIX Memory Mapping
#include <sys/mman.h>
" Establish mapping from the address space of the process to the object represented by
the file descriptor
Parameters:
#
" addr: the starting memory address into which to map the file (not previously
allocated with malloc; argument can just be NULL)
" len: the length of the data to map into memory
" prot: the kind of access to the memory mapped region
" flags: flags that can be set for the system call
" fd: file descriptor
" off: the offset in the file to start mapping from
11
POSIX Memory Mapping
#include <sys/mman.h>
off len
File fd
12
POSIX Memory Mapping
#include <sys/mman.h>
13
POSIX Memory Mapping
#include <sys/mman.h>
File fd
Memory
addr
14
mmap options
Protection Flags
• PROT_READ Data can be read
• PROT_WRITE Data can be written
• PROT_EXEC Data can be executed
• PROT_NONE Data cannot be accessed
Flags
• MAP_SHARED Changes are shared.
• MAP_PRIVATE Changes are private.
• MAP_FIXED Interpret addr exactly
15
mmap example
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
static const int MAX_INPUT_LENGTH = 20;
int main(int argc, char** argv) {
....
16
mmap example
int main(int argc, char** argv) {
int fd;
char * shared_mem;
fd = open(argv[1], O_RDWR | O_CREAT);
shared_mem = mmap(NULL, MAX_INPUT_LENGTH, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
close(fd);
if (!strcmp(argv[2], "read")) {
while (1) {
shared_mem[MAX_INPUT_LENGTH-1] = '\0';
printf("%s", shared_mem);
sleep(1);
}
}
else if (!strcmp(argv[2], "write"))
while (1)
fgets(shared_mem, MAX_INPUT_LENGTH, stdin);
else
printf("Unrecognized command\n");
Run demo!
}
17
munmap
#include <sys/mman.h>
int munmap(void *addr, size_t len);
Remove a mapping
Return value
• 0 on success
• -1 on error, sets errno
Parameters:
• addr: returned from mmap()
• len: same as the len passed to mmap()
18
msync
#include <sys/mman.h>
int msync(void *addr, size_t len, int flags);
Write all modified data to permanent storage locations
Return value
• 0 on success
• -1 on error, sets errno
Parameters:
• addr: returned from mmap()
• len: same as the len passed to mmap()
• flags:
! MS_ASYNC = Perform asynchronous writes
! MS_SYNC = Perform synchronous writes
! MS_INVALIDATE = Invalidate cached data
19
Recall POSIX Shared Memory...
#include <sys/types.h>
#include <sys/shm.h>
20
How do mmap and POSIX shared
memory compare?
Persistence!
shared memory
• Kept in memory
• Remains available until system is shut down
mmap
• Backed by a file
• Persists even after programs quit or machine reboots
21
Pipes
photo: autowitch
http://www.flickr.com/photos/autowitch/2098428964/ 22
Google Chrome architecture (figure
https://sites.google.com/a/chromium.org/dev/developers/design-documents/multi-process-architecture
borrowed from Google)
23
Google Chrome architecture (figure
https://sites.google.com/a/chromium.org/dev/developers/design-documents/multi-process-architecture
borrowed from Google)
A named pipe is
allocated for each
renderer process for
communication with the
browser process
Pipes are used in
asynchronous mode to
ensure that neither end
is blocked waiting for
the other
24
Process A Process B
Operating
System
msg msg msg msg msg msg msg msg msg msg msg
msg msg msg msg
photo: theilr
http://www.flickr.com/photos/theilr/4283377543/ 25
UNIX Pipes
#include <unistd.h>
int pipe(int fildes[2]);
" Anything can be written to the pipe, and read from the other end
" Data is received in the order it was sent
" OS enforces mutual exclusion: only one process at a time
" Accessed by a file descriptor, like an ordinary file
" Processes sharing the pipe must have same parent process
Returns a pair of file descriptors
#
26
Pipe example
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
...
}
27
Pipe example
int main(void) {
int pfds[2];
char buf[30];
pfds[0]: read end of pipe
pfds[1]: write end of pipe
pipe(pfds);
if (!fork()) {
printf(" CHILD: writing to pipe\n");
write(pfds[1], "test", 5);
printf(" CHILD: exiting\n");
} else {
printf("PARENT: reading from pipe\n");
read(pfds[0], buf, 5);
printf("PARENT: read \"%s\"\n", buf);
wait(NULL);
}
return 0;
}
28
A pipe dream
ls | wc -l
29
Duplicating a file descriptor
#include <unistd.h>
Parameters:
• oldfd: the open file descriptor to be duplicated
30
Duplicating a file descriptor
#include <unistd.h>
Returns:
• Return value ≥ 0 : Success. Returns new file descriptor
• Return value = -1: Error. Check value of errno
Parameters:
• oldfd: the open file descriptor to be duplicated
31
Pipe dream come true: ls | wc –l
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
int pfds[2];
pipe(pfds);
if (!fork()) {
???
} else {
???
}
return 0;
}
Run demo
32