精华内容
下载资源
问答
  • 更多相关内容
  • 消息队列函数实例代码及相关教程 包括msgget、msgctl、msgsnd、msgrcv
  • msgsnd/msgrcv 注意typeflag

    2021-02-25 18:28:08
    if (msgsnd(qid, (void *) &msg, sizeof(msg.mtext),IPC_NOWAIT) == -1) {//注意数据长度 perror("msgsnd error"); exit(EXIT_FAILURE); } printf("sent: %s\n", msg.mtext); } static void get_msg(int qid, int ...

    struct msgbuf {
               long mtype;//指定type
               char mtext[80];//要传的内容
    };

    使用时要注意发送和接收时的mtype(typeflag值不为0的情况下)保持一致。(具体如下面例子)

     

     

     

    bug

    In Linux 3.13 and earlier, if msgrcv() was called with the MSG_COPY flag, but without IPC_NOWAIT, and the message queue contained less than msgtyp messages, then the call would block until the next message is written to the queue. At that point, the call would return a copy of the message, regardless of whether that message was at the ordinal position msg‐typ. This bug is fixed in Linux 3.14.

    Specifying both MSG_COPY and MSC_EXCEPT in msgflg is a logical error (since these flags impose different interpretations on msgtyp). In Linux 3.13 and earlier, this error was not diagnosed by msgrcv(). This bug is fixed in Linux 3.14.

    官方例子

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <unistd.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    
           struct msgbuf {
               long mtype;
               char mtext[80];
           };
    
           static void
           usage(char *prog_name, char *msg)
           {
               if (msg != NULL)
                   fputs(msg, stderr);
    
               fprintf(stderr, "Usage: %s [options]\n", prog_name);
               fprintf(stderr, "Options are:\n");
               fprintf(stderr, "-s        send message using msgsnd()\n");
               fprintf(stderr, "-r        read message using msgrcv()\n");
               fprintf(stderr, "-t        message type (default is 1)\n");
               fprintf(stderr, "-k        message queue key (default is 1234)\n");
               exit(EXIT_FAILURE);
           }
    
    static void send_msg(int qid, int msgtype)
     {
               struct msgbuf msg;
               time_t t;
    
               msg.mtype = msgtype;//注意类型,要和接收的类型保持一致
    
               time(&t);
               snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
                       ctime(&t));
    
          if (msgsnd(qid, (void *) &msg, sizeof(msg.mtext),IPC_NOWAIT) == -1) {//注意数据长度
                   perror("msgsnd error");
                   exit(EXIT_FAILURE);
               }
               printf("sent: %s\n", msg.mtext);
     }
    
    static void get_msg(int qid, int msgtype)
           {
               struct msgbuf msg;
    
               if (msgrcv(qid, (void *) &msg, sizeof(msg.mtext), msgtype,//注意这个长度,类型要和发送保持一致
                          MSG_NOERROR | IPC_NOWAIT) == -1) {
                   if (errno != ENOMSG) {
                       perror("msgrcv");
                       exit(EXIT_FAILURE);
                   }
                   printf("No message available for msgrcv()\n");
               } else
                   printf("message received: %s\n", msg.mtext);
           }
    
    int main(int argc, char *argv[])
           {
               int qid, opt;
               int mode = 0;               /* 1 = send, 2 = receive */
               int msgtype = 1;
               int msgkey = 1234;
    
               while ((opt = getopt(argc, argv, "srt:k:")) != -1) {
                   switch (opt) {
                   case 's':
                       mode = 1;
                       break;
                   case 'r':
                       mode = 2;
                       break;
                   case 't':
                       msgtype = atoi(optarg);
                       if (msgtype <= 0)
                           usage(argv[0], "-t option must be greater than 0\n");
                       break;
                   case 'k':
                       msgkey = atoi(optarg);
                       break;
                   default:
                       usage(argv[0], "Unrecognized option\n");
                   }
               }
    
               if (mode == 0)
                   usage(argv[0], "must use either -s or -r option\n");
    
               qid = msgget(msgkey, IPC_CREAT | 0666);//创建并取得句柄,不同进程中使用的话,msgkey(值如1122唯一)要一致,才能通信
    
               if (qid == -1) {
                   perror("msgget");
                   exit(EXIT_FAILURE);
               }
               if (mode == 2)
                   get_msg(qid, msgtype);
               else
                   send_msg(qid, msgtype);
    
               exit(EXIT_SUCCESS);
          }
    

     man msgsnd 手册介绍

    #include <sys/types.h>
           #include <sys/ipc.h>
           #include <sys/msg.h>
    
           int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
    
           ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
                          int msgflg);
    
    DESCRIPTION
           The  msgsnd()  and  msgrcv()  system  calls are used, respectively, to send messages to, and
           receive messages from, a System V message queue.  The calling process must have  write  per‐
           mission  on  the  message queue in order to send a message, and read permission to receive a
           message.
    
           The msgp argument is a pointer to a caller-defined structure of the following general form:
    
               struct msgbuf {
                   long mtype;       /* message type, must be > 0 */
                   char mtext[1];    /* message data */
               };
    
           The mtext field is an array (or other structure) whose size is specified by msgsz, a nonneg‐
           ative  integer  value.   Messages  of zero length (i.e., no mtext field) are permitted.  The
           mtype field must have a strictly positive integer value.  This value  can  be  used  by  the
           receiving process for message selection (see the description of msgrcv() below).
    
    msgsnd()
           The  msgsnd()  system  call  appends a copy of the message pointed to by msgp to the message
           queue whose identifier is specified by msqid.
    
           If sufficient space is available in the queue, msgsnd()  succeeds  immediately.   The  queue
           capacity  is  governed by the msg_qbytes field in the associated data structure for the mes‐
           sage queue.  During queue creation this field is initialized to MSGMNB bytes, but this limit
           can  be modified using msgctl(2).  A message queue is considered to be full if either of the
           following conditions is true:
    
           * Adding a new message to the queue would cause the total number of bytes in  the  queue  to
             exceed the queue's maximum size (the msg_qbytes field).
    
           * Adding  another message to the queue would cause the total number of messages in the queue
             to exceed the queue's maximum size (the msg_qbytes field).  This  check  is  necessary  to
             prevent  an  unlimited number of zero-length messages being placed on the queue.  Although
             such messages contain no data, they nevertheless consume (locked) kernel memory.
    
           If insufficient space is available in the queue, then the default behavior of msgsnd() is to
           block  until  space  becomes available.  If IPC_NOWAIT is specified in msgflg, then the call
           instead fails with the error EAGAIN.
    
           A blocked msgsnd() call may also fail if:
    
           * the queue is removed, in which case the system call fails with errno set to EIDRM; or
    
           * a signal is caught, in which case the system call fails with errno set to  EINTR;see  sig‐
             nal(7).   (msgsnd()  is  never automatically restarted after being interrupted by a signal
             handler, regardless of the setting of the SA_RESTART flag when establishing a signal  han‐
             dler.)
    
           Upon successful completion the message queue data structure is updated as follows:
    
                  msg_lspid is set to the process ID of the calling process.
    
                  msg_qnum is incremented by 1.
    
                  msg_stime is set to the current time.
    msgrcv()
           The  msgrcv()  system call removes a message from the queue specified by msqid and places it
           in the buffer pointed to by msgp.
    
           The argument msgsz specifies the maximum size in bytes for the member mtext of the structure
           pointed  to  by  the msgp argument.  If the message text has length greater than msgsz, then
           the behavior depends on whether MSG_NOERROR is specified in msgflg.  If MSG_NOERROR is spec‐
           ified,  then  the  message  text will be truncated (and the truncated part will be lost); if
           MSG_NOERROR is not specified, then the message isn't removed from the queue and  the  system
           call fails returning -1 with errno set to E2BIG.
    
           Unless  MSG_COPY  is specified in msgflg (see below), the msgtyp argument specifies the type
           of message requested, as follows:
    
           * If msgtyp is 0, then the first message in the queue is read.
    
           * If msgtyp is greater than 0, then the first message in the queue of type msgtyp  is  read,
             unless MSG_EXCEPT was specified in msgflg, in which case the first message in the queue of
             type not equal to msgtyp will be read.
    
           * If msgtyp is less than 0, then the first message in the queue with the  lowest  type  less
             than or equal to the absolute value of msgtyp will be read.
    
           The  msgflg argument is a bit mask constructed by ORing together zero or more of the follow‐
           ing flags:
    
           IPC_NOWAIT
                  Return immediately if no message of the requested type is in the queue.   The  system
                  call fails with errno set to ENOMSG.
    
           MSG_COPY (since Linux 3.8)
                  Nondestructively  fetch  a  copy  of the message at the ordinal position in the queue
                  specified by msgtyp (messages are considered to be numbered starting at 0).
    
                  This flag must be specified in conjunction with IPC_NOWAIT, with the result that,  if
                  there  is no message available at the given position, the call fails immediately with
                  the error ENOMSG.  Because they alter the  meaning  of  msgtyp  in  orthogonal  ways,
                  MSG_COPY and MSG_EXCEPT may not both be specified in msgflg.
    
                  The  MSG_COPY  flag was added for the implementation of the kernel checkpoint-restore
                  facility and is available only  if  the  kernel  was  built  with  the  CONFIG_CHECK‐
                  POINT_RESTORE option.
    MSG_EXCEPT
                  Used  with  msgtyp greater than 0 to read the first message in the queue with message
                  type that differs from msgtyp.
    
           MSG_NOERROR
                  To truncate the message text if longer than msgsz bytes.
    
           If no message of the requested type is available and IPC_NOWAIT isn't specified  in  msgflg,
           the calling process is blocked until one of the following conditions occurs:
    
           * A message of the desired type is placed in the queue.
    
           * The  message  queue  is removed from the system.  In this case, the system call fails with
             errno set to EIDRM.
    
           * The calling process catches a signal.  In this case, the system call fails with errno  set
             to  EINTR.  (msgrcv() is never automatically restarted after being interrupted by a signal
             handler, regardless of the setting of the SA_RESTART flag when establishing a signal  han‐
             dler.)
    
           Upon successful completion the message queue data structure is updated as follows:
    
                  msg_lrpid is set to the process ID of the calling process.
    
                  msg_qnum is decremented by 1.
    
                  msg_rtime is set to the current time.
    
    RETURN VALUE
           On  failure  both  functions  return  -1 with errno indicating the error, otherwise msgsnd()
           returns 0 and msgrcv() returns the number of bytes actually copied into the mtext array.
    ERRORS
           When msgsnd() fails, errno will be set to one among the following values:
    
           EACCES The calling process does not have write permission on the message queue, and does not
                  have the CAP_IPC_OWNER capability.
    
           EAGAIN The  message  can't  be sent due to the msg_qbytes limit for the queue and IPC_NOWAIT
                  was specified in msgflg.
    
           EFAULT The address pointed to by msgp isn't accessible.
    
           EIDRM  The message queue was removed.
    
           EINTR  Sleeping on a full message queue condition, the process caught a signal.
    
           EINVAL Invalid msqid value, or nonpositive mtype value, or invalid msgsz value (less than  0
                  or greater than the system value MSGMAX).
    
           ENOMEM The  system  does  not have enough memory to make a copy of the message pointed to by
                  msgp.
    
           When msgrcv() fails, errno will be set to one among the following values:
    
           E2BIG  The message text length is greater than msgsz  and  MSG_NOERROR  isn't  specified  in
                  msgflg.
    
           EACCES The  calling process does not have read permission on the message queue, and does not
                  have the CAP_IPC_OWNER capability.
    
           EFAULT The address pointed to by msgp isn't accessible.
    
           EIDRM  While the process was sleeping to receive a message, the message queue was removed.
    
           EINTR  While the process was sleeping to receive a message, the process caught a signal; see
                  signal(7).
    
           EINVAL msgqid was invalid, or msgsz was less than 0.
    
           EINVAL (since Linux 3.14)
                  msgflg specified MSG_COPY, but not IPC_NOWAIT.
    
           EINVAL (since Linux 3.14)
                  msgflg specified both MSG_COPY and MSG_EXCEPT.
    
           ENOMSG IPC_NOWAIT  was  specified  in msgflg and no message of the requested type existed on
                  the message queue.
    
           ENOMSG IPC_NOWAIT and MSG_COPY were specified in msgflg and the  queue  contains  less  than
                  msgtyp messages.
    ENOSYS (since Linux 3.8)
                  MSG_COPY  was  specified  in  msgflg,  and  this  kernel  was configured without CON‐
                  FIG_CHECKPOINT_RESTORE.
     ENOSYS (since Linux 3.8)
                  MSG_COPY  was  specified  in  msgflg,  and  this  kernel  was configured without CON‐
                  FIG_CHECKPOINT_RESTORE.
    

     

     

     

     

     

     

    展开全文
  • 如果msgflg和常数IPC_NOWAIT合用,则在msgsnd()执行时若是消息队列已满,则msgsnd()将不会阻塞,而会立即返回-1,如果执行的是msgrcv(),则在消息队列呈空时,不做等待马上返回-1,并设定错误码为ENOMSG。...

    系统建立IPC通讯(如消息队列、共享内存时)必须指定一个ID值。通常情况下,该id值通过ftok函数得到。

    ftok原型如下:

    key_t ftok( char * fname, int id )

    fname就时你指定的文件名,id是子序号。

    在一般的UNIX实现中,是将文件的索引节点号取出,前面加上子序号得到key_t的返回值。

    如指定文件的索引节点号为65538,换算成16进制为0x010002,而你指定的ID值为38,换算成16进制为0x26,则最后的key_t返回值为0x26010002。

    查询文件索引节点号的方法是: ls -i

    当删除重建文件后,索引节点号由操作系统根据当时文件系统的使用情况分配,因此与原来不同,所以得到的索引节点号也不同。

    如果要确保key_t值不变,要目确保ftok的文件不被删除,要么不用ftok,指定一个固定的key_t值,比如:

    #define IPCKEY 0x111

    char path[256];

    sprintf( path, "%s/etc/config.ini", (char*)getenv("HOME") );

    msgid=ftok( path, IPCKEY );[/code]

    同一段程序,用于保证两个不同用户下的两组相同程序获得互不干扰的IPC键值。

    由于etc/config.ini(假定)为应用系统的关键配置文件,因此不存在被轻易删除的问题——即使被删,也会很快被发现并重建(此时应用系统也将被重起)。

    ftok()的设计目的也在于此。

    #include

    #include

    #include

    int msgget(key_t key, int msgflg); //创建消息队列

    参数:

    key:消息队列关联的键。

    msgflg:消息队列的建立标志和存取权限。

    返回说明:

    成功执行时,返回消息队列标识值。失败返回-1,

    #include #include #include

    int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

    //将消息送入消息队列

    参数:msqid:消息队列的识别码。msgp:指向消息缓冲区的指针,此位置用来暂时存储发送和接收的消息,是一个用户可定义的通用结构,形态如下

    struct msgbuf {

    long mtype;     /*消息类型,必须> 0 */

    char mtext[1];  /*消息文本*/

    };

    msgsz:消息的大小。msgtyp:从消息队列内读取的消息形态。如果值为零,则表示消息队列中的所有消息都会被读取。msgflg:用来指明核心程序在队列没有数据的情况下所应采取的行动。如果msgflg和常数IPC_NOWAIT合用,则在msgsnd()执行时若是消息队列已满,则msgsnd()将不会阻塞,而会立即返回-1,如果执行的是msgrcv(),则在消息队列呈空时,不做等待马上返回-1,并设定错误码为ENOMSG。当msgflg为0时,msgsnd()及msgrcv()在队列呈满或呈空的情形时,采取阻塞等待的处理模式。返回说明:成功执行时,msgsnd()返回0, 失败返回-1

    ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

    //从消息队列读取信息.

    成功执行时,返回0, 失败返回-1

    msgctl(qid, IPC_RMID, NULL)  //删除消息队列

    以下为例子:

    #include #include #include #include #include #include #include

    struct msgmbuf

    {

    long msg_type;

    char msg_text[512];

    };

    int main()

    {

    int qid;

    key_t key;

    int len;

    struct msgmbuf msg;

    if ((key = ftok(".", 'a')) == -1)

    {

    perror("creat key error");

    exit(1);

    }

    if ((qid = msgget(key, IPC_CREAT | 0666)) == -1)

    {

    perror("creat message queue erro");

    exit(1);

    }

    printf("creat , open queue qid is %d\n", qid);

    puts("please input message to add to queue\n");

    if ((fgets((&msg)->msg_text, 512, stdin)) == NULL)

    {

    puts("no message");

    exit(1);

    }

    msg.msg_type = getpid();

    len = strlen(msg.msg_text);

    printf("send message\n");

    if ((msgsnd(qid, &msg, len, 0)) < 0)

    {

    perror("add message error");

    exit(1);

    }

    printf("read message\n");

    if ((msgrcv(qid, &msg, 512, 0, 0)) < 0)

    {

    perror("read message error");

    exit(1);

    }

    printf("read message is: %s\n", (&msg)->msg_text);

    if ((msgctl(qid, IPC_RMID, NULL)) < 0)

    {

    perror("delete message error");

    exit(1);

    }

    exit(0);

    }

    [root@localhost pipe]# ./message

    creat , open queue qid is 0

    please input message to add to queue

    hello linux

    send message

    read message

    read message is: hello linux

    [root@localhost pipe]#

    展开全文
  • WinSock_MsgSnd.rar

    2021-09-29 17:33:58
    利用socket进行数据通讯,本例实现服务器客户端通过socket套接字进行消息传送,服务器采用基本的select方式进行客户端数据处理
  • msgsnd、msgrcv——添加数据到消息队列、从消息队列获取数据 msgctl——控制消息队列 一、msgget——创建一个消息队列 1.原函数 表头文件 #include <sys/types.h> #include <sys/ipc.h> #include <

    消息队列特性

    1. 消息队列存在于Linux内核中,可以使数据双向流动
    2. 数据在内核中,即使进程结束数据依然存在
    3. 消息队列实际上是消息链表,每个队列都有自己的标识符

    消息队列API

    1. msgget——创建一个消息队列
    2. msgsnd、msgrcv——添加数据到消息队列、从消息队列获取数据
    3. msgctl——控制消息队列

    一、msgget——创建一个消息队列

    1.原函数

    表头文件
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    定义函数
    int msgget(key_t key, int msgflg);
    

    函数说明——创建和访问一个消息队列
    key——消息队列的名称(使用ftok()函数获取)
    msgflg——消息队列的访问权限
    返回值——成功返回此消息队列的标识符(非零整数)。失败时返回-1,失败原因在errno中

    2.示例

    key_t key;
    key = ftok(".",'z');
    
    int msgId = msgget(key,IPC_CREAT|0777);//创建和访问一个消息队列
    

    二、msgsnd、msgrcv——添加数据到消息队列、从消息队列获取数据

    1.原函数

    表头文件
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    定义函数
    int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
    ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
    

    函数说明——msgsnd()添加数据到消息队列、msgrcv()从消息队列获取数据
    msqid —— 由msgget()返回的队列标识符
    msgp —— 需要存放/读取的结构体
    msgsz —— 需要存放/读取的大小
    msgtyp —— 读取消息队列的位置
    msgflg —— 默认值为0
    返回值——成功msgsnd()返回0,msgrcv()返回实际复制到mtext数组中的字节数。错误两个函数都返回-1,errno表示错误

    2.参数取值
    msgp默认结构体如下:

    struct msgbuf {
               long mtype;       /* message type, must be > 0 */
               char mtext[1];    /* message data */
           };
    

    3.示例

    struct msgbuf {
           long mtype;       /* message type, must be > 0 */
           char mtext[128];    /* message data */
    };
    
    struct msgbuf sendBuf = {988,"thank your welcome\n"};
    struct msgbuf readBuf;
    
    msgrcv(msgId,&readBuf,sizeof(readBuf.mtext),888,0);//从消息队列获取数据
    msgsnd(msgId,&sendBuf,strlen(sendBuf.mtext),0);//添加数据到消息队列
    

    三、msgctl——控制消息队列

    1.原函数

    表头文件
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    定义函数
    int msgctl(int msqid, int cmd, struct msqid_ds *buf);
    

    函数说明—— 控制消息队列,它与共享内存的shmctl函数相似
    msqid —— 由msgget()返回的队列标识符
    cmd —— 控制msqid_ds结构体指令
    buf —— 记录消息队列的信息(消息队列模式和访问权限)(默认为NULL)
    返回值——成功时返回0。失败时返回-1,errno表示错误

    2.参数取值
    command是将要采取的动作,它可以取3个值,

    IPC_STAT:把msgid_ds结构中的数据设置为消息队列的当前关联值,即用消息队列的当前关联值覆盖msgid_ds的值。
    IPC_SET:如果进程有足够的权限,就把消息列队的当前关联值设置为msgid_ds结构中给出的值
    IPC_RMID:删除消息队列

    msqid_ds默认结构体如下:

    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 (nonstandard) */
               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) */
           };
    

    3.示例

    msgctl(msgid,IPC_RMID,NULL);//删除消息队列
    

    消息队列通信例程

    程序1

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    
    struct msgbuf
    {
            long mtype;       /* message type, must be > 0 */
            char mtext[50];    /* message data */
    };
    
    int main()
    {
            struct msgbuf sndbuf={889,"Hello"};
            struct msgbuf rcvbuf;
            key_t key;
            int msgid;
    
            key=ftok(".",'k');//键值获取
    
            msgid=msgget(key,IPC_CREAT|0777);//创建一个消息队列 
    
            msgsnd(msgid,&sndbuf,sizeof(sndbuf.mtext),0);//添加数据到消息队列 
    
            msgrcv(msgid,&rcvbuf,sizeof(rcvbuf.mtext),888,0);//从消息队列获取数据
    
            printf("rcvbuf.mtype=%ld,rcvbuf.mtext=%s\n",rcvbuf.mtype,rcvbuf.mtext);
    
            msgctl(msgid,IPC_RMID,NULL);//删除消息队列
    
            return 0;
    }
    

    程序2

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    
    struct msgbuf
    {
            long mtype;       /* message type, must be > 0 */
            char mtext[50];    /* message data */
    };
    
    int main()
    {
            struct msgbuf sndbuf={888,"World"};
            struct msgbuf rcvbuf;
            key_t key;
            int msgid;
    
            key=ftok(".",'k');//键值获取
    
            msgid=msgget(key,IPC_CREAT|0777);//访问一个消息队列 
    
            msgrcv(msgid,&rcvbuf,sizeof(rcvbuf.mtext),889,0);//从消息队列获取数据
    
            printf("rcvbuf.mtype=%ld,rcvbuf.mtext=%s\n",rcvbuf.mtype,rcvbuf.mtext);
    
            msgsnd(msgid,&sndbuf,sizeof(sndbuf.mtext),0);//添加数据到消息队列 
    
    
            msgctl(msgid,IPC_RMID,NULL);//删除消息队列
    
            return 0;
    }
    

    运行结果:打开任意程序后消息队列阻塞,等待另外一个程序将所需数据录入,开启另外一程序后两程序达到消息通信的目的。
    rcvbuf.mtype=889,rcvbuf.mtext=Hello
    rcvbuf.mtype=888,rcvbuf.mtext=World

    星辰~念

    展开全文
  • 文章目录 1.msgsnd函数 2.msgrcv函数 1.msgsnd函数 功能:把一条消息添加到消息队列中 原型:int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); 参数: msgid:由msgget函数返回的消息队列标识码...
  • linux systemV消息队列msgsnd失败在linux下使用syetmV消息队列方式来传递数据现在msgsnd返回-1,但是使用ipcs查看对应的消息队列messages个数有相应的增加。。这是为嘛?实际上我使用msgrcv函数也能接收到数据,但是...
  • msgsnd和msgrcv,只有在一下三种情况下,才会退出阻塞状态 写或读消息队列成功 对应的消息队列被删除 信号中断 为了做到超时退出阻塞状态,一般常用的做法是通过alarm,触发信号中断,但是只能是在一个线程的情况...
  • 进程间通信设计 题目要求:消息的创建,发送和接收。 设计思路:使用系统调用msgget(),msgsnd(),msgrcv()及msgctl()编制一长度为1K的消息的发送和接收程序。 (站在巨人的肩膀上)
  • 消息队列函数由msgget、msgctl、msgsnd、msgrcv四个函数组成。下面的表格列出了这四个函数的函数原型及其具体说明。1. &amp;nbsp; msgget函数原型msgget(得到消息队列标识符或创建一个消息队列对象)所需头文件#...
  • 文章目录消息队列介绍msgget:创建和访问一个消息队列msgsnd:发送信息,把一条消息添加到消息队列里去msgrcv:从一个消息队列里检索(接收)消息代码实例 消息队列介绍 消息队列:IPC之一 内核开辟的一个队列,进程...
  • msgsnd.c文件

    2013-06-27 17:41:41
    消息队列基础实验代码,详看博客http://blog.csdn.net/mybelief321
  • msgsnd/msgrcv系统调用】功能描述:在消息队列上进行收发消息。为了发送消息,调用进程对消息队列必须有写权能。接收消息时必须有读权能。用法:#include #include #includeint msgsnd(int msqid, const void *...
  • 客户端和服务器,进行消息收发,利用Winsock实现
  • msgsnd()函数

    千次阅读 2018-08-01 16:34:36
    从函数名就可以看出,msgsnd()函数是用来向消息队列发送消息的。在linux/msg.h 它 的函数定义是这样的: 系统调用: msgsnd() 函数声明: int msgsnd ( int msqid, struct msgbuf *msgp, int msgsz, int msgflg ) ...
  • #include #define N 10 ... } msgsnd 显示结果: 0 -- hello 1 -- aaa 2 -- hello 3 -- hello 4 -- hello 5 -- hello 6 -- hello 7 -- hello 8 -- hello 9 -- end 我知道了,可能是 不稳定信号的问题。信号丢失。。。
  • 消息队列函数由msgget、msgctl、msgsnd、msgrcv四个函数组成。下面的表格列出了这四个函数的函数原型及其具体说明。1.   msgget函数原型msgget(得到消息队列标识符或创建一个消息队列对象)所需头文件#include &...
  • linux消息队列函数msgget,msgsnd, msgrcv, ftok().doc下载提示(请认真阅读)1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。2.下载的文档,不会出现我们的网址水印。3...
  • #include #define N 10 ... } msgsnd 显示结果: 0 -- hello 1 -- aaa 2 -- hello 3 -- hello 4 -- hello 5 -- hello 6 -- hello 7 -- hello 8 -- hello 9 -- end 我知道了,可能是 不稳定信号的问题。信号丢失。。。
  • 3、msgsnd(2) #include #include #include int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); struct msgbuf { long mtype; /* 消息类型,正整数即可 */ char mtext[256]; /* 需要发送数据的...
  • 这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、...
  • 消息队列函数由msgget、msgctl、msgsnd、msgrcv四个函数组成。下面的表格列出了这四个函数的函数原型及其具体说明。 1. msgget函数原型 msgget(得到消息队列标识符或创建一个消息队列对象) 所需...
  • 消息队列函数由msgget、msgctl、msgsnd、msgrcv四个函数组成。下面的表格列出了这四个函数的函数原型及其具体说明。 1. msgget函数原型 msgget(得到消息队列标识符或创建一个消息队列对象) ...
  • msgsnd()函数参数详解

    千次阅读 2018-03-18 09:36:07
    进程间通信函数msgsnd()函数原型: int msgsnd(int msgid,const void *ptr,size_t length,int flag);参数: 1.msgid:消息队列标识符,由msgget得到 2.ptr:消息缓冲区指针,即一个指向要发送的消息所在的内存。...
  • 函数msgsnd()及函数msgrcv()参数详解

    千次阅读 2018-12-28 14:44:31
     最近看system V消息队列,在看到msgsnd()这个函数时,发现了一个错误。(注:我看的是UNIX网络编程卷二:进程间通信 第二版) msgsnd()函数:追加一条新消息到消息队列的系统调用语法: #include &lt;sys.msg...
  • 在编写发送消息程序时,遇到运行错误:msgsnd Invalid argument,该错误意思是msgasnd函数的参数无效。于是开始检查参数,在linux中执行命令man msgsnd,得到msgsnd函数相关说明文档。部分如下:int msgsnd(int ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 9,225
精华内容 3,690
关键字:

msgsnd

友情链接: bink2w64.rar