【十】 进程间通信——[System V IPC对象]消息队列(message queue)
/*server.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define N 64
#define TypeA 100
#define TypeB 200
#define TypeS 10
#define LEN (sizeof(MSG) - sizeof(long))
typedef struct
{
long mtype;
char name[16];
char mtext[N];
} MSG;
int main()
{
key_t key;
int msgid;
MSG buf;
if ((key = ftok(".", 'm')) < 0)
{
perror("fail to ftok");
exit(-1);
}
if ((msgid = msgget(key, 0666|IPC_CREAT)) < 0)
{
printf("server is down...\n");
exit(-1);
}
while ( 1 )
{
msgrcv(msgid, &buf, LEN, TypeS, 0);
buf.mtype = TypeA;
msgsnd(msgid, &buf, LEN, 0);
buf.mtype = TypeB;
msgsnd(msgid, &buf, LEN, 0);
if (strncmp(buf.mtext, "quit", 4) == 0) break;
}
usleep(100000);
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
/*clientA.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define N 64
#define TypeA 100
#define TypeB 200
#define TypeS 10
#define LEN (sizeof(MSG) - sizeof(long))
typedef struct
{
long mtype;
char name[16];
char mtext[N];
} MSG;
int main()
{
key_t key;
int msgid;
MSG buf;
pid_t pid;
if ((key = ftok(".", 'm')) < 0)
{
perror("fail to ftok");
exit(-1);
}
if ((msgid = msgget(key, 0666)) < 0)
{
printf("server is down...\n");
exit(-1);
}
strcpy(buf.name, "clientA");
if ((pid = fork()) < 0)
{
perror("fail to fork");
exit(-1);
}
else if (pid == 0) // recv message
{
while ( 1 )
{
msgrcv(msgid, &buf, LEN, TypeA, 0);
if (strncmp(buf.mtext, "quit", 4) == 0)
{
kill(getppid(), SIGUSR1);
//msgctl(msgid, IPC_RMID, NULL);
exit(0);
}
printf("[%s] %s\n", buf.name, buf.mtext);
}
}
else // send message
{
buf.mtype = TypeS;
while ( 1 )
{
printf("send : ");
fgets(buf.mtext, N, stdin);
msgsnd(msgid, &buf, LEN, 0);
if (strncmp(buf.mtext, "quit", 4) == 0)
{
kill(pid, SIGUSR1);
exit(0);
}
usleep(100000);
}
}
return 0;
}
/*clientB.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define N 64
#define TypeA 100
#define TypeB 200
#define TypeS 10
#define LEN (sizeof(MSG) - sizeof(long))
typedef struct
{
long mtype;
char name[16];
char mtext[N];
} MSG;
int main()
{
key_t key;
int msgid;
MSG buf;
pid_t pid;
if ((key = ftok(".", 'm')) < 0)
{
perror("fail to ftok");
exit(-1);
}
if ((msgid = msgget(key, 0666)) < 0)
{
printf("server is down...\n");
exit(-1);
}
strcpy(buf.name, "clientB");
if ((pid = fork()) < 0)
{
perror("fail to fork");
exit(-1);
}
else if (pid == 0) // recv message
{
while ( 1 )
{
msgrcv(msgid, &buf, LEN, TypeB, 0);
if (strncmp(buf.mtext, "quit", 4) == 0)
{
kill(getppid(), SIGUSR1);
//msgctl(msgid, IPC_RMID, NULL);
exit(0);
}
printf("[%s] %s\n", buf.name, buf.mtext);
}
}
else // send message
{
buf.mtype = TypeS;
while ( 1 )
{
printf("send : ");
fgets(buf.mtext, N, stdin);
msgsnd(msgid, &buf, LEN, 0);
if (strncmp(buf.mtext, "quit", 4) == 0)
{
kill(pid, SIGUSR1);
exit(0);
}
usleep(100000);
}
}
return 0;
}