进程和线程编程之四

【代码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);
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百里杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值