【代码1】msg_recv.c
#include "header.h"
int main(int argc, char *argv[])
{
key_t key;
if (0 > (key = ftok(".", 'b')))
err_exit("ftok");
printf("key is %x\n", key);
int msgid;
if (0 > (msgid = msgget(key, IPC_CREAT | 0666)))
err_exit("msgget");
printf("msgid is %d\n", msgid);
msgbuf buf;
memset(&buf, 0, sizeof(msgbuf));
if (0 > msgrcv(msgid, &buf, MAX, 0, 0))
err_exit("msgrcv");
printf("msgrcv : %s\n", buf.mtext);
exit(0);
}
【代码2】msg_send.c
#include "header.h"
int main(int argc, char *argv[])
{
key_t key;
if (0 > (key = ftok(".", 'b')))
err_exit("ftok");
printf("key is %x\n", key);
int msgid;
if (0 > (msgid = msgget(key, IPC_CREAT | 0666)))
err_exit("msgget");
printf("msgid is %d\n", msgid);
msgbuf buf;
memset(&buf, 0, sizeof(msgbuf));
buf.mtype = 10;
strncpy(buf.mtext, "10", sizeof("10"));
if (0 > msgsnd(msgid, &buf, sizeof("10"), 0))
err_exit("msgsnd");
memset(&buf, 0, sizeof(msgbuf));
buf.mtype = 20;
strncpy(buf.mtext, "20", sizeof("20"));
if (0 > msgsnd(msgid, &buf, sizeof("20"), 0))
err_exit("msgsnd");
memset(&buf, 0, sizeof(msgbuf));
buf.mtype = 30;
strncpy(buf.mtext, "30", sizeof("30"));
if (0 > msgsnd(msgid, &buf, sizeof("30"), 0))
err_exit("msgsnd");
exit(0);
}
【代码3】msg_size.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/time.h>
#define N 512
typedef struct {
long mtype; /* message type, must be > 0 */
char mtext[N]; /* message data */
}msgbuf;
int main(int argc, char *argv[])
{
int msgid;
key_t key;
msgbuf buf;
if (0 > (key = ftok(".", 'b'))) {
fprintf(stderr, "ftok : %s\n", strerror(errno));
exit(1);
}
printf("key is %#0x\n", key);
if (0 > (msgid = msgget(key, IPC_CREAT | 0600))) {
fprintf(stderr, "msgget : %s\n", strerror(errno));
exit(1);
}
buf.mtype = 1;
int sum = 0;
while(1) {
if (0 > msgsnd(msgid, &buf, N, IPC_NOWAIT)) {
fprintf(stderr, "msgsnd : %s\n", strerror(errno));
exit(1);
}
sum += N;
printf("sum is %d\n", sum);
}
exit(0);
}
【代码4】header.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
#include <errno.h>
#include <sys/shm.h>
#include <sys/msg.h>
#define MAX 128
typedef struct {
long mtype; /* message type, must be > 0 */
char mtext[MAX]; /* message data */
}msgbuf;
union semun{
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
#define err_exit(function) \
do { \
fprintf(stderr, "in %s at %s %d:\n%s : %s\n", __FUNCTION__, __FILE__, __LINE__ - 1, function, strerror(errno)); \
exit(EXIT_FAILURE); \
} while(0)
【代码5】test_shm.c
#include "header.h"
int main(int argc, char *argv[])
{
key_t key;
if (0 > (key = ftok("/", 'a')))
err_exit("ftok");
printf("key is %x\n", key);
int shmid;
if (0 > (shmid = shmget(key, 512, IPC_CREAT | 0666)))
err_exit("shmget");
printf("shmid is %d\n", shmid);
char *p = NULL;
if ((char *)0 > (p = shmat(shmid, NULL, 0)))
err_exit("shmat");
while(1) {
fgets(p, 512, stdin);
if (!strncmp(p, "quit", 4))
break;
printf("shm : %s\n", p);
}
if (0 > shmdt(p))
err_exit("shmdt");
if (0 > shmctl(shmid, IPC_RMID, NULL))
err_exit("shmctl");
exit(0);
}