LINUX 下,编程自学五:msg,mem,pipe通信
来源:互联网 发布:淘宝商品制作 编辑:程序博客网 时间:2024/06/14 09:37
msg.c
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/ipc.h>#include<sys/shm.h>#include<sys/types.h>int main(int argc,char *argv[]){int shmsid;pid_tpid;key_tkey;char*addr;//creat shmkey = IPC_PRIVATE;shmsid = shmget(key,1024,IPC_CREAT|0660);if(shmsid < 0){perror("shmget");return -1;}addr = (char *)shmat(shmsid,NULL,0);/*if(addr = (char*)(-1)){perror("shmat");return -1;}*/printf("create share memeory add :%08x\n",addr);strcpy(addr,"this is share memory");pid = fork();if(pid < 0){perror("fork");return -1;}if(pid == 0){printf("here is child process,read shm:%s\n",addr);}else{wait(NULL);printf("here is parent process,read shm:%s\n",addr);system("ipcs -m");//delete shmif(shmdt(addr) < 0){perror("shmdt");return -1;}if(shmctl(shmsid,IPC_RMID,NULL) < 0){perror("shmctl");return -1;}printf("delete sucess\n");}return 0;}pipe.c
#include<stdio.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#define MAX_SIZE 128int main(int argc,char *argv[]){intfd[2];pid_tpid;charmessage[MAX_SIZE];memset(message,0,sizeof(message));if(pipe(fd) < 0){perror("pipe");return -1;}pid = fork();if(pid < 0){perror("fork");return -1;}if(pid == 0){close(fd[0]);printf("Here is Child Process,wile be written\n");write(fd[1],"I'm child!!",15);}else{close(fd[1]);printf("Here is Parent Process ,wile be read\n");read(fd[0],message,15);printf("message = %s\n",message);}return 0;}
sharemem.c
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/ipc.h>#include<sys/shm.h>#include<sys/types.h>int main(int argc,char *argv[]){int shmsid;pid_tpid;key_tkey;char*addr;//creat shmkey = IPC_PRIVATE;shmsid = shmget(key,1024,IPC_CREAT|0660);if(shmsid < 0){perror("shmget");return -1;}addr = (char *)shmat(shmsid,NULL,0);/*if(addr = (char*)(-1)){perror("shmat");return -1;}*/printf("create share memeory add :%08x\n",addr);strcpy(addr,"this is share memory");pid = fork();if(pid < 0){perror("fork");return -1;}if(pid == 0){printf("here is child process,read shm:%s\n",addr);}else{wait(NULL);printf("here is parent process,read shm:%s\n",addr);system("ipcs -m");//delete shmif(shmdt(addr) < 0){perror("shmdt");return -1;}if(shmctl(shmsid,IPC_RMID,NULL) < 0){perror("shmctl");return -1;}printf("delete sucess\n");}return 0;}
0 0
- LINUX 下,编程自学五:msg,mem,pipe通信
- Linux下pipe通信实例
- linux系统编程:进程间通信-pipe
- LINUX 下,编程自学一:单一C/S 通信
- Linux下的进程间通信PIPE
- 【Linux系统编程】进程间通信--无名管道(pipe)
- Linux编程 进程间通信------无名管道(PIPE)
- linux编程---进程间通信---PIPE---无名管道
- Linux编程之pipe管道通信(1)
- Linux编程之pipe管道通信(2)
- Linux下进程间通信:管道-pipe函数
- Linux下进程间通信:管道-pipe函数
- Linux 下进程间通信机制(二) Pipe和Fifo
- Linux下进程间通信:管道-pipe函数
- Linux下进程间通信:管道-pipe函数
- 浅谈linux系统下的进程通信之管道pipe
- linux下/dev/mem分析
- LINUX 下,编程自学四:UDP通信(connect用于UDP)
- 制作1像素表格
- [慢查优化]建索引时注意字段选择性 & 范围查询注意组合索引的字段顺序
- shell中用expr命令做字符串截取操作
- UVA-10881 - Piotr's Ants-2014年4月20日161403
- 全面剖析XMLHttpRequest对象
- LINUX 下,编程自学五:msg,mem,pipe通信
- javascript基础篇--function类型(上)
- struct class
- 作业调度方案——模拟(水)
- 转java实现数组全排列
- CC_2_链表
- iBatis SqlMap的配置总结
- jvm内存管理与监控
- java 解析excel文件