管道
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FILEPATH "/home/adce/mycode/vscode/unix_process/pipe/file/pipeFile.txt"
#define PATHSIZE 1024
void Client(int writefd, int readfd);
void Server(int writefd, int readfd);
int main(int argc, char **argv)
{
int pipe_client[2];
int pipe_server[2];
if (pipe(pipe_client) < 0 || pipe(pipe_server) < 0)
{
perror("pipe");
exit(-1);
}
pid_t server_pid;
if ((server_pid = fork()) < 0)
{
perror("fork");
exit(-1);
}
if (server_pid == 0)
{
close(pipe_client[1]);
close(pipe_server[0]);
Server(pipe_server[1], pipe_client[0]);
exit(0);
}
close(pipe_client[0]);
close(pipe_server[1]);
Client(pipe_client[1], pipe_server[0]);
waitpid(server_pid, NULL, 0);
exit(0);
}
void Client(int writefd, int readfd)
{
char pathbuf[PATHSIZE];
strncpy(pathbuf, FILEPATH, PATHSIZE);
size_t pathlen = strlen(pathbuf);
ssize_t writesize = write(writefd, pathbuf, pathlen);
if (writesize < pathlen)
{
fprintf(stderr, "write size less than pathlen.\n");
exit(-1);
}
ssize_t readsize;
char buf[1024];
printf("client begin read:\n");
while ((readsize = read(readfd, buf, 1024)) > 0)
{
write(STDOUT_FILENO, buf, readsize);
}
printf("client read over.\n");
exit(0);
}
void Server(int writefd, int readfd)
{
char buf[PATHSIZE];
ssize_t readsize;
if((readsize = read(readfd, buf, PATHSIZE)) < 0)
{
perror("read");
exit(-1);
}
int filefd;
filefd = open(buf, O_RDONLY);
if (filefd < 0)
{
fprintf(stderr, "open() failed.");
exit(-1);
}
while ((readsize = read(filefd, buf, PATHSIZE)) > 0)
{
write(writefd, buf, readsize);
}
write(writefd, "send over!\n", 12);
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILEPATH "/home/adce/mycode/vscode/unix_process/pipe/file/pipeFile.txt"
#define PATHSIZE 1024
int main(int argc, char **argv)
{
char command[PATHSIZE];
char buf[1024];
FILE *fp;
sprintf(command, "cat %s", FILEPATH);
fp = popen(command, "r");
int readsize;
while (fgets(buf, 1024, fp) != NULL)
{
printf(buf);
}
exit(0);
}
FIFO
两个无关系的进程之间通信。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
#define FIFOCLIENT "/home/adce/mycode/vscode/unix_process/fifo/file/fifo.client"
#define FIFOSEVER "/home/adce/mycode/vscode/unix_process/fifo/file/fifo.server"
#define FILEPATH "/home/adce/mycode/vscode/unix_process/fifo/file/fifoFile.txt"
#define PATHSIZE 1024
void Client(int writefd, int readfd);
int main(int argc, char **argv)
{
int readfd, writefd;
writefd = open(FIFOCLIENT, O_WRONLY, 0);
readfd = open(FIFOSEVER, O_RDONLY, 0);
if (readfd < 0 || writefd < 0)
{
perror("open");
exit(-1);
}
Client(writefd, readfd);
unlink(FIFOCLIENT);
unlink(FIFOSEVER);
exit(0);
}
void Client(int writefd, int readfd)
{
char pathbuf[PATHSIZE];
strncpy(pathbuf, FILEPATH, PATHSIZE);
size_t pathlen = strlen(pathbuf);
ssize_t writesize = write(writefd, pathbuf, pathlen);
if (writesize < pathlen)
{
fprintf(stderr, "write size less than pathlen.\n");
exit(-1);
}
ssize_t readsize;
char buf[1024];
printf("client begin read:\n");
while ((readsize = read(readfd, buf, 1024)) > 0)
{
write(STDOUT_FILENO, buf, readsize);
}
printf("client read over.\n");
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
#define FIFOSEVER "/home/adce/mycode/vscode/unix_process/fifo/file/fifo.server"
#define FIFOCLIENT "/home/adce/mycode/vscode/unix_process/fifo/file/fifo.client"
#define PATHSIZE 1024
void Server(int writefd, int readfd);
int main(int argc, char **argv)
{
if ((mkfifo(FIFOSEVER, FILE_MODE) < 0) && (errno != EEXIST))
{
perror("mkfifo");
exit(-1);
}
if ((mkfifo(FIFOCLIENT, FILE_MODE) < 0) && (errno != EEXIST))
{
perror("mkfifo");
exit(-1);
}
int readfd, writefd;
readfd = open(FIFOCLIENT, O_RDONLY, 0);
writefd = open(FIFOSEVER, O_WRONLY, 0);
if (readfd < 0 || writefd < 0)
{
perror("open");
exit(-1);
}
Server(writefd, readfd);
exit(0);
}
void Server(int writefd, int readfd)
{
char buf[PATHSIZE];
ssize_t readsize;
if((readsize = read(readfd, buf, PATHSIZE)) < 0)
{
perror("read");
exit(-1);
}
int filefd;
filefd = open(buf, O_RDONLY);
if (filefd < 0)
{
fprintf(stderr, "open() failed.");
exit(-1);
}
while ((readsize = read(filefd, buf, PATHSIZE)) > 0)
{
write(writefd, buf, readsize);
}
write(writefd, "send over!\n", 12);
exit(0);
}
一个服务端,多个客户端通信:
#ifndef PROTO_H_
#define PROTO_H_
#include <limits.h>
#include <unistd.h>
#include <string.h>
#define FIFO_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
#define SERVER_FIFO "/home/adce/mycode/vscode/unix_process/fifo/file/serverfifo"
#define CLIENT_FIFO "/home/adce/mycode/vscode/unix_process/fifo/file/clientfifo."
#define MEGDATESIZE (PIPE_BUF - sizeof(int))
struct msg_st
{
int pid;
char data[MEGDATESIZE];
};
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include "proto.h"
#include <signal.h>
void handle(int p)
{
printf("logout.\n");
exit(0);
}
int main(int agrc, char **argv)
{
signal(SIGINT, handle);
if (mkfifo(SERVER_FIFO, FIFO_MODE) < 0 && errno != EEXIST)
{
perror("mkfifo");
exit(-1);
}
int readfd = open(SERVER_FIFO, O_RDONLY);
if (readfd < 0)
{
perror("open");
exit(-1);
}
int nofd = open(SERVER_FIFO, O_WRONLY);
if (nofd < 0)
{
perror("open");
exit(-1);
}
char buf[1024];
char client_path[1024];
struct msg_st msg;
printf("server begin recv:\n");
while (read(readfd, &msg, sizeof(msg)) > 0)
{
printf("[%d]:%s\n", msg.pid, msg.data);
sprintf(client_path, "%s%d", CLIENT_FIFO, msg.pid);
sprintf(buf, "[%d]:OK.", msg.pid);
int sendfd = open(client_path, O_WRONLY);
if (sendfd < 0)
{
perror("open");
exit(-1);
}
strncpy(msg.data, buf, sizeof(buf));
write(sendfd, &msg, sizeof(msg));
}
exit(0);
}
#include "proto.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
void handle(int p)
{
printf("logout.\n");
exit(0);
}
int main(int argc, char **argv)
{
signal(SIGINT, handle);
pid_t pid = getpid();
char path[1024];
sprintf(path, "%s%d", CLIENT_FIFO, pid);
if (mkfifo(path, FIFO_MODE) < 0 && errno != EEXIST)
{
perror("mkfifo");
exit(-1);
}
int sendfd = open(SERVER_FIFO, O_WRONLY);
struct msg_st msg;
char buf[1024];
gets(buf);
msg.pid = pid;
strncpy(msg.data, buf, strlen(buf));
write(sendfd, &msg, sizeof(msg));
int recvfd = open(path, O_RDONLY);
while(1)
{
read(recvfd, &msg, sizeof(msg));
puts(msg.data);
gets(buf);
strncpy(msg.data, buf, strlen(buf));
write(sendfd, &msg, sizeof(msg));
}
exit(0);
}
|