반응형
1. Server
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
int main() {
int pd, n;
char msg[] = "Hello, FIFO";
printf("Server =====\n");
if(mkfifo("./HAN-FIFO", 0666) == -1){
perror("mkfifo");
exit(1);
}
if((pd = open("./HAN-FIFO", O_WRONLY)) == -1){
perror("open");
exit(1);
}
printf("To Client : %s\n", msg);
n = write(pd, msg, strlen(msg)+1);
if(n==-1){
perror("write");
exit(1);
}
close(pd);
return 0;
}
2. client
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
int main() {
int pd, n;
char inmsg[80];
if((pd = open("./HAN-FIFO", O_RDONLY)) == -1){
perror("open");
exit(1);
}
printf("Client ====\n");
write(1, "From Server :", 13);
while((n=read(pd, inmsg, 80)) > 0)
write(1, inmsg, n);
if(n==-1){
perror("read");
exit(1);
}
write(1, "\n", 1);
close(pd);
return 0;
}
반응형
'TIL > os' 카테고리의 다른 글
POSIX 공유메모리와 세마포어 (0) | 2022.11.19 |
---|---|
tmpnam, tempnam, mktemp 임시 파일명 생성 api (0) | 2022.10.14 |
PIPE 양방향 통신하기 (0) | 2022.10.04 |
wait를 이용하여 자식 프로세스의 종료를 기다리기 (0) | 2022.10.03 |
exec를 이용하여 fork 후 자식 프로세스는 다른 프로그램 실행하기 (0) | 2022.10.03 |