IPC 참고 링크

IPC 관련 내용을 찾다보니 괜찮은 예제가 있길래 링크를 걸어둔다.

IPC 쉬운 예제 이해하기 쉬운 설명되어 있음.
http://beej.us/guide/bgipc/output/html/singlepage/bgipc.html

복잡하지만 좋은 예제 존재
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzab6/rzab6xnonblock.htm

멀티플렉싱 sender 테스트(udp socket, fifo, select)

[참고 : 멀티플렉싱 receiver 테스트]


멀티플렉싱 sender 테스트를 위한 코드로 udp socket, select, 그리고 IPC(fifo) 를 썼다.

--------------------------------------------------------
sender.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h> // for socket(), bind(), connet()
#include <arpa/inet.h> // for sockaddr_in, inet_ntoa()

#include <pthread.h>

typedef int SOCKET;
#define NET_INVALID_SOCKET -1
#define NET_SOCKET_ERROR -1

#define MAXCONN   10
#define MAX_BUF_SIZE 500

#define FIFO_NAME "my_test"


int main(void)
{
    char s[300] = "FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_\
FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_\
FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_";
    int num, fd;
int input;
int pid;
int run =1;

int l_ii, l_i;

SOCKET ServerSocket;
SOCKET numbytes;

struct sockaddr_in ServerAddress;
unsigned short ServerPort = 5060;

SOCKET ClientSocket;//sockfd;
    int servlen;
    int state;

char buf[MAX_BUF_SIZE] = "SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG\
SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_\
SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_\n";



    servlen = sizeof(ServerAddress);
    ClientSocket = socket(AF_INET, SOCK_DGRAM, 0);

if(ClientSocket == NET_INVALID_SOCKET)
{
printf("Fail to create the socket\n");
close(ClientSocket);
exit(0);        
    }

    bzero(&ServerAddress, sizeof(ServerAddress));
    ServerAddress.sin_family = AF_INET;
    ServerAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
    ServerAddress.sin_port = htons(ServerPort);

#if 1


for(l_ii=0; l_ii<50 ; l_ii++){
printf("[FIFO] send msg\n");
mknod(FIFO_NAME, S_IFIFO | 0666, 0);

printf("waiting for readers...\n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some stuff\n");

#if 0
while (gets(s), !feof(stdin)) {
#endif
if ((num = write(fd, s, strlen(s))) == -1)
perror("write error");
else
printf("speak: wrote %d bytes\n", num);
#if 0
}
#endif
close(fd);
usleep(200000);
}
printf("[%d]FIFO FINISHED well]\n\n", l_ii);

for(l_i=0; l_i<50 ; l_i++){
printf("[SOCKET][send][%d bytes]\n%s\n",strlen(buf),buf);
sendto(ClientSocket, (void *)&buf, strlen(buf), 0, (struct sockaddr *)&ServerAddress, servlen);    

memset(buf, 0x00, sizeof(buf));
recvfrom(ClientSocket, (void *)&buf, sizeof(buf), 0, (struct sockaddr *)&ServerAddress, &servlen);

printf("[recv][%d bytes]\n%s\n",strlen(buf),buf);

usleep(110000);
}
printf("[%d]SOCKET FINISHED well]", l_i);
close(ClientSocket);

#else

while(run){
printf("Start : ");
scanf("%d",&input);

switch(input){
case 1:
printf("[FIFO] send msg\n");
mknod(FIFO_NAME, S_IFIFO | 0666, 0);

printf("waiting for readers...\n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some stuff\n");

//while (gets(s), !feof(stdin)) {

if ((num = write(fd, s, strlen(s))) == -1)
perror("write error");
else
printf("speak: wrote %d bytes\n", num);
//}
close(fd);
break;

case 2:
printf("[SOCKET] send msg\n");
sendto(ClientSocket, (void *)&buf, sizeof(buf), 0, (struct sockaddr *)&ServerAddress, servlen);    
printf("[send][%d bytes]\n%s\n",strlen(buf),buf);

memset(buf, 0x00, sizeof(buf));

recvfrom(ClientSocket, (void *)&buf, sizeof(buf), 0, (struct sockaddr *)&ServerAddress, &servlen);

printf("[recv][%d bytes]\n%s\n",strlen(buf),buf);
//

break;

case 3:
run = 0;
break;
}
close(ClientSocket);

#endif
    return 0;
}


멀티플렉싱 receiver 테스트(udp socket, fifo, select)

[참고 : 멀티플렉싱 sender 테스트]


멀티플렉싱 receiver(서버 역할) 테스트를 위한 코드로 udp socket, select, 그리고 IPC(fifo) 를 썼다.

----------------------------------------------------------------------------
receiver.c

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <errno.h>
#include <fcntl.h> 
#include <sys/time.h> 
#include <sys/types.h> 
#include <sys/stat.h>
#include <unistd.h> 

#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define FIFO_NAME "my_test"
#define MAX_FD 2

int main()
{   
// 
    int i;    
    int state;


    // for fifo & select
int num;
int fd[MAX_FD];
char fifo_buf[300];
struct timeval tv; // not used
    fd_set readfds, writefds;


// for socket
int n;
int sockfd;
    int clilen;    
char rcv_buf[255];
struct sockaddr_in serveraddr, clientaddr;


// for aging result
int cnt_skt=0, cnt_fifo=0;
int dbg_skt[100]={0,},dbg_fifo[100]={0,};

    clilen = sizeof(clientaddr);
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        perror("socket error : ");
        exit(0);
    }

    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddr.sin_port = htons(5060);

    state = bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));

    if (state == -1)
    {
        perror("bind error : ");
        exit(0);
    }

