---
title: "UNIX编程(15)-进程间通信"
author: "brxonline"
date: "2011-08-07T12:09:21Z"
tags: []
categories: ["操作系统"]
slug: "unix-15"
draft: false
lastmod: "2011-08-07T12:09:36Z"
id: 1141911
url: "https://brxonline.iteye.com/blog/1141911"
---

. 管道

 

```
#include <unistd.h>

int pipe(int filedes[2]);
```

 

Returns: 0 if OK, 1 on error

参数filedes返回两个文件描述符：filedes\[0]为读而打开，filedes\[1]为写而打开。

 

例：经由管道父进程向子进程传递数据

```
#include "apue.h"

int
main(void)
{
    int     n;
    int     fd[2];
    pid_t   pid;
    char    line[MAXLINE];

    if (pipe(fd) < 0)
        err_sys("pipe error");
    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid > 0) {       /* parent */
        close(fd[0]);
        write(fd[1], "hello world\n", 12);
    } else {                /* child */
        close(fd[1]);
        n = read(fd[0], line, MAXLINE);
        write(STDOUT_FILENO, line, n);
    }
    exit(0);
}
```

例：将文件复制到分页程序

 

```
#include "apue.h"
#include <sys/wait.h>

#define DEF_PAGER   "/bin/more"     /* default pager program */

int
main(int argc, char *argv[])
{
    int    n;
    int    fd[2];
    pid_t  pid;
    char   *pager, *argv0;
    char   line[MAXLINE];
    FILE   *fp;

    if (argc != 2)
        err_quit("usage: a.out <pathname>");

    if ((fp = fopen(argv[1], "r")) == NULL)
        err_sys("can't open %s", argv[1]);
    if (pipe(fd) < 0)
        err_sys("pipe error");

    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid > 0) {                              /* parent */
        close(fd[0]);       /* close read end */

        /* parent copies argv[1] to pipe */
        while (fgets(line, MAXLINE, fp) != NULL) {
            n = strlen(line);
            if (write(fd[1], line, n) != n)
                err_sys("write error to pipe");
        }
        if (ferror(fp))
            err_sys("fgets error");

        close(fd[1]);   /* close write end of pipe for reader */

        if (waitpid(pid, NULL, 0) < 0)
            err_sys("waitpid error");
        exit(0);
    } else {                                        /* child */
        close(fd[1]);   /* close write end */
        if (fd[0] != STDIN_FILENO) {
            if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
                err_sys("dup2 error to stdin");
            close(fd[0]);   /* don't need this after dup2 */
        }

        /* get arguments for execl() */
        if ((pager = getenv("PAGER")) == NULL)
            pager = DEF_PAGER;
        if ((argv0 = strrchr(pager, '/')) != NULL)
            argv0++;        /* step past rightmost slash */
        else
            argv0 = pager;  /* no slash in pager */

        if (execl(pager, argv0, (char *)0) < 0)
            err_sys("execl error for %s", pager);
    }
    exit(0);
}
```

使父子进程同步的例程

#include "apue.h"

static int  pfd1\[2], pfd2\[2];

void  
TELL\_WAIT(void)  
{  
    if (pipe(pfd1) &lt; 0 || pipe(pfd2) &lt; 0)  
        err\_sys("pipe error");  
}

void  
TELL\_PARENT(pid\_t pid)  
{  
    if (write(pfd2\[1], "c", 1) != 1)  
        err\_sys("write error");  
}

void  
WAIT\_PARENT(void)  
{  
    char    c;

    if (read(pfd1\[0], &c, 1) != 1)  
        err\_sys("read error");

    if (c != 'p')  
        err\_quit("WAIT\_PARENT: incorrect data");  
}

void  
TELL\_CHILD(pid\_t pid)  
{  
    if (write(pfd1\[1], "p", 1) != 1)  
        err\_sys("write error");  
}

void  
WAIT\_CHILD(void)  
{  
    char    c;

    if (read(pfd2\[0], &c, 1) != 1)  
        err\_sys("read error");

    if (c != 'c')  
        err\_quit("WAIT\_CHILD: incorrect data");  
}

2.popen和pclose函数

 

```
#include <stdio.h>

FILE *popen(const char *cmdstring, const char *type);
```

 

Returns: file pointer if OK, `NULL` on error

 

```
int pclose(FILE *fp);
```

 

Returns: termination status of cmdstring, or 1 on error

例：用popen向分页程序传递文件

 

```
#include "apue.h"
#include <sys/wait.h>

#define PAGER   "${PAGER:-more}" /* environment variable, or default */

int
main(int argc, char *argv[])
{
    char    line[MAXLINE];
    FILE    *fpin, *fpout;

    if (argc != 2)
        err_quit("usage: a.out <pathname>");
    if ((fpin = fopen(argv[1], "r")) == NULL)
        err_sys("can't open %s", argv[1]);

    if ((fpout = popen(PAGER, "w")) == NULL)
        err_sys("popen error");

    /* copy argv[1] to pager */
    while (fgets(line, MAXLINE, fpin) != NULL) {
        if (fputs(line, fpout) == EOF)
            err_sys("fputs error to pipe");
    }
    if (ferror(fpin))
        err_sys("fgets error");
    if (pclose(fpout) == -1)
        err_sys("pclose error");

    exit(0);
}
```

