0905使用msgsnd函數向消息隊列中發送一個字符串數據信息「Hello!This is a te

/*ide

  • 編寫一個程序,使用msgsnd函數向消息隊列中發送一個字符串數據信息「Hello!This is a test!」,
  • 並經過查看消息隊列的屬性信息檢驗發送是否成功。
    */

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>函數

int main(int argc, char argv[])
{
int gflags,sflags,rflags;
key_t key;
int msgid;
int reval;
struct msgsbuf
{
int mtype;
char mtext[20];
}msg_sbuf;
struct msqid_ds msg_ginfo,msg_sinfo;
char
msgpath="/unix/msgqueue";
gflags=IPC_CREAT|IPC_EXCL;
key=ftok(msgpath,'a');
msgid=msgget(key,gflags|00666);
if(msgid==-1)
{
printf("msg create error\n");
return ;
}
sflags=IPC_NOWAIT;
msg_sbuf.mtype=10;
msg_sbuf.mtext[20]="hello! This is a test!";
reval=msgsnd(msgid,&msg_sbuf,sizeof(msg_sbuf.mtext),sflags);
if(reval==-1)
printf("message send error\n");
return 0;
}unix

相關文章
相關標籤/搜索