Linux????????????????
?????yamin.wang ???????[ 2016/10/21 10:47:16 ] ???????????? Linux
?????????????е????
????1.?????????????????????????????????????????в?????????б???????.
????2.??????????????????????????д?????????.
????3.?????????????????????????????????????
????4.??????п??????????????????????????????????????????????????????????????.??FIFO?????????
??????????????????????????У?POSIX????????????V??????У???V?????????????????á???V????????????????????????????????????????????????????в???????
??????????????
????1. ???key?
????key_t ftok(char *pathname?? int projid)
????#include <sys/types.h>
????#include <sys/ipc.h>
????????:
????pathname???????????·???????????????????“.”
????projid?????ID?????????0??????0-255??.
????2. ???????????
????int msgget(key_t key?? int msgflag)
????#include <sys/types.h>
????#include <sys/ipc.h>
????#include <sys/msg.h>
?????????
???????????????μ????????????????????У????????????key??????
??????????
????key??????ftok????????IPC_PRIVATE??
????msgflag??
????IPC_CREAT:?????μ???????С?
????IPC_EXCL:??IPC_CREAT?????????????????????????????????????????
????IPC_NOWAIT:??д????????????????????????????
???????????
???????ó????????б????????????-1.
??????????????????£???????????μ???????У?
????1????????????key????????????У?????msgflag?а?????IPC_CREAT???λ??
????2??key?????IPC_PRIVATE??
????3. ??????????????
????int msgctl(int msqid?? int cmd?? struct msqid_ds *buf)
????????:
????????????н??и??????????????????????cmd?????
????????:
????msqid:???????ID????????б??????????msgget??????????е???????
????cmd:
????IPC_STAT:??msqid??????????и????????????????buf???????.
????IPC_SET:??msqid??????????е???????????buf??????е????.
????IPC_RMID:?????msqid??????????У???????????????????????????.
????buf:??????л?????
struct msqid_ds {
struct ipc_perm msg_perm; /* Ownership and permissions*/
time_t msg_stime; /* Time of last msgsnd() */
time_t msg_rtime; /* Time of last msgrcv() */
time_t msg_ctime; /* Time of last change */
unsigned long __msg_cbytes; /* Current number of bytes in queue (non-standard) */
msgqnum_t msg_qnum; /* Current number of messages in queue */
msglen_t msg_qbytes; /* Maximum number of bytesallowed in queue */
pid_t msg_lspid; /* PID of last msgsnd() */
pid_t msg_lrpid; /* PID of last msgrcv() */
};
struct ipc_perm {
key_t key; /* Key supplied to msgget() */
uid_t uid; /* Effective UID of owner */
gid_t gid; /* Effective GID of owner */
uid_t cuid; /* Effective UID of creator */
gid_t cgid; /* Effective GID of creator */
unsigned short mode; /* Permissions */
unsigned short seq; /* Sequence number */
};
????4??????????????????
????int msgsnd(int msqid?? struct msgbuf *msgp?? size_t msgsz?? int msgflag)
????#include <sys/types.h>
????#include <sys/ipc.h>
????#include <sys/msg.h>
????????:
???????????????????β???????????????з???????????
??????????
????msqid:????????????id
????msgp:??????????????
????msgflag:??????????????
?????????msgbuf?:
????struct msgbuf
????{
????long mtype;//???????
????char mtext[1];//????????????????????
????}
????msgsz:????????????
????msgflag:
????IPC_NOWAIT: ????????????????????????????????????msgsnd?????????
????0:msgsnd?????????????????????.???????????
????5. ????????н??????
????ssize_t msgrcv(int msqid?? struct msgbuf *msgp?? size_t msgsz?? long msgtype?? int msgflag)
????#include <sys/types.h>
????#include <sys/ipc.h>
????#include <sys/msg.h>
????????:
??????????н??????
????????:
????msqid:????????????id
????msgp:??????????????
????msgsz:???????????????mtext???С??
????msgtype:??????????????? mtype?????
????msgflag:??????????????
????msgflag:
????MSG_NOERROR:???????????nbytes??????????????nbytes???????????????????.
????IPC_NOWAIT:???y????????????.???????????????-1.
????0:msgrcv?????????????????????.
????????????????????????????е?????????????????
???????磺??????????
#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
struct msg_buf
{
int mtype;
char data[255];
};
int main( int argc?? char *argv[] )
{
key_t key;
int msgid;
int ret;
struct msg_buf msgbuf;
//get key
key = fork( "."?? "a" );
printf( "key = [%x]
"?? key );
//create quee
msgid = msgget( key?? IPC_CREATE|0666 );
if( msgid = -1 )
{
printf( "create err!
" );
return -1;
}
//??????????????????????????"test data"?????????
msgbuf.mtype = getpid();
stpcpy( msgbuf.data?? "test data" );
ret = msgsnd( msgid; &msgbuf?? sizeof( msgbuf.data)?? IPC_NOWAIT );
if( ret == -1 )
{
printf( "send err!
" );
return -1;
}
//??????????????????
memset ( &msgbuf?? 0x00?? sizeof( msgbuf) );
ret = msgrcv( msgid?? &msgbuf?? sizeof( msgbuf.data)?? getpid()?? IPC_NOWAIT );
if( ret == -1 )
{
printf( "rcv_err ret =[%d]!
"?? ret );
return -1;
}
printf( "receive msg = [%s]
!" ?? msgbuf.data );
return 0;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11