基础:systemV 信号 create send recv rmid

本文提供了一组基于Linux的消息队列操作示例代码,包括创建消息队列、发送消息、接收消息及删除消息队列等功能。通过这些示例,读者可以了解如何使用系统调用实现进程间通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/*
@gcc version 3.2.2 20030222
@Linux version 2.4.20-8
*/



/*
@function:create
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
int c, oflag, mqid;
key_t keyMsg;

oflag = 0666|IPC_CREAT;
while( (c=getopt(argc, argv, "e")) != -1 )
{
switch(c)
{
case 'e':
oflag |= IPC_EXCL;
break;
}
}

if( optind != argc-1 )
{
cout<<"usage:msgcreate[-e] <pathname>"<<endl;
return 0;
}

keyMsg = ftok(argv[optind], 0);
if( keyMsg==-1 )
{
cout<<"err keyMsg:"<<endl;
return 0;
}

mqid = msgget(keyMsg, oflag);
if( mqid==-1 )
{
cout<<"err megget:"<<strerror(errno)<<endl;
return 0;
}

return 0;
}



/*
@function:send
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
int mqid, iResult;
size_t len;
long type;
struct msgbuf *ptr;

if(argc != 4)
{
cout<<"usage:msgsnd <pathname> <#bytes> <type>"<<endl;
return 0;
}

len = atoi(argv[2]);
type = atoi(argv[3]);

mqid = msgget(ftok(argv[1],0), 060);
if( mqid==-1 )
{
cout<<"err msgget:"<<strerror(errno)<<endl;
return 0;
}

ptr = (struct msgbuf *)calloc(sizeof(long)+len, sizeof(char));
ptr->mtype=type;

iResult = msgsnd(mqid, ptr, len, 0);
if(iResult == -1)
{
cout<<"err msgsnd:"<<strerror(errno)<<endl;
free(ptr);
return 0;
}

free(ptr);
return 0;
}


/*
@function:recv
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <iostream>

using namespace std;

#define MAXMSG (8192+sizeof(long))

int main(int argc, char **argv)
{
int c, flag, mqid;
long type;
ssize_t n;
struct msgbuf *buff;

type = flag = 0;
while( (c=getopt(argc, argv, "nt:")) != -1 )
{
switch(c)
{
case 'n':
flag |= IPC_NOWAIT;
break;
case 't':
type = atoi(optarg);
break;
}
}

if( optind != argc-1 )
{
cout<<"argc err: usage:msgrcv [-n] [-t type] <pathname>"<<endl;
}
mqid = msgget( ftok(argv[optind], 0), 040 );//rwx:only read
buff = (struct msgbuf *)malloc(MAXMSG);

n = msgrcv( mqid, buff, MAXMSG, type, flag );
if(n==-1)
{
cout<<"msgget err:"<<strerror(errno)<<endl;
free(buff);
return 0;
}

cout<<"buff.mtype:"<<buff->mtype<<endl;
free(buff);
return 0;
}


/*
@function:rmid
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
int mqid, iResult;

if(argc != 2)
{
cout<<"usage:msgrmid <pathname>"<<endl;
return 0;
}

mqid = msgget(ftok(argv[1], 0), 0);
if( mqid==-1 )
{
cout<<"err msgget:"<<strerror(errno)<<endl;
return 0;
}

iResult = msgctl(mqid, IPC_RMID, NULL);
if(iResult == -1)
{
cout<<"err msgctl:"<<strerror(errno)<<endl;
return 0;
}

return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值