#include <unistd.h>
int pipe(int pipefd[2]);#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int pipefd[2];
pid_t cpid;
char buf[128];
int readlen;
if (argc != 2) {
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
return -1;
}
if (pipe(pipefd) < 0) {
fprintf(stderr, "pipe: %s\n", strerror(errno));
return -1;
}
cpid = fork();
if (cpid < 0) {
fprintf(stderr, "fork: %s\n", strerror(errno));
return -1;
}
if (0 == cpid) { /* 子进程 */
close(pipefd[1]); /* 子进程关闭写端 */
readlen = read(pipefd[0], buf, 128); //子进程阻塞在读上,等待父进程写
if (readlen < 0) {
fprintf(stderr, "read: %s\n", strerror(errno));
return -1;
}
write(STDOUT_FILENO, buf, readlen);
write(STDOUT_FILENO, "\n", 1);
close(pipefd[0]); //读完之后关闭读描述符
return 0;
} else { /* 父进程 */
close(pipefd[0]); /*父进程关闭没用的读端 */
sleep(2);
write(pipefd[1], argv[1], strlen(argv[1])); //父进程开始写
close(pipefd[1]); /* 父进程关闭写描述符 */
wait(NULL); /* 父进程等待子进程退出,回收子进程资源 */
return 0;
}
} #include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);pathname 为系统路径名,mode为文件权限位,类似open函数第二个参数。#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "slnipc.h"
int main(int argc, const char *argv[])
{
int rc;
int wr_fd, rd_fd;
char sendbuf[128];
char recvbuf[128];
rc = mkfifo(SLN_IPC_2SER_PATH, O_CREAT | O_EXCL); //建立服务进程读的fifo
if ((rc < 0 ) && (errno != EEXIST)) {
fprintf(stderr, "mkfifo: %s\n", strerror(errno));
return -1;
}
rc = mkfifo(SLN_IPC_2CLT_PATH, O_CREAT | O_EXCL); //建立服务进程写的fifo
if ((rc < 0 ) && (errno != EEXIST)) {
fprintf(stderr, "mkfifo: %s\n", strerror(errno));
return -1;
}
wr_fd = open(SLN_IPC_2CLT_PATH, O_RDWR, 0);
if (wr_fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
return -1;
}
rd_fd = open(SLN_IPC_2SER_PATH, O_RDWR, 0);
if (rd_fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
return -1;
}
for (;;) {
rc = read(rd_fd, recvbuf, sizeof(recvbuf)); //循环等待接受客户进程数据
if (rc < 0) {
fprintf(stderr, "read: %s\n", strerror(errno));
continue;
}
printf("server recv: %s\n", recvbuf);
snprintf(sendbuf, sizeof(sendbuf), "Hello, this is server!\n");
rc = write(wr_fd, sendbuf, strlen(sendbuf));
if (rc < 0) {
fprintf(stderr, "write: %s\n", strerror(errno));
continue;
}
}
close(wr_fd);
close(rd_fd);
return 0;
}#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "slnipc.h"
int main(int argc, const char *argv[])
{
int rc;
int rd_fd, wr_fd;
char recvbuf[128];
char sendbuf[128];
if (argc != 2) {
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
return -1;
}
snprintf(sendbuf, sizeof(sendbuf), "%s", argv[1]);
wr_fd = open(SLN_IPC_2SER_PATH, O_RDWR, 0);
if (wr_fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
return -1;
}
rd_fd = open(SLN_IPC_2CLT_PATH, O_RDWR, 0);
if (rd_fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
return -1;
}
rc = write(wr_fd, sendbuf, strlen(sendbuf));
if (rc < 0) {
fprintf(stderr, "write: %s\n", strerror(errno));
return -1;
}
rc = read(rd_fd, recvbuf, sizeof(recvbuf));
if (rc < 0) {
fprintf(stderr, "write: %s\n", strerror(errno));
return -1;
}
printf("client read: %s\n", recvbuf);
close(wr_fd);
close(rd_fd);
return 0;
}# ./server server recv: hi,this is fifo client # ./client "hi,this is fifo client" client read: Hello, this is server!
本节源码下载:
http://download.csdn.net/detail/gentleliu/8183027
原文地址:http://blog.csdn.net/shallnet/article/details/41346743