Click here to load reader

第七章 进程通信

  • View
    57

  • Download
    0

Embed Size (px)

DESCRIPTION

第七章 进程通信. 进程通信. 进程之间的相互通信被称为进程间通讯 ——IPC 管道 命名管道 FIFO 消息队列 信号量 共享内存. 管道. 管道是 UNIX IPC 的最老形式,所有的 UNIX 系统都支持此种通讯机制 管道有两种限制: ( 1 )它们是半双工的,数据只能在一个方向上流动; ( 2 ) 它们只能在具有公共祖先的进程之间使用。通常,一个管道由一个进程创建,然后该进程调用 fork() 函数,此后父进程和子进程之间就可以使用该管道。. 管道. 管道的创建 - PowerPoint PPT Presentation

Text of 第七章 进程通信

  • IPC

    FIFO*

  • UNIX IPCUNIX

    1

    2 fork()*

  • fork()

    UNIXUNIX

    *

  • write()read()inode*

  • *#include

    intpipe(int filedes[2])0; -1

    filedes[0]filedes[1]filedes[1]filedes[0]

  • fork()filedes[1]filedes[0]fork

    *

  • *

  • write()write()read()write()read00*

  • read()write()

    *

  • read()0

    write()1write()*

  • pwrite.cpread.c

    *

  • writereadwrite

    *

  • FIFOUNIX System FIFO

    *#include #include

    intmkfifo(const char *pathname, mode_t mode)0; -1

  • FIFOmkfifo()FIFOI/O

    fd = open("pipename", O_WRONLY);write(fd, buf, len);fd = open("pipename", O_RDONLY);len = read(fd, buf, sizeof buf);

    *

  • FIFO

    FIFOshell

    FIFO-*

  • FIFO*

  • UNIXSystem VIPCinter-process communication

    *

  • KEYKEY*

  • open()filedeswrite()read()lsrm

    *KEYmsgget() qid msgsnd msgrcv ipcs ipcrm

  • qid

    *#include #include #include

    intmsgget(key_t key, int flag)qid; -1

    1KEY462101qid = msgget(462101, IPC_CREAT | 0666);

    2KEY462101qid = msgget(462101, 0);

  • *#include

    int msgsnd(int msqid, void *ptr, size_t nbytes, int flag);0 -1

    flag0msgsnd()flagIPC_NOWAITmsgsnd()-1

  • *#include

    int msgrcv(int msqid, void *ptr, size_t nbytes, long mtype, int flags);0 -1

    mtype0FIFOmtype0mtypemtype0-mtypemtype0

  • KEY*

  • *

  • UNIX

    *

  • 899899*

  • *

  • *

  • ipcsIPC

    ipcsipcs -qipcs -aq-qqueuemsgqid_ds

    *

  • semaphoreUNIX1. 2. -13. 01*

  • UNIXKEYsemget()IDfdKEYidsemctl()*

  • semget()ID*#include #include #include

    intsemget(key_t key, int nsems, int flag);ID; -1

    keyKEYnsems0

  • *#include #include #include

    int semctl(int sem_id, int snum, int cmd, void *arg);-1

    cmdsnumarg

  • PV*int semop(int sem_id, struct sembuf sops[], int nsops);-1

    sem_idIDsemget()sopsnsops

  • sembuf*struct sembuf { short sem_num; /* 0 */ short sem_op; /* */ short sem_flg; /* */};

    sem_numC0sem_opPsem_op=-1Vsem_op=1

  • share memoryUNIX

    *

  • *

  • *

  • Windows *#include #include #include

    intshmget(key_t key, int nbytes, int flag);ID; -1

  • KEYshmget()IDnbytesflags9nbytesflags9flagsIPC_CREATKEYIDnbytesflags0

    *

  • *void *shmat(int shm_id, void *shmaddr, int shmflg);;-1

    shm_idIDshmget()shmat()shm_idattachshmaddrshmflg0

  • *int shmctl(int shm_id, int cmd, void *arg);-1

    shmctl(shm_id, IPC_RMID, 0);

  • ipcsipcrm ipcs -mipcrm -mSHMMAX128KB1281024B)SHMMNI100SHMSEG6

    *

  • *

  • *

  • readwriteUNIXWindowsmemory map

    *

  • *

  • *

  • readwriteread*

  • Windows

    *

  • *fdoffsetlenaddr0#include #include void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);

  • protPROT_READPROT_WRITEopenPROT_WRITEPROT_READflagsMAP_SHARE

    *char *pa;pa = mmap(0, 65536, PROT_READ | PROTO_WRITE, MAP_SHARED, fd, 0);

  • munmap*int munmap(void *addr, size_t len);

Search related