IPC 통신의 그다음 핵심인 Shared Memory 이용이다.

 

출처 : http://www.cs.cf.ac.uk/Dave/C/node27.html ,  http://failinux.com

 

int shmget(key_t key, int size, int shmflg);

 

key_t key 공유 메모리를 구별하는 식별 번호
int size 공유 메모리 크기
int shmflg 동작 옵션

 

 

void *shmat(int shmid, const void *shmaddr, int shmflg);

 

int shmid 공유 메모리를 구별하는 식별 번호
void *shmaddr 첨부되는 어드레스 주소. 일반적으로 NULL을 지정
int shmflg

동작 옵션

key값만 서로 맞추면 제대로 동작한다.

예제코드는 shm_server와 shm_client이고

서버쪽은 메모리에 쓰는역할

클라이언트쪽은 메모리에서 읽는 역할을 한다.

=============================================================

==== shm_server.c ====
#include "sys/types.h"
#include "sys/ipc.h"
#include "sys/shm.h"
#include 

#define SHMSZ     27

int main()
{
    char c;
    int shmid;
    key_t key;
    char *shm, *s;

    /* We'll name our shared memory segment
     * "5678".  */
    key = 9678;

    /* Create the segment.  */
    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
        perror("shmget");
        return -1;
    }

    /* Now we attach the segment to our data space.  */
    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        return -1;
    }

    /* Now put some things into the memory for the
     * other process to read.  */
    s = shm;

    for (c = 'a'; c <= 'z'; c++)
        *s++ = c;
    *s = NULL;

    /*
     * Finally, we wait until the other process 
     * changes the first character of our memory
     * to '*', indicating that it has read what 
     * we put there.
     */
    while (*shm != '*')
        sleep(1);

    return 0;
}
==== shm_client.c ====
#include "sys/types.h"
#include "sys/ipc.h"
#include "sys/shm.h"
#include 

#define SHMSZ     27

int main()
{
    int shmid;
    key_t key;
    char *shm, *s;

    /* We need to get the segment named
     * "5678", created by the server.  */
    key = 5678;

    /* Locate the segment.  */
    if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
        perror("shmget");
        return -1;
    }

    /* Now we attach the segment to our data space.  */
    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        return -1;
    }

    /* Now read what the server put in the memory.  */
    for (s = shm; *s != NULL; s++)
        putchar(*s);
    putchar('\n');

    /*
     * Finally, change the first character of the 
     * segment to '*', indicating we have read 
     * the segment.
     */
    *shm = '*';

    return 0;
}
== 서버측은 결과가 나오지않는다. ==
== 클라이언트측 결과는 다음과 같다. ==
$ ./clie 
abcdefghijklmnopqrstuvwxyz

 

=============================================================

 

위의 프로그램은 a-z까지 출력하는건데

키값은 5678이다. 이걸 16진수로 변환하면 162e가 되는데 이건

%ipcs 명령으로 확인하면 생성된것을 볼 수 있다.

---- 중략 ----

0x0000162e 4292650    test      666        27         2

---- 후략 ----

 

위에서 define으로 결정한 크기를 넘어서게 되면 앞쪽은 잘리고

뒤에만 출력되게 된다.

반응형

'Language > C' 카테고리의 다른 글

semget. semop, semctl 함수 예제  (12) 2012.07.17
mmap / munmap 함수 예제  (10) 2012.07.17
C FAQ (포인터 증가 3)  (6) 2012.07.13
nmemb 의미  (8) 2012.07.12
msgsnd/ msgrcv 함수 예제  (12) 2012.07.12

+ Recent posts