Skip to content

Latest commit

ย 

History

History
1749 lines (1352 loc) ยท 48.1 KB

File metadata and controls

1749 lines (1352 loc) ยท 48.1 KB

UnixProgramming

20181029

  • p8-1.c Parent process๋Š” ์„ธ ๊ฐœ์˜ child process๋ฅผ ๋งŒ๋“ค๊ณ , 1์ดˆ ๊ฐ„๊ฒฉ์œผ๋กœ ๊ฐ child process์—๊ฒŒ SIGUSR1 signal์„ ๋ณด๋‚ธ ํ›„, child process๋“ค์ด ์ข…๋ฃŒ ํ•  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ ํ•˜์˜€๋‹ค๊ฐ€, ์ข…๋ฃŒํ•œ child process์˜ id์™€ exit status๋ฅผ ์ถœ๋ ฅ ํ›„ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ child process๋Š” ์ƒ์„ฑ ํ›„ parent๋กœ๋ถ€ํ„ฐ signal์ด ์˜ฌ ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ ํ•˜์˜€๋‹ค๊ฐ€ (pause() ์‚ฌ์šฉ), signal์„ ๋ฐ›์œผ๋ฉด ์ž์‹ ์˜ process id๋ฅผ ์„ธ ๋ฒˆ ์ถœ๋ ฅ ํ•œ ํ›„ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ ํ•˜์‹œ์˜ค.

    #define BUFSIZE 512
    
    void catchusr(int);
    
    void do_child(int i){
            int pid;
            static struct sigaction act;
    
            act.sa_handler = catchusr;
            sigaction(SIGUSR1, &act, NULL);
    
            printf("%d-th child is created...\n", i);
    
            pause();
    
            pid = getpid();
    
            for(i=0;i<3;i++){
                    printf("[child process id] : %d ...\n", getpid());
            }
    
            exit(0);
    }
    
    void catchusr(int signo){
            printf(".....catch SIGUSR1 = %d\n", signo);
    }
    
    int main(int argc, char **argv){
    
            int i, k, status;
            pid_t pid[3];
    
            for(i=0;i<3;i++){
                    pid[i] =fork();
                    if(pid[i] == 0){
                            do_child(i);
                    }
            }
    
            for(i=0;i<3;i++){
                    sleep(1);
                    kill(pid[i], SIGUSR1);
            }
    
            for(i=0;i<3;i++){
                    k = wait(&status);
                    printf("child #%d is terminated...%d\n", k, WEXITSTATUS(status));
            }
    
            exit(0);
    }
  • P8-2.c Parent process๋Š” ๋‹ค์„ฏ ๊ฐœ์˜ child process๋“ค์„ ๋งŒ๋“ค๊ณ , ๋Œ€๊ธฐํ•˜์˜€๋‹ค๊ฐ€ child process๊ฐ€ ๋ชจ๋‘ ์ข…๋ฃŒ ํ•œ ํ›„ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ child process๋Š” ์ž์‹ ์˜ ์ˆœ์„œ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ ํ•˜์˜€๋‹ค๊ฐ€, 1์ดˆ์”ฉ ์‰ฌ๋ฉด์„œ (sleep (1); ์‚ฌ์šฉ) ์ž์‹ ์˜ process id(getpid(); ์‚ฌ์šฉ)๋ฅผ 5ํšŒ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์„ ํ•œ ํ›„ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค. child process์˜ id ์ธจ๋ ฅ ์ˆœ์„œ๋Š” ์ƒ์„ฑ ์ˆœ์„œ์˜ ์—ญ์ˆœ์ด๋ฉฐ, ์ด์™€ ๊ฐ™์€ ์ˆœ์„œ ๋™๊ธฐํ™” ์ž‘์—…์€ signal์„ ๋ณด๋‚ด์„œ ์ง„ํ–‰ ํ•ฉ๋‹ˆ๋‹ค.

    • ์ƒ์„ฑ ์ˆœ์„œ์˜ ์—ญ์ˆœ์œผ๋กœ ์ž‘์—… ์ˆ˜ํ–‰ํ•ด์•ผํ•˜๋‹ˆ๊นŒ, ์ฒซ ๋ฒˆ์งธ๋กœ ๋งŒ๋“ค์–ด์ง„ child ์ด์™ธ์˜ child๋“ค์€ pause๋กœ ๊ธฐ๋‹ค๋ฆผ
    void catchusr(int);
    
    void do_child(int i, int *pid_array){
            int j, pid;
            static struct sigaction act;
    
            act.sa_handler = catchusr;
            sigaction(SIGUSR1, &act, NULL);
    
            printf("%d-th child is created ... pid = %d\n", i, getpid());
    
            //์ƒ์„ฑ ์ˆœ์„œ์˜ ์—ญ์ˆœ์œผ๋กœ ์ž‘์—… ์ˆ˜ํ–‰ํ•ด์•ผํ•˜๋‹ˆ๊นŒ, ์ฒซ ๋ฒˆ์งธ๋กœ ๋งŒ๋“ค์–ด์ง„ child ์ด์™ธ์˜ child๋“ค์€ pause๋กœ ๊ธฐ๋‹ค๋ฆผ
            if(i<4)
                    pause();
    
            pid = getpid();
    
            for(j=0;j<5;j++){
                    printf("[child process id] : %d ...\n", getpid());
                    sleep(1);
            }
    
            if(i>0){
                    kill(pid_array[i-1], SIGUSR1);
            }
    
            exit(0);
    }
    
    void catchusr(int signo){
            printf(".....catch SIGUSR1 = %d\n", signo);
    }
    
    int main(int argc, char **argv){
    
            int i, status;
            pid_t pid[5];
    
            for(i=0;i<5;i++){
                    pid[i] =fork();
                    if(pid[i] == 0){
                            do_child(i, pid);
                    }
            }
    
            for(i=0;i<5;i++){
                    wait(&status);
            }
    
            exit(0);
    }
  • P8-3.c 10๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ๊ทธ ํ•ฉ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, 10์ดˆ๊ฐ„ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์˜ ์ž…๋ ฅ์ด ์—†์œผ๋ฉด, ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋‹ค์‹œ ์ž…๋ ฅ์„ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <string.h>
    #include <time.h>
    #include <ftw.h>
    #include <stdlib.h>
    
    #define BUFSIZE 512
    
    void catchalarm(int);
    
    int main(int argc, char **argv){
    
            int i, num, sum = 0;
            static struct sigaction act;
    
            act.sa_handler = catchalarm;
            sigaction(SIGALRM, &act, NULL);
    
            for(i=0;i<10;i++){
                    do{
                            alarm(10);
                    }while(scanf("%d", &num) < 0);
                    alarm(0);
                    sum += num;
                    printf("sum = %d\n", sum);
            }
            exit(0);
    }
    
    void catchalarm(int signo){
            printf("[Warning]No input for 10 seconds ... %d\n", signo);
    }

20181031

  • ์›๊ฒฉ ์„œ๋ฒ„์˜ ๋””๋ ‰ํ† ๋ฆฌ ๋กœ์ปฌ์— ์ €์žฅ. scp ๋ช…๋ น

    • -r ์˜ต์…˜ ์ฃผ๋ฉด ๋””๋ ‰ํ† ๋ฆฌ ๋ณต์‚ฌ ๊ฐ€๋Šฅ
    • -r ์˜ต์…˜ ์•ˆ ์ฃผ๋ฉด ํŒŒ์ผ๋งŒ ๋ณต์‚ฌ ๊ฐ€๋Šฅ
    $ scp -P 1074 -r s13011022@203.250.148.93:/home/account/class/tspark/s13011022 ./unix
    
    [์ฐธ๊ณ ]
    http://faq.hostway.co.kr/?mid=Linux_ETC&page=8&document_srl=1426
    https://skylit.tistory.com/90
    

