IPC - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

IPC

Description:

Title: IPC Subject: System Programming Author: MF Last modified by: moni Created Date: 9/18/2001 8:40:36 AM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 22
Provided by: MF63
Category:
Tags: ipc

less

Transcript and Presenter's Notes

Title: IPC


1
????????? ??????
  • ??. ??. ?????? ???????????, ??????? ????????????
    ???????

2
??????????
  • ?????? ????????? ??????
  • ????????? ?? ????????? ?????
  • ?????? ? ????????? ?????
  • ?????? ?? ????????? ?????
  • ????????? ?? ????????? ?????
  • ????????? ?? FIFO ????

3
?????? ????????? ??????
  • ??????????? ????????? ?????? (unnamed pipe)
  • ????????? ????????? ?????? (named pipe, FIFO
    file)

4
????????? ?? ????????? ?????
  • int pipe(int fd2)
  • ????? 0 ??? ?????, -1 ??? ??????.
  • fd0 - ?????? ?????????? ?? ?????? ?? ??????
  • fd1 - ?????? ?????????? ?? ?????? ? ??????

5
?????? ? ????????? ?????
  • ssize_t write(int fd, char buf, size_t nbytes)
  • fd ? ?????? ?????????? ?? ?????? ? ?????????
    ?????.
  • ??? ? ?????? ??? ?????????? ?????, ?? ??????? ??
    ???????? ? ???? ?? ?????, ????????? ?? ??????? ??
    ????? ? ?? ???????? ?????? ???????, ?????? ??
    ????? ?? ??????.
  • ??? ? ?????? ???? ?????????? ????? ?? ????????
    ????? ? nbytes ? ??-????? ??? ????? ?? PIPE_BUFF
    (??? ?????????? ?? ??????), ?? ?????? ???????
    ???????. ?????? ???? ??????? ?? ???? ??????,
    ?????????? read, ??? ?????????? ????? ? ?????? 1.
  • ??? ? ?????? ???? ?????????? ????? ?? ????????
    ?????, ?? nbytes ? ??-????? ?? PIPE_BUFF (???
    ?????????? ?? ??????), ?? ? ?????? ?? ????????
    ??????? ?????, ??????? ? ???????? ? ??????
    ??????? ???????. ?????? ???? ???????, ???
    ?????????? ?? ????.

6
?????? ?? ????????? ?????
  • ssize_t read(int fd, char buf, size_t nbytes)
  • fd ? ?????? ?????????? ?? ?????? ?? ?????????
    ?????.
  • ??? ? ?????? ??? ??????? ?????, ?? ??????? ??????
    ?? ???????? ?? ????? ?????? ?? ???????????
    ???????? ?? ??????? ??? ?????? ??? ????? ?
    ??????. ???????? ??????? ?? ????? ? ??????????
    ???? ????? ? ??????? ?????? ???????, ?????? ??
    ????? ? ??????.
  • ??? ??????? ? ??????, ?????? ??????? ???????.
    ?????? ???? ??????? ?? ???? ??????, ??????????
    write, ??? ?????????? ????? ? ?????? 1.

7
????????? ?? ????????? ?????
  • int close(int fd)
  • fd ? ?????? ?????????? ?? ??????/?????? ?
    ????????? ?????.
  • ??? ??? close ?? ???????? ?????????? ??????
    ?????????? ?? ?????? ? ??????, ?? ?? ????????
    ?????? ???????, ?????? ?? ????? ?? ??????, ????
    read ????? 0.
  • ??? ??? close ?? ???????? ?????????? ??????
    ?????????? ?? ?????? ?? ??????, ?? ?? ????????
    ?????? ???????, ?????? ?? ????? ? ?????? ???? ??
    ?? ??????? ?????? SIGPIPE.
  • ??? ??? close ?? ???????? ?????????? ??????
    ?????????? ?? ??????? ? ?? ? ?????? ? ??????, ??
    ??????????? ????? ?? ?????????.

