IP phone日志2
2004-11-04 15:35
423 查看
昨天把socket学习了一下:
今天开始学习queue.
以下是queue.c代码
**********************************************************
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#define MSG_MAX 4056 // <= 4056 max size of message (bytes)
/* message buffer for msgsnd and msgrcv calls */ //定义的信息
struct sMsgBuf {
long mtype; // type of message
long ModuleID;//module ID
char mtext[MSG_MAX]; //message text */
};
//创建queue
int open_queue( key_t keyval )
{
int qid;
if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
{
return(-1);
}
return(qid);
}
//send queue message
//qid = queue id
//qbuf = send message buffer
int send_message( int qid, struct sMsgBuf *qbuf )
{
int result, length;
/* The length is essentially the size of the structure minus sizeof(mtype) */
length = sizeof(struct sMsgBuf) - sizeof(long);
if((result = msgsnd( qid, qbuf, length, 0)) == -1)
{
return(-1);
}
return(result);
}
//read queue message
//qid = queue id
//type = message type
//qbuf = read message buffer
//return int = err code
int read_message( int qid, long type, struct sMsgBuf *qbuf )
{
int result, length;
/* The length is essentially the size of the structure minus sizeof(mtype) */
length = sizeof(struct sMsgBuf) - sizeof(long);
if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
{
return(-1);
}
return(result);
}
//查看队列是否有消息
int peek_message( int qid, long type )
{
int result, length;
//忽略了缓冲区的地址和长度。这样,系统调用将会失败。尽管如此,
//可以检查返回的E 2 B I G值,它说明符合条件的消息确实存在。
if((result = msgrcv( qid, NULL, 0, type, IPC_NOWAIT)) == -1)
{
if(errno == E2BIG)
return 1;
}
return 0;
}
//sample queue
int CreateQue()
{
int qid;
key_t msgkey;
struct sMsgBuf msgbuf;
/* Generate our IPC key value */
msgkey = ftok(".", 'm');
/* Open/create the queue */
if(( qid = open_queue( msgkey)) == -1) {
perror("open_queue");
exit(1);
}
msgbuf.mtype = 1;
msgbuf.ModuleID=100;
strcpy(msgbuf.mtext,"love");
if((send_message( qid, &msgbuf )) == -1) {
perror("send_message");
exit(1);
}
}
int get_queue_ds( int qid, struct msqid_ds *qbuf )
{
if( msgctl( qid, IPC_STAT, qbuf) == -1)
{
return(-1);
}
return(0);
}
int change_queue_mode( int qid, char *mode )
{
struct msqid_ds tmpbuf;
/* Retrieve a current copy of the internal data structure */
get_queue_ds( qid, &tmpbuf);
/* Change the permissions using an old trick */
sscanf(mode, "%ho", &tmpbuf.msg_perm.mode);
/* Update the internal data structure */
if( msgctl( qid, IPC_SET, &tmpbuf) == -1)
{
return(-1);
}
return(0);
}
int Remove_queue(int qid )
{
if( msgctl( qid, IPC_RMID, 0) == -1)
{
return(-1);
}
return(0);
//(void)msgctl(qid,IPC_RMID,0);
}
**********************************************************
今天开始学习queue.
以下是queue.c代码
**********************************************************
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#define MSG_MAX 4056 // <= 4056 max size of message (bytes)
/* message buffer for msgsnd and msgrcv calls */ //定义的信息
struct sMsgBuf {
long mtype; // type of message
long ModuleID;//module ID
char mtext[MSG_MAX]; //message text */
};
//创建queue
int open_queue( key_t keyval )
{
int qid;
if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
{
return(-1);
}
return(qid);
}
//send queue message
//qid = queue id
//qbuf = send message buffer
int send_message( int qid, struct sMsgBuf *qbuf )
{
int result, length;
/* The length is essentially the size of the structure minus sizeof(mtype) */
length = sizeof(struct sMsgBuf) - sizeof(long);
if((result = msgsnd( qid, qbuf, length, 0)) == -1)
{
return(-1);
}
return(result);
}
//read queue message
//qid = queue id
//type = message type
//qbuf = read message buffer
//return int = err code
int read_message( int qid, long type, struct sMsgBuf *qbuf )
{
int result, length;
/* The length is essentially the size of the structure minus sizeof(mtype) */
length = sizeof(struct sMsgBuf) - sizeof(long);
if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
{
return(-1);
}
return(result);
}
//查看队列是否有消息
int peek_message( int qid, long type )
{
int result, length;
//忽略了缓冲区的地址和长度。这样,系统调用将会失败。尽管如此,
//可以检查返回的E 2 B I G值,它说明符合条件的消息确实存在。
if((result = msgrcv( qid, NULL, 0, type, IPC_NOWAIT)) == -1)
{
if(errno == E2BIG)
return 1;
}
return 0;
}
//sample queue
int CreateQue()
{
int qid;
key_t msgkey;
struct sMsgBuf msgbuf;
/* Generate our IPC key value */
msgkey = ftok(".", 'm');
/* Open/create the queue */
if(( qid = open_queue( msgkey)) == -1) {
perror("open_queue");
exit(1);
}
msgbuf.mtype = 1;
msgbuf.ModuleID=100;
strcpy(msgbuf.mtext,"love");
if((send_message( qid, &msgbuf )) == -1) {
perror("send_message");
exit(1);
}
}
int get_queue_ds( int qid, struct msqid_ds *qbuf )
{
if( msgctl( qid, IPC_STAT, qbuf) == -1)
{
return(-1);
}
return(0);
}
int change_queue_mode( int qid, char *mode )
{
struct msqid_ds tmpbuf;
/* Retrieve a current copy of the internal data structure */
get_queue_ds( qid, &tmpbuf);
/* Change the permissions using an old trick */
sscanf(mode, "%ho", &tmpbuf.msg_perm.mode);
/* Update the internal data structure */
if( msgctl( qid, IPC_SET, &tmpbuf) == -1)
{
return(-1);
}
return(0);
}
int Remove_queue(int qid )
{
if( msgctl( qid, IPC_RMID, 0) == -1)
{
return(-1);
}
return(0);
//(void)msgctl(qid,IPC_RMID,0);
}
**********************************************************
相关文章推荐
- ATL实现Connection Point的一种简单的方法
- oracle中的分页
- IP地址的隐藏(delphi实现 )
- 力争最简单把*、&说清楚
- 一个画图类,类似ms_chart的线图,适用于科学仿真数据分析(一)
- 怎样为任何控件和区域添加提示信息:用自己封装的CTip类
- 从注册表中还原MSNMessenger口令
- Ant应用(1)
- 从数据库中动态选取下拉列表的方法
- Tomcat中通过JNDI配置访问数据库
- servlet的2种重定向方法
- Mysql中左连接的使用
- [转贴]Phrack 最新公开的Win32用户态rootkit技术
- 末公开的CreateProcess()函数技术细节(不错的e文,不过还没有空翻译)
- [转贴]绕过xp sp2防火墙的代码
- creating a Self Deleting Executable
- Java socket模拟HTTP协议客户端之POST请求
- Java socket模拟HTTP协议客户端之GET请求
- 有关C语言程序内存问题的5条规则