大小写过滤程序

```
#include "apue.h"
#include <ctype.h>

int
main(void)
{
    int     c;

    while ((c = getchar()) != EOF) {
        if (isupper(c))
            c = tolower(c);
        if (putchar(c) == EOF)
            err_sys("output error");
        if (c == '\n')
            fflush(stdout);
    }
    exit(0);
}
```

 

```
#include "apue.h"
#include <sys/wait.h>

int
main(void)
{
    char    line[MAXLINE];
    FILE    *fpin;

    if ((fpin = popen("myuclc", "r")) == NULL)
        err_sys("popen error");
    for ( ; ; ) {
        fputs("prompt> ", stdout);
        fflush(stdout);
        if (fgets(line, MAXLINE, fpin) == NULL) /* read from pipe */
            break;
        if (fputs(line, stdout) == EOF)
            err_sys("fputs error to pipe");
    }
    if (pclose(fpin) == -1)
        err_sys("pclose error");
    putchar('\n');
    exit(0);
}

```

3.协同进程

 

对两个数求和的简单过滤程序

```
#include "apue.h"

int
main(void)
{
    int     n,  int1,  int2;
    char    line[MAXLINE];

    while ((n = read(STDIN_FILENO, line, MAXLINE)) > 0) {
        line[n] = 0;        /* null terminate */
        if (sscanf(line, "%d%d", &int1, &int2) == 2) {
            sprintf(line, "%d\n", int1 + int2);
            n = strlen(line);
            if (write(STDOUT_FILENO, line, n) != n)
                err_sys("write error");
        } else {
            if (write(STDOUT_FILENO, "invalid args\n", 13) != 13)
                err_sys("write error");
        }
    }
    exit(0);
}
```

 

include "apue.h"

static void sig\_pipe(int);      /* our signal handler \*/

int  
main(void)  
{  
    int     n, fd1\[2], fd2\[2];  
    pid\_t   pid;  
    char    line\[MAXLINE];

    if (signal(SIGPIPE, sig\_pipe) == SIG\_ERR)  
        err\_sys("signal error");

    if (pipe(fd1) &lt; 0 || pipe(fd2) &lt; 0)  
        err\_sys("pipe error");

    if ((pid = fork()) &lt; 0) {  
        err\_sys("fork error");  
    } else if (pid &gt; 0) {                         /* parent \*/  
        close(fd1\[0]);  
        close(fd2\[1]);  
        while (fgets(line, MAXLINE, stdin) != NULL) {  
            n = strlen(line);  
            if (write(fd1\[1], line, n) != n)  
                err\_sys("write error to pipe");  
            if ((n = read(fd2\[0], line, MAXLINE)) &lt; 0)  
                err\_sys("read error from pipe");  
            if (n == 0) {  
                err\_msg("child closed pipe");  
                break;  
            }  
            line\[n] = 0;    /* null terminate \*/  
            if (fputs(line, stdout) == EOF)  
                err\_sys("fputs error");  
        }  
        if (ferror(stdin))  
            err\_sys("fgets error on stdin");  
        exit(0);  
    } else {                                  /* child \*/  
        close(fd1\[1]);  
        close(fd2\[0]);  
        if (fd1\[0] != STDIN\_FILENO) {  
            if (dup2(fd1\[0], STDIN\_FILENO) != STDIN\_FILENO)  
                err\_sys("dup2 error to stdin");  
            close(fd1\[0]);  
        }

        if (fd2\[1] != STDOUT\_FILENO) {  
            if (dup2(fd2\[1], STDOUT\_FILENO) != STDOUT\_FILENO)  
                err\_sys("dup2 error to stdout");  
            close(fd2\[1]);  
        }  
        if (execl("./add2", "add2", (char \*)0) &lt; 0)  
            err\_sys("execl error");  
    }  
    exit(0);  
}

static void  
sig\_pipe(int signo)  
{  
    printf("SIGPIPE caught\\n");  
    exit(1);  
}

4.FIFO

 

```
#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode);

```

 

Returns: 0 if OK, 1 on error

5.XSI IPC

消息队列，信号量，共享存储称作XSI IPC

1)标识符和键

标识符是IPC对象的内部名

键是IPC对象的外部名

 

2）权限结构

XSI IPC 为每一个IPC结构设置了一个ipc\_perm结构

struct ipc\_perm {  
     uid\_t  uid;  /* owner's effective user id \*/  
     gid\_t  gid;  /* owner's effective group id \*/  
     uid\_t  cuid; /* creator's effective user id \*/  
     gid\_t  cgid; /* creator's effective group id \*/  
     mode\_t mode; /* access modes \*/  
     .  
     .  
     .  
   };

6.消息队列

每个队列都有一个msq\_ds与其相关联

