qsnd.c

来自「基于网络编程的例子」· C语言 代码 · 共 53 行

C
53
字号
/* * qsnd.c - Send a message to previously opened queue */#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#define BUFSZ 512/* Message structure */struct msg {    long msg_type;    char msg_text[BUFSZ];};int main(int argc, char *argv[]){    int qid; /* The queue identifier */    int len; /* Length of data sent */    struct msg pmsg; /* Pointer to message structure */         /* Expect the queue ID passed on the command-line */    if(argc != 2) {        puts("USAGE: qsnd <queue ID>");        exit(EXIT_FAILURE);    }    qid = atoi(argv[1]);    /* Get the message to add to the queue */    puts("Enter message to post:");    if((fgets((&pmsg)->msg_text, BUFSZ, stdin)) == NULL) {        puts("no message to post");        exit(EXIT_SUCCESS);    }         /* Associate the message with this process */    pmsg.msg_type = getpid();    /* Add the message to the queue */    len = strlen(pmsg.msg_text);    if((msgsnd(qid, &pmsg, len, 0)) < 0) {        perror("msgsnd");        exit(EXIT_FAILURE);    }    puts("message posted");    exit(EXIT_SUCCESS);}     

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?