20181105

  • p9-1 ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘์„ ์ด์šฉํ•œ ๋‘ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑ ํ•ฉ๋‹ˆ๋‹ค.

    • (a) reader ํ”„๋กœ๊ทธ๋žจ์€ โ€œtemp" ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘ ํ•œ ํ›„, scanf()๋กœ 10๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ฝ์–ด์„œ ๋งคํ•‘๋œ ํŒŒ์ผ์— ์ €์žฅํ•˜๋Š” ์ž‘์—…์„ 10ํšŒ ์‹คํ–‰ ํ•ฉ๋‹ˆ๋‹ค.

      #define BUFSIZE 512
      
      int main(int argc, char **argv){
              int i, fd;
              int *addr;
      
              fd = open("temp", O_RDWR | O_CREAT, 0600);
              addr = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
          
              ftruncate(fd, sizeof(int)*10);
      
              for(i=0;i<10;i++){
                      scanf("%d", addr+i);
              }
              exit(0);
      }
    • (b) writer ํ”„๋กœ๊ทธ๋žจ์€ โ€œtemp" ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘ ํ•œ ํ›„, ๋งคํ•‘๋œ ํŒŒ์ผ์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์„ 10ํšŒ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค (printf() ์‚ฌ์šฉ). ๋‹จ, ์ถœ๋ ฅ ํ”„๋กœ๊ทธ๋žจ์€ 5์ดˆ๊ฐ„ sleep() ํ•œ ํ›„ 5ํšŒ์˜ ์ถœ๋ ฅ ์ž‘์—…์„ ์—ฐ์† ์ง„ ํ–‰ ํ•˜๊ณ  ๋‹ค์‹œ 5์ดˆ๊ฐ„ sleep() ํ•œ ํ›„ 5ํšŒ์˜ ์ถœ๋ ฅ ์ž‘์—…์„ ์ง„ํ–‰ ํ•ฉ๋‹ˆ๋‹ค.

      #define BUFSIZE 512
      
      int main(int argc, char **argv){
              int i, j, fd;
              int *addr;
      
              fd = open("temp", O_RDWR | O_CREAT, 0600);
              addr = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
      
          	//๊ตณ์ด sleepํ•ด์ฃผ๋Š”๊ฑด ๋™๊ธฐํ™” ๋•Œ๋ฌธ์—
              sleep(5);
      
              for(i=0;i<10;i++){
                      sleep(5);
                      for(j=0;j<5;j++){
                              printf("%d ", *(addr+i));
                      }
                      printf("\n");
              }
              exit(0);
      }
    • (c) ๋‘ ํ”„๋กœ๊ทธ๋žจ์ด ๋ชจ๋‘ ์ข…๋ฃŒ ํ•œ ํ›„ โ€temp" ํŒŒ์ผ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

      40์œผ๋กœ ๋™์ผ
  • P9-2 ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘์„ ์ด์šฉํ•œ ๋‘ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑ ํ•ฉ๋‹ˆ๋‹ค.

    • (a) reader ํ”„๋กœ๊ทธ๋žจ์€ โ€œtemp" ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘ ํ•œ ํ›„, ์™ธ๋ถ€ ์ž…๋ ฅ์„ ์ฝ์–ด์„œ (read() ์‹œ์Šคํ…œ ์ฝœ ์‚ฌ์šฉ) ๋งคํ•‘๋œ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๋Š” ์ž‘์—…์„ 3ํšŒ ์‹คํ–‰ ํ•ฉ๋‹ˆ๋‹ค.

      #include <stdio.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <sys/wait.h>
      #include <fcntl.h>
      #include <unistd.h>
      #include <dirent.h>
      #include <string.h>
      #include <time.h>
      #include <ftw.h>
      #include <stdlib.h>
      #include <sys/mman.h>
      
      #define BUFSIZE 512
      
      int main(int argc, char **argv){
      
              int i, fd, len = 0;
              char *addr;
      
              fd = open("temp", O_RDWR | O_CREAT, 0600);
      
              addr = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
      
              ftruncate(fd, BUFSIZE);
              for(i=0;i<3;i++){
                      len = len + read(0, addr + len, BUFSIZE);
                      printf("[read] : %d\n", len);
                      if(len > BUFSIZE)
                              break;
              }
      
              exit(0);
      }
    • (b) writer ํ”„๋กœ๊ทธ๋žจ์€ โ€œtemp" ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘ ํ•œ ํ›„, ๋งคํ•‘๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์„ 3ํšŒ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค (write() ์‹œ์Šคํ…œ ์ฝœ ์‚ฌ์šฉ). ๋‹จ. 3์ดˆ๊ฐ„ sleep() ํ•˜๋ฉด์„œ ์ถœ๋ ฅ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

      #include <stdio.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <sys/wait.h>
      #include <fcntl.h>
      #include <unistd.h>
      #include <dirent.h>
      #include <string.h>
      #include <time.h>
      #include <ftw.h>
      #include <stdlib.h>
      #include <sys/mman.h>
      
      #define BUFSIZE 512
      
      int main(int argc, char **argv){
      
              int i, fd, len = 0;
              char *addr;
      
              fd = open("temp", O_RDWR | O_CREAT, 0600);
      
              addr = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
              printf("[test]%s\n", addr);
      
              for(i=0;i<3;i++){
                      sleep(3);
                      len = len + write(1, addr + len, BUFSIZE);
                      write(1, "-------\n", 8);
                      if(len > BUFSIZE)
                              break;
              }
      
              exit(0);
      }
  • p9-3.c Parent process๋Š” ์„ธ ๊ฐœ์˜ child process๋“ค์„ ๋งŒ๋“ค๊ณ , ๋ชจ๋“  child process๊ฐ€ ์ข…๋ฃŒ ํ•œ ํ›„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. ๊ฐ child process๋Š” ์ž์‹ ์˜ ์ˆœ์„œ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ ํ•˜์˜€๋‹ค๊ฐ€, 1์ดˆ์”ฉ ์‰ฌ๋ฉด์„œ ์ž์‹ ์˜ process id๋ฅผ 5 ํšŒ ์ถœ๋ ฅ ํ•œ ํ›„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. child process์˜ id ์ธจ๋ ฅ ์ˆœ์„œ๋Š” ์ƒ์„ฑ ์ˆœ์„œ์˜ ์—ญ์ˆœ์ด๋ฉฐ, ์ด์™€ ๊ฐ™์€ ์ˆœ์„œ ๋™ ๊ธฐํ™” ์ž‘์—…์€ ๋งคํ•‘๋œ ํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ ์ง„ํ–‰ ํ•ฉ๋‹ˆ๋‹ค.

    • ์ผ์ผ์ด ๋ฉ”๋ชจ๋ฆฌ ๋งตํ•‘ํ•˜์ง€ ๋ง๊ณ , ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘ํ•œ ๋ฐฐ์—ด์„ child ํ•จ์ˆ˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค˜์„œ ์‹คํ–‰ํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ ์ž‘์„ฑ ๊ฐ€๋Šฅ
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <string.h>
    #include <time.h>
    #include <ftw.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    
    #define BUFSIZE 512
    
    void do_child(int i, int *addr){
            int fd, child, j;
    
            child = *(addr + 0);
    
            while(i != child){
                    sleep(2);
                    printf(".........................pid : %d waiting\n", i);
    
                    child = *(addr + 0);
            }
    
            for(j=0;j<5;j++){
                    sleep(1);
                    printf("%dth child pid = %d\n", i, getpid());
            }
            *(addr + 0) = child - 1;
    
            exit(0);
    }
    
    int main(int argc, char **argv){
    
            int i, status, fd;
            pid_t pid[3];
            int *addr;
    
            fd = open("temp", O_RDWR | O_CREAT | O_TRUNC, 0600);
            addr = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
            ftruncate(fd, sizeof(int));
            *(addr + 0) = 2;
    
            for(i=0;i<3;i++){
                    pid[i] =fork();
                    if(pid[i] == 0){
                            do_child(i, addr);
                    }
            }
    
            for(i=0;i<3;i++){
                    wait(&status);
            }
    
            exit(0);
    }
    • ๊ต์ˆ˜๋‹˜ ๋‹ต์•ˆ

      • ์ผ์ผ์ด ๋ฉ”๋ชจ๋ฆฌ ๋งตํ•‘ํ•˜์ง€ ๋ง๊ณ , ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘ํ•œ ๋ฐฐ์—ด์„ child ํ•จ์ˆ˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค˜์„œ ์‹คํ–‰ํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ ์ž‘์„ฑ ๊ฐ€๋Šฅ
      #include <stdio.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <sys/wait.h>
      #include <fcntl.h>
      #include <unistd.h>
      #include <dirent.h>
      #include <string.h>
      #include <time.h>
      #include <ftw.h>
      #include <stdlib.h>
      #include <sys/mman.h>
      
      #define BUFSIZE 512
      
      void do_child(int i, int *turn){
              int j, pid;
      
              if(i<2){
                      do{
                      }while(i!=*turn);
              }
      
              pid = getpid();
      
              for(j=0;j<5;j++){
                      printf("child %d ... \n", pid);
                      sleep(1);
              }
      
              if(i>0)
                      *turn = i-1;
      
              exit(0);
      }
      
      int main(int argc, char **argv){
              int i, status, fd;
              pid_t pid[3];
              int *addr;
      
              fd = open("temp", O_RDWR | O_CREAT | O_TRUNC, 0600);
              addr = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
              ftruncate(fd, sizeof(int));
              *addr = 2;
      
              for(i=0;i<3;i++){
                      pid[i] =fork();
                      if(pid[i] == 0){
                              do_child(i, addr);
                      }
              }
      
              for(i=0;i<3;i++){
                      wait(&status);
              }
      
              exit(0);
      }