8
??????????? ????? ??? ??????? ? pipe
  • - ???????? ??????? ????????? ????? ? pipe
  • - ???????? ??????? ??? ?????? ? fork
  • - ?????? ??????? ???????? ?????????? ?? ??????, ?
    ???? ?? ?????? ? ?????????? ?????.

9
??????. ??????????? ?? ???? ??? ??? ? pipe
  • include "ourhdr.h"
  • main(void)
  • int pd2, n
  • pid_t pid
  • char buff1024
  • if (pipe(pd) lt 0) / parent creates a pipe
    /
  • perror("pipe error") exit(1)
  • if ((pid fork()) lt 0)
  • perror("fork error") exit(1)
  • else if ( pid gt 0) / in parent process /
  • close(pd0)
  • write(pd1, "Hello World\n", 12)
  • else / in child process /
  • close(pd1)
  • n read(pd0, buff, 1024)
  • write(1, buff, n)
  • exit(0)

10
??????. ???????????? ?? ???????? "ls wc -l"
  • ????? ???????
  • ????????, ? ????? ?????? ??????????, ??????? ???
    ???? ????? ???? ???????????? ??.
  • ????? ??????? ????????? ????? ? ???? ??????? ???
    (???? ?? ???????, ? ????? ?????? ??????????).
  • ????????-??? ??????? ?????????? wc.
  • ????????-???? ??????? ?????????? ls.
  • ????????-?????? ???? ???????????? ?? ???????-???.
  • ????? ???????
  • ????? ??????? ?? ????????? ls ? wc ?? ?????? ??
    ???????, ? ????? ?????? ??????????.
  • ????????? ?? ls ? wc ?? ? ???? ????? ? ????? ls.
  • ????????-???? ???? ???????????? ?? ?????? ???
    (wc).

11
????? ??????? (1)
  • include "ourhdr.h"
  • main (void)
  • int pd2, status
  • pid_t pid
  • if ( (pid fork() ) lt 0 )
  • err_sys_exit("fork error")
  • if ( pid 0 ) / in child /
  • if (pipe(pd) lt 0)
  • err_sys_exit("pipe error")
  • if ( (pid fork() ) lt 0)
  • err_sys_exit("fork error")
  • else if ( pid 0 ) / in grandchild /
  • close(1)
  • dup(pd1)
  • close(pd0)
  • close(pd1)
  • execlp("ls", "ls", 0)
  • err_sys_exit("exec ls error")

12
????? ??????? (2)
  • else / in child /
  • close(0)
  • dup(pd0)
  • close(pd0)
  • close(pd1)
  • execlp("wc", "wc", "-l", 0)
  • err_sys_exit("exec wc error")
  • wait(status) / in parent /
  • printf("Parent after end of pipe statusd\n",
    status)
  • exit(0)

13
????? ??????? (?????? ???????)
  • main (void)
  • int pd2, status
  • if ( fork() 0 ) / in child /
  • pipe(pd)
  • if ( fork() 0) / in grandchild /
  • close(1)
  • dup(pd1)
  • close(pd0)
  • close(pd1)
  • execlp("ls", "ls", 0)
  • perror("exec ls error") exit(1)
  • else / in child /
  • close(0)
  • dup(pd0)
  • close(pd0)
  • close(pd1)
  • execlp("wc", "wc", "-l", 0)
  • perror("exec wc error") exit(1)

14
????? ??????? (1)
  • include "ourhdr.h"
  • main (void)
  • int pd2, status
  • pid_t pid, pid2
  • if ( pipe(pd) lt 0 )
  • err_sys_exit("pipe error")
  • if ( (pid fork() ) lt 0 )
  • err_sys_exit("fork error for first child")
  • if ( pid 0 ) / in first child /
  • setpgrp()
  • printf("First pidd, grpd, ppidd\n",
    getpid(), getpgrp(), getppid())
  • close(1)
  • dup(pd1)
  • close(pd0)
  • close(pd1)
  • execlp("ls", "ls", "-l", 0)
  • err_sys_exit("exec ls error")

