Advanced Linux Programming 5
5 types of interprocess communication
- Shared memory permits processes to communicate by simply reading and writing to a specified memory location.
- Mapped memory is similar to shared memory, except that it is associated with a file in the filesystem.
- Pipes permit sequential communication from one process to a related process.
- FIFOs are similar to pipes, except that unrelated processes can communicate, because the pipe is given a name in the filesystem.
- Sockets support communication between unrelated processes even on different computers.
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main(void) {
int segment_id;
char* shared_memory;
struct shmid_ds shmbuf;
size_t segment_size;
const int shared_seg_size = 0x6400;
/** Allocate */
segment_id = shmget(IPC_PRIVATE, shared_seg_size, IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR);
/** Attach */
shared_memory = (char*) shmat(segment_id, 0, 0);
printf("Shared memory address: %p\n", shared_memory);
/** Determine size. */
shmctl(segment_id, IPC_STAT, &shmbuf);
segment_size = shmbuf.shm_segsz;
printf("Shared memory size: %d\n", segment_size);
/** Write data to shared memory */
sprintf(shared_memory, "Hello, Shared memory!");
/** Detach */
shmdt(shared_memory);
/** Reattach shared memory at a different address. */
shared_memory = (char*) shmat(segment_id, (void*)0x5000000, 0);
printf("Shared memory address: %p\n", shared_memory);
printf("Read data: %s\n", shared_memory);
shmdt(shared_memory);
/** Dellocate */
shmctl(segment_id, IPC_RMID, 0);
return 0;
}Mapped Memory - Fast access to files#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#define FILE_LENGTH 0x100
void mmap_write(const char* file_name, const size_t size, void* data) {
int fd;
void* file_memory;
/** Prepare a file */
fd = open(file_name, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
lseek(fd, size+1,SEEK_SET);
write(fd, "", 1);
lseek(fd, 0, SEEK_SET);
/** Create memory mapping. */
file_memory = mmap(0, size, PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
/** Write data */
sprintf((char*) file_memory, "%s\n", (char*) data);
/** Release the memory */
munmap(file_memory, size);
}
void mmap_read(const char* file_name, const size_t size) {
int fd;
void* file_memory;
char buffer[size];
/** Prepare a file */
fd = open(file_name, O_RDWR, S_IRUSR|S_IWUSR);
/** Create memory mapping. */
file_memory = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
/** Output to console */
sprintf(buffer, "%s", (char*)file_memory);
printf(buffer);
/** Release the memory */
munmap(file_memory, size);
}
/** Just a test */
int main(int argc, char* argv[]) {
mmap_write(argv[1], FILE_LENGTH, argv[2]);
mmap_read(argv[1], FILE_LENGTH);
return 0;
}tom@debian:~/workspace/adl/Debug$ ./adl t.txt SomethingReallyFunPipes - unidirectional communication device and automatically synchronize
SomethingReallyFun
tom@debian:~/workspace/adl/Debug$ cat t.txt
SomethingReallyFun
tom@debian:~/workspace/adl/Debug$
int pipe_fds[2]; int read_fd; int write_fd; pipe (pipe_fds); read_fd = pipe_fds[0]; write_fd = pipe_fds[1];
标签: Linux


0 条评论:
发表评论
订阅 帖子评论 [Atom]
<< 主页