-

26_5번에서 메세지를 쓰고 대기 하고 있고

26_6번에서 메세지를 읽으면 그값을 출력하고 프로세스를 둘다 종료한다.
--------------------------------------26_5----------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#define SIZE 1024

void signalHandler(int signo);
int shmid;

main()
{
    void *shmaddr;

    /* 1234 키의 공유메모리 생성 */
    if((shmid=shmget((key_t)1234, SIZE, IPC_CREAT|0666)) == -1) {
       perror("shmid failed");
       exit(1);
    }

    /* shmid 공유메모리를 호출 프로세스 메모리 영역으로 첨부 */
    if((shmaddr=shmat(shmid, (void *)0, 0)) == (void *)-1) {
       perror("shmat failed");
       exit(1);
    }

    /* 공유메모리에 데이터 쓰기 */
    strcpy((char *)shmaddr, "Linux Programming");

    /* 공유메모리를 호출 프로세스의 메모리 영역에서 분리 */
    if(shmdt(shmaddr) == -1) {
       perror("shmdt failed");
       exit(1);
    }

    /* SIGINT 시그널 받으면 signalHandler 실행하도록 설정 */
    signal(SIGINT, signalHandler);
    pause();
}

void signalHandler(int signo)
{
    /* shmid 공유메모리 삭제 */
    if(shmctl(shmid, IPC_RMID, 0) == -1) {
       perror("shmctl failed");
       exit(1);
    }
    exit(0);
}
----------------------------------------26_6----------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#define SIZE 1024

main()
{
    int shmid;
    void *shmaddr;
    struct shmid_ds shm_stat;

    /* 1234 키의 공유메모리 있으면 접근해서 식별자 얻음 */
    if((shmid=shmget((key_t)1234, SIZE, IPC_CREAT|0666)) == -1) {
       perror("shmid failed");
       exit(1);
    }

    /* shmid 공유메모리를 호출 프로세스 메모리 영역으로 첨부 */
    if((shmaddr=shmat(shmid, (void *)0, 0)) == (void *)-1) {
       perror("shmat failed");
       exit(1);
    }

    /* 공유메모리에 저장된 데이터 출력 */
    printf("data read from shared memory : %s\n", (char *)shmaddr);

    /* shmid 공유메모리 정보를 얻어 shm_stat에 저장 */
    if(shmctl(shmid, IPC_STAT, &shm_stat) == -1) {
       perror("shmctl failed");
       exit(1);
    }

    /* 공유메모리를 호출 프로세스의 메모리 영역에서 분리 */
    if(shmdt(shmaddr) == -1) {
       perror("shmdt failed");
       exit(1);
    }

    /* shm_stat.shm_cpid 프로세스에게 SIGINT 시그널 보냄 */
    kill(shm_stat.shm_cpid, SIGINT);

    exit(0);
}

      c 언어  |  2007. 9. 18. 11:49




세마포어 메세지큐 공유메모리 IPC객체

----------------------------------------26-4.c읽기 파일-----------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>

#define SIZE 124
#define PRIOR 10   /* 우선순위 최대값 */

struct {
    long type;
    char data[SIZE];
} msg_data;

main()
{
    int msqid, data_len;

    /* 1234 키의 메시지 큐가 있으면 접근해서 식별자 받음 */
    if((msqid = msgget((key_t)1234, IPC_CREAT|0666)) == -1) {
       perror("msgget failed");
       exit(1);
    }

    while(1) {
       /* (-1*PRIOR)에 의해 메시지 큐에서 메시지 형식 값이 작은 메시지부터 읽음.
       IPC_NOWAIT에 의해 메시지 없으면 실패로 -1 반환하는데 이때의 errno는 ENOMSG */
       if((data_len=msgrcv(msqid, &msg_data, SIZE, (-1*PRIOR), IPC_NOWAIT)) == -1) {
          if(errno == ENOMSG) {
             printf("no message\n");
             break;
          }
          perror("msgrcv failed");
          break;
       }
       msg_data.data[data_len]='\0';
       printf("data : %s [%ld]\n", msg_data.data, msg_data.type);
    }

    /* msqid 메시지 큐 삭제 */
    if(msgctl(msqid, IPC_RMID, 0) == -1) {
       perror("msgctl failed");
       exit(1);
    }

    exit(0);
}
----------------------------------------26-3.c쓰기 파일-----------------------------------------
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define SIZE 124
/* 메시지 형식을 저장하는 type 멤버를 우선순위를 저장하는 용도로 사용한다. */
struct {
    long type;
    char data[SIZE];
} msg_data;

main()
{
    int msqid, data_len;
    char buffer[SIZE];

    /* 1234 키의 메시지 큐 생성 */
    if((msqid = msgget((key_t)1234, IPC_CREAT|0666)) == -1) {
       perror("msgget failed");
       exit(1);
    }

    while(1) {
       /* 표준입력장치로부터 데이터 입력 */
       printf("input data => ");
       scanf("%s", buffer);
       /* 입력받은 내용이 ‘quit’이면 while 문 벗어남 */
       if(!strcmp(buffer, "quit"))
          break;
       /* 표준입력장치로부터 우선순위 입력 */
       printf("input priority => ");
       scanf("%ld", &(msg_data.type));
       strcpy(msg_data.data, buffer);
       /* msg_data 메시지를 msqid 메시지 큐에 전송 */
       if(msgsnd(msqid, &msg_data, strlen(msg_data.data), 0) == -1) {
          perror("msgsnd failed");
          exit(1);
       }
    }
    exit(0);
}

      c 언어  |  2007. 9. 18. 11:21




#include <stdio.h>
#include <unistd.h>
#define SIZE 4

main()
{
    char *arg[3] = {"abc", "def", "ghi"};
    char buffer[SIZE];
    int pipes[2], i;

    /* 파이프 생성 */
    if(pipe(pipes) == -1) {
       perror("pipe failed");
       exit(1);
    }

    /* 자식 프로세스를 생성하고 부모 프로세스는 */
    if(fork()) {
       for(i=0; i<3; i++) {
          /* 파이프에 arg[i]를 쓰기 */
          write(pipes[1], arg[i], SIZE);
          printf("parent process write to pipe: %s\n", arg[i]);
       }
    /* 자식 프로세스는 */
    } else {
       for(i=0; i<3; i++) {
          /* 파이프로부터 읽기 */
          read(pipes[0], buffer, SIZE);
          printf("child process read from pipe: %s\n", buffer);
       }
    }
    exit(0);
}

//자식프로세르를 만들어서 부모프로세스와의 정보를 교환
25-11도 그런예제
코드영역은 공유를 하고, 전역 변수같은 부분은 공유할 부분은 공유하고 복사할부분은 복사
변수를 복사를 한다.

      c 언어  |  2007. 9. 17. 12:09



sokoban's Blog is powered by Daum & Tattertools