Hi there!

I am a student studying computer science.

시스템 프로그래밍

시스템 프로그래밍 4장 - Concurrent Process

만능성구 2020. 5. 5. 16:02
728x90

Process관련된 system call interface

Concurrent Process란?

- 동시에 실행되는 여러 개의 process라는 뜻이다.
이렇게 여러 개의 process가 하나의 목적을 가지고 일을 나누어서 실행되는 Program이 많이 있다.

cocurrent process가 가능하도록 여러 api 즉 system call 이라든지 progrming interface를 제공해주는 방식으로

- 일반적으로 IPC(Inter Process Comunication)가 있다. //process간의 통신기법

- cocurrent process 도구로는

cocurrent language인 Ada, Java, PathPascal, Modula II 들이 있고 이 언어들은 interface를 제공하고  
C, C++에서 system차원에서 system call에서 interface를 제공한다.


예를 들어서 많은 application들이 concrrent process방식으로 실행된다.

Applications (Objectives)

  • Networked (or distributed) applications
    • Network App : client processes, server processes
    • Distributed App : chess program, cloud computing // 분산 1:! 다:다
  • Real-time applications // 실시간 응용 application
    • Aircraft control : cockpit display, Flight management,
    • Missile system, Automobile : many sensors, controllers, and servo motors(actuators) are handled by multiple processes // 자동차, 동기화, 협업으로
    • Usually, a process for each sensor
  • Parallel applications: 3D graphic, many CPUs, many parallel processes // 요즘 그래픽스에서 hot
    • To utilize more CPUs, GPUs // Thread를 병렬적으로 실행시키는 것
  • CPU/IO overlap in a program leveㅣ // cpu에서 I/O작업하는 것
    • When a process does computing, another process does I/O in a program.
  • Asynchronous event handling // Event를 handling하는 작업
    • Handling of multiple input sources: don’t know which device makes input first.
    • In this case, assign a process to each input device.

Courrent Process Programming을 하기 위해서 process를 어떻게 하냐.

Process란?

OS내에서 자신의 주소 공간을 가지고 실행되는 Program 단위이다.

하나의 process는자신의 text, data, stack 영역을 가지고 실행된다.

이런 Process는 OS에서 하나의 ID를 할당에서 관리한다. (PID)

PID

ID는 unique하게 할당되고 종료된 뒤에 다시 재사용된다.

PID 0 : swapper (scheduler)

PID 1 : the init process (invoked at the end of the boostrap procedure, located in /etc/init or /sbin/init)

PID 2 : pagedaemon // Linux의 virtual memory system을 관리하는 daemon process

PCB

하나의 process는 하나의 데이터 구조인 PCB(process control block)으로 관리된다.

PID, UID, GID, 현재 사용되는 dir, terminal, 우선순위, 상태, CPU상태, 자신의 Memory map상태, 자신이 관리하는 file의 open된 상태, lock상태 들이 PCB에서 관리된다.

Process ID를 알아보는 interface

#include <unistd.h>
pid_t getpid(void);
// Returns: process ID of calling process
pid_t getppid(void);
// Returns: parent process ID of calling process
uid_t getuid(void);
// Returns: real user ID of calling process
uid_t geteuid(void);
// Returns: effective user ID of calling process
gid_t getgid(void);
// Returns: real group ID of calling process
gid_t getegid(void);
// Returns: effective group ID of calling process

Linux에서 Process를 생성하는 방법.

#include <unistd.h>

pid_t fork(void);

process를 생성하는 system call

현재 실행되는 process와 동일한 process를 생성한다. -> child process

child process는 fork(..)된 위치부터 실행된다.

return child ID(부모, 원래 자식의 PID) / 0 (자식) / -1

fork(..)이후 각자 실행된다.

자식 process가 부모 process의 code와 data들을 copy해서 생성된다.

- code와static data

- user id group id

- working dir, root dir

- 이전에 open된 file 모두 전달

fork(..)이후 private되는 부분 ->

독립된 PID, PCB

kernel에서 scheduling되는 방식도 독립적으로

자원들이 각각의 porcess에 종속된다.