20181107

  • p10-1.c parent process๋Š” ์„ธ ๊ฐœ์˜ child process๋ฅผ ๋งŒ๋“ค๊ณ , parent process์—์„œ child process๋กœ์˜ ๋‹จ๋ฐฉํ–ฅ ํ†ต์‹ ์ด ๊ฐ€๋Šฅ ํ•œ pipe๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. parent process๋Š” ์™ธ๋ถ€ ์ž…๋ ฅ์œผ๋กœ ์ •์ˆ˜๋ฅผ 12๊ฐœ ์ž…๋ ฅ ๋ฐ›์•„, ์„ธ child process์—๊ฒŒ ์ˆœ์„œ๋Œ€๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. child process๋Š” ์ž์‹ ์ด ๋ฐ›์€ ์ •์ˆ˜๋ฅผ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id์™€ ํ•จ๊ป˜ ์ถœ๋ ฅ ํ•ฉ๋‹ˆ๋‹ค. parent process๋Š” ๋ชจ๋“  ์ •์ˆ˜๋ฅผ ์ „๋‹ฌ ํ•œ ํ›„ ์ •์ˆ˜ -1์„ ์ „๋‹ฌํ•˜๊ณ , -1์„ ์ „๋‹ฌ ๋ฐ›์œผ๋ฉด child process๋Š” ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  child process์˜ ์ข…๋ฃŒ๋ฅผ ํ™•์ธ ํ•œ ํ›„ parent process๋Š” ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค.

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <string.h>
    #include <time.h>
    #include <ftw.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    
    #define BUFSIZE 512
    
    void do_child(int id, int p[3][2]){
        int i, in, pid = getpid();
    
        for(i=0;i<3;i++){
            close(p[i][1]);
            if(id != i)
                close(p[i][0]);
        }
    
        while(1){
            read(p[id][0], &in, sizeof(int));
            if(in == -1)
                exit(0);
            else
                printf("id : %d, pid : %d, input : %d\n", id, pid, in);
        }
    }
    
    int main(int argc, char **argv){
        int i, in, pid, p[3][2];
    
        for(i=0;i<3;i++){
            pipe(p[i]);
        }
    
        for(i=0;i<3;i++){
            if(fork() == 0){
                do_child(i, p);
            }
        }
    
        for(i=0;i<3;i++){
            close(p[i][0]);
        }
    
        for(i=0;i<12;i++){
            scanf("%d", &in);
            write(p[i%3][1], &in, sizeof(int));
        }
    
        in = -1;
    
        for(i=0;i<3;i++){
            write(p[i][1], &in, sizeof(int));
        }
    
        for(i=0;i<3;i++){
            wait(0);
        }
    
        exit(0);
    }
  • p10-2.c parent process๋Š” ์„ธ ๊ฐœ์˜ child process๋ฅผ ๋งŒ๋“ค๊ณ , ๋ชจ๋“  child process๊ฐ€ ์ข…๋ฃŒ ํ•œ ํ›„ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ child process๋Š” ์ž์‹ ์˜ ์ˆœ์„œ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ ํ•˜์˜€๋‹ค๊ฐ€, 1์ดˆ์”ฉ ์‰ฌ๋ฉด์„œ (sleep (1); ์‚ฌ์šฉ) ์ž์‹ ์˜ process id(getpid(); ์‚ฌ์šฉ)๋ฅผ 5ํšŒ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์„ ํ•œ ํ›„ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค. child process์˜ id ์ธจ๋ ฅ ์ˆœ์„œ๋Š” ์ƒ์„ฑ ์ˆœ์„œ์˜ ์—ญ์ˆœ์ด๋ฉฐ, ์ด์™€ ๊ฐ™์€ ์ˆœ์„œ ๋™๊ธฐํ™” ์ž‘์—…์€ pipe๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง„ํ–‰ ํ•ฉ๋‹ˆ๋‹ค.

    • signal์—์„œ์˜ pause / kill์€,,, pipe์—์„œ read / write๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
    • pipe์—์„œ ๊ธฐ๋ณธ์œผ๋กœ blocking read๋กœ ์„ค์ •์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— write๊ฐ€ ์˜ค๊ธฐ ์ „๊นŒ์ง€ read๋Š” block์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋‹ค๋ฆด ์ˆ˜ ์žˆ๋‹ค
    #define BUFSIZE 512
    
    void do_child(int id, int pipe[2][2]){
            char buffer = 'a';
            int i, pid = getpid();
    
            if(id < 2){
                    read(pipe[id][0], &buffer, 1);
            }
    
            for(i=0;i<5;i++){
                    sleep(1);
                    printf("id : %d, pid : %d\n", id, pid);
            }
    
            if(id > 0){
                    write(pipe[id-1][1], &buffer, 1);
            }
    
            exit(0);
    }
    
    int main(int argc, char **argv){
            int i, status, fd, p[2][2];
            pid_t pid[3];
    
            for(i=0;i<2;i++){
                    pipe(p[i]);
            }
    
            for(i=0;i<3;i++){
                    pid[i] =fork();
                    if(pid[i] == 0){
                            do_child(i, p);
                    }
            }
    
            for(i=0;i<3;i++){
                    wait(&status);
            }
    
            exit(0);
    }

