ipc msg msgget msgctl msgsnd msgrcv semget semctl semop shmget shmctl
programming/C_C++ 2017. 9. 21. 19:15
/**********************************************************************/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
/* 접근 가능하고 실제로 존재하는 파일의 경로와 프로젝트 식별자를 key_t 타입의 IPC 키 값으로 변환 */
key_t ftok (__const char *__pathname, int __proj_id);
int msgget ( key_t key, int msgflg );
int msgctl ( int msqid, int cmd, struct msqid_ds *buf );
int msgsnd ( int msqid, const void *msgp, size_t msgsz, int msgflg );
ssize_t msgrcv ( int msqid, void *msgp, size_t msgsz, long msgtype, int msgflg );
<msgget>
/* queue의 key가 존재하는지 체크 : 이미 생성된 message queue ID를 return 함. 없으면 오류로 -1을 return 함. */
msqid = msgget ( key, 0 );
/* queue의 key 생성 : queue가 없으며 새로 생성하고 있으면 오류로 -1을 return 함. 접근 권한은 0666 */
msqid = msgget( key, IPC_CREAT | IPC_EXCL | 0666 );
/* queue의 key 생성 : queue가 없으며 새로 생성하고 이미 생성되어 있으면 이미 생성되어 있는 message queue ID를 return 함. */
msqid = msgget( key, IPC_CREAT | 0666 );
<msgctl>
message queue 정보를 조회, 변경, 삭제 작업 수행.( IPC_STAT, IPC_SET, IPC_RMID )
/* msqid 메시지 큐 정보를 얻어 msg_stat에 저장 */
struct msqid_ds msg_stat;
msgctl( msqid, IPC_STAT, &msg_stat );
struct msqid_ds
{
struct ipc_perm msg_perm; /* structure describing operation permission */
__time_t msg_stime; /* time of last msgsnd command */
#if __WORDSIZE == 32
unsigned long int __unused1;
#endif
__time_t msg_rtime; /* time of last msgrcv command */
#if __WORDSIZE == 32
unsigned long int __unused2;
#endif
__time_t msg_ctime; /* time of last change */
#if __WORDSIZE == 32
unsigned long int __unused3;
#endif
unsigned long int __msg_cbytes; /* current number of bytes on queue */
msgqnum_t msg_qnum; /* number of messages currently on queue */
msglen_t msg_qbytes; /* max number of bytes allowed on queue */
__pid_t msg_lspid; /* pid of last msgsnd() */
__pid_t msg_lrpid; /* pid of last msgrcv() */
unsigned long int __unused4;
unsigned long int __unused5;
};
/****
struct ipc_perm
{
__key_t __key; /* Key. */
__uid_t uid; /* Owner's user ID. */
__gid_t gid; /* Owner's group ID. */
__uid_t cuid; /* Creator's user ID. */
__gid_t cgid; /* Creator's group ID. */
unsigned short int mode; /* Read/write permission. */
unsigned short int __pad1;
unsigned short int __seq; /* Sequence number. */
unsigned short int __pad2;
unsigned long int __unused1;
unsigned long int __unused2;
};
/* 큐의 msqid_ds 구조체의 값을 설정 변경 */
msgctl( msqid, IPC_SET, &msg_stat );
/* message queue 삭제 */
msgctl ( msqid, IPC_RMID, (struct msqid_ds *)NULL );
<msgsnd>
struct my_queue_msg_form {
long msgtype;
char mtext[1024];
};
struct my_queue_msg_form qbuf;
qbuf.msgtype = 100;
sprintf(qbuf.mtext, "1234");
msgsnd ( msqid, &qbuf, sizeof(qbuf.mtext), 0 );
<msgrcv>
msgtype = 100;
msgrcv ( msqid, &qbuf, sizeof(qbuf.mtext), msgtype, 0 );
/**********************************************************************/
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/sem.h>
int semget ( key_t key, int semnum, int semflg );
int semctl ( int semid, int semnum, int cmd, union semun arg );
int semop ( int semid, struct sembuf *sops, unsigned nsops );
//
// /* semop 시스템 호출은 이러한 배열을 갖는다 */
// struct sembuf {
// ushort sem_num; /* 배열안에서의 세마포어 인덱스 */
// short sem_op; /* 수행할 동작 (양수,음수, 또는 0) */
// short sem_flg; /* 동작 플래그 */
// };
//
/* 세마포어 조회 */
semid = semget(key, 0, 0666);
/* 생성 */
semid = semget(key, semnum, IPC_CREAT | IPC_EXCL | 0666);
/* lock */
struct sembuf sem_lock = { 0, -1, IPC_NOWAIT };
semop(sid, &sem_lock, 1);
/* unlock */
struct sembuf sem_unlock = { 0, 1, IPC_NOWAIT };
semop(sid, &sem_unlock, 1);
/* 조회 */
union semun semopts;
semctl(semid, 0, IPC_STAT, semopts);
/* 변경 */
semctl(semid, 0, IPC_SET, semopts);
/* 삭제 */
semctl(semid, 0, IPC_RMID, 0);
/**********************************************************************/
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget ( key_t key, int size, int shmflg );
int shmctl ( int shmid, int cmd, struct shmid_ds *buf );
void *shmat ( int shmid, const void *shmaddr, int shmflg );
int shmdt ( const void *shmaddr );
/* 생성 */
#define SHMSIZE 100
shmid = shmget(key, SHMSIZE, 0);
shmid = shmget(key, SHMSIZE, IPC_CREAT | IPC_EXCL | 0666);
/* Data structure describing a shared memory segment. */
struct shmid_ds
{
struct ipc_perm shm_perm; /* operation permission struct */
size_t shm_segsz; /* size of segment in bytes */
__time_t shm_atime; /* time of last shmat() */
#if __WORDSIZE == 32
unsigned long int __unused1;
#endif
__time_t shm_dtime; /* time of last shmdt() */
#if __WORDSIZE == 32
unsigned long int __unused2;
#endif
__time_t shm_ctime; /* time of last change by shmctl() */
#if __WORDSIZE == 32
unsigned long int __unused3;
#endif
__pid_t shm_cpid; /* pid of creator */
__pid_t shm_lpid; /* pid of last shmop */
shmatt_t shm_nattch; /* number of current attaches */
unsigned long int __unused4;
unsigned long int __unused5;
};
/* 조회 */
struct shmid_ds myshmds;
shmctl(shmid, IPC_STAT, &myshmds);
/* 변경 */
shmctl(shmid, IPC_SET, &myshmds);
/* 삭제 */
shmctl(shmid, IPC_RMID, 0);