λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Basics/Develop

[μ‹œμŠ€ν…œν”„λ‘œκ·Έλž˜λ°]νŒŒμ΄ν”„ 톡신

by IworldT 2020. 11. 9.
λ°˜μ‘ν˜•

νŒŒμ΄ν”„ ν†΅μ‹ μ˜ κ°œλ…κ³Ό κ°„λ‹¨ν•œ 예제 μ½”λ“œλ₯Ό ν¬μŠ€νŒ…ν•˜λ„λ‘ ν•˜κ² λ‹€.

νŒŒμ΄ν”„μ˜ κ°œλ…

  • 두 ν”„λ‘œμ„ΈμŠ€ κ°„ 톡신을 μ§€μ›ν•˜λŠ” 특수 파일
  • κ·Έλƒ₯ νŒŒμ΄ν”„(μ΄λ¦„μ—†λŠ” νŒŒμ΄ν”„)λŠ” λΆ€λͺ¨-μžμ‹ ν”„λ‘œμ„ΈμŠ€ 톡신을 지원
  • 기본적으둜 단방ν–₯이닀.
  • μ–‘λ°©ν–₯ 톡신을 μœ„ν•΄μ„œλŠ” νŒŒμ΄ν”„λ₯Ό 2개 μƒμ„±ν•˜λ©΄ λœλ‹€.

 

νŒŒμ΄ν”„ λ§Œλ“€κΈ°

int pipe(int fd[2]);둜 μƒμ„±ν•œλ‹€. 이 λ•Œ, fd[0]은 읽기, fd[1]은 μ“°κΈ°μš©μ΄λ‹€.

이 읽기와 μ“°κΈ°λ₯Ό μ–΄λ–»κ²Œ μ‚¬μš©ν•˜λŠλƒμ— 따라 λ°©ν–₯이 κ²°μ •λœλ‹€κ³  보면 λœλ‹€.

μ„±κ³΅μ‹œ 0, μ‹€νŒ¨μ‹œ -1을 λ°˜ν™˜ν•΄μ€€λ‹€.

pipe ν•¨μˆ˜μ˜ 톡신 κ³Όμ •

1. pipe ν•¨μˆ˜ 호좜 ν›„ 파일기술자(fd[])생성. 

 

2. fork() ν•¨μˆ˜λ‘œ μžμ‹ ν”„λ‘œμ„ΈμŠ€λ₯Ό μƒμ„±ν•˜λ©΄, pipe도 ν•¨κ»˜ λ³΅μ‚¬λœλ‹€. κ·ΈλŸ¬λ‚˜ λ°”λ‘œ μˆ˜ν–‰λ˜λŠ” 것은 μ•„λ‹ˆλ‹€!!

 

3. 단방ν–₯의 경우 톡신 λ°©ν–₯을 κ²°μ •ν•œλ‹€.

이 λ•Œ read와 write 파일 기술자λ₯Ό 각각 λˆ„κ°€ μ‚¬μš©ν•˜λŠλƒμ— λ”°λΌμ„œ λ°©ν–₯이 κ²°μ •λœλ‹€. 사진은 λΆ€λͺ¨κ°€ μ“°κ³  μžμ‹μ΄ μ½λŠ” λΆ€λͺ¨ -> μžμ‹ λ°©ν–₯ 톡신이닀.

 

4.μ–‘λ°©ν–₯의 경우 파일기술자λ₯Ό ν•˜λ‚˜ 더 μƒμ„±ν•˜μ—¬, 단방ν–₯ νŒŒμ΄ν”„λ₯Ό 2개 μ‚¬μš©ν•˜λŠ” μ›λ¦¬λ‘œ μ–‘λ°©ν–₯ 톡신을 ν•œλ‹€.

 

예제 μ½”λ“œ 및 문제

예제 μ½”λ“œ : λΆ€λͺ¨-μžμ‹ μ–‘λ°©ν–₯ 톡신

...

07  int main(void) {

08      int fd1[2], fd2[2];

09      pid_t pid;

10      char buf[257];

11      int len, status;

12

13      if (pipe(fd1) == -1) {

14          perror("pipe");

15          exit(1);

16      }

17 

18      if (pipe(fd2) == -1) {

19          perror("pipe");

20          exit(1);

21      }

22 

23      switch (pid = fork()) {

24          case -1 :

25              perror("fork");

26              exit(1);

27              break;

28          case 0 : /* child */

29              close(fd1[1]);

30              close(fd2[0]);

31              write(1, "Child Process:", 15);

32              len = read(fd1[0], buf, 256);

33              write(1, buf, len);

34 

35              strcpy(buf, "Good\n");

36              write(fd2[1], buf, strlen(buf));

37              break;

38          default :

39              close(fd1[0]);

40              close(fd2[1]);

41              buf[0] = '\0';

42              write(fd1[1], "Hello\n", 6);

43 

44              write(1, "Parent Process:", 15);

45              len = read(fd2[0], buf, 256);

46              write(1, buf, len);

47              waitpid(pid, &status, 0);

48              break;

49      }

50 

51      return 0;

52  }

 

μ‹€ν–‰ κ²°κ³Ό

Child Process:Hello

Parent Process:Good

 

μ‘μš© 문제 μ½”λ“œ : λΆ€λͺ¨-μžμ‹ μ–‘λ°©ν–₯ νŒŒμ΄ν”„λ‘œ μˆœμ„œλŒ€λ‘œ μ—¬λŸ¬ 번 ν†΅μ‹ ν•˜κΈ°

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>

int main(void) {
        int fd1[2], fd2[2];
        pid_t pid;
        char buf[257], str[257];
        int len, status, i, j;

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

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

        switch (pid = fork()) {
            case -1 :
                perror("fork");
                exit(1);
                break;

                case 0 :
                close(fd1[1]);
                close(fd2[0]);
                for(i=1; i<11; i++){
sleep(1);
str[0]='\0';
sprintf(str, "Child Process %d : ", i);
write(1, str, 17);
len=read(fd1[0],buf,256);
write(1,buf,len);

buf[0]='\0';
sprintf(buf, "Good %d",i);
strcat(buf,"\n");
write(fd2[1],buf,strlen(buf));
sleep(1);
    }
                break;
            default :
                close(fd1[0]);
                close(fd2[1]);
                buf[0] = '\0';
    str[0]='\0';
                for(j=1;j<11;j++){
buf[0]='\0';
sleep(1);
sprintf(buf, "Hello %d", j);
strcat(buf,"\n");
write(fd1[1], buf, strlen(buf));
sleep(1);

sprintf(str,"Parent Process %d : ",j);
write(1, str, 19);
len=read(fd2[0],buf,256);
write(1,buf,len);
   }
                waitpid(pid, &status, 0);
                break;
        }

        return 0;
    }

μ‹€ν–‰ κ²°κ³Ό

μžμ‹ ν”„λ‘œμ„ΈμŠ€ : λΆ€λͺ¨ν”„λ‘œμ„ΈμŠ€ 응닡

λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€ : μžμ‹ν”„λ‘œμ„ΈμŠ€ 응닡

이 μˆœμ„œλŒ€λ‘œ, μ°¨λ‘€μ°¨λ‘€ λ‚˜νƒ€λ‚œλ‹€.(sleep(1) μ‚¬μš©)

 

이미지 파일 좜처 : μœ λ‹‰μŠ€ μ‹œμŠ€ν…œ ν”„λ‘œκ·Έλž˜λ°(ν•œλΉ›λ―Έλ””μ–΄)

728x90
λ°˜μ‘ν˜•

λŒ“κΈ€