20181112

  • p11-1 fifo๋ฅผ ์ด์šฉํ•˜์—ฌ ํ†ต์‹ ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ A๋Š” ์™ธ๋ถ€ ์ž…๋ ฅ์œผ๋กœ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ํ”„๋กœ๊ทธ๋žจ B์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ B๋Š” ์ „๋‹ฌ ๋ฐ›์€ ์ •์ˆ˜์— +8์„ ํ•œ ๋’ค ํ”„๋กœ๊ทธ๋žจ A์— ๋Œ๋ ค์ค๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ A๋Š” ๋Œ๋ ค๋ฐ›์€ ์ •์ˆ˜ ๊ฐ’์„ ์ถœ๋ ฅ ํ•ฉ๋‹ˆ๋‹ค. -1์ด ์ž…๋ ฅ๋˜๋ฉด ๋‘ ํ”„๋กœ๊ทธ๋žจ์€ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

    • O_WRONLY, O_RDONLY ๋ช…์‹œํ•ด์ฃผ๋Š” ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•จ

      //A
      fd[0] = open(fifo[0], O_WRONLY);
      fd[1] = open(fifo[1], O_RDONLY);
      //B
      fd[0] = open(fifo[0], O_RDONLY);
      fd[1] = open(fifo[1], O_WRONLY);
    • fifo f[0]์„ ์ด์šฉํ•œ ํŒŒ์ดํ”„ fd[0]์„ A์—์„œ WRONLY๋กœ ๋งŒ๋“ค์—ˆ์œผ๋ฉด RDONLY๋กœ ์ง์„ ๋งž์ถฐ์ค˜์•ผํ•˜๋Š”๋ฐ, ์•„๋‹ˆ๊ฒŒ ๋˜๋ฉด ์„œ๋กœ๋ฅผ ๊ณ„์†ํ•ด์„œ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ ๋˜๋Š” deadlock ์ƒํƒœ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค. ๋‘ ๊ฐœ๋งŒ ์žˆ์„ ๋•Œ๋Š” ์ ์–ด์„œ ํฐ ๋ฌธ์ œ๊ฐ€ ์•ˆ ์ƒ๊ธฐ๋Š”๋ฐ, ๋” ๋งŽ์€ ํ”„๋กœ๊ทธ๋žจ์ด ๊ฐœ์ž…์„ ํ•˜๊ฒŒ๋˜๋ฉด ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

    p11-1A.c

    int main(int argc, char **argv){
            char fifo[2][3] = {"f1", "f2"};
            int i, in, fd[2];
    
            for(i=0;i<2;i++){
                    mkfifo(fifo[i], 0600);
            }
    
            fd[0] = open(fifo[0], O_WRONLY);
            fd[1] = open(fifo[1], O_RDONLY);
    
            for(;;){
                    scanf("%d", &in);
                    write(fd[0], &in, sizeof(int));
                    if(in == -1)
                            exit(0);
                    read(fd[1], &in, sizeof(int));
                    printf("final A received : %d\n", in);
            }
    
            exit(0);
    }

    p11-1B.c

    int main(int argc, char **argv){
            char fifo[2][3] = {"f1", "f2"};
            int i, in, fd[2];
    
            fd[0] = open(fifo[0], O_RDONLY);
            fd[1] = open(fifo[1], O_WRONLY);
    
            for(;;){
                    read(fd[0], &in, sizeof(int));
                    printf("first B received : %d\n", in);
                    if(in == -1)
                            exit(0);
                    in = in + 8;
                    write(fd[1], &in, sizeof(int));
            }
    
            exit(0);
    }
  • p11-2 server process๋Š” ์„ธ ๊ฐœ์˜ client process๋“ค๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ธฐ ์œ„ํ•œ fifo๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ฐ client๋Š” ๋ฏธ๋ฆฌ ์ •ํ•ด์ง„ ์ด๋ฆ„์˜ FIFO๋กœ ์ ‘์†ํ•˜์—ฌ, ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ์ž…๋ ฅ๋œ ์ •์ˆ˜๋ฅผ server process์—๊ฒŒ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. server process๋Š” client process๋กœ๋ถ€ํ„ฐ ์ „์†ก๋œ ์ •์ˆ˜ ๊ฐ’์— +8์„ ํ•œ ํ›„, ํ•ด๋‹น client์—๊ฒŒ ๋‹ค์‹œ ๋ณด๋ƒ…๋‹ˆ๋‹ค. client process๋Š” ๋Œ๋ ค๋ฐ›์€ ์ •์ˆ˜ ๊ฐ’์„ ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. client process๋Š” ์ •์ˆ˜ ๋ฐ์ดํ„ฐ์˜ ์ž…/์ถœ๋ ฅ ์ž‘์—…์„ 5ํšŒ ๋ฐ˜๋ณตํ•œ ํ›„ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค. client process๋กœ ๋ถ€ํ„ฐ์˜ ์ž…๋ ฅ์„ blocking์œผ๋กœ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ ์œ„ํ•ด select ๋ฌธ์žฅ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    • O_RDONLY ํ•  ๋•Œ, O_RDWR์„ ํ•ด์•ผํ•˜๋Š”์ง€ ์•„๋‹ˆ๋ฉด O_RDONLY๋ฅผ ํ•ด์•ผํ•˜๋Š”์ง€ ์•„์ง ๋ชจ๋ฅด๊ฒ ์Œ
    • p11-2server.c (ํ•™๊ต ๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„ ๋ฒ„์ „).
      • ๊ต์ˆ˜๋‹˜ ์ˆ˜์—… ์‹œ๊ฐ„๋•Œ ๋ง์”€ํ•˜์‹ ๊ฑฐ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ผ ์ด๊ฒŒ ์ •๋‹ต์ธ๋“ฏํ•จ. clientํ”„๋กœ๊ทธ๋žจ ์ „๋ถ€ ๋‹ซํžˆ๋ฉด ์ „๋ถ€ 0์„ return ํ•˜์—ฌ select๋Š” 3์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋Š”๊ฑฐ๊ณ , ๊ทผ๋ฐ readํ•œ ๊ฐ’์ด 0๋ฐ”์ดํŠธ์ธ ๊ฒฝ์šฐ ๋ชจ๋“  ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒํ•œ ๊ฒฝ์šฐ๋ผ๊ณ  ํ•˜์…”์„œ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ์ง€๋งŒ, ํ˜น์‹œ๋‚˜ ์ž˜๋ชป๋œ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ด์•ผํ•จ
    int main(int argc, char **argv) {
        char fifo[6][15] = {"p2_f1", "p2_f2", "p2_f3", "p2_f4", "p2_f5", "p2_f6"};
        int i, in, fd[6], select_check, nread, nread_count[3] = {0};
        fd_set set, master;
    
        for(i=0;i<6;i++){
            mkfifo(fifo[i], 0600);
        }
    
        fd[0] = open(fifo[0], O_RDONLY);
        fd[1] = open(fifo[1], O_RDONLY);
        fd[2] = open(fifo[2], O_RDONLY);
    
        fd[3] = open(fifo[3], O_WRONLY);
        fd[4] = open(fifo[4], O_WRONLY);
        fd[5] = open(fifo[5], O_WRONLY);
    
        FD_ZERO(&master);
    
        for (i=0;i<3;i++){
            FD_SET(fd[i], &master);
        }
    
        while (set=master, (select_check = select(fd[2]+1, &set, NULL, NULL, NULL)) > 0) {
            for (i=0;i<3;i++) {
                if (FD_ISSET(fd[i], &set)) {
                    if((nread = read(fd[i], &in, sizeof(int))) > 0){
                        printf("received : %d\n", in);
                        in = in + 8;
                        write(fd[i+3], &in, sizeof(int));
                    }
                    else if(select_check == 3 && nread == 0)
                        return 0;
                }
            }
        }
        exit(0);
    }
    • p11-2server.c (๋‚ด ์ปดํ“จํ„ฐ ๋ฒ„์ „)
    int main(int argc, char **argv) {
        char fifo[6][15] = {"p2_f1", "p2_f2", "p2_f3", "p2_f4", "p2_f5", "p2_f6"};
        int i, in, fd[6], select_check, nread, nread_count[3] = {0};
        fd_set set, master;
    
        for(i=0;i<6;i++){
            mkfifo(fifo[i], 0600);
        }
    
        fd[0] = open(fifo[0], O_RDONLY);
        fd[1] = open(fifo[1], O_RDONLY);
        fd[2] = open(fifo[2], O_RDONLY);
    
        fd[3] = open(fifo[3], O_WRONLY);
        fd[4] = open(fifo[4], O_WRONLY);
        fd[5] = open(fifo[5], O_WRONLY);
    
        FD_ZERO(&master);
    
        for (i=0;i<3;i++){
            FD_SET(fd[i], &master);
        }
    
        while (set=master, (select_check = select(fd[2]+1, &set, NULL, NULL, NULL)) > 0) {
            for (i=0;i<3;i++) {
                if (FD_ISSET(fd[i], &set)) {
                    if((nread = read(fd[i], &in, sizeof(int))) > 0){
                        printf("received : %d\n", in);
                        in = in + 8;
                        write(fd[i+3], &in, sizeof(int));
                    }
                    else if(nread == 0)
                        nread_count[i] = 1;
                }
            }
            if(nread_count[0] == 1 && nread_count[1] == 1 && nread_count[2] == 1)
                return 0;
        }
        exit(0);
    }
    • p11-2client.c
    int main(int argc, char **argv){
        char fifo[6][15] = {"p2_f1", "p2_f2", "p2_f3", "p2_f4", "p2_f5", "p2_f6"};
        int i, in, fd[6];
        int id = atoi(argv[1]);
    
        fd[id] = open(fifo[id], O_WRONLY);
        fd[id+3] = open(fifo[id+3], O_RDONLY);
    
        scanf("%d", &in);
        write(fd[id], &in, sizeof(int));
        read(fd[id+3], &in, sizeof(int));
        for(i=0;i<5;i++){
            sleep(1);
            printf("received : %d\n", in);
        }
        exit(0);
    }
  • p11-3 ๋„ค ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋™๊ธฐํ™”๋ฅผ ํ•˜๋ฉฐ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id๋ฅผ 5ํšŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์€ main() ํ•จ์ˆ˜์˜ arguments๋กœ ๋™๊ธฐํ™”์— ์ฐธ์—ฌํ•˜๋Š” ์ „์ฒด ํ”„๋กœ์„ธ์Šค ์ค‘ ์ž์‹ ์˜ ์ถœ๋ ฅ ์ˆœ์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋˜๋ฉด, ์ˆœ์„œ๋Œ€๋กœ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋™๊ธฐํ™” ์ž‘์—…์€ fifo๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

    • ๋„ค ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐ๊ฐ ์‹คํ–‰ํ•œ ํ›„์— ๋ฉ”์ธํ•จ์ˆ˜ ์ธ์ž๋กœ 0, 1, 2, 3์„ ์ฃผ๋ฉด ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰ ํ›„ ์ข…๋ฃŒ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.
    int main(int argc, char **argv){
        char buf, f[3][3] = {"f1", "f2", "f3"};
        int i, k ,in ,fd[2];
    
        for(i=0;i<3;i++){
            mkfifo(f[i], 0600);
        }
    
        k = atoi(argv[1]);
    
        if(k > 0)
            fd[0] = open(f[k-1], O_RDONLY);
        if(k < 3)
            fd[1] = open(f[k], O_WRONLY);
    
        if(k > 0)
            read(fd[0], &buf, 1);
    
        for(i=0;i<5;i++){
            printf("pid : %d\n", getpid());
            sleep(1);
        }
    
        if(k < 3)
            write(fd[1], "a", 1);
    
        exit(0);
    }

