Linux IPC

  • Pipe
#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{
    int pfd[2];
    pid_t cpd;
    char buf;
    
    if(argc != 2)
    {
        fprintf(stderr, "need argv\n");
        exit(EXIT_FAILURE);
    }

    if(pipe(pfd) == -1)
    {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    cpd = fork();

    if(cpd == -1) 
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    else if(cpd == 0)
    {
        close(pfd[1]);
        while(read(pfd[0], &buf, 1) > 0)
            write(STDOUT_FILENO, &buf, 1);
        close(pfd[0]);
        exit(EXIT_SUCCESS);
    }
    else
    {
        close(pfd[0]);
        write(pfd[1], argv[1], strlen(argv[1]));
        close(pfd[1]);
        wait(NULL);
        exit(EXIT_SUCCESS);
    }
    
}
  • Named Pipe
#include 
#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{
    pid_t cpd;
    
    if(argc != 2)
    {
        fprintf(stderr, "need argv\n");
        exit(EXIT_FAILURE);
    }

    if(access("my_fifo", F_OK) == 0)
        unlink("my_fifo");
    
    if(mkfifo("my_fifo", 0777) == -1)
    {
        perror("mkfifo");
        exit(EXIT_FAILURE);
    }

    cpd = fork();

    if(cpd == -1)
    {   
        perror("fork");
        exit(EXIT_FAILURE);
    }
    else if(cpd == 0)
    {
        int fd;
        char buf;
        fd = open("my_fifo", O_RDONLY);
        while(read(fd, &buf, 1) > 0)
            write(STDOUT_FILENO, &buf, 1);
        close(fd);
        exit(EXIT_SUCCESS);
    }
    else
    {
        int fd;
        fd = open("my_fifo", O_WRONLY);
        write(fd, argv[1], strlen(argv[1]));
        close(fd);
        wait(NULL);
        exit(EXIT_SUCCESS);
    }
}
  • Signal
#include 
#include 
#include 
#include 

void sig_act(int sig)
{
    printf("thread%d got signal%d\n", pthread_self(), sig);
    sleep(5);
    printf("thread%d quit\n", pthread_self());
}

int main(int argc, char** argv)
{
    struct sigaction act;
    struct sigaction old_act;

    act.sa_handler = sig_act;
    sigaddset(&act.sa_mask, SIGQUIT);
    act.sa_flags = SA_NODEFER | SA_RESETHAND;
    
    sigaction(SIGINT, &act, &old_act);
    
    printf("main thread%d\n", pthread_self());
    for(;;) sleep(1);
    
    return 0;
}
  • Semaphore
#include 
#include 
#include 
#include 
#include 
#include 

char *cache;
sem_t sem;

void *thread_function(void *arg)
{
    sem_wait(&sem);

    while(strncmp("#", cache, 1) != 0)
    {
        printf("%s from thread%d\n", cache, pthread_self());
        sem_wait(&sem);
    }

    pthread_exit(NULL);

}

