给消息队列写入消息
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MSG_TYPE1 1
#define MSG_TYPE2 2
struct msgbuf
{
long mtype;
char mtext[80];
};
int main (void)
{
// key_t key = ftok (".", 510);
key_t key = 12345;
int msg_id = msgget (key, IPC_CREAT | 0666);
struct msgbuf msg;
memset (&msg, 0, sizeof(msg));
msg.mtype = MSG_TYPE2;
strncpy (msg.mtext, "hello world\n", 80);
if (msgsnd (msg_id, (void *)&msg, \
strlen (msg.mtext), 0) == -1)
{
perror ("msgsnd");
exit (EXIT_FAILURE);
}
return 0;
}
读取消息队列中的消息
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MSG_TYPE1 1
#define MSG_TYPE2 2
struct msgbuf
{
long mtype;
char mtext[80];
};
int main (int argc, char *argv[])
{
// key_t key = ftok (".", 510);
key_t key = 12345;
int msg_id = msgget (key, IPC_CREAT | 0666);
struct msgbuf msg;
memset (&msg, 0, sizeof(msg));
if (msgrcv (msg_id, (void *)&msg, \
sizeof(msg.mtext), MSG_TYPE2, 0) == -1)
{
perror ("msgrcv");
exit (EXIT_FAILURE);
}
printf ("%s", msg.mtext);
msgctl (msg_id, IPC_RMID, NULL);
return 0;
}
修改消息队列中的消息属性
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define handle_error(msg) \
{perror(msg);exit(EXIT_FAILURE);}
int main (void)
{
// key_t key = ftok (".", 510);
key_t key = 12345;
int msg_id = msgget (key, IPC_CREAT | 0666);
struct msqid_ds info;
if (msgctl (msg_id, IPC_STAT, &info) == -1)
handle_error("msgctl");
printf ("uid:%d, gid:%d, cuid:%d, cgid:%d\n", info../.uid, \
info.msg_perm.gid, info.msg_perm.cuid, info.msg_perm.cgid);
printf ("mode:%03o, cbytes:%lu, qnum:%lu, qbytes:%lu\n", \
info.msg_perm.mode & 0777, info.msg_cbytes, \
info.msg_qnum, info.msg_qbytes);
info.msg_qbytes = 16380;
if (msgctl (msg_id, IPC_SET, &info) == -1)
handle_error("msgctl");
if (msgctl (msg_id, IPC_STAT, &info) == -1)
handle_error("msgctl");
printf ("mode:%03o, cbytes:%lu, qnum:%lu, qbytes:%lu\n", \
info.msg_perm.mode & 0777, info.msg_cbytes, \
info.msg_qnum, info.msg_qbytes);
return 0;
}
进程之间点对点通讯
client1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define handle_error(msg) \
{perror(msg);exit(EXIT_FAILURE);}
#define MSG_TYPE1 1
#define MSG_TYPE2 2
struct msgbuf
{
long mtype;
char mtext[80];
};
int main (int argc, char *argv[])
{
// key_t key = ftok (".", 511);
key_t key = 511;
int msg_id = msgget (key, IPC_CREAT | 0666);
struct msgbuf msg1, msg2;
int ret_from_fork;
ret_from_fork = fork ();
if (ret_from_fork == -1)
handle_error("fork")
else if (ret_from_fork == 0)
{
while (1)
{
gets (msg1.mtext);
msg1.mtype = MSG_TYPE1;
msgsnd (msg_id, &msg1, 80, 0);
}
}
else
{
while (1)
{
memset (&msg2, 0, sizeof(msg2));
if (msgrcv (msg_id, (void *)&msg2, \
sizeof(msg2.mtext), MSG_TYPE2, 0) == -1)
handle_error("msgrcv");
printf ("client2: %s\n", msg2.mtext);
}
}
msgctl (msg_id, IPC_RMID, NULL);
return 0;
}
client2
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define handle_error(msg) \
{perror(msg);exit(EXIT_FAILURE);}
#define MSG_TYPE1 1
#define MSG_TYPE2 2
struct msgbuf
{
long mtype;
char mtext[80];
};
int main (void)
{
// key_t key = ftok (".", 511);
key_t key = 511;
int msg_id = msgget (key, IPC_CREAT | 0666);
struct msgbuf msg1, msg2;
int ret_from_fork;
ret_from_fork = fork ();
if (ret_from_fork == -1)
handle_error("fork")
else if (ret_from_fork == 0)
{
while (1)
{
gets (msg2.mtext);
msg2.mtype = MSG_TYPE2;
msgsnd (msg_id, &msg2, 80, 0);
}
}
else
{
while (1)
{
memset (&msg1, 0, sizeof(msg1));
if (msgrcv(msg_id, (void *)&msg1, \
sizeof(msg1.mtext), MSG_TYPE1, 0) == -1)
handle_error("msgrcv");
printf ("client1: %s\n", msg1.mtext);
}
}
return 0;
}
多人聊天室
client
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define handle_error(msg) \
{perror(msg);exit(EXIT_FAILURE);}
#define TO_SERVER_MSGTYPE 1000
struct msgbuf
{
long mtype;
int client_id;
char mtext[80];
};
int main (void)
{
// key_t key = ftok ("./", 512);
key_t key = 512;
int msg_id = msgget (key, IPC_CREAT | 0666);
struct msgbuf msg_snd, msg_rcv;
printf ("input guest ID:");
int client_id;
scanf ("%d", &client_id);
printf ("client_id: %d\n", client_id);
int ret_from_fork;
ret_from_fork = fork ();
if (ret_from_fork == -1)
handle_error("fork")
else if (ret_from_fork == 0)
{
while (1)
{
gets (msg_snd.mtext);
msg_snd.mtype = TO_SERVER_MSGTYPE;
msg_snd.client_id = client_id;
msgsnd (msg_id, &msg_snd, 80, 0);
}
}
else
{
while (1)
{
memset (&msg_rcv, 0, sizeof(msg_rcv));
if (msgrcv (msg_id, (void *)&msg_rcv, \
sizeof(msg_rcv.mtext), client_id, 0) == -1)
handle_error("msg_rcv")
else
printf ("client%d: %s\n", msg_rcv.client_id, msg_rcv.mtext);
}
}
return 0;
}
server
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define TO_SERVER_MSGTYPE 1000
#define CLIENT_NUMBER 3
struct msgbuf
{
long mtype;
int client_id;
char mtext[80];
};
int main (void)
{
// key_t key = ftok ("./", 512);
key_t key = 512;
int msg_id = msgget (key, IPC_CREAT | 0666);
struct msgbuf msg;
while (1)
{
memset (&msg, 0, sizeof(msg));
if (msgrcv (msg_id, (void *)&msg, sizeof(msg.mtext), \
TO_SERVER_MSGTYPE, 0) == -1)
{
perror ("msgrcv");
exit (EXIT_FAILURE);
}
else
{
for (int i = 1; i <= CLIENT_NUMBER; i++)
{
if (i == msg.client_id)
continue;
else
{
msg.mtype = i;
msgsnd (msg_id, &msg, 80, 0);
}
}
}
printf ("server: %s\n", msg.mtext);
}
msgctl (msg_id, IPC_RMID, NULL);
return 0;
}
readme
usage:
$ ./client
$ input client ID :
support >3 clients