20181114

  • p12-1 server process๋Š” ์„ธ ๊ฐœ์˜ client process๋“ค๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ธฐ ์œ„ํ•ด message queue๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ฐ client๋Š” message queue๋ฅผ ์ด์šฉํ•˜์—ฌ, ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ์ž…๋ ฅ๋œ ์ •์ˆ˜๋ฅผ server process์—๊ฒŒ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. server process๋Š” client process๋กœ๋ถ€ํ„ฐ ์ „์†ก๋œ ์ •์ˆ˜ ๊ฐ’์— +8์„ ํ•œ ํ›„, ํ•ด๋‹น client์—๊ฒŒ ๋‹ค์‹œ ๋ณด๋ƒ…๋‹ˆ๋‹ค. client process๋Š” ๋Œ๋ ค๋ฐ›์€ ์ •์ˆ˜ ๊ฐ’์„ ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. client process๋Š” ์ •์ˆ˜ ๋ฐ์ดํ„ฐ์˜ ์ž…/์ถœ๋ ฅ ์ž‘์—…์„ 5ํšŒ ๋ฐ˜๋ณต ํ•œ ํ›„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. ๊ฐ client process๋Š” main() ํ•จ์ˆ˜์˜ arguments๋กœ ์ž์‹ ์˜ id๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.

    • p12-1server.c
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <string.h>
    #include <time.h>
    #include <ftw.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    
    struct q_entry{
        long mtype;
        int data;
    };
    
    int main(int argc, char **argv){
        int qid, i, in;
        struct q_entry msg;
        key_t key;
    
        key = ftok("key", 1);
        qid = msgget(key, 0600|IPC_CREAT);
    
        if(qid == -1){
            perror("msgget");
            exit(0);
        }
    
        for(i=0;i<15;i++){
            msgrcv(qid, &msg, sizeof(int), -3, 0);
            msg.mtype += 3;
            msg.data += 8;
            msgsnd(qid, &msg, sizeof(int), 0);
        }
        
        //๋ฉ”์„ธ์ง€ํ ๊ธฐ๋Šฅ ๋‹ค ์‚ฌ์šฉํ•˜๊ณ  ์‚ญ์ œํ•ด์ฃผ๋Š” ์‹œ์Šคํ…œ ์ฝœ ์ถ”๊ฐ€
        msgctl(qid, IPC_RMID, 0);
    
        exit(0);
    }
    • p12-1client.c
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <string.h>
    #include <time.h>
    #include <ftw.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    
    struct q_entry{
        long mtype;
        int data;
    };
    
    int main(int argc, char **argv){
        int qid, i, in, id;
        struct q_entry msg;
        key_t key;
    
        id = atoi(argv[1]);
    
        key = ftok("key", 1);
        qid = msgget(key, 0600|IPC_CREAT);
    
        if(qid == -1){
            perror("msgget");
            exit(0);
        }
    
        for(i=0;i<5;i++){
            scanf("%d", &in);
            msg.mtype = id;
            msg.data = in;
            msgsnd(qid, &msg, sizeof(int), 0);
            msgrcv(qid, &msg, sizeof(int), id+3, 0);
            printf("%d\n", msg.data);
        }
    
        exit(0);
    }
  • p12-2.c ๋„ค ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋™๊ธฐํ™”๋ฅผ ํ•˜๋ฉฐ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id๋ฅผ 5ํšŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์€ main() ํ•จ์ˆ˜์˜ arguments๋กœ ๋™๊ธฐํ™”์— ์ฐธ์—ฌํ•˜๋Š” ์ „์ฒด ํ”„๋กœ์„ธ์Šค ์ค‘ ์ž์‹ ์˜ ์ถœ๋ ฅ ์ˆœ์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋˜๋ฉด, ์ˆœ์„œ๋Œ€๋กœ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋™๊ธฐํ™” ์ž‘์—…์€ message queue๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <string.h>
    #include <time.h>
    #include <ftw.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    
    struct q_entry{
        long mtype;
        int data;
    };
    
    int main(int argc, char **argv){
        int i, id, qid;
        key_t key;
        struct q_entry msg;
    
        key = ftok("key", 3);
        qid = msgget(key, IPC_CREAT|0600);
    
        id = atoi(argv[1]);
    
        if(id > 1)
            msgrcv(qid, &msg, sizeof(int), id, 0);
    
        for(i=0;i<5;i++){
            sleep(1);
            printf("id = %d pid = %d\n", id, getpid());
        }
    
        if(id < 4){
            msg.mtype = id + 1;
            msg.data = id;
            msgsnd(qid, &msg, sizeof(int), 0);
        }
    
        exit(0);
    }

