`

基础:systemV 信号 create send recv rmid

阅读更多
/*
@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;	
}

2
0
分享到:
评论
2 楼 lin_style 2008-10-25  
simohayha 写道

还是posix的好用。

如果有全双工管道的平台直接全双工管道了。。


systemV 只能算是T&T的一个产物了。
1 楼 simohayha 2008-10-23  
还是posix的好用。

如果有全双工管道的平台直接全双工管道了。。

相关推荐

Global site tag (gtag.js) - Google Analytics