

#include
int pipe(int fd[2]); // 返回值:若成功返回0,失败返回-1


#include
#include
int main(){
int fd[2]; // 两个文件描述符
pid_t pid;
char buff[20];
if(pipe(fd) < 0) // 创建管道
printf("Create Pipe Error!\n");
if((pid = fork()) < 0) // 创建子进程
printf("Fork Error!\n");
else if(pid > 0){ // 父进程
close(fd[0]); // 关闭读端
write(fd[1],"hello world\n",12);
}else{ //子进程
close(fd[1]); // 关闭写端
read(fd[0],buff,20);
printf("%s",buff);
}
return 0;
}

#include
#include //标准c库 pipe fork
#include
#include //标准c库 exit
#include
#include //wait
int main(){
int fd[2];
pid_t pid;
char buf[128];
// int pipe(int pipefd[2]);
if(pipe(fd) == -1){ //创建管道
printf("creat pipe failed\n");
}
pid = fork(); //创建子进程
if(pid<0){
printf("creat child failed\n");
}
else if(pid > 0){ //父进程
sleep(3); //休眠确保子进程先运行,到read时阻塞,3s后父进程往下走
printf("this is father\n");
close(fd[0]); //关闭读端
write(fd[1],"hello from father",strlen("hello form father"));
wait(NULL);
}else{ //子进程
printf("this is child\n");
close(fd[1]); //关闭写端
read(fd[0],buf,128);
printf("read from father: %s\n",buf);
exit(0);
}
return 0;
}

#include
// 返回值:成功返回0,出错返回-1
int mkfifo(const char *pathname, mode_t mode);
#include
#include //mkfifo的
int main(){
//int mkfifo(const char *pathname, mode_t mode);
mkfifo("./file",0600);//创建管道,权限为可读可写
return 0;
}
#include
#include
#include
int main(){
//int mkfifo(const char *pathname, mode_t mode);
int ret = mkfifo("./file",0600);
if(ret == 0){
printf("mkfifo suscceess\n");
}
if(ret == -1){
printf("mkfifo failed\n");
perror("why");
}
return 0;
}

#include
#include
#include
#include
int main(){
//int mkfifo(const char *pathname, mode_t mode);
if( (mkfifo("./file",0600) == -1) && errno==EEXIST){
printf("mkfifo failed\n");
perror("why");
}
else{
if(errno= =EEXIST){
printf("file exists\n");
}else
printf("mkfifo suscceess\n");
}
return 0;
}

#include
#include
#include
#include
int main(){
//int mkfifo(const char *pathname, mode_t mode);
if( (mkfifo("./file",0600) == -1) && errno!=EEXIST){
//只有在错误不是 EEXIST 时才打印错误消息
perror("mkfifo failed");
}
return 0;
}
#include
#include
#include
#include
#include
int main(){
//int mkfifo(const char *pathname, mode_t mode);
if( (mkfifo("./file",0600) == -1) && errno!=EEXIST){
printf("mkfifo failuer\n");
perror("why");
}
int fd = open("./file",O_RDONLY);//若没有指定O_NONBLOCK(默认)
printf("open success\n");//只读 open 会阻塞到某个其他进程为写而打开此 FIFO
//close(fd);
return 0;
}
#include
#include
#include
#include
#include
int main(){
int fd = open("./file",O_WRONLY);//若没有指定O_NONBLOCK(默认)
printf("write open success\n");//只写 open 会阻塞到某个其他进程为读而打开它
//close(fd);
return 0;
}
#include
#include
#include
#include
#include // O_WRONLY open
#include //strlen
#include //write
//int mkfifo(const char *pathname, mode_t mode);
int main(){
char buf[30] = {0};
//int nread = 0;
int cnt = 0;
if( (mkfifo("./file",0600) == -1) && errno!=EEXIST){//创建fifo管道
printf("mkfifo failed\n");
perror("why");
}
int fd = open("./file",O_RDONLY);
printf("open success\n");
while(1){
int nread = read(fd,buf,30);//读内容
printf("read %d byte from fifo,context:%s\n",nread,buf);
cnt++;
if(cnt == 5){
break;
}
}
close(fd);
return 0;
}
#include
#include
#include
#include
#include // O_WRONLY open
#include //strlen
#include //write
//int mkfifo(const char *pathname, mode_t mode);
int main(){
int cnt = 0;
char *str = "I'm Message from fifo_write";
int fd = open("./file",O_WRONLY);
printf("write open success\n");
while(1){
write(fd, str, strlen(str));//写内容
cnt++;
sleep(1);
if(cnt == 5){
break;
}
}
close(fd);
return 0;
}