struct msqid\_ds {  
     struct ipc\_perm  msg\_perm;     /* see Section 15.6.2 \*/  
     msgqnum\_t        msg\_qnum;     /* # of messages on queue \*/  
     msglen\_t         msg\_qbytes;   /* max # of bytes on queue \*/  
     pid\_t            msg\_lspid;    /* pid of last msgsnd() \*/  
     pid\_t            msg\_lrpid;    /* pid of last msgrcv() \*/  
     time\_t           msg\_stime;    /* last-msgsnd() time \*/  
     time\_t           msg\_rtime;    /* last-msgrcv() time \*/  
     time\_t           msg\_ctime;    /* last-change time \*/  
     .  
     .  
     .  
   };

[]()[]()[]()[]()[]()[]()[]()[]()[]()[]()[]()[]()[]()[]()[]()

```
#include <sys/msg.h>

int msgget(key_t key, int flag);
```

 

Returns: message queue ID if OK, 1 on error

 

 

```
#include <sys/msg.h>

int msgctl(int msqid, int cmd, struct msqid_ds *buf );
```

 

Returns: 0 if OK, 1 on error

 

[]()

[\[View full width\]](http://brxonline.iteye.com/admin/blogs/ch15lev1sec7.html#PLID3)

```
#include <sys/msg.h>

int msgsnd(int msqid, const void *ptr, size_t
nbytes, int flag);
```

 

Returns: 0 if OK, 1 on error

[]()

[\[View full width\]](http://brxonline.iteye.com/admin/blogs/ch15lev1sec7.html#PLID5)

```
#include <sys/msg.h>

ssize_t msgrcv(int msqid, void *ptr, size_t nbytes
, long type, int flag);
```

 

Returns: size of data portion of message if OK, 1 on error

7.信号量

 

struct semid\_ds {  
     struct ipc\_perm  sem\_perm;  /* see Section 15.6.2 \*/  
     unsigned short   sem\_nsems; /* # of semaphores in set \*/  
     time\_t           sem\_otime; /* last-semop() time \*/  
     time\_t           sem\_ctime; /* last-change time \*/  
     .  
     .  
     .  
   };

 

```
#include <sys/sem.h>

int semget(key_t key, int nsems, int flag);
```

 

Returns: semaphore ID if OK, 1 on error

 

 

```
#include <sys/sem.h>

int semctl(int semid, int semnum, int  cmd,
           ... /* union semun arg */);
```

 

Returns: (see following)

 

[]()

[\[View full width\]](http://brxonline.iteye.com/admin/blogs/ch15lev1sec8.html#PLID5)

```
#include <sys/sem.h>

int semop(int semid, struct sembuf semoparray[],
 size_t nops);
```

 

Returns: 0 if OK, 1 on error

8.共享存储

struct shmid\_ds {  
     struct ipc\_perm  shm\_perm;    /* see Section 15.6.2 \*/  
     size\_t           shm\_segsz;   /* size of segment in bytes \*/  
     pid\_t            shm\_lpid;    /* pid of last shmop() \*/  
     pid\_t            shm\_cpid;    /* pid of creator \*/  
     shmatt\_t         shm\_nattch;  /* number of current attaches \*/  
     time\_t           shm\_atime;   /* last-attach time \*/  
     time\_t           shm\_dtime;   /* last-detach time \*/  
     time\_t           shm\_ctime;   /* last-change time \*/  
     .  
     .  
     .  
   };

 

```
#include <sys/shm.h>

int shmget(key_t key, size_t size, int flag);
```

 

Returns: shared memory ID if OK, 1 on error

 

 

```
#include <sys/shm.h>

int shmctl(int shmid, int cmd, struct shmid_ds *buf);
```

 

Returns: 0 if OK, 1 on error

[]()[]()[]()[]()[]()[]()

```
#include <sys/shm.h>

void *shmat(int shmid, const void *addr, int flag);
```

 

Returns: pointer to shared memory segment if OK, 1 on error

 

 

```
#include <sys/shm.h>

int shmdt(void *addr);
```

 

Returns: 0 if OK, 1 on error

打印不同数据所存放的区域

```
#include "apue.h"
#include <sys/shm.h>

#define ARRAY_SIZE  40000
#define MALLOC_SIZE 100000
#define SHM_SIZE    100000
#define SHM_MODE    0600    /* user read/write */

char    array[ARRAY_SIZE];  /* uninitialized data = bss */

int
main(void)
{
    int     shmid;
    char    *ptr, *shmptr;

    printf("array[] from %lx to %lx\n", (unsigned long)&array[0],
      (unsigned long)&array[ARRAY_SIZE]);
    printf("stack around %lx\n", (unsigned long)&shmid);

    if ((ptr = malloc(MALLOC_SIZE)) == NULL)
        err_sys("malloc error");
    printf("malloced from %lx to %lx\n", (unsigned long)ptr,
      (unsigned long)ptr+MALLOC_SIZE);

    if ((shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE)) < 0)
        err_sys("shmget error");
    if ((shmptr = shmat(shmid, 0, 0)) == (void *)-1)
        err_sys("shmat error");
    printf("shared memory attached from %lx to %lx\n",
      (unsigned long)shmptr, (unsigned long)shmptr+SHM_SIZE);

    if (shmctl(shmid, IPC_RMID, 0) < 0)
        err_sys("shmctl error");

    exit(0);
}
```