int main(int argc, char** argv)
{
    pthread_t pth;
    void *pthret;
    
    cache = (char*)calloc(1, 1024); 

    if(sem_init(&sem, 0, 0) != 0)
    {
        perror("sem_init");
        exit(EXIT_FAILURE);
    }
    
    if(pthread_create(&pth, NULL, thread_function, NULL) != 0)
    {
        perror("pthread_create");
        exit(EXIT_FAILURE);
    }

    while(strncmp("#", cache, 1) != 0)
    {
        fgets(cache, 1024, stdin);
        sem_post(&sem);
    }

    if(pthread_join(pth, &pthret) != 0)
    {
        perror("pthread_join");
        exit(EXIT_FAILURE);
    }

    free(cache);
    sem_destroy(&sem);
    return 0;

}
  • Message Queue
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{
    pid_t cpd;
    struct mq_attr attr;
    mqd_t mqd;
    char* messages[3];

    attr.mq_maxmsg = 8;
    attr.mq_msgsize = 1024;

    messages[0] = "message1";
    messages[1] = "message2";
    messages[2] = "message3";

    mqd = mq_open("/mymq", 
        O_RDWR | O_CREAT, 0777, &attr);

    if(mqd == -1)
    {
        perror("mq_open");
        exit(EXIT_FAILURE);
    }

    cpd = fork();

    if(cpd == -1)
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    else if(cpd == 0)
    {
        sleep(3);
        
        char *msg;
        size_t msg_len;
        int pro;
        struct timespec t;
        
        t.tv_sec = 2;
        t.tv_nsec = 0;
        msg_len = attr.mq_msgsize;      
        msg = (char*)calloc(1, msg_len);
        pro = 0;

        while(mq_timedreceive(mqd, msg, msg_len, &pro, &t) > 0)
            printf("message is %s, priority is %d\n", msg, pro);
        printf("no more message\n");

        free(msg);
        mq_close(mqd);
        exit(EXIT_SUCCESS);
    }
    else
    {
        mq_send(mqd, messages[0], strlen(messages[0]), 1);
        mq_send(mqd, messages[1], strlen(messages[1]), 2);
        mq_send(mqd, messages[2], strlen(messages[2]), 3);
        
        mq_close(mqd);

        wait(NULL);
        mq_unlink("/mymq");
        exit(EXIT_SUCCESS);
    }

    return 0;

}
  • Memory Share
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{
    int shmid;
    char* shmaddr;
    pid_t pid;

    shmid = shmget(IPC_PRIVATE, 1024, IPC_CREAT | 0600);
    
    if(shmid < 0)
    {
        perror("shmget");
        exit(EXIT_FAILURE);
    }
    
    pid = fork();

    if(pid == -1)
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    else if(pid == 0)
    {
        sleep(3);

        struct shmid_ds buf;
        
        if(shmctl(shmid, IPC_STAT, &buf) == -1)
        {
            perror("shmctl");
            exit(EXIT_FAILURE);
        }

        printf("the creator is %d\n", buf.shm_cpid);

        shmaddr = (char*)shmat(shmid, NULL, 0);

        if((int)shmaddr == -1)
        {
            perror("shmat");
            exit(EXIT_FAILURE);
        }

        printf("i am the child %d, i got message \"%s\" from %d\n", 
            getpid(), shmaddr, buf.shm_lpid);

        shmdt(shmaddr);
        return 0;
    }
    else
    {
        shmaddr = (char*)shmat(shmid, NULL, 0);
        
        if((int)shmaddr == -1)
        {
            perror("shmat");
            exit(EXIT_FAILURE);
        }

        strcpy(shmaddr, "i am the parent");
        
        shmdt(shmaddr);
        wait(NULL);
        return 0;
    }
}
  • Memory Map
#include 
#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{
    char *addr;
    int pid;
    
    addr = mmap(0, 512, PROT_READ | PROT_WRITE, 
            MAP_SHARED | MAP_ANONYMOUS, -1, 0);
        
    pid = fork();

    if(pid == -1)
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    else if(pid == 0)
    {
        sleep(3);
        printf("i am the child %d, i got the message \"%s\"\n",
             getpid(), addr);
        return 0;
    }
    else
    {
        strcpy(addr, "i am the parent");
        printf("i am the parent %d\n", getpid());
        wait(NULL);
        return 0;
    }
}
  • Domain Socket
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{
    int socketfd[2];
    pid_t pid;
    char buf[64];

    if(socketpair(AF_UNIX, SOCK_STREAM, 0, socketfd) == -1)
    {
        perror("socketpair");
        exit(EXIT_FAILURE);
    }

    pid = fork();

    if(pid == -1)
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    else if(pid == 0)
    {
        sleep(3);

        char message[]  = "child to parent";

        close(socketfd[0]);

        if(read(socketfd[1], buf, sizeof(buf)) < 0)
            perror("read");
    
        if(write(socketfd[1], message, sizeof(message)) < 0)
            perror("write");

        printf("child %d got message \"%s\"\n", getpid(), buf);

        close(socketfd[1]);
        return 0;
    }
    else
    {
        char message[] = "parent to child";

        close(socketfd[1]);
        
        if(write(socketfd[0], message, sizeof(message)) < 0)
            perror("write");

        if(read(socketfd[0], buf, sizeof(buf)) < 0)
            perror("read");

        printf("parent %d got message \"%s\"\n", getpid(), buf);

        close(socketfd[0]); 
        wait(NULL);
        return 0;
    }
}

你可能感兴趣的:(Linux IPC)