20181119

  • p13-1 ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘์„ ์ด์šฉํ•œ ๋‘ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‘ ํ”„๋กœ๊ทธ๋žจ ๋ชจ๋‘ "data" ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค. ํ•œ ํ”„๋กœ๊ทธ๋žจ์€ ์™ธ๋ถ€ ์ž…๋ ฅ์„ ์ฝ์–ด์„œ ๋งคํ•‘๋œ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๋Š” ์ž‘์—…์„ 3ํšŒ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์€ ๋งคํ•‘๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์„ 3ํšŒ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ํ”„๋กœ๊ทธ๋žจ ์‚ฌ์ด์˜ ์ฝ๊ธฐ-์“ฐ๊ธฐ ๋™๊ธฐํ™”๋ฅผ ์œ„ํ•ด semaphore๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    p13-1a.c

    union semun{
            int val;
            struct semid_ds *buf;
            ushort *array;
    };
    
    int main(int argc, char **argv){
            int i, len = 0, status, fd, semid;
            char *addr;
            key_t key;
            union semun arg;
            struct sembuf p_buf;
    
            key = ftok("key", 3);
            semid = semget(key, 1, 0600|IPC_CREAT|IPC_EXCL);
    
            if(semid == -1){
                    semid = semget(key, 1, 0600);
            }
            else{
                    arg.val = 0;
                    semctl(semid, 0, SETVAL, arg);
            }
    
            fd = open("temp", O_RDWR | O_CREAT | O_TRUNC, 0600);
            addr = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    
            ftruncate(fd, BUFSIZE);
    
            for(i=0;i<3;i++){
                    len = len + read(0, addr + len, BUFSIZE);
                    len = len-1;
                    *(addr+len) = '\0';
    
                    p_buf.sem_num = 0;
                    p_buf.sem_op = 1;
                    p_buf.sem_flg = 0;
                    semop(semid, &p_buf, 1);
                
                    printf("semval : %d\n", semctl(semid, 0, GETVAL, arg));
            }
    
            exit(0);
    }

    p13-1b.c

    union semun{
            int val;
            struct semid_ds *buf;
            ushort *array;
    };
    
    int main(int argc, char **argv){
            int i, len = 0, fd, semid;
            char *addr;
            key_t key;
            union semun arg;
            struct sembuf p_buf;
    
            key = ftok("key", 3);
            semid = semget(key, 1, 0600|IPC_CREAT|IPC_EXCL);
    
            if(semid == -1){
                    semid = semget(key, 1, 0600);
            }
            else{
                    arg.val = 0;
                    semctl(semid, 0, SETVAL, arg);
            }
    
            fd = open("temp", O_RDWR | O_CREAT, 0600);
    
            addr = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    
            for(i=0;i<3;i++){
                    p_buf.sem_num = 0;
                    p_buf.sem_op = -1;
                    p_buf.sem_flg = 0;
                    semop(semid, &p_buf, 1);
    
                    printf("semval : %d\n", semctl(semid, 0, GETVAL, arg));
    
                    len = len + write(1, addr + len, strlen(addr+len));
                    write(1, "-------\n", 8);
            }
    
            exit(0);
    }
  • p13-2.c ๋„ค ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋™๊ธฐํ™”๋ฅผ ํ•˜๋ฉฐ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id๋ฅผ 5ํšŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์€ main()ํ•จ์ˆ˜์˜ arguments๋กœ ๋™๊ธฐํ™”์— ์ฐธ์—ฌํ•˜๋Š” ์ „์ฒด ํ”„๋กœ์„ธ์Šค ์ค‘ ์ž์‹ ์˜ ์ถœ๋ ฅ ์ˆœ์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋˜๋ฉด, ์ˆœ์„œ๋Œ€๋กœ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋™๊ธฐํ™” ์ž‘์—…์€ semaphore๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

    union semun{
        int val;
        struct semid_ds *buf;
        ushort *array;
    };
    
    int main(int argc, char **argv){
        int i, id, pid, semid;
        key_t key;
        union semun arg;
        ushort buf[3] = {0};
        struct sembuf p_buf;
    
        id = atoi(argv[1]);
        key = ftok("key", 3);
        semid = semget(key, 3, 0600|IPC_CREAT|IPC_EXCL);
    
        if(semid == -1){
            semid = semget(key, 3, 0600);
        }
        else{
            arg.array = buf;
            semctl(semid, 0, SETALL, arg);
        }
    
        if(id>1){
            p_buf.sem_num = id-2;
            p_buf.sem_op = -1;
            p_buf.sem_flg = 0;
            semop(semid, &p_buf, 1);
        }
    
        pid = getpid();
    
        for(i=0;i<5;i++){
            sleep(1);
            printf("pid : %d\n", pid);
        }
    
        if(id<4){
            p_buf.sem_num = id-1;
            p_buf.sem_op = 1;
            p_buf.sem_flg = 0;
            semop(semid, &p_buf, 1);
        }
    
        exit(0);
    }
  • p13-3.c 2๋ฒˆ ๋ฌธ์ œ์—์„œ ๋” ์ ์€ semaphore๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ ์ž‘์„ฑํ•˜์‹œ์˜ค.

    union semun{
        int val;
        struct semid_ds *buf;
        ushort *array;
    };
    
    int main(int argc, char **argv){
        int i, id, pid, semid;
        key_t key;
        union semun arg;
        struct sembuf p_buf;
    
        id = atoi(argv[1]);
        key = ftok("key", 1);
        semid = semget(key, 3, 0600|IPC_CREAT|IPC_EXCL);
    
        if(semid == -1){
            semid = semget(key, 3, 0600);
        }
        else{
            arg.val = 0;
            semctl(semid, 0, SETVAL, arg);
        }
    
        if(id>1){
            p_buf.sem_num = 0;
            p_buf.sem_op = -id;
            p_buf.sem_flg = 0;
            semop(semid, &p_buf, 1);
        }
    
        pid = getpid();
    
        for(i=0;i<5;i++){
            sleep(1);
            printf("pid : %d\n", pid);
        }
    
        if(id<4){
            p_buf.sem_num = 0;
            p_buf.sem_op = id + 1;
            p_buf.sem_flg = 0;
            semop(semid, &p_buf, 1);
        }
    
        exit(0);
    }
  • p13-4.c ์„ธ ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋™๊ธฐํ™”๋ฅผ ํ•˜๋ฉฐ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id๋ฅผ 5ํšŒ ์”ฉ 3๋ฒˆ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์€ main() ํ•จ์ˆ˜์˜ arguments๋กœ ๋™๊ธฐํ™”์— ์ฐธ์—ฌํ•˜๋Š” ์ „์ฒด ํ”„๋กœ์„ธ์Šค ์ค‘ ์ž์‹ ์˜ ์ถœ๋ ฅ ์ˆœ์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋˜๋ฉด, 1 โ†’ 2 โ†’ 3 โ†’ 1 โ†’ 2 โ†’ 3 โ†’ 1 โ†’ 2 โ†’ 3 ์ˆœ์„œ๋Œ€๋กœ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

    • (a) ๋™๊ธฐํ™” ์ž‘์—…์€ message queue๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ ํ•ฉ๋‹ˆ๋‹ค.
    struct q_entry{
        long mtype;
        int data;
    };
    
    int main(int argc, char **argv){
        int i, j, id, qid;
        key_t key;
        struct q_entry msg;
        
        key = ftok("key", 3);
        qid = msgget(key, IPC_CREAT|0600);
        
        id = atoi(argv[1]);
        
        for(i=4;i<7;i++){
            //id = 1,2,3
            //i = 4,5,6
            if(id > 1)
                msgrcv(qid, &msg, sizeof(int), id, 0);
            
            if(id == 1 && i > 4)
                msgrcv(qid, &msg, sizeof(int), i, 0);
            
            for(j=0;j<3;j++){
                sleep(1);
                printf("id = %d pid = %d\n", id, getpid());
            }
            
            if(id < 3){
                msg.mtype = id + 1;
                msg.data = id;
                msgsnd(qid, &msg, sizeof(int), 0);
            }
            else if(id == 3 && i < 6){
                    msg.mtype = i + 1;
                    msg.data = id;
                    msgsnd(qid, &msg, sizeof(int), 0);
            }
        }
        
        if(id == 3)
            msgctl(qid, IPC_RMID, 0);
        
        exit(0);
    }
    • (b) ๋™๊ธฐํ™” ์ž‘์—…์€ semaphore๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ ํ•ฉ๋‹ˆ๋‹ค.