fd[0] = sockfd;

mknod(FIFO_NAME, S_IFIFO | 0666, 0);

//fd[1] = open(FIFO_NAME, O_RDONLY);
//printf("waiting for FIFO writer...\n");

//fd[1] = open(FIFO_NAME, O_RDWR);

fd[1] = open(FIFO_NAME, O_RDONLY | O_NONBLOCK);
//printf("I'm not going to wait for a writer!!!\n");

//fd[1] = open(FIFO_NAME, O_RDONLY | O_NDELAY);


printf("[SOCKET AGING RESULT\n");
for(cnt_skt =1; cnt_skt<51; cnt_skt++){
printf("[%d]",dbg_skt[cnt_skt-1]);
if(cnt_skt%10 == 0)
printf("\n");
}
printf("\n[FIFO AGING RESULT\n");
for(cnt_fifo =1; cnt_fifo<51; cnt_fifo++){
printf("[%d]",dbg_fifo[cnt_fifo-1]);
if(cnt_fifo%10 == 0)
printf("\n");
}

cnt_skt = cnt_fifo = 0;

for(;;)
{
FD_SET(fd[0], &readfds);
FD_SET(fd[1], &readfds);


state = select(fd[1]+1, &readfds, NULL, NULL, NULL);

//if(state <= 0)
// continue;
switch(state)
{
case -1:
perror("select error : ");
exit(0);
break;

default :
if (FD_ISSET(fd[0], &readfds))
{
dbg_skt[cnt_skt++] = 1;

n =recvfrom(sockfd, (void *)&rcv_buf, sizeof(rcv_buf), 0, (struct sockaddr *)&clientaddr, &clilen);
printf("[%d][SOCKET][Recv][%d byte]\n%s\n",cnt_skt,n,rcv_buf);
printf("send %d\n",sendto(sockfd, (void *)&rcv_buf, strlen(rcv_buf), 0, (struct sockaddr *)&clientaddr, clilen));

}

if (FD_ISSET(fd[1], &readfds))
{
dbg_fifo[cnt_fifo++] = 1;

do {
if ((num = read(fd[1], fifo_buf, 300)) == -1)
perror("[FIFO]read error");
else {
fifo_buf[num] = '\0';
printf("[%d][FIFO][Recv][%d byte]\n%s\n",cnt_fifo, num, fifo_buf);
}
} while (num > 0);
//FD_CLR(fd[1],&readfds);
//close(fd[1]);
unlink(FIFO_NAME);

mknod(FIFO_NAME, S_IFIFO | 0666, 0);

fd[1] = open(FIFO_NAME, O_RDONLY | O_NONBLOCK);

}
break;
}
usleep(1000);

if(cnt_fifo == 50 && cnt_skt == 50){
printf("[SOCKET AGING RESULT\n");
for(cnt_skt =1; cnt_skt<51; cnt_skt++){
printf("[%d]",dbg_skt[cnt_skt-1]);
if(cnt_skt%10 == 0)
printf("\n");
}
printf("[FIFO AGING RESULT\n");
for(cnt_fifo =1; cnt_fifo<51; cnt_fifo++){
printf("[%d]",dbg_fifo[cnt_fifo-1]);
if(cnt_fifo%10 == 0)
printf("\n");
}

}
//break;

}

close(fd[0]); // close socket fd
close(sockfd); // close socket

close(fd[1]); // close fifo fd
unlink(FIFO_NAME); // close fifo

}