티스토리 뷰

System

[Linux Kernel] Process Management

RealJuney 2024. 5. 1. 14:05

Process

Process는 executing program code, open files, pending signals, internal kernel data, process state, memory address space와 함께 하나 이상의 threads of execution, data section (global variable) 을 가진다.

Thread는 process의 activity 단위이다. 하나의 thread는 고유의 program counter, process stack, process register 등을 가진다. Linux의 경우 실행 단위는 thread이고 process와 구별하지 않는다.

 

 

Process Descriptor

 

  • Task List
    • process들을 담은 circular doubly linked list
    • 각 element는 process descriptor이다. (type: struct task_struct)
  • struct task_struct (Process Descriptor)
    • 각 프로세스 별로 kernel이 필요한 information을 들고 있음
    • slab allocator을 사용하여 할당됨
    • 2.6 Linux 이전엔 각 프로세스의 kernel stack 마지막 부분에 각각 저장됨
      • x86 처럼 register 많지 않는 아키텍처에서 stack pointer만으로 process descripotor 위치를 구할 있었음
    • 이젠 dynamic 하기 할당되므로 struct thread_info 타입의 새로운 데이터가 각 프로세스의 kernel stack 마지막에 위치함
      • thread_info에는 실제 task_struct의 pointer를 포함하여 몇 가지 정보를 포함
    • state field
      • TASK_RUNNING
      • TASK_INTERRUPTIBLE
        • sleeping (blocked), waiting for some condition. The process awakes prematurely and becomes runnable if it receives a signal
      • TASK_UNINTERRUPTIBLE
        • identical to TASK_INTERRUPTIBLE but does not wake up when receives signal
      • __TASK_TRACED
        • traced by another process such as debuger via ptrace
      • __TASK_STOPPED
        • task is not running nor is it eligible to run (SIGSTOP SIGSTP, SIGTTIN, or SIGTTOU)

 

Process Family Tree

Unix system에선 process간의 hierarchy가 존재한다 (Linux 포함). task_struct의 parent field와 children field는 그 process tree의 부모와 자식 리스트를 가지고 있다.

 

// todo

Process Creation

  • fork()
    • pid, ppid (parent pid), pending signals 등을 제외하곤 다 상속됨
    • copy-on-write (COW)
      • share for read only
      • 따라서 paget table copy와 process descriptor 할당만이 overhead
    • clone() syscall
      • fork(), vfork(), __clone() library 모두 clone() system call을 사용
      • clone() system call은 do_fork()를 call
        • do_fork()
          • copy_process() 를 call
            • 1. call dup_task_struct(), which creates new kernel stack, thread_info, and task_struct for the new process. (현재 process descriptor는 부모와 똑같다)
            • 2. process descriptor (task_struct)의 몇가지 부분을 리셋하거나 clear out 시킨다.
            • 3. child의 state를 TASK_UNINTERRUPTIBLE로 바꾼다. (갑자기 실행되면 안되므로)
            • 4. PID alloate

 

Linux Implementation of Threads

  • creating threads
    • clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0)
      • fork(), but address space, fs, file descriptors, signal handlers are shared
    • clone(SIGCHLD, 0)
      • just fork()
    • clone(CLONE_VFORK | CLONE_VM | SIGCHLD, 0)
      • vfork()

Kernel Threads

  • stanadard processes that exist soely in kernel space for some operations in the background
  • no address space (mm pointer is NULL)
  • no context switch into user-space

 

todo: Process Termination

'System' 카테고리의 다른 글

[Linux Kernel] Overview  (0) 2024.03.21
[OS] Thread API  (0) 2024.03.02
[OS] Concurrency and Threads  (0) 2024.03.01
[OS] Complete VM Systems: Linux  (0) 2024.02.12
[OS] Complete VM Systems: VAX/VMS  (1) 2024.02.12
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함