20181121

  • p14-1 ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋Š” ๋‘ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ A๋Š” scanf() ๋ช…๋ น์œผ๋กœ 10๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ ํ•˜๋Š” ์ž‘์—…์„ 10ํšŒ ๋ฐ˜๋ณต ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ B๋Š” ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ์— ์ €์žฅ๋œ ๋‚ด์šฉ์„ printf() ๋ช…๋ น์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์„ 10ํšŒ ๋ฐ˜๋ณต ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ํ”„๋กœ๊ทธ๋žจ B๋Š” ํ”„๋กœ ๊ทธ๋žจ A๊ฐ€ ์ •์ˆ˜๋ฅผ ์“ด ํ›„ ์ฝ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋™๊ธฐํ™” ์ž‘์—…์€ semaphore๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    • write

      union semun{
              int val;
              struct semid_ds *buf;
              ushort *array;
      };
      
      int main(int argc, char **argv){
          key_t semkey, shmkey;
          int semid, shmid, i, n;
          int *buf;
          union semun arg;
          struct sembuf p_buf;
          
          semkey = ftok("semkey", 3);
          semid = semget(semkey, 1, 0600|IPC_CREAT|IPC_EXCL);
          
          if(semid == -1){
              semid = semget(semkey, 1, 0600);
          }
          else{
              arg.val = 0;
              semctl(semid, 0, SETVAL, arg);
          }
      
          shmkey = ftok("shmfile", 1);
          shmid = shmget(shmkey, 10*sizeof(int), IPC_CREAT|0600);
      
          buf = (int *)shmat(shmid, 0, 0);
      
          for(i=0;i<10;i++){
              scanf("%d", (buf+i));
              
              //signal
              p_buf.sem_num = 0;
              p_buf.sem_op = 1;
              p_buf.sem_flg = 0;
              semop(semid, &p_buf, 1);
          }
      
          semctl(semid, IPC_RMID, 0);
          shmdt(buf);
          shmctl(shmid, IPC_RMID, 0);
      
          return 0;
      }
    • read

      union semun{
              int val;
              struct semid_ds *buf;
              ushort *array;
      };
      
      int main(int argc, char **argv){
          key_t semkey, shmkey;
          int semid, shmid, i, n;
          int *buf;
          union semun arg;
          struct sembuf p_buf;
          
          semkey = ftok("semkey", 3);
          semid = semget(semkey, 1, 0600|IPC_CREAT|IPC_EXCL);
          
          if(semid == -1){
              semid = semget(semkey, 1, 0600);
          }
          else{
              arg.val = 0;
              semctl(semid, 0, SETVAL, arg);
          }
      
          shmkey = ftok("shmfile", 1);
          shmid = shmget(shmkey, 10*sizeof(int), IPC_CREAT|0600);
      
          buf = (int *)shmat(shmid, 0, 0);
      
          for(i=0;i<10;i++){
              //wait
              p_buf.sem_num = 0;
              p_buf.sem_op = -1;
              p_buf.sem_flg = 0;
              semop(semid, &p_buf, 1);
              
              printf("%d\n", *(buf+i));
          }
      
          semctl(semid, IPC_RMID, 0);
          shmdt(buf);
          shmctl(shmid, IPC_RMID, 0);
      
          return 0;
      }
  • p14-2 1๋ฒˆ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ์ž์ฒด ์ •๋ณด์— ์˜ํ•ด ๋™๊ธฐํ™” ์ž‘์—…์ด ์ด๋ฃจ์–ด์ง€๋„๋ก ์ˆ˜์ • ํ•ฉ๋‹ˆ๋‹ค. semaphore๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— busy waiting์œผ๋กœ block์„ ํ•  ์ˆ˜๋ฐ–์— ์—†์–ด์„œ, ๋น„ํšจ์œจ์ ์ธ ํ”„๋กœ๊ทธ๋žจ์ด ๋œ๋‹ค.

    • write

      struct databuf{
          int flag;
          int data;
      };
      
      int main(int argc, char **argv){
          key_t key;
          int shmid, i, n;
          struct databuf *buf;
      
          key = ftok("shmfile", 1);
          shmid = shmget(key, 10* sizeof(struct databuf), IPC_CREAT|0600);
      
          buf = (struct databuf *)shmat(shmid, 0, 0);
      
          for(i=0;i<10;i++){
              scanf("%d", &(buf+i)->data);
              (buf+i)->flag = 1;
          }
      
          shmdt(buf);
          shmctl(shmid, IPC_RMID, 0);
      
          return 0;
      }
    • read

      struct databuf{
          int flag;
          int data;
      };
      
      int main(int argc, char **argv){
          key_t key;
          int shmid, i, n;
          struct databuf *buf;
      
          key = ftok("shmfile", 1);
          shmid = shmget(key, 10* sizeof(struct databuf), IPC_CREAT|0600);
      
          buf = (struct databuf *)shmat(shmid, 0, 0);
      
          for(i=0;i<10;i++){
              while((buf+i)->flag == 0);
              printf("%d\n", (buf+i)->data);
          }
      
          shmdt(buf);
          shmctl(shmid, IPC_RMID, 0);
      
          return 0;
      }
  • p14-3 Server process๋Š” ์„ธ ๊ฐœ์˜ client process๋“ค๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ธฐ ์œ„ํ•ด ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ฐ client๋Š” ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ด์šฉํ•˜์—ฌ ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ์ž…๋ ฅ๋œ ์ •์ˆ˜๋ฅผ server process์—๊ฒŒ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. Server process๋Š” client process๋กœ๋ถ€ํ„ฐ ์ „์†ก๋œ ์ •์ˆ˜ ๊ฐ’์— +8์„ ํ•œ ํ›„, ํ•ด๋‹น client์—๊ฒŒ ๋‹ค์‹œ ๋ณด๋ƒ…๋‹ˆ๋‹ค. Client process๋Š” ๋Œ๋ ค๋ฐ›์€ ์ •์ˆ˜ ๊ฐ’์„ ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. Client process๋Š” ์ •์ˆ˜ ๋ฐ์ดํ„ฐ์˜ ์ž…/์ถœ๋ ฅ ์ž‘์—…์„ 5ํšŒ ๋ฐ˜๋ณต ํ•œ ํ›„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

    • server๋ž‘ client ๋ชจ๋‘ busy waiting์œผ๋กœ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ์Œ. ๊ทธ๋ž˜์„œ ๋น„ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ธ๋ฐ, shared memory๋Š” ์ƒ์„ฑํ•  ๋•Œ๋งŒ OS๋กœ switching ์ผ์–ด๋‚˜๊ณ  ์ดํ›„์—๋Š” context switching์ด ์•ˆ ์ผ์–ด๋‚˜๊ณ  ํ”„๋กœ์„ธ์Šค์—์„œ ์ž‘์—…์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋‚˜๋งˆ ํšจ์œจ์ ์ž„

    • ๊ทธ๋ž˜์„œ ์„ธ๋งˆํฌ๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋” ํšจ๊ณผ์ ์ธ ์ฝ”๋“œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ

    • shmgetํ•  ๋•Œ EXCL ์˜ต์…˜์„ ์•ˆ ๋ถ™์ด๋Š” ์ด์œ 

      • Flag ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๊ฐ€ ์™”๋‚˜, ์•ˆ ์™”๋‚˜๋ฅผ ํ™•์ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ์— 0์œผ๋กœ ๋ชจ๋‘ ๋‹ค ์„ค์ •๋ผ์žˆ์–ด์•ผํ•จ. 0๋ณด๋‹ค ํฐ ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์œผ๋ฉด ์•ˆ ๋จ
      • shared memory๋ฅผ ๋งŒ๋“ค๋ฉด ๊ธฐ๋ณธ์œผ๋กœ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋˜๊ธฐ ๋•Œ๋ฌธ์— EXCL ์˜ต์…˜์œผ๋กœ ๋‹ค์‹œ ์žฌ์„ค์ •์„ ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์Œ
      • ํ˜น์‹œ๋‚˜ 1๋กœ ๋ชจ๋“  flag ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ  1์ด ์•„๋‹Œ ๊ฐ’์ด ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ์˜จ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ๋ฅผ ํ•  ๊ฒƒ์ด๋ผ๋ฉด EXCL ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  flag๊ฐ’์„ ์žฌ์„ค์ •ํ•ด์ค„ ํ•„์š”๊ฐ€ ์žˆ์Œ.
    • server

      #include <stdio.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <sys/wait.h>
      #include <fcntl.h>
      #include <unistd.h>
      #include <dirent.h>
      #include <string.h>
      #include <time.h>
      #include <ftw.h>
      #include <stdlib.h>
      #include <sys/mman.h>
      #include <sys/ipc.h>
      #include <sys/msg.h>
      #include <sys/sem.h>
      #include <sys/shm.h>
      
      #define BUFSIZE 512
      
      struct databuf{
          int flag;
          int data;
      };
      
      int main(int argc, char **argv){
          int i, j, shmid;
          struct databuf *buf;
          key_t shmkey;
          
          shmkey = ftok("shmkey", 3);
          shmid = shmget(shmkey, 6*sizeof(struct databuf), 0600|IPC_CREAT);
          buf = (struct databuf *)shmat(shmid, 0 ,0);
          
          for(i=0;i<15;i++){
              for(j=0;;j=(j+1)%3)
                  if((buf+j)->flag == 1)
                      break;
              (buf+j+3)->data = (buf+j)->data + 8;
              (buf+j)->flag = 0;
              (buf+j+3)->flag = 1;
          }
          
          shmctl(shmid, IPC_RMID, 0);
      
          exit(0);
      }
    • client

      #include <stdio.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <sys/wait.h>
      #include <fcntl.h>
      #include <unistd.h>
      #include <dirent.h>
      #include <string.h>
      #include <time.h>
      #include <ftw.h>
      #include <stdlib.h>
      #include <sys/mman.h>
      #include <sys/ipc.h>
      #include <sys/msg.h>
      #include <sys/sem.h>
      #include <sys/shm.h>
      
      #define BUFSIZE 512
      
      struct databuf{
          int flag;
          int data;
      };
      
      int main(int argc, char **argv){
          int i, j, id, shmid;
          struct databuf *buf;
          key_t shmkey;
          
          id = atoi(argv[1]);
          shmkey = ftok("shmkey", 3);
          shmid = shmget(shmkey, 6*sizeof(struct databuf), 0600|IPC_CREAT);
          buf = (struct databuf *)shmat(shmid, 0, 0);
          
          for(i=0;i<5;i++){
              scanf("%d", &((buf+id)->data));
              (buf+id)->flag = 1;
              while((buf+id+3)->flag == 0);
              printf("id : %d, input + 8 = %d\n", id, (buf+id+3)->data);
              (buf+id+3)->flag = 0;
          }
      
          exit(0);
      }
  • p14-4

    p14-3 ๋ฌธ์ œ busy waiting ์‚ฌ์šฉํ•˜์ง€ ๋ง๊ณ  ์„ธ๋งˆํฌ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“ค์–ด๋ณด๊ธฐ

    • ์„ธ๋งˆํฌ 2๊ฐœ ๋งŒ๋“ค์–ด์„œ client์—์„œ server๋กœ ๋ณด๋‚ด๋Š” ๋™๊ธฐํ™”๋ฅผ ์œ„ํ•œ ์„ธ๋งˆํฌ ํ•˜๋‚˜, server์—์„œ client๋กœ ๋ณด๋‚ด๋Š” ๋™๊ธฐํ™”๋ฅผ ์œ„ํ•œ ์„ธ๋งˆํฌ ํ•˜๋‚˜, ์˜ ๋‘ ๊ฐœ ์„ธ๋งˆํฌ๋ฅผ ์‚ฌ์šฉ

    • ์ด์ „ busy waiting์ด๋ž‘ ๋‹ค๋ฅด๊ฒŒ shared memory ๊ตฌ์กฐ์ฒด ๋ง๊ณ  ๊ทธ๋ƒฅ int ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์œผ๋กœ ์‚ฌ์šฉ.

    • flag ์—†์ด int size ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์—…

    • semgetํ•  ๋•Œ semid = semget(semkey, 2, 0600|IPC_CREAT|IPC_EXCL); ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์„ธ๋งˆํฌ ์ง‘ํ•ฉ ๋‚ด์˜ ์„ธ๋งˆํฌ ๊ฐฏ์ˆ˜์ด๋‹ˆ๊นŒ ์ฃผ์˜ํ•  ๊ฒƒ

    • server

      int main(int argc, char **argv){
          int i, semid, shmid;
          key_t semkey, shmkey;
          union semun arg;
          struct sembuf p_buf;
          ushort sembuf[2] = {0};
          int *shmbuf;
          
          semkey = ftok("semkey", 3);
          semid = semget(semkey, 2, 0600|IPC_CREAT|IPC_EXCL);
          
          if(semid == -1){
              semid = semget(semkey, 1, 0600);
          }
          else{
              arg.array = sembuf;
              semctl(semid, 0, SETALL, arg);
          }
          
          shmkey = ftok("shmkey", 3);
          shmid = shmget(shmkey, sizeof(int), 0600|IPC_CREAT);
          shmbuf = (int *)shmat(shmid, 0 ,0);
          
          for(i=0;i<15;i++){
              //0 wait
              p_buf.sem_num = 0;
              p_buf.sem_op = -1;
              p_buf.sem_flg = 0;
              semop(semid, &p_buf, 1);
              
              *(shmbuf+0) = *(shmbuf+0) + 8;
              
              //1 signal
              p_buf.sem_num = 1;
              p_buf.sem_op = 1;
              p_buf.sem_flg = 0;
              semop(semid, &p_buf, 1);
          }
          
          semctl(semid, IPC_RMID, 0);
          shmctl(shmid, IPC_RMID, 0);
      
          exit(0);
      }
    • client

      int main(int argc, char **argv){
          int i, semid, shmid;
          key_t semkey, shmkey;
          union semun arg;
          struct sembuf p_buf;
          ushort sembuf[2] = {0};
          int *shmbuf;
          
          semkey = ftok("semkey", 3);
          semid = semget(semkey, 2, 0600|IPC_CREAT|IPC_EXCL);
          
          if(semid == -1){
              semid = semget(semkey, 1, 0600);
          }
          else{
              arg.array = sembuf;
              semctl(semid, 0, SETALL, arg);
          }
          
          shmkey = ftok("shmkey", 3);
          shmid = shmget(shmkey, sizeof(int), 0600|IPC_CREAT);
          shmbuf = (int *)shmat(shmid, 0 ,0);
          
          for(i=0;i<5;i++){
              scanf("%d", (shmbuf+0));
              
              //0 signal
              p_buf.sem_num = 0;
              p_buf.sem_op = 1;
              p_buf.sem_flg = 0;
              semop(semid, &p_buf, 1);
              
              //1 wait
              p_buf.sem_num = 1;
              p_buf.sem_op = -1;
              p_buf.sem_flg = 0;
              semop(semid, &p_buf, 1);
              
              printf("[input + 8] ==> %d\n", *(shmbuf+0));
          }
      
          exit(0);
      }

