OS-3-Process concept

dragonappear·2021년 3월 16일
0

Operating System

목록 보기
3/6

# Outline:
1. process concept
2. process scheduling
3. operations on processes
4. Inter-process communication

1. process concept:

process = a program in execution

Process state:

  1. new: The process is being created
  2. running: Instructions are being executed
  3. waiting: The process is waiting for some event to occur (I/O request)
  4. ready : The process is waiting to be assigned to a processor
  5. terminated : The process

PCB(process control block)

  • 프로세스의 모든 정보를 담고 있는 자료구조( process state, program counter, cpu registers, cpu scheduling information, memory-management information , accounting information , i/0 status information 등등)

  • context switch가 발생하면:

  • 다른 프로세스로 전환될때 실행중이던 process의 모든 정보인 snapshot를 PCB에 저장해놓는다.
  • 나중에 이 프로세스의 상태가 ready->running 으로 바뀌어도 PCB의 값을 보고 다시 실행을 이어나갈수있다.


2. Process scheduling:

  • Scheduling queue: Process들을 담을 공간
    1. Job queue: 모든 프로세스들의 집합
    2. Ready queue: State가 Ready인 프로세스들의 집합
    3. Device queue: I/O 디바이스를 기다리는 프로세스의 집합
    프로세스들은 다양한 큐 사이를 이동한다.
  • Scheduler:
    1. Long-term scheduler(Job scheduler):
      메모리를 어떻게 할당할지를 결정
      어떤 프로세스를 Ready queue에 넣을지 결정
    2. Short-term scheduler(CPU scheduler):
      빠르게 context switching을 해준다.
      Ready queue에서 프로세스를 선택
      concurrently처럼 보이게 해준다.
    3. Medium-term scheduler:
      Swapping을 담당한다.

Context switch:

When CPU switches to another process, the system must
save the state of the old process and
load the saved state for the new process
Context-switch time is a big overhead;


3. Operation on processes

Process creation(fork)
Process termination(exit)

Operation on process

  • Resoource sharing options

    child share subset of parent's resources

  • Execution options

    Parent and child execute concurrently

    Parent waits until child terminates (wait() 함수를 이용하여)

  • Address space

    child duplicates parent's address space

    child has a program loaded into it(exec() 함수를 이용하여)

Fork system call

  1. resource sharing:
    • parent and children share files
    • CPU time or memory(address space) are not shared (부모와자식프로세스는 별도의 프로세스이므로)
  2. Execution
    • parent and children execute concurrently
  3. Address space
    • Child Duplicate parent address space
  4. Call once, return twice:
    • fork() return value의 값이 parent(=pid of child)와child(=0)에 따라 다르다.

  • Every process has the same program!
    exec system call is used to load a new program
  • Reaping child processes(부모가 자식을 거둔다)
    parent waits for its children to terminate

zombie process

자식 프로세스가 exit()하였는데 부모 프로세스가 wait() 하지않아 자식 프로세스가 zombie 상태가 되는것.

Process termination

  • exit: 마지막문장에서 실행되고 OS에게 자발적으로 삭제해달라고 요청한다.

  • abort: 부모가 자식 프로세스를 종료시키는 것.

위 그림에서 1번은 orpahn process가 발생한것이고, 2번은 cascading termination이 발생한것이다.


4. Inter-process communication(IPC)

  • Independent process cannot affect or be affected by the execution of another process

  • Cooperationg process can affect or be affected by the execution of another process.-> Process 간 communication이 필요하다.

  • 장점:

    1. information sharing(ex:file)
    2. computation speed-up
    3. modularity
    4. convinience

IPC:

  1. Message passing
  2. Shared Memory

위 두가지 방법은 os의 도움이 있어야 한다.

  • Producer-Consumer problem using a programmed shared-memory
  • producer process produces information
  • consumer consumes informathion
  • Must be synchronized

Spinlock:

버퍼가 비어있거나, 가득차있을때 발생하는 것.
while(); 루프를 계속돌고 있는 상황

Shared memory:

  1. shmget: 프로세스가 share memory를 만든다.

  2. shmat: 프로세스가 자신의 address space를 shared memory에 붙인다.

Message-passing:

  • IPC facility provides two operations:
    - send(message)
    - receive(message)
  • If P and Q communicate:
    - establish Communication link (maibox) between them
    - exchange messages via send/receive
  1. Direct communication

    • process must name each other explicitly (= "hard-coding") -> 이름이 바뀌면 recompile 해야한다.
    • send(P,message) - send a message to process P
    • receive(Q,message) - receive a message from process Q

  2. Indirect communication:

    • Messages are directed and received from mailboxes
    • each mailbox has a unique id
    • processes can communicate only if thy share a mailbox

Direct communication vs Indirect communication:

  • Direct communication(pipe):
    - Links are established automatically
    - usually bi-directional
    - Between each pair there exists exactly one link
    - shortcoming: can we know the name in advance?

  • Indirect communication
    - link established only if processes share a common mailbox
    - A link may be associated with many processes
    - Each pair of processes may share several communication links
    - link may be unidirectional or bi-directional

     

  1. A link may be associated with many processes.

  2. Each pair of processes may share several commuication links.

Indirect communication operations:

  1. create a new mailbox.
  2. send and receive messages through mailbox.
  3. destroy a mailbox.

who gets the message (if p2,p3 tries)?

  1. allow a link to be assocaited with at most two processes.
  2. Broadcasting
  3. allow only one process to receive a message.
  4. Allow the sending process to select the receiver.

Mailbox synchronization:

  • Message passing may be either blocking or non-blocking.

  • Blocking is considered Synchronous.

  • Non-blocking is considered Asynchronous.

OS는 Primitive만 제공해주고, 옵션은 프로그래머가 선택한다.

IPC comparsion

Message VS Shared memory:

  1. Message-passing:
    useful for inter-computer communication , exchanging smaller amounts of data and implemented using system calls so it is time-consuming.
  2. Shared memory:
    producer-consumer system -> synchronization을 위한 spinlock이 개발되었다.

0개의 댓글