⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgsend.c

📁 msgsend函数的使用例子
💻 C
字号:
#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <string.h>#define MSGSZ     128/* * Declare the message structure. */typedef struct msgbuf {         long    mtype;         char    mtext[MSGSZ];} message_buf;main(){    int msqid;    int msgflg = IPC_CREAT | 0666;    key_t key;    message_buf sbuf;    size_t buf_length;	    /*     * Get the message queue id for the     	 * "name" 1234, which was created by     * the server.     	 */    	 	 key = 1234;		(void) fprintf(stderr, 			"\nmsgget: Calling msgget(%#lx,\%#o)\n",			key, msgflg);    	if ((msqid = msgget(key, msgflg )) < 0) {	    perror("msgget");        exit(1);    	}    	else 		(void) fprintf(stderr,			"msgget: msgget succeeded: msqid = %d\n", 			msqid);    /*     	 * We'll send message type 1     	 */         	 	 sbuf.mtype = 1;         (void) fprintf(stderr,			"msgget: msgget succeeded: msqid = %d\n", 			msqid);         (void) strcpy(sbuf.mtext, "Did you get this?");         (void) fprintf(stderr,			"msgget: msgget succeeded: msqid = %d\n", 			msqid);         buf_length = strlen(sbuf.mtext) ;            	 	 /*     	  * Send a message.     	  */     if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {		printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);        perror("msgsnd");        		exit(1);    	 }   	 else 		printf("Message: \"%s\" Sent\n", sbuf.mtext);          		 exit(0);}

⌨️ 快捷键说明

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