20181126

  • p15-1.c Parent process๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ์ •์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ž…๋ ฅ ๋ฐ›์•„, โ€œdata" ํŒŒ์ผ์— ์“ด ํ›„, ์„ธ ๊ฐœ์˜ child process๋“ค์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ฐ child process๋Š” "data" ํŒŒ์ผ์˜ ์ •์ˆ˜ ๊ฐ’์„ ์ฝ๊ณ  5์ดˆ๊ฐ„ ๊ธฐ๋‹ค๋ ธ๋‹ค +10 ํ•œ ๊ฐ’์„ ์”๋‹ˆ๋‹ค. ์„ธ child์˜ ๋ง์…ˆ์ด ์ •ํ™•ํžˆ ๋˜๋„๋ก file locking์„ ์จ์„œ ๋™๊ธฐํ™”๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <string.h>
    #include <time.h>
    #include <ftw.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <sys/sem.h>
    #include <sys/shm.h>
    
    #define BUFSIZE 512
    
    void do_child(int fd){
        int in;
        struct flock lock;
        
        lock.l_whence = SEEK_SET;
        lock.l_start = 0;
        lock.l_len = 4;
        lock.l_type = F_WRLCK;
        fcntl(fd, F_SETLKW, &lock);
        
        lseek(fd, 0, SEEK_SET);
        read(fd, &in, sizeof(int));
        sleep(2);
        
        in = in + 10;
        lseek(fd, 0, SEEK_SET);
        write(fd, &in, sizeof(int));
        
        lock.l_type = F_UNLCK;
        fcntl(fd, F_SETLK, &lock);
        
        exit(0);
    }
    
    int main(int argc, char **argv){
        int fd, i, num;
        struct flock lock;
        pid_t pid[3];
        
        fd=open("data", O_RDWR|O_CREAT, 0600);
        scanf("%d", &num);
        write(fd, &num, sizeof(int));
    
        for(i=0;i<3;i++){
            pid[i] = fork();
            if(pid[i] == 0){
                do_child(fd);
            }
            else{
                wait(0);
            }
        }
        
        lseek(fd, 0, SEEK_SET);
        read(fd, &num, sizeof(int));
        printf("%d\n", num);
        
        return 0;
    }
  • p15-2.c ๋„ค ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋™๊ธฐํ™”๋ฅผ ํ•˜๋ฉฐ ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค id๋ฅผ 5ํšŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑ ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์€ "turn1" file์„ ์ด์šฉํ•˜์—ฌ ๋™๊ธฐํ™”์— ์ฐธ์—ฌํ•˜๋Š” ์ „์ฒด ํ”„๋กœ์„ธ์Šค ์ค‘ ์ž์‹ ์˜ ์ถœ๋ ฅ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ • ํ•ฉ๋‹ˆ ๋‹ค. ๋‘˜ ์ด์ƒ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋™์‹œ์— ์ž์‹ ์˜ id๋ฅผ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๋™๊ธฐํ™” ์ž‘์—…์€ file locking์„ ์‚ฌ์šฉ ํ•ฉ๋‹ˆ๋‹ค. ๋„ค ํ”„๋กœ์„ธ์Šค์˜ ์ถœ๋ ฅ ์ˆœ์„œ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋™์‹œ์— ์ถœ๋ ฅ์„ ํ•˜์ง€ ์•Š๋„๋ก๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

    • ๋˜๊ธด ์ž˜๋˜๋Š”๋ฐ, ์•„์ง ์™„๋ฒฝํ•œ๊ฑด ์•„๋‹Œ ๊ฒƒ ๊ฐ™์Œ
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <string.h>
    #include <time.h>
    #include <ftw.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <sys/sem.h>
    #include <sys/shm.h>
    
    #define BUFSIZE 512
    
    int main(int argc, char **argv){
        int fd, i, in = 5;
        struct flock lock;
        
        fd=open("turn1", O_RDWR|O_CREAT, 0600);
        
        lock.l_whence = SEEK_SET;
        lock.l_start = 0;
        lock.l_len = 4;
        lock.l_type = F_WRLCK;
        fcntl(fd, F_SETLKW, &lock);
        
        lseek(fd, 0, SEEK_SET);
        read(fd, &in, sizeof(int));
        
        for(i=0;i<5;i++){
            sleep(1);
            printf("pid = %d\n", getpid());
        }
        
        lock.l_type = F_UNLCK;
        fcntl(fd, F_SETLK, &lock);
        
        return 0;
    }

asdf