15
????? ??????? (2)
  • if ( (pid2 fork() ) lt 0 ) / in parent /
  • err_sys_exit("fork error for second child")
  • if ( pid2 0 ) / in second child /
  • setpgid(0, pid)
  • printf("Second pidd, grpd,
    ppidd\n",getpid(),getpgrp(),getppid())
  • close(0)
  • dup(pd0)
  • close(pd0)
  • close(pd1)
  • execlp("wc", "wc", "-l", 0)
  • err_sys_exit("exec wc error")
  • close(pd0) / in parent /
  • close(pd1)
  • waitpid(pid2, status, 0)
  • printf("Parent after end of pipe statusd\n",
    status)
  • exit(0)

16
????????? ?? FIFO ????
  • int mknod(const char filename, mode_t mode,
  • dev_t dev)
  • ????? 0 ??? ?????, -1 ??? ??????.
  • int mkfifo(const char filename,
  • mode_t mode)
  • ????? 0 ??? ?????, -1 ??? ??????.
  • ??????
  • int mknod("fifoname", S_IFIFO0600, 0)
  • int mkfifo("fifoname", 0600)

17
??????. ??????????? ?? ???? ??? ??? ? FIFO ????
  • include "ourhdr.h"
  • include ltfcntl.hgt
  • main(int argc, char argv)
  • int fdr, fdw, n
  • pid_t pid
  • char buffMAXLINE
  • if (argc lt 2)
  • err_exit("usage a.out fifo_name")
  • if (mknod(argv1, S_IFIFO0600, 0) lt 0)
    / or mkfifo(argv1, 0600) /
  • err_sys_exit("make fifo error")
  • if ((pid fork()) lt 0)
  • err_sys_exit("fork error")
  • else if (pid gt 0) / in parent /
  • if ((fdw open(argv1, O_WRONLY)) -1)
  • err_sys_exit("open for write error")
  • write(fdw, "Hello World\nHello World\n",
    24)
  • else / in child /
  • if ((fdr open(argv1, O_RDONLY)) -1)

18
???????? ?? ??????????
  • a.out myfifo
  • Hello World
  • ls -l myfifo
  • prw------- 1 moni staff 0 Jul 21 1022 myfifo
  • rm myfifo

19
??????. ????????????-?????????? ? FIFO ????
  • / Producer with fifo file /
  • include "ourhdr.h"
  • include ltfcntl.hgt
  • main(int argc, char argv)
  • int fd, n
  • if (argc lt 2)
  • err_exit("usage prod fifo_name")
  • if (mkfifo(argv1, 0600) lt 0)
  • err_sys_exit("prod make fifo error")
  • if ((fd open(argv1, O_WRONLY)) lt 0)
  • err_sys_exit("prod open fifo error")
  • for (n1 nlt100 n)
  • if ( write(fd, n, sizeof(int)) lt 0 )
  • err_sys_exit("write error")

20
  • / Consumer with fifo file /
  • include "ourhdr.h"
  • include ltfcntl.hgt
  • main(int argc, char argv)
  • int fd, n
  • int buff
  • if (argc lt 2)
  • err_exit("usage cons fifo_name")
  • if ( (fd open(argv1, O_RDONLY) ) lt 0)
  • err_sys_exit("cons open fifo error")
  • while ( ( n read(fd, buff, sizeof(int)) ) gt
    0 )
  • buff 2
  • printf("Consumer d \n", buff)
  • if (unlink(argv1) -1)

21
???????? ?? ??????????
  • prod myfifo
  • 1 5808
  • cons myfifo
  • Consumer 2
  • Consumer 4
  • Consumer 6
  • Consumer 8
  • Consumer 10
  • Consumer 12
  • . . .
  • Consumer 200
  • 1 Done prod myfifo
  • ls -l myfifo
  • ls myfifo No such file or directory
Write a Comment
User Comments (0)
About PowerShow.com