별도의 file descriptor를 가진다.

그런데 동일한 file table에서 접근할 수 있게 해준다.

fork(..)가 실패하는 경우

process가 너무 많아서 maximum이된 경우(CHILD_MAX)


생성된 Process 종료시키는 방법

#include <stdlib.h>

void exit(int status);

일반적인 porcess를 종료시킨다.

자식 process는 상태(status)를 부모에게 전달한다. 부모는 wait(..)를 이용해서 확인한다.

 
return 자신의 PID

1. exit(..)

2. main함수가 끝나면

3. 마지막 thread에서 pthread_exit하면

종료될 때 Kernel

- open descriptor를 close

- 해당 Memory 영역, PCB 해제

만약 자식process에 대한 정보를 부모가 받지 않고 먼저 종료되면 자식 Process에 status를 받을 수 없어서 zombie가 된다.

#include <sys/wait.h>

pid_t wait(int *status);
자식 process가 종료될 때까지 기다리는 system call
status : 자식 process의 status가 저장될 주소
return 자식 PID / -1
#include <sys/wait.h>

pid_t waitpid(pid_t pid, int *status, int options);
특정 pid에 대해서 wait하는 system call

pid : wait하는 process ID

    if  -1 // 설정을 하지 않아서 wait()와 동일해진다.

status : 자식 process의 status가 저장될 주소

options : WNOHANG : for non-blocking call // 어떤 상태로 기다릴거냐 상태로 만들어줄거냐

return

자식 process가 많을 수도 있기 때문에 waitpid를 씀

Macros for exit status

  • WEXISTATUS(status)
    • fetch the exit code which the child sends
    • 자식 process가 보내는 exit code를 받을 수 있다.
  • WIFEXITED(status)
    • true if the child terminated normally
    • 정상적으로 종료되었나 T/F
  • WIFSIGNALED(status)
    • true if status was child terminated abnormally by a signal
    • signal에 의해서 갑자기 종료되면 T/F
  • WIFSTOPPED(status)
    • true if status was returned for a child that is currently stopped
    • stop되었으면 T/F
  • WTERMSIG(status)
    • fetch the signal number that caused the child to be terminated
    • 어떤 signal에 의해 종료되었는가
  • WCOREDUMP(status)
    • true if a core file or the terminated process was generated System Programming 17 wait() and exit()
    • core file이나 정보가 있을 경우 T/F

exec(..)

- 동일한 것을 새로 생성하는 것이 아니라 새로 불러온 Process가 현재 자신의 Process를 대치한다.

- 처음부터 실행된다.

- 변경 안되는 것 : PID, priority, 현재 dir, time left until an alarm, pending signals, open files and file locks.

//정보들은 바뀌지 않는다.

- 변경되는 것 : text, data, heap, stack seqment는 바뀐다

exec family

#include <unistd.h>

int execl(const char *pathname, const char *arg0, ... /* (char *)0 */ );
int execv(const char *pathname, char *const argv[]);
int execle(const char *pathname, const char *arg0, ... /* (char *)0, char *const envp[] */ );
int execve(const char *pathname, char *const argv[], char *const envp[]);
int execlp(const char *filename, const char *arg0, ... /* (char *)0 */ );
int execvp(const char *filename, char *const argv[]);
int fexecve(int fd, char *const argv[], char *const envp[]);

// All return: -1 on error, no return on success
execl 실행할 prgram의 path, argument를 char형태로
execv 실행할 prgram의 path, argument를 vector array형태로
execle 실행할 prgram의 path, argument를 char형태로, + 환경변수
execve 실행할 prgram의 path, argument를 vector array형태로, + 환경변수
...
All return None / -1

fork와 exec가 linux system에서 booting과 동시에 초기화 되는 상태.

pid = 1인 init이 만들어진다.

user가 접속할 때마다 getty로 consol을 제공한다. connection을 기다린다.

exec를 통해서 getty를 login으로 대치.

login하면 exec로 login이 shell로 대치

여기서 command를 입력하면 fork와 exec로 a.out실행 한 다음 exit하면 shell은 wait하고 있는다.

728x90