标签:style blog class c code java
1. Unix IPC(InterProcess Communication)
#include <stdio.h> FILE* popen(const char* cmdstring, const char* type); int pclose(FILE* fp);
函数popen先执行fork,然后调用exec以执行cmdstring,并且返回一个标准I/O文件指针
1 #include <unistd.h> 2 #include <stdio.h> 3 #include <errno.h> 4 #include <sys/wait.h> 5 #include <fcntl.h> 6 7 /* pointer to array allocated at run-time */ 8 static pid_t* childpid = NULL; 9 10 /* from our open_max() */ 11 static int maxfd; 12 13 FILE* my_popen(const char* cmdstring, const char* type) 14 { 15 int pfd[2]; 16 pid_t pid; 17 18 /* only allow type = "r" or "w" */ 19 if ((type[0] != ‘r‘ && type[0] != ‘w‘) || type[1] != 0) { 20 errno = EINVAL; 21 return NULL; 22 } 23 childpid = (pid_t*)calloc(maxfd, sizeof(pid_t)); 24 if (childpid == NULL) { 25 return NULL; 26 } 27 } 28 29 if (pipe(pfd) < 0) { 30 return NULL; 31 } 32 33 if ((pid = fork()) < 0) { 34 return NULL; 35 } else if (pid == 0) { /* child */ 36 if (*type == ‘r‘) { 37 close(pfd[0]); 38 if (pfd[1] != STDOUT_FILENO) { 39 dup2(pfd[1], STDOUT_FILENO); 40 close(pfd[1]); 41 } 42 } else { 43 close(pfd[1]); 44 if (pfd[0] != STDIN_FILENO) { 45 dup2(pfd[0], STDIN_FILENO); 46 close(pfd[0]); 47 } 48 } 49 50 /* close all fds in childpid[] */ 51 for (int i = 0; i < maxfd; ++i) { 52 if (childpid[i] > 0) { 53 close(i); 54 } 55 } 56 57 } 58 59 /* parent continue */ 60 FILE* fp; 61 if (*type == ‘r‘) { 62 close(pfd[1]); 63 if ((fp = fdopen(pfd[0], type)) == NULL) { 64 return NULL; 65 } 66 } else { 67 close(pfd[0]); 68 if ((fp = fdopen(pfd[1], type)) == NULL) { 69 return NULL; 70 close(pfd[0]); 71 if ((fp = fdopen(pfd[1], type)) == NULL) { 72 return NULL; 73 } 74 } 75 76 childpid[fileno(fp)] = pid; 77 return fp; 78 }
#include <sys/shm.h> /* 获得共享存储标识符 */ int shmget(key_t key, size_t size, int flag); /* 对共享存储区执行多种操作 */ int shmctl(int shmid, int cmd, struct shmid_ds* buf); /* 进程将共享存储区连接到它的地址空间中 */ void* shmat(int shmid, const void* addr, int flag);
如果addr为0,则此段连接到内核选择的第一个可用地址上。一般将addr指定为0,以便由内核选择地址
打印各种不同类型的数据所存放的位置:
1 #include <unistd.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <sys/shm.h> 5 6 #define ARRAY_SIZE 40000 7 #define MALLOC_SIZE 100000 8 #define SHM_SIZE 100000 9 #define SHM_MODE 0600 /* user read/write */ 10 11 char array[ARRAY_SIZE]; /* uninitialized data = bss */ 12 13 int main(int argc, char* argv[]) 14 { 15 int shmid; 16 char* ptr = NULL; 17 char* shmptr = NULL; 18 19 fprintf(stdout, "array[] from %p to %p\n", array, array + ARRAY_SIZE); 20 fprintf(stdout, "stack around %p\n", &shmid); 21 ptr = (char*)malloc(MALLOC_SIZE); 22 if (ptr == NULL) { 23 fprintf(stderr, "malloc error\n"); 24 } 25 26 fprintf(stdout, "malloc from %p to %p\n", ptr, ptr + MALLOC_SIZE); 27 28 shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE); 29 if (shmid < 0) { 30 fprintf(stderr, "shmget error\n"); 31 } 32 33 shmptr = shmat(shmid, 0, 0); 34 if (shmptr == (void*)-1) { 35 fprintf(stderr, "shmat error\n"); 36 } 37 38 fprintf(stdout, "shared memory from %p to %p\n", shmptr, shmptr + SHM_SIZE); 39 if (shmctl(shmid, IPC_RMID, 0) < 0) { 40 fprintf(stderr, "shmctl error\n"); 41 } 42 free(ptr); 43 return 0; 44 }
在基于Intel的Linux系统上运行此程序,其输出如下:
APUE 学习笔记(十) 高级I/O,布布扣,bubuko.com
标签:style blog class c code java
原文地址:http://www.cnblogs.com/wwwjieo0/p/3739107.html