Signals
A very simple way of sending messages between two proecesses → Information transferred is limited to a signal number (standard signal)
시그널은 프로세스간의 메시지를 주고 받는 가장 간단한 방법이지만, 그 정보가 시그널 숫자로 제한된다는 한계를 갖는다.
Inter-Process Communication (IPC)
- Pipe → Stream oriented (e.g., TCP) → Data가 쌓이면 그냥 뭉쳐버린다.
- Message queue → Message oriented (e.g., UDP) → Data가 오면, 덩어리 (메시지) 단위로 쌓는다.
- Shared Memory → A virtual memory area shared between processes → 공통의 공간을 여러 프로세스가 사용하는 것이기 때문에 Race Condition이 발생할 수 있다. 이는 sigprocmask()로도 해결할 수 없다.
- Semaphore → Synchronization → Shared Memory의 Race condition문제를 해결하기 위해 사용한다.
- Socket → An endpoint of an inter-process communication flow across a computer network → 다른 컴퓨터의 프로세스에"도" 사용할 수 있다. 같은 컴퓨터의 내부에 사용한다면 쓸데 없는 overhead가 발생한다. socket은 process의 위치를 모르는 경우에 해결책이 될 수 있다.
System V (SysV) IPC
SysV
- UNIX OS developed by AT&T
- System V release 4, or SVR4, was commercially the most successful version
- System V interface definition
- IBM's AIX
- Sun's Solaris
- Hewlett-Packard's HP-UX
Supported IPCs
- Message queue
- Shared Memory
- Semaphore
POSIX IPC
Portable Operating System Interface (POSIX)
- Standards specified by the IEEE for maintaining cimpatibility between operating systems
Supported IPCs
- Pipe (POSIX.1)
- Message queue (POSIX.1b)
- Shared memory (POSIX.1b)
- Semaphore (POSIX.1b)
Pipe
Pipe
- A unidirectional data channel → 데이터의 방향이 하나로, 오고 가는 데이터의 채널을 만들고 싶다면 두 개의 pipe를 만들어야 한다.
- Stream oriented → 데이터의 구분 없이, 뚝뚝 떼어내서 데이터를 받을 수 있다.
- Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe → 쓰여진 데이터는 pipe의 한 쪽 끝에 쓰이고, 이는 커널에 저장되는데, 그 한 쪽 끝에서부터 읽힌다.
- Strict FIFO behavior
- System calls
- pipe() → pipe만들기
- read(), write()
Pipe examples
- cmd1 | cmd2
- Pipes '|' send the output of one command as input of another command → | 를 통해 cmd1의 출력을 cmd2의 입력으로 넣고, cmd2의 출력을 터미널에 보여준다.
- the standard input to cmd1 comes from the terminal keyboard
- the standard output from cmd1 fed to cmd2 as its standard input
- the standard output from cmd2 is connected to the terminal screen
- Pipes '|' send the output of one command as input of another command → | 를 통해 cmd1의 출력을 cmd2의 입력으로 넣고, cmd2의 출력을 터미널에 보여준다.
Useful commands
- cat
- Concatenate files and print on the standard outuput
- more
- Pages through text one screenful at a time → 화면 단위로 출력을 보여준다
- grep
- prints lines matching a pattern
- sort
- sort lines of text files
examples
Message Queue
- A bidirectional data channel → 방향이 양방향이라기 보다는 양방향 통신이 하나의 Queue로 가능하다는 것이다
- Message oriented → 데이터를 메세지, 덩어리 단위로 저장하고 꺼내 쓴다.
- Allows one or more processes to write message to be read by other processes → 하나 이상의 프로세스들이 다른 프로세스들을 위해 메시지를 쓸 수 있다.
- Each message queue is identified by a message queue identifier
- A message queue is implemented as a linked list of messages in the kernel → message queue는 커널에 linked list의 형태로 존재한다
- Messages can be retrieved out of FIFO order → 기본적으로 FIFO이지만, priority개념이 있어서, 우선순위에 따라 읽힐 수도 있다
- message queue system call
- SysV message queue
- msgget() → Message Queue 생성, identifier 반환
- msgsnd() → send
- msgrcv() → receive
- POSIX message queue
- mq_opne() → Message Queue 생성, identifier 반환
- mq_send() → send
- mq_receive() → receive
- mq_close()
- mq_unlink()
- SysV message queue
Shared Memory
- A portion of memory mapped into the address space of one or more processes → 하나 이상의 프로세스들의 주소 공간에 할당된 메모리 공간이다. 이때 프로세스들의 할당된 주소 번지는 다를 수 있다.
- Allows processes to communicate information by sharing a region of memory
- Processes must synchronize their access to a shared memory object, using semaphore → semaphore를 이용해서 프로세스들의 접든을 통해하여, 싱크를 맞춰야만 한다
- shared memory system call
- SysV shared memory
- shmget() → shared memory 만들기
- shmat() → shared memory를 virtual address에 attatch
- shmdt() → shared memory를 virtual address에 dittatch
- POSIX shared memory
- shm_open() → shared memory를 만들고 attatch까지
- shm_unlink() → unlink
- SysV shared memory
Semaphore
- A sharable integer whose value is never allowed to fall below zero
- Allow processes and threads to shynchronize their actions
'[학교 수업] > [학교 수업] System Programming' 카테고리의 다른 글
[시스템 프로그래밍] Message Queue (3) | 2024.10.17 |
---|---|
[시스템 프로그래밍] Pipes (3) | 2024.10.15 |
[시스템 프로그래밍] Real-Time Signal (5) | 2024.10.08 |
[시스템 프로그래밍] Assembly Language (1) | 2024.10.06 |
[시스템 프로그래밍] Signal Examples (2) | 2024.10.06 |