Skip to content

Latest commit

ย 

History

History
225 lines (161 loc) ยท 8.51 KB

File metadata and controls

225 lines (161 loc) ยท 8.51 KB

Chapter5. ํ”„๋กœ์„ธ์Šค ์ •๋ณด

  • Process๋ž€,

    • ํ”„๋กœ๊ทธ๋žจ์ด ํŒŒ์ผ ํ˜•ํƒœ๋กœ ํ•˜๋“œ๋””์Šคํฌ์— ์ €์žฅ๋ผ์žˆ๋‹ค๊ฐ€, ์‹คํ–‰์„ ์‹œํ‚ค๋ฉด process๊ฐ€ ๋œ๋‹ค
    • [Program code]
    • [Data : ํ”„๋กœ๊ทธ๋žจ ์ „์—ญ ๋ณ€์ˆ˜] : ์ฒ˜์Œ ์ปดํŒŒ์ผํ•  ๋•Œ๋ถ€ํ„ฐ ๋ฉ”๋ชจ๋ฆฌ Data ์˜์—ญ์— ๋”ฐ๋กœ ์ €์žฅ๋จ. ํ”„๋กœ๊ทธ๋žจ์ด ๋๋‚˜์•ผ ์‚ฌ๋ผ์ง
    • [Stack : ํ”„๋กœ๊ทธ๋žจ ์ง€์—ญ ๋ณ€์ˆ˜] : ํ•จ์ˆ˜์—์„œ ํ˜ธ์ถœ๋œ ๋ณ€์ˆ˜๋“ค ๋ฉ”๋ชจ๋ฆฌ Stack ์˜์—ญ์— ๋”ฐ๋กœ ์ €์žฅ๋จ. ํ•จ์ˆ˜ return ์‹œ ์‚ฌ๋ผ์ง
    • [PCB : Process Control Block]
      • ํ”„๋กœ์„ธ์Šค ์ œ์–ด ๋ธ”๋ก
      • Process๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์š”์†Œ๋“ค์— ์˜ํ•ด ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„๋  ์ˆ˜ ์žˆ๋‹ค (์‹๋ณ„์ž, ์ƒ…์• , ์šฐ์„ ์ˆœ์œ„, ํ”„๋กœ๊ทธ๋žจ ์นด์šดํ„ฐ, ๋ฉ”๋ชจ๋ฆฌ ํฌ์ธํ„ฐ, ๋ฌธ๋งฅ ๋ฐ์ดํ„ฐ, ์ž…์ถœ๋ ฅ์ƒํƒœ์ •๋ณด, ์–ด์นด์šดํŒ…์ •๋ณด) ์ด๋Ÿฐ๊ฒŒ ์žˆ๋Š”๋ฐ, ์ด๋Ÿฐ ์š”์†Œ๋“ค์ด PCB์— ์ €์žฅ์ด ๋˜๊ณ , OS์— ์˜ํ•ด ์ƒ์„ฑ๋˜๊ณ  ๊ด€๋ฆฌ๋œ๋‹ค
      • PCB์ด ์ฃผ์š” ์—ญํ• ์€ process๋ฅผ interruptํ•œ ํ›„ ๋‹ค์‹œ ๊ทธ process๋ฅผ ์ˆ˜ํ–‰์žฌ๊ฐœ ํ•  ์ˆ˜ ์žˆ๊ฒŒ process์˜ ์ •๋ณด๋ฅผ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ. ๋‹ค์ˆ˜์˜ process๋ฅผ ์ง€์›ํ•˜๊ณ , ๋ฉ€ํ‹ฐํ”„๋กœ์„ธ์‹ฑ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค
      • [Process Identification]
        • Process Identifier : ํ”„๋กœ์„ธ์Šค๋“ค์ด ์„œ๋กœ ํ†ต์‹ ํ•  ๋•Œ, ํ†ต์‹ ํ•  ์ƒ๋Œ€๋ฐฉ์„ OS์—๊ฒŒ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด ํ”„๋กœ์„ธ์Šค ์‹๋ณ„์ž ์‚ฌ์šฉ
        • Parent Process Identifier : ํ”„๋กœ์„ธ์Šค๋“ค์ด ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, ์‹๋ณ„์ž๋ฅผ ์ด์šฉํ•ด์„œ ๋ถ€๋ชจ/์ž์‹ ํ”„๋กœ์„ธ์Šค ๊ตฌ๋ณ„ ๊ฐ€๋Šฅ
        • User Identifier : ์ž‘์—…์— ๋Œ€ํ•ด ์ฑ…์ž„์ง€๊ณ  ์žˆ๋Š” ์‚ฌ์šฉ์ž๋ฅผ ์ง€์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž ์‹๋ณ„์ž๊ฐ€ ๊ฐ ํ”„๋กœ์„ธ์Šค์— ํ• ๋‹น๋  ์ˆ˜ ์žˆ๋‹ค
      • [Processor State Information]
      • [Process Control Information]
  • Process์˜ ๊ณ„์ธต ๊ตฌ์กฐ : Parent process -> Child proceses ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด, ํ”„๋กœ์„ธ์Šค๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ child ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ, ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๋“ค๋กœ ์‹คํ–‰์ด ๋œ๋‹ค

  • UNIX ์‹œ์Šคํ…œ์˜ ๋ชจ๋“  process๋Š” [init] ์ด๋ผ๋Š” 1๋ฒˆ process์˜ descendent process์ด๋‹ค [init] ํ”„๋กœ์„ธ์Šค๊ฐ€ [shell] process๋ฅผ ๋งŒ๋“ค๊ณ , shell์ด [child]๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•œ๋‹ค

  • ์˜ˆ๋ฅผ ๋“ค์–ด์„œ a.out ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด, init ํ”„๋กœ์„ธ์Šค๊ฐ€ shell ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งŒ๋“ค๊ณ , shell ํ”„๋กœ์„ธ์Šค๊ฐ€ a.out์ด๋ผ๋Š” child ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‹คํ–‰ํ•œ๋‹ค

  • Shell์ƒ์—์„œ ํ”„๋กœ์„ธ์Šค ๋ชฉ๋ก ํ™•์ธ

    $ ps

    ์‹คํ–‰ ๊ฒฐ๊ณผ

      PID TTY          TIME CMD
    16945 pts/7    00:00:00 bash
    20763 pts/7    00:00:00 ps
    
  • ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ์‹œํ‚ค๊ธฐ

    PID๋ฅผ ์•Œ์•„์•ผ ์ข…๋ฃŒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ps๋กœ ํ™•์ธํ•˜๊ณ  ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค

    $ kill -9 [ํ”„๋กœ์„ธ์Šค ๋ฒˆํ˜ธ : PID]
    
  • Process Identifier

    • ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜
    • 0 : swapper
  • 1 : init

    • getpid() : ์ง€๊ธˆ ์‹คํ–‰ ์ค‘์ธ process ID
  • getppid() : ์ง€๊ธˆ ์‹คํ–‰ ์ค‘์ธ process์˜ parent ID

    include<unistd.h>
    
    pid_t getpid(void);
    pid_t getppid(void);
    
    getpid();
    getppid();
  • Process Group

    • ํŠน์ • ํ”„๋กœ์„ธ์Šค๋“ค์„ ๋ฌถ์–ด์„œ ํ•˜๋‚˜์˜ group์œผ๋กœ ๋งŒ๋“ค์–ด์„œ, ๊ฐ™์€ group์— ์†ํ•œ process๋“ค์—๊ฒŒ ๋™์‹œ์— signal์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค ์ดˆ๊ธฐ์— fork๋‚˜ exec์— ์˜ํ•ด group id ๊ณ„์Šน(์ƒ์†) : ๋”ฑํžˆ ๋ช…์‹œ ์•ˆ ํ•ด์ฃผ๋ฉด, parent ์•„๋ž˜ ๋ชจ๋“  process๋Š” ๊ฐ™์€ group์ž„ ๊ทธ๋ž˜์„œ group id๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๊ทธ ์•„๋ž˜ gid๋„ ๋‹ค ์ƒ์†์„ ๋ฐ›์œผ๋‹ˆ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค
  • Group leader : group leader๋Š” pid์™€ pgid๋ฅผ ๊ฐ™๊ฒŒ ํ•ด์ค˜์•ผํ•จ. ๋”ฐ๋ผ์„œ, ์ž์‹ ์˜ pid๊ฐ€ group id์ด๋ฉด group leader์ด๋‹ค.

    #include <sys/types.h>
    #include <unistd.h>
    
    //๋‚˜์˜ group id ์•Œ์•„๋ณด๊ธฐ
    pid_t getpgrp(void);
    
    //๋‹ค๋ฅธ child์˜ id๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด, ๊ทธ process์˜ gid ๋ฐ˜ํ™˜
    //์ธ์ž๊ฐ€ 0์ด๋ฉด, ํ˜ธ์ถœ ํ”„๋กœ์„ธ์Šค ์ž์‹ ์˜ group id๋ฅผ ๋ฐ˜ํ™˜
    pid_t getpgid(pid_t pid);
    getpgrp();
    getpgid(0);
    • process group ๋ณ€๊ฒฝ / setpgid(id, gid);

      ์ง€๊ธˆ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด group leader๊ฐ€ ๋˜๊ฒ ๋‹ค๋Š” ๋ช…๋ น

      #include <sys/types.h>
      #include <unistd.h>
      
      //์ธ์ž(๋‚ด id, ๋‚ด group id)
      int setpgid(pid_t pid, pid_t pgid);
      #include <stdio.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <unistd.h>
      #include <dirent.h>
      #include <string.h>
      #include <ftw.h>
      
      int main(int argc, char **argv) {
        pid_t pid;
      
        printf("%ld : %ld\n", getpid(), getppid());
        printf("%ld : %ld\n", getpgrp(), getpgid(0));
      
        pid = fork();
      
        //child์ธ ๊ฒฝ์šฐ์— 0 ๋ฐ˜ํ™˜
        if(pid == 0) {
          printf("%ld : %ld\n", getpid(), getppid());
          printf("%ld : %ld\n", getpgrp(), getpgid(0));
      
          //group id ๋ณ€๊ฒฝ
          setpgid(getpid(), getpid());
      
          printf("%ld : %ld\n", getpid(), getppid());
          printf("%ld : %ld\n", getpgrp(), getpgid(0));
        }
        wait(0);
        return 0;
      }
    • ์‹คํ–‰๊ฒฐ๊ณผ

      • ./example.out ์‹คํ–‰ํ•˜๋ฉด, shell ํ”„๋กœ์„ธ์Šค๊ฐ€ example ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰. example์„ group์˜ ๋ฆฌ๋”๋กœ ์‹คํ–‰์‹œํ‚ด
      29816 : 16945 //example ํ”„๋กœ์„ธ์Šค id : shell ํ”„๋กœ์„ธ์Šค id
      29816 : 29816 //example ํ”„๋กœ์„ธ์Šค gid
        
      29817 : 29816 //child ํ”„๋กœ์„ธ์Šค id : parent์ธ example ํ”„๋กœ์„ธ์Šค id
      29816 : 29816 //child ํ”„๋กœ์„ธ์Šค gid. example ํ”„๋กœ์„ธ์Šค์˜ child์ด๋‹ˆ๊นŒ parent์˜ gid์™€ ๊ฐ™์Œ
        
      29817 : 29816 //child ํ”„๋กœ์„ธ์Šค id : parent์ธ example ํ”„๋กœ์„ธ์Šค id
      29817 : 29817 //child ํ”„๋กœ์„ธ์Šค๋ฅผ group leader๋กœ ๋งŒ๋“ค์–ด์„œ pid์™€ pgid ๊ฐ™์•„์ง
      
  • Session

    • ํ•œ session์€ ํ•œ ๋‹จ๋ง๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” foreground process group๊ณผ background process group์˜ ์ง‘ํ•ฉ์ฒด์ด๋‹ค

    • ๊ฐ process group์€ ํ•˜๋‚˜์˜ session์— ์†ํ•œ๋‹ค A [process group] is a collection of related processes which can all be signalled at once.

      A session is a collection of process groups, which are either attached to a single terminal device (known as the controlling terminal) or not attached to any terminal.

      Sessions are used for job control: one of the process groups in the session is the foreground process group, and can be sent signals by terminal control characters. You can think of a session with a controlling terminal as corresponding to a "login" on that terminal. (Daemons normally disassociate themselves from any controlling terminal by creating a new session without one.)

      e.g. if you run some_app from the shell, the shell creates a new process group for it, and makes that the foreground process group of the session. (some_app might create some child processes; by default they will be part of the same process group.) If you then press ^Z, some_app's process group is signalled to stop it; and the shell's process group is switched to be the foreground process group again. Then e.g.bg %1 would start some_app's process group again, but keep it running in the background.

    • getsid ์‹œ์Šคํ…œ ํ˜ธ์ถœ : session id๋ฅผ ํš๋“ / ์–ด๋–ค session์— ์†ํ•ด์žˆ๋Š”์ง€ ํ™•์ธ

      #include <sys/types.h>
      #include <unistd.h>
      
      pid_t getsid(pid_t pid);
    • setsid() ์‹œ์Šคํ…œ ํ˜ธ์ถœ

      #include <sys/types.h>
      #include <unistd.h>
      
      pid_t setsid(void);
      • ์ œ์–ด ๋‹จ๋ง๊ธฐ๋ฅผ ๊ฐ–์ง€ ์•Š๋Š” ์ƒˆ๋กœ์šด session๊ณผ group ์ƒ์„ฑ

        ํ˜ธ์ถœ ํ”„๋กœ์„ธ์Šค์˜ id๊ฐ€ session๊ณผ group์˜ id๊ฐ€ ๋œ๋‹ค

        ๋งŒ์•ฝ, ํ˜ธ์ถœ process๊ฐ€ ํ˜„์žฌ group์˜ leader์ด๋ฉด -1์„ returnํ•œ๋‹ค

      • session์„ ์ข…๋ฃŒํ•ด๋„ ๊ณ„์† ์‹คํ–‰ ๋˜๊ฒŒ ๋งŒ๋“ค ๋•Œ session์„ ๋ณ€๊ฒฝํ•œ๋‹ค

        ์ด session๊ณผ ๊ด€๊ณ„์—†์ด ์‹คํ–‰ํ•˜๊ฒ ๋‹ค๋ผ๋Š” ๋ช…๋ น์ด๋‹ˆ, shell ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ๋ผ๋„ ์ข…๋ฃŒ๋ฅผ ๋ชป์‹œํ‚ค๊ฒŒ ๋ผ์„œ ์‚ฌ์šฉํ•˜์ง€ ๋ง ๊ฒƒ

  • Mainํ•จ์ˆ˜ ์ธ์ž ์‚ฌ์šฉ. ์ด์ œ๋Š” ์ด๋ ‡๊ฒŒ ์ธ์ž๋ฅผ ์ „๋‹ฌ๋ฐ›์•„์„œ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค ๊ฒƒ

    //argc ๋ฌธ์ž์—ด ๋ช‡ ๊ฐœ๋ฅผ ๋„˜๊ฒจ๋ฐ›์•˜๋Š”์ง€ ์•Œ๋ ค์คŒ
    //argv ํฌ์ธํ„ฐ ๋ฐฐ์—ด๋กœ, ์–ด๋–ค ๊ฐ’์„ ๋„˜๊ฒจ๋ฐ›์•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Œ
    int main(int argc, char **argv) {
      int i = 0;
      printf("%d\n", argc);
    
      while(argv[i] != NULL) {
        printf("%s\n", argv[i]);
        i++;
      }
      return 0;
    }
  • ๋ชจ๋“  ์ž…์ถœ๋ ฅ์€ OS๊ฐ€ ๋‹ด๋‹นํ•จ. program์ด I/O ์ž‘์—…์€ OS์—๊ฒŒ ๋ถ€ํƒ์„ ํ•˜๋Š” ๊ฒƒ์ž„. ๊ทธ๋Ÿฌ๋ฉด ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’๋“ค์„ OS๋Š” mainํ•จ์ˆ˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ์ž„

    //ls ํ˜„์žฌ์œ„์น˜ ํ”„๋กœ๊ทธ๋žจ ์ด๋ฆ„
    //-l ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์„ ์œ„ํ•œ ์ธ์ž
    $ ls -l
    
    //chmod ํ˜„์žฌ์œ„์น˜ ํ”„๋กœ๊ทธ๋žจ ์ด๋ฆ„
    //0777 ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์„ ์œ„ํ•œ ์ธ์ž
    //a.c ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์„ ์œ„ํ•œ ์ธ์ž
    $ chmod 0777 a.c
    
  • getpid() / getppid() / getpgrp() / getpgid(0) / setpgid(pid, gid) / getsid(pid) / setsid()