기본 콘텐츠로 건너뛰기

socketpair

socketpair는 IPC 가운데 하나로 보통 부모 자식 간의 대화를 전달할 때 쓴다고 한다. 얘도 pipe처럼 두개 file-descriptor를 던져주는데, pipe와 달리 두 file-descriptor가 모두 읽기/쓰기가 가능하다. 아래는 예제이다.
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <errno.h>
#include <sys/wait.h>
using namespace std;

int
main(int,char**)
{
    int s[2];
    if ( -1 == socketpair(AF_UNIX, SOCK_STREAM, AF_LOCAL,s) )
    {
        cout << strerror(errno) << endl;
        return 1;
    }

    char buf[1024];
    if ( fork() )
    {
        strcpy(buf, "Hello, world!");
        send(s[0], buf, strlen(buf)+1, 0);
        int res;
        wait(&res);
    }
    else
    {
        recv(s[1], buf, sizeof(buf), 0);
        cout << buf << endl;
    }

    return 0;
}


어디선가 pipe와 다른 점은 부모자식이 아닌 프로세스 간 통신이 가능하다는데, 부모자식이 아니면 어떻게 소켓을 넘겨주는지 궁금하다.

댓글

이 블로그의 인기 게시물

탐색기에서 OneDrive 이 2개로 보이는 문제

왜 2개가 보이는지 모르겠지만, Registry 삭제하면 됨 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace 하위 키에서 OneDrive 둘 중에 하나만 지워도 바로 반영됨. 참조:  https://answers.microsoft.com/en-us/msoffice/forum/all/duplicate-onedrives-in-file-explorer/49c935a6-287b-43a5-aed5-2dee2a1c1b22