리루

Process Concept 본문

# Study/OS

Process Concept

뚱보리루 2017. 4. 27. 21:59

1. Process vs Program

 1-1) Program 

     - 실행파일

 1-2) Process

- Program이 실행돼서 OS에 의해서 수행되는 되체(Instance)


- Code segment와 Data segment는 프로그램 File(*.exe)에 들어가있다.

- Code segment에는 전역변수, Static변수 등이 포함된다.

- Stack은 Function Call 할때마다 수행된다.(Return address, Function Parameter, Local value가 저장된다)


2. Process State


- 프로세스마다의 상태를 운영체제가 관리하기 위함.

- Scheduling

: Ready 상태의 Process들의 실행될 순서를 정해준다(Ready 상태 중에 고른다. Not in waiting state)

: 프로세스 고르는 작업

cf) Dispatch : CPU에 올려주는 작업




  2-1) new 

- The process is being created

- 운영체제가 Process를 만드는 과정


  2-2) running

- Instructions are being executed


  2-3) waiting

- The process is waiting for some event to occur

- 어떤 이벤트가 발생해야지만 다시 실행할 준비가 된다.

- 실행될 준비가 다 되면 Interrupt가 발생해서 Ready상태로 바뀜

- Ready상태로 바뀐다고 바로 실행되는 것이 아니라 Scheduling을 통해서 Running 상태로 변경


  2-4) Ready

- The process is waiting to be assigned to a process

- 프로세스가 다 만들어지면 실행될 준비가 된다.


  2-5) Terminated

- The process has finished execution

- Process에 할당되었던 메모리 반환


  •  Interrupt
    • 지금 수행중인 Process는 수행될 수 있는 상태지만 어떤 다른 작업 때문에 Ready 상태로 돌아가고 OS가 수행됨

  • I/O or event wait
    • running 중인 Process가 file에서 data를 하나 읽어야 될때, 
    • OS에 요청 -> I/O에게 읽어라 요청 -> I/O가 물리적으로 읽는동안 CPU는 다른 작업  -> 해당 프로세스는 수행안됨 -> 해당 프로세스가 waiting 상태로 바뀜



3. PCB(Process Control Block)

 - 특정 Process와 관련된 모든 정보를 관리하는 Data Structure(block)

 - Process state

 - Program counter

 - CPU register

 - CPU scheduling information

 - Memory-Management Information

 - When the OS stops running a process, it saves the register's values in the PCB

 - When the OS puts the process in the running state, It loads the hardware registers from the values in that process's PCB



4. Context switch

 - CPU가 Process에서 Process로 Switch하는 과정

 - CPU의 갯수는 정해져있고, 실행 시켜야 하는 Process가 다수일 때, 적용

 - Context : CPU의 register(특정 Process의 상태가 어떤지 나타내는 것)의 집합

 - Time sharing system에서 사용자의 응답속도를 높히기 위해서 CPU는 손해(PCB에 상태 저장 및 Reload)를 보면서까지 Context Switching (Administrative overhead, 꼭 지불해야 하는 것은 아닌데 다른 무언가를 위해 추가적으로 지불하는 비용의 개념

 -



5. Process Creation

 5-1) PCB 할당, Process 관련 기록

 5-2) 메모리 공간을 잡고, Data Segment에 데이터를 채운다(실행파일에서 읽어서 메모리에 채워넣음)

 5-3) Process 상태를 Ready로 만들고 Ready Queue에 넣는다.

 5-4) Scheduling


 - fork()의 내부적인 작업

- 새로운 PCB를 만든다.

- 새로운 Address space를 만들고 초기화

- 부모의 Address space의 entire contents를 복하해온다.

- PCB를 Ready Queue에 넣는다.



6. IPC(Inter Process Communication)

 - 프로세스 간 통신

 - 


 6-1) Message passing(a)

- ProcessA와 Process B 간에 직접 접근을 하지 않는 이유는 Memory Protection때문이다.

- Kernel이 수행한다.

- 기존의 Kernel을 통해 구현하는 부분이라 구현이 쉽다.

- 단, Kernel의 한 구석에 M을 저장히기 위한 공간이 필요하다.( 이 때마다 매번 System call을 수행해야하고 이로인한 Overhead가 발생힌다.)

- 메시지함에 write 될때까지 계속 Blocking하며 기다림( kernel에 의한 자동 동기화)

    • Direct Communication

      • 직접 프로세스를 지칭한다.

      • Process A가 Kernel에 B한테 주라고 지칭해서 넘김

      • Process B가 Kernel한테 A가 준거 달라고 지칭해서 요청한다.

      • Between each pair there exists exactly one link

    • Indirect Communication

      • Kernel에 Mail box를 만들어 놓고 해당 Mail box를 중심으로 자원 공유.

      • Each mailbox has a unique id

      • Each pair of processes may share several communication link

      • A link may be associated with many processes

      • 실제로 사용되는 방식

 6-2) Shared Memory(b)

- Shared memory : Process A,B가 동시에 접근할 수 있는 메모리다.

- 일종의 동적할당이라고 보면됨. (But, 두 개의 Process가 동시에 접근)

- System call이 필요없다.(Kernel 의존성이 덜하다.)

- Preocess간의 공유메모리를 위해 인접해 있어야한다.

- 동기화 작업이 Application 단에서 추가적으로 구현이 요해진다.(세마포아 등)


- Producer-Consumer Problem(Shared-Memory)

- Shared Data

#define BUFFER_SIZE 10

Typedef struct {

...

}item;

item buffer[BUFFER_SIZE];

int in = 0;

int out = 0;


- Producer Process

item nextProduced;


while(1){

while( ((int+1) % BUFFER_SIZE) ==out)

;  // do noting

buffer[in] = nextProduced;

in = (in+1) % BUFFER_SIZE;

}


- Consumer Process

item nextConsumed;


while(1){

while(in==out)

; // do noting

nextConsumed = buffer[out];

out = (out+1) %BUFFER_SIZE;

}

'# Study > OS' 카테고리의 다른 글

Memory Management Strategies  (0) 2017.04.25
암호와 종류와 느낌  (0) 2017.03.31
[Virtual Memory] Frame management  (0) 2017.01.08
[Virtual Memory] Page replacement  (0) 2017.01.08