msgctl函数

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

typedef struct msg_t
{
 long type;//必须为整型(int或 long 或无符号),不能为chr
 char name[20];
 char mtext[50];
}MSG;

int main(void)
{
 struct msqid_ds tmpbuf;//struct msqid_ds是系统定义的结构体可以 man msgctl 查看
 key_t key;
 int msgqid, ret;
 MSG msg;
 pid_t pid;
 long  chr;
 char buf[100];
 strcpy(msg.name,"Lucy");
 //IPC_RMID();
 key = ftok(".", 12);
 if (key < 0)
 {
  perror("ftok");
 }
 
 msgqid = msgget(key, IPC_CREAT|0666);
 if (msgqid < 0)
 {
  perror("msgget");
  //exit(0);
 }
 
 printf("\rLucy:");
 pid=fork();
 if(pid==-1)
 perror("fork");
 
 
 if(pid==0)
 {   sleep(1);
  while(1)
  {  
   //printf("test2\n");//!!!!
     // sleep(1);
     ret = msgrcv(msgqid, &msg, sizeof(msg.mtext)+sizeof(msg.name), (long int)('L'), 0);
     //printf("%s\n",msg.name);
   if (ret < 0)
   perror("msgsnd");
      if(ret>0)
   {
   printf("\r%s:%s\n",msg.name,msg.mtext);
   printf("\rLucy:");
   fflush(stdout);
   }
  }
  
  
 }
 while(1)
 {
  
  //printf("\rLucy:");
  
  memset(buf,0,sizeof(buf));
  fgets(buf,sizeof(buf),stdin);
  sscanf(buf,"%c %s",&chr,buf);
  strcpy(msg.mtext, buf);
  msg.type=chr;
  printf("----------%s %c--------\n",msg.mtext,msg.type);
  if((chr=='B')||(chr=='L')||(chr=='P'))
  {
   printf("test\n");
   
   ret=msgsnd(msgqid,&msg,sizeof(msg.mtext)+sizeof(msg.name),IPC_NOWAIT);
   if (ret < 0)
      perror("msgsnd");
   printf("test1\n");
    //fflush(stdout);
   
  }
  if (ret < 0)
  perror("msgsnd");
  
  //printf("test2\n");
  printf("\rLucy:");
  fflush(stdout);
   
 }
    msgctl(msgqid,IPC_RMID,&tmpbuf);//!!!
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
msgctl函数是一个用于控制消息队列的系统调用函数,在Unix/Linux系统中使用。其函数原型如下: ```c #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int msgctl(int msqid, int cmd, struct msqid_ds *buf); ``` 函数参数说明如下: - `msqid`:消息队列的标识符,由msgget函数返回。 - `cmd`:命令参数,用于指定msgctl函数的操作类型,可以是IPC_STAT、IPC_SET、IPC_RMID等。 - `buf`:用于传递消息队列的状态信息,是一个指向msqid_ds结构体类型的指针。 函数返回值为操作成功时返回0,否则返回-1。 msqid_ds结构体类型定义如下: ```c struct msqid_ds { struct ipc_perm msg_perm; /* Ownership and permissions */ time_t msg_stime; /* Time of last msgsnd(2) */ time_t msg_rtime; /* Time of last msgrcv(2) */ time_t msg_ctime; /* Time of last change */ unsigned long msg_cbytes; /* Current number of bytes in queue */ msgqnum_t msg_qnum; /* Current number of messages in queue */ msglen_t msg_qbytes; /* Maximum number of bytes allowed in queue */ pid_t msg_lspid; /* PID of last msgsnd(2) */ pid_t msg_lrpid; /* PID of last msgrcv(2) */ }; ``` msgctl函数的命令参数`cmd`的取值如下: - IPC_STAT:获取消息队列的状态信息,将消息队列的状态信息保存到`buf`指向的结构体中。 - IPC_SET:设置消息队列的状态信息,将`buf`指向的结构体中的状态信息更新到消息队列中。 - IPC_RMID:删除消息队列。 使用示例: ```c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int main() { int msgid; key_t key; struct msqid_ds info; // 创建消息队列 key = ftok(".", 'a'); msgid = msgget(key, IPC_CREAT|0666); if (msgid == -1) { perror("msgget error"); exit(1); } // 获取消息队列状态信息 if (msgctl(msgid, IPC_STAT, &info) == -1) { perror("msgctl error"); exit(1); } printf("Message queue size: %ld\n", info.msg_qbytes); // 删除消息队列 if (msgctl(msgid, IPC_RMID, NULL) == -1) { perror("msgctl error"); exit(1); } return 0; } ``` 上面的示例中,首先使用`ftok`函数创建一个IPC键值,然后使用`msgget`函数创建一个消息队列并返回该队列的标识符。接着,调用`msgctl`函数获取消息队列的状态信息,并打印出消息队列的大小。最后,调用`msgctl`函数删除消息队列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值