#include
// 创建或打开消息队列:成功返回队列ID,失败返回-1
int msgget(key_t key, int flag);//通过key(非负数 索引值)找到对应消息队列,打开队列的方式//返回值是消息队列的标识符
// 添加消息:成功返回0,失败返回-1
int msgsnd(int msqid, const void *ptr, size_t size, int flag);//flag为0是默认阻塞的方式添加消息
// 读取消息:成功返回消息数据的长度,失败返回-1
int msgrcv(int msqid, void *ptr, size_t size, long type,int flag);//long type消息类型,用于找到对应消息,flag为0是默认阻塞的方式读取消息
// 控制消息队列:成功返回0,失败返回-1
int msgctl(int msqid, int cmd, struct msqid_ds *buf);


#include
#include
#include
#include
#include
// int msgget(key_t key, int msgflg);
// int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
// ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
struct msgbuf{
long mtype; /* message type, must be > 0 */
char mtext[256]; /* message data */
};
int main(){
struct msgbuf readBuf;
int msgId = msgget(0x1234,IPC_CREAT|0777);//创建/打开消息队列,通过key找到对应消息队列,可读可写可执行
if(msgId == -1 ){
printf("get que failed\n");//创建/打开失败
}
msgrcv(msgId, &readBuf,sizeof(readBuf.mtext),888,0);//读取消息
printf("read from que:%s\n",readBuf.mtext);
struct msgbuf sendBuf = {988,"thank you for reach"};
msgsnd(msgId,&sendBuf,strlen(sendBuf.mtext),0);//添加消息
return 0;
}
#include
#include
#include
#include
#include
// int msgget(key_t key, int msgflg);
// int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
// ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[256]; /* message data */
};
int main(){
struct msgbuf sendBuf = {888,"this is message from quen"};
int msgId = msgget(0x1234, IPC_CREAT|0777);//创建/打开消息队列,通过key找到对应消息队列,可读可写可执行
if(msgId == -1 ){
printf("get que failed\n");
}
msgsnd(msgId,&sendBuf,strlen(sendBuf.mtext),0);//添加消息
printf("send over\n");
struct msgbuf readBuf;
msgrcv(msgId,&readBuf,sizeof(readBuf.mtext),988,0);//读取消息
printf("reaturn from get:%s\n",readBuf.mtext);
return 0;
}

#include
#include
#include
#include
#include
// int msgget(key_t key, int msgflg);
// int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
// ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
//int msgctl(int msqid, int cmd, struct msqid_ds *buf);
struct msgbuf{
long mtype; /* message type, must be > 0 */
char mtext[256]; /* message data */
};
int main(){
struct msgbuf readBuf;
key_t key;
key = ftok(".",'m');
printf("key=%x\n",key);
int msgId = msgget(key,IPC_CREAT|0777);//创建/打开消息队列,通过key找到对应消息队列,可读可写可执行
if(msgId == -1 ){
printf("get que failuer\n");
}
//memset(&readBuf,0,sizeof(struct msgbuf));
msgrcv(msgId, &readBuf,sizeof(readBuf.mtext),888,0);//读取消息
printf("read from que:%s\n",readBuf.mtext);
struct msgbuf sendBuf = {988,"thank you for reach"};
msgsnd(msgId,&sendBuf,strlen(sendBuf.mtext),0);//添加消息
msgctl(msgId,IPC_RMID,NULL);//控制消息队列,将消息队列从内核中删除
return 0;
}
#include
#include
#include
#include
#include
// int msgget(key_t key, int msgflg);
// int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
// ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
//int msgctl(int msqid, int cmd, struct msqid_ds *buf);
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[256]; /* message data */
};
int main(){
struct msgbuf sendBuf = {888,"this is message from quen"};
key_t key;
key = ftok(".",'m');
printf("key=%x\n",key);
int msgId = msgget(key, IPC_CREAT|0777);//创建/打开消息队列,通过key找到对应消息队列,可读可写可执行
if(msgId == -1 ){
printf("get que failuer\n");
}
msgsnd(msgId,&sendBuf,strlen(sendBuf.mtext),0);//添加消息
printf("send over\n");
struct msgbuf readBuf;
//memset(&readBuf,0,sizeof(struct msgbuf));
msgrcv(msgId,&readBuf,sizeof(readBuf.mtext),988,0);//读取消息
printf("reaturn from get:%s\n",readBuf.mtext);
msgctl(msgId,IPC_RMID,NULL);//控制消息队列,将消息队列从内核中删除
return 0;
}