#include <unistd.h> int pipe(int filedes[2]);
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> int main(void) { int result = -1; /* 文件描述符,字符个数 */ int fd[2], nbytes; /* PID值 */ pid_t pid; /* 文件描述符1用于写 文件描述符0用于读 */ int * write_fd = &fd[1]; int * read_fd = &fd[0]; /* 缓冲区 */ char buf[1024]; char string[] = "hello,linux."; /* 创建管道 */ result = pipe(fd); if(result == -1) { printf("创建管道失败\n"); return -1; } pid = fork(); if(-1 == pid) { printf("fork进程失败\n"); return -1; } if(0 == pid) { close(*read_fd); result = write(*write_fd, string, strlen(string)); if(0 < result) { printf("write %d bytes.\n", result); } close(*write_fd); } else if(fd > 0) { close(*write_fd); read(*read_fd, buf, 1024); printf("recv: %s\n", buf); close(*read_fd); } return 0; }
mkfifo /ipc/namedfifo
#include <sys/types.h> #include <sys/stat.h> int mkfifo(const char * pathname, mode_t mode);
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> #define BUF_SIZ (1024) #define FIFO_NAME "/tmp/fifo" int main(void) { char buf[BUF_SIZ];/* buffer */ int fd; /* file descriptor */ unlink(FIFO_NAME); if(0 == mkfifo(FIFO_NAME, 0777)) { printf("create fifo successfully.\n"); } else { printf("create fifo failed.\n"); return -1; } if(fork() > 0) { fd = open(FIFO_NAME, O_WRONLY); if(fd > 0) { printf("Process %d open %s successfully.\n", getpid(), FIFO_NAME); write(fd, "hello,world", strlen("hello,world")); } } else { fd = open(FIFO_NAME, O_RDONLY); if(fd > 0) { printf("Process %d open %s successfully.\n", getpid(), FIFO_NAME); read(fd, buf, BUF_SIZ); printf("recv:%s\n", buf); } } close(fd); }
struct msgbuf { long int mtype; /* type of received/sent message */ char mtext[1]; /* text of the message */ };
struct msqid_ds { struct ipc_perm msg_perm; struct msg *msg_first; /* first message on queue,unused */ struct msg *msg_last; /* last message in queue,unused */ __kernel_time_t msg_stime; /* last msgsnd time */ __kernel_time_t msg_rtime; /* last msgrcv time */ __kernel_time_t msg_ctime; /* last change time */ unsigned long msg_lcbytes; /* Reuse junk fields for 32 bit */ unsigned long msg_lqbytes; /* ditto */ unsigned short msg_cbytes; /* current number of bytes on queue */ unsigned short msg_qnum; /* number of messages in queue */ unsigned short msg_qbytes; /* max number of bytes on queue */ __kernel_ipc_pid_t msg_lspid; /* pid of last msgsnd */ __kernel_ipc_pid_t msg_lrpid; /* last receive pid */ };
/* Obsolete, used only for backwards compatibility and libc5 compiles */ struct ipc_perm { __kernel_key_t key;/* 用于区分消息队列 */ __kernel_uid_t uid;/* 消息队列用户的ID号 */ __kernel_gid_t gid;/* 消息队列用户组的ID号 */ __kernel_uid_t cuid;/* 消息队列创建者的UID号 */ __kernel_gid_t cgid; __kernel_mode_t mode; /* 权限,用户控制读写,0666,可以对消息进行读写操作 */ unsigned short seq; };
#include <sys/types.h> #include <sys/ipc.h> key_t ftok(const char * path_name, int proj_id);
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/msg.h> #include <sys/stat.h> #include <sys/ipc.h> #define BUF_SZ (10) struct msgmbuf { int mtype; char mtext[BUF_SZ]; }; int main(void) { int ret = 1; int msg_flags , msg_id; key_t key; struct msqid_ds msg_info; struct msgmbuf msg_buf; int msg_sflags, msg_rflags; char * msg_path = "."; /* 1.使用ftok创建键值 */ key = ftok(msg_path, ‘b‘); if(key != -1) printf("Create Key Successfully.\n"); else { printf("Create Key Failed.\n"); return -1; } /* 2.使用msgget获取消息队列标识符*/ msg_flags = IPC_CREAT; msg_id = msgget(key, msg_flags | 0666); if(-1 != msg_id) { printf("msgget succeed.\n"); } else { printf("msgget failed.\n"); return -1; } /* 3.使用msgsnd发送消息 */ msg_flags = IPC_NOWAIT; msg_buf.mtype = 10; memcpy(msg_buf.mtext, "hello,linux kernel.", strlen("hello,linux kernel.")); ret = msgsnd(msg_id, &msg_buf, 10, msg_flags); if(ret != -1) { printf("msgsnd succeed.\n"); } /* 4.使用msgrcv接收消息 */ msg_flags = IPC_NOWAIT; ret = msgrcv(msg_id, &msg_buf, 10, 10, msg_flags); if(ret != -1) { printf("msgrcv succeed.\n"); } /* 5.删除队列 */ ret = msgctl(msg_id, IPC_RMID, NULL); if(ret != -1) { printf("msgctl succeed.\n"); } }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/msg.h> #include <sys/stat.h> #include <sys/ipc.h> #include <sys/sem.h> static char msg[] = "hello,shared memory."; int main() { key_t key; int shmid; char i, * shms, * shmc; struct semid_ds buf; int value = 0; char buffer[80]; pid_t p; key = ftok(".", ‘a‘); shmid = shmget(key, 1024, IPC_CREAT | 0604); p = fork(); if(p > 0) { shms = (char *)shmat(shmid, 0, 0); memcpy(shms, msg, strlen(msg)); shmdt(shms); } else { sleep(10); shmc = (char*)shmat(shmid, 0, 0); printf("shared memory content: %s\n", shmc); shmdt(shmc); } return 0; }
原文:http://blog.csdn.net/freeitdog/article/details/18942249