공부이야기/Network
IPC(interProcessCommunication)의 이해(2)
coyotek
2008. 6. 9. 16:44
IPC(Inter Process Communicaiton) 이해 (2)
정의:
IPC는 프로그램들간의 데이터를 공유하고 동기화하기 위해 사용되는 방법을 뜻한다. 이때 프로그램은 보통 프로세스를 의미한다. IPC를 할 수 있게 해주는 일반적인 도구는 세마포어, 공유메모리( Shared memory), 내부 메시지큐등이 있다. 이런 방법이 갖는 이점은 디스크를 통한 파일공유와 관련IO를 통한 공유하는 방법의 오버헤드를 줄일 수 있다는 것이다.
Use IPCs when you need to talk between programs, you want the talking to be 프로그램들간에 데이터를 빠르게 공유하고 싶을 때, 혹은 전하고 싶을 때, 또한 프로세스간의 통신의 복잡한 하부구조를 알지 않아도 되는 경우 등이다. IPC는 커널이 세부적인 통신을 맡아서 해주기 때문이다. 예룰 들어 당신의 프로세스가 세마포어에 의해 보호되는 자원을 기다리고 있다고 한다면 커널은 당신의 프로세스를 대기행렬( Waiting gueue )에 넣어 버린다. 그리고 자원을 쓸 수 있는 순간이 되면 커널은 프로세스에게 자원을 할당하게 된다. 또한 커널은 이런 오퍼레이션이 원자적(atomic)일 것을 확인한다. 이것은 데이터의 무결성을 위해 세마포어를 세팅하는 오퍼레이션은 어떠한 경우에도 방해 받지 않는다는 것이다.
IPC 통신 루틴상의 모든 자원은 비슷한 방법과 매우 유사한 함수 콜을 이용한다.
IPC루틴은 다루는 데이터의 양에 따라 다르다.
다음 두개의 명령어를 활용할 수 있다.
Ipcs: 세마포어와 메시지큐, 공유메모리의 상태를 볼수 있다.
ipcs -m ( shared memory )
ipcs -q ( message gueues )
ipcs -s ( semaphore )
Ipcrm: 세마포어, 메세지큐,공유메모리부분을 시스템에서 제거한다. Ipcrm 명령은 shmctl,semctl,msgctl system call의 front end명령(사용자가 직접 부르는)으로서 적절한 실행관한과 옵션을 사용하면 자원을 D(deletion)상태로 마크하게 된다. 그러나, 자원을 사용하고 있는 모든 프로세스가 detach를 해야만 완전히 ipcs를 통해 보면 사라지게 된다.
Fields common to each of the IPC types
Ipcs명령의 결과에서 보면 일부는 공통적으로 사용된다. 다음부분이 그러하다.
The first two characters can be one of these: RSCD
IPCs need permissions for owner, group and other.
Headings that are special to message queues
Headings that are special to semaphores
Headings that are special to shared memory
IPC란 무엇인가? |
정의:
IPC는 프로그램들간의 데이터를 공유하고 동기화하기 위해 사용되는 방법을 뜻한다. 이때 프로그램은 보통 프로세스를 의미한다. IPC를 할 수 있게 해주는 일반적인 도구는 세마포어, 공유메모리( Shared memory), 내부 메시지큐등이 있다. 이런 방법이 갖는 이점은 디스크를 통한 파일공유와 관련IO를 통한 공유하는 방법의 오버헤드를 줄일 수 있다는 것이다.
IPC를 쓰는 이유 |
Use IPCs when you need to talk between programs, you want the talking to be 프로그램들간에 데이터를 빠르게 공유하고 싶을 때, 혹은 전하고 싶을 때, 또한 프로세스간의 통신의 복잡한 하부구조를 알지 않아도 되는 경우 등이다. IPC는 커널이 세부적인 통신을 맡아서 해주기 때문이다. 예룰 들어 당신의 프로세스가 세마포어에 의해 보호되는 자원을 기다리고 있다고 한다면 커널은 당신의 프로세스를 대기행렬( Waiting gueue )에 넣어 버린다. 그리고 자원을 쓸 수 있는 순간이 되면 커널은 프로세스에게 자원을 할당하게 된다. 또한 커널은 이런 오퍼레이션이 원자적(atomic)일 것을 확인한다. 이것은 데이터의 무결성을 위해 세마포어를 세팅하는 오퍼레이션은 어떠한 경우에도 방해 받지 않는다는 것이다.
IPC calls의 유사성과 차이점 |
IPC 통신 루틴상의 모든 자원은 비슷한 방법과 매우 유사한 함수 콜을 이용한다.
기능 | 메세지큐 | 세마포어 | 공유메모리 |
1.IPC할당방법 | msgget | semget | shmget |
2.IPC제어방법 | msgctl | semctl | shmctl |
(상태변경,해제) | |||
3.IPC작동방법 | msgsnd | semop | shmat |
(send/receive) | msgrcv | shmdt |
IPC루틴은 다루는 데이터의 양에 따라 다르다.
- 세마포어는 long integer이며 단일숫자를 나타낸다.
- 메시지큐는 4096문자를 가질 수 있다.
- 공유메모리 세그먼트는 256MB이다.
- 메시지큐는 4096문자를 가질 수 있다.
- 공유메모리 세그먼트는 256MB이다.
현재 활동중인 상태를 보는 방법 |
다음 두개의 명령어를 활용할 수 있다.
Ipcs: 세마포어와 메시지큐, 공유메모리의 상태를 볼수 있다.
ipcs -m ( shared memory )
ipcs -q ( message gueues )
ipcs -s ( semaphore )
Ipcrm: 세마포어, 메세지큐,공유메모리부분을 시스템에서 제거한다. Ipcrm 명령은 shmctl,semctl,msgctl system call의 front end명령(사용자가 직접 부르는)으로서 적절한 실행관한과 옵션을 사용하면 자원을 D(deletion)상태로 마크하게 된다. 그러나, 자원을 사용하고 있는 모든 프로세스가 detach를 해야만 완전히 ipcs를 통해 보면 사라지게 된다.
Fields common to each of the IPC types
Ipcs명령의 결과에서 보면 일부는 공통적으로 사용된다. 다음부분이 그러하다.
T | Type= m (shared memory), s (semaphore) or q (message queues) |
ID | This is the identifier for the entry similar to a file descriptor. It is used by the operations function calls to access the resource after a get is performed on it. |
KEY | Similar to a file name, this is what the routine uses to get, or open, the resource. When you get this name, the return value is the ID. If the key is 0x00000000 (IPC_PRIVATE), this entry can only be used by related, parent/child processes. |
MODE | Permissions and status. The meaning of the field is different for each of the IPC types. |
The first two characters can be one of these: RSCD
R and S | These are for the message queues and indicate a process is waiting on a message send or receive call. |
D | Indicates the shared memory segment has been deleted but will not disappear until the last process attached to it releases it.(NOTE: The key will be changed to ipc_private, that is, all zeros (0) when the segment has been marked for removal but still has attached processes.) |
C | This shared memory segment will be cleared when the first process attaches to it.Flag not set. The next nine characters are permission bits with r indicating read access for this position; w indicating either write or alter access, depending upon the IPC facility it is attached to. No permissions for this operation. |
IPCs need permissions for owner, group and other.
OWNER | The login name of the owner |
GROUP | The name of the group that owns the entry |
CREATOR | The login name of the creator of the entry |
CGROUP | The group of the creator of the entry |
CTIME | The time when the associated entry was created or changed |
Headings that are special to message queues
CBYTES | The current total byte count of all messages in the queue |
QNUM | The number of messages currently in the queue |
QBYTES | The maximum number of bytes allowed for all messages in the queue |
LSPID | The ID of the last process that sent a message to the queue |
LRPIC | The ID of the last process that received a message in the queue |
STIME | The time the last message was sent to the queue |
RTIME | The time the last message was received, or read, from the queue |
Headings that are special to semaphores
NSEMS | The number of semaphores in the set for this entry |
OTIME | The time the last operations was done for this semaphore entry |
Headings that are special to shared memory
NATTCH | The current number of processes attached to this segment |
SEGSZ | The size of the segment in bytes |
CPID | The process ID of the creator of the segment |
LPID | The process ID of the last process to either attach or detach from this segment |
ATIME | The time of the last attach to the segment |
DTIME | The time of the last detach from the segment |