Ipcs |
shmA.c |
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<signal.h> #include<sys/shm.h> #include<sys/ipc.h> //ftok()
main() { key_t key; int shmid; //1 创建共享内存shmget key = ftok(".", 255); if(key == -1) { printf("ftok error:%m\n"), exit(-1); } shmid = shmget(key, 4, IPC_CREAT|IPC_EXCL|0666); if(shmid == -1) { printf("shmget error:%m\n"), exit(-1); } //2 挂载共享内存shmat //3 访问共享内存 //4 卸载共享内存shmdt //5 删除共享内存shctl } |
运行两次 |
Ipcrm –m shmid编号 |
再次运行,产生的shmid是不同的 |
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<signal.h> #include<sys/shm.h> #include<sys/ipc.h> //ftok()
main() { key_t key; int shmid; //1 创建共享内存shmget key = ftok(".", 255); if(key == -1) { printf("ftok error:%m\n"), exit(-1); } shmid = shmget(key, 4, IPC_CREAT|IPC_EXCL|0666); if(shmid == -1) { printf("shmget error:%m\n"), exit(-1); } //2 挂载共享内存shmat //3 访问共享内存 //4 卸载共享内存shmdt //5 删除共享内存shctl shmctl(shmid, IPC_RMID, 0); } |
ShmA2.c |
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<signal.h> #include<sys/shm.h> #include<sys/ipc.h> //ftok()
main() { key_t key; int shmid; int *p; int i =0; //1 创建共享内存shmget key = ftok(".", 255); if(key == -1) { printf("ftok error:%m\n"), exit(-1); } shmid = shmget(key, 4, IPC_CREAT|IPC_EXCL|0666); if(shmid == -1) { printf("shmget error:%m\n"), exit(-1); } //2 挂载共享内存shmat p = shmat(shmid, 0,0); if(p == (int *)-1) { printf("shmat error: %m\n"), exit(-1); } //3 访问共享内存 while(1) {
*p = i; i ++; sleep(1); } //4 卸载共享内存shmdt shmdt(p); //5 删除共享内存shctl shmctl(shmid, IPC_RMID, 0); } |
shmB2.c |
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<signal.h> #include<sys/shm.h> #include<sys/ipc.h> //ftok()
main() { key_t key; int shmid; int *p; //1 创建共享内存shmget key = ftok(".", 255); if(key == -1) { printf("ftok error:%m\n"), exit(-1); } shmid = shmget(key, 4,0); if(shmid == -1) { printf("shmget error:%m\n"), exit(-1); } //2 挂载共享内存shmat p = shmat(shmid, 0,0); if(p = (int *)-1) { printf("shmat error: %m\n"), exit(-1); } //3 访问共享内存 while(1) { sleep(1); printf("%d\n", *p); } //4 卸载共享内存shmdt shmdt(p); } |
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<signal.h> #include<sys/shm.h> #include<sys/ipc.h> //ftok() key_t key; int shmid; int *p; int i =0;
void deal(int s) { if(s == SIGINT) { //4 卸载共享内存shmdt shmdt(p); //5 删除共享内存shctl shmctl(shmid, IPC_RMID, 0); exit(0); } } main() { signal(SIGINT, deal); //1 创建共享内存shmget key = ftok(".", 255); if(key == -1) { printf("ftok error:%m\n"), exit(-1); } shmid = shmget(key, 4, IPC_CREAT|IPC_EXCL|0666); if(shmid == -1) { printf("shmget error:%m\n"), exit(-1); } //2 挂载共享内存shmat p = shmat(shmid, 0,0); if(p == (int *)-1) { printf("shmat error: %m\n"), exit(-1); } //3 访问共享内存 while(1) {
*p = i; i ++; sleep(1); }
} |
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<signal.h> #include<sys/shm.h> #include<sys/ipc.h> //ftok()
key_t key; int shmid; int *p;
void deal(int s) { if(s == 2) { //4 卸载共享内存shmdt shmdt(p); exit(0); } }
main() { signal(SIGINT, deal); //1 创建共享内存shmget key = ftok(".", 255); if(key == -1) { printf("ftok error:%m\n"), exit(-1); } shmid = shmget(key, 4,0); if(shmid == -1) { printf("shmget error:%m\n"), exit(-1); } //2 挂载共享内存shmat p = shmat(shmid, 0,0); if(p = (int *)-1) { printf("shmat error: %m\n"), exit(-1); } //3 访问共享内存 while(1) { sleep(1); printf("%d\n", *p); } } |
msgA.c |
#include<unistd.h> #include<sys/ipc.h> #include<sys/msg.h> #include<stdio.h> #include<stdlib.h> #include<string.h> struct msbuf { long type; char data[32]; };
main() { key_t key; int msgid; int i; //1 创建消息队列 key = ftok(".", 200); if(key == -1) { printf("ftok error %m"), exit(-1); } msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666); if(msgid == -1) { printf("msgid error %m"), exit(-1); }
//2 构造消息 struct msgbuf msg;
//3 发送消息 for(i = 1; i <= 10; i ++) { bzero(msg.data, sizeof(msg.data)); msg.type = 1; sprintf(msg.data, "Message:%d", i); msgsnd(msgid, &msg, sizeof(msg.data),0); } for(i = 1; i <= 10; i ++) { bzero(msg.data, sizeof(msg.data)); msg.type = 2; sprintf(msg.data, "MessageII":%d, i); msgsnd(msgid, &msg, sizeof(msg.data),0); } //4 删除队列 //msgctl(msgid, IPC_RMID, 0); }
|
#include<unistd.h> #include<sys/ipc.h> #include<sys/msg.h> #include<stdio.h> #include<stdlib.h> #include<string.h> struct msbuf { long type; char data[32]; };
main() { key_t key; int msgid; int i; //1 得到消息队列 key = ftok(".", 200); if(key == -1) { printf("ftok error %m"), exit(-1); } msgid = msgget(key, 0); if(msgid == -1) { printf("msgid error %m"), exit(-1); }
//2 构造消息 struct msgbuf msg;
//3 接收消息 while(1) { bzero(&msg, sizeof(msg)); msg.type = 2; msgrc(msgid, &msg, sizeof(msg.data), 1, 0); printf("%s", msg.data); } }
|
|
socketA.c |
#include<sys/socket.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> main() { int fd; //1 建立socket fd = socket(AF_UNIX, SOCK_DGRAM, 0); if(fd == -1) { printf("socket error: %m\n"), exit(-1); } printf("%d : %d\n", getpid(), fd); while(1); } |
#include<sys/socket.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<linux/un.h> main() { int fd; int r; char buf[200]; //1 建立socket fd = socket(AF_UNIX, SOCK_DGRAM, 0); if(fd == -1) { printf("socket error: %m\n"), exit(-1); } printf("socket success\n"); //2 构造本地文件地址 struct sockaddr_un addr={0}; addr.sun_family=AF_UNIX; memcpy(addr.sun_path, "my.socket", strlen("my.socket")); //3 把socket绑定在地址上 r = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); if(r == -1) { printf("bind error: %m\n"), exit(-1); } printf("bind success\n");
//4 接收数据 bzero(buf, sizeof(buf)); r = read(fd, buf, sizeof(buf); buf[r] = 0; printf("%s\n", buf);
//5 关闭 close(fd);
//6 删除socket文件 //unlink("my.sock"); } |
#include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<linux/un.h> #include<string.h> #include<unistd.h>
main() { int fd; int r; struct sockaddr_un addr={0}; //1 建立socket fd = socket(Af_UNIX, SOCK_DGRAM, 0); //2 连接到指定的地址 add.sun_family = AF_UNIX; memcpy(add.sun_path, "my.sock", strlen("my.sock")); r = connect(fd, (struct sockaddr*)&addr, sizeof(addr)); //3 发送数据 write(fd, "hello", strlen("hello")); //4 关闭 close(fd);
} |
ipA.c |
#include<sys/socket.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> //1 #include<netinet/in.h> //socket 结构体 #include<arpa/inet.h> main() { int fd; int r; char buf[200]; //1 建立socket //2 fd = socket(AF_INET,SOCK_DGRAM, 0); if(fd == -1) { printf("socket error: %m\n"), exit(-1); } printf("socket success\n"); //2 构造本地文件地址 struct sockaddr_in addr={0}; //3 addr.sin_family=AF_INET; addr.sin_port = htons(9999); addr.sin_addr.s_addr = inet_addr("192,168.1.66"); //3 把socket绑定在地址上 r = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); if(r == -1) { printf("bind error: %m\n"), exit(-1); } printf("bind success\n");
//4 接收数据 while(1) { bzero(buf, sizeof(buf)); r = read(fd, buf, sizeof(buf)); buf[r] = 0; printf("%s\n", buf); } //5 关闭 close(fd);
//6 删除socket文件 unlink("my.sock");
} |
ipB.c |
#include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<string.h> #include<unistd.h> //1 #include<netinet/in.h> #include<arpa/inet>
main() { int fd; int r; //2 struct sockaddr_in addr={0}; //1 建立socket //3 fd = socket(Af_INET, SOCK_DGRAM, 0); //2 连接到指定的地址 //4 addr.sin_family = AF_INET; addr.sin_port = htons(9999); addr.sin_addr.s_addr = inet_addr("192,168.1.66"); r = connect(fd, (struct sockaddr*)&addr, sizeof(addr)); //3 发送数据 write(fd, "hello", strlen("hello")); //4 关闭 close(fd);
} |