sen.c
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "stdlib.h"
#include "string.h"
#include "errno.h"
#include "fcntl.h"
#include "sys/stat.h"
#include "sys/ipc.h"
#include "sys/msg.h"
struct msgbuf{
long mtype;
char mtext[1024];
};
int main(int argc,char const *argv[])
{
while(1)
{
puts("please input infomation label:");
int label;
scanf("%d",&label);
getchar();
//create key
key_t key=ftok(".",label);
if(-1==key)
{
perror("ftok erro");
exit(1);
}
//create msgid
int msgid=msgget(key,IPC_CREAT|0666);
if(-1==msgid)
{
perror("msgget erro");
exit(1);
}
struct msgbuf *msg;
msg = malloc(sizeof(struct msgbuf));
msg->mtype=1;
memset(msg->mtext,0,1024);
puts("please input:");
fgets(msg->mtext,1024,stdin);
//int ret=msgsnd(msgid,msg,strlen(msg->mtext)+sizeof(msg->mtype),0);
int ret=msgsnd(msgid,msg,sizeof(*msg),0);
if(-1==ret)
{
perror("msgsnd erro");
continue;
}
if(!strncmp(msg->mtext,"quit",4))
{
break;
}
}
return 0;
}
recv.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf{
long mtype;
char mtext[1024];
};
int main(int argc, char const *argv[])
{
while (1)
{
puts("please input infomation label:");
int label;
scanf("%d",&label);
getchar();
// create key
key_t key = ftok(".", label);
if (-1 == key)
{
perror("ftok erro");
exit(1);
}
// create msgid
int msgid = msgget(key, IPC_CREAT | 0666);
if (-1 == msgid)
{
perror("msgget erro");
exit(1);
}
struct msgbuf msg; // 改为局部数组
memset(&msg, 0, sizeof(msg)); // 使用 memset 初始化数组
int ret = msgrcv(msgid, &msg, sizeof(struct msgbuf), 1, 0); // 修改为 msg 的地址
if (-1 == ret)
{
perror("msgrecv erro");
continue;
}
if (!strncmp(msg.mtext, "quit", 4))
{
break;
}
printf("recvbuf:%s", msg.mtext);
}
}
优化后的信号量控制共享内存
send.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define SPACE 0
#define DATA 1
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
int main(int argc,char const *argv[])
{
key_t key =ftok(".",1);
//create shm
int shmid=shmget(key,1024,IPC_CREAT|0666);
if(-1==shmid)
{
perror("shmget erro");
exit(1);
}
//create 2 sem
int semid=semget(key,2,IPC_CREAT|0666);
if(-1==semid)
{
perror("semget erro");
exit(1);
}
union semun a;
//init sem
//a.val=1;
semctl(semid,SPACE,SETVAL,1);//space设置信号量0为1
//a.val=0;
semctl(semid,DATA,SETVAL,0);//data设置信号量1为0
char *p=shmat(shmid,NULL,0);
if(NULL==p)
{
perror("shmat erro");
exit(1);
}
while (1)
{
if(semctl(semid,SPACE,GETVAL)==1 && semctl(semid,DATA,GETVAL)==0 )
{
//如果空间为1,且数据为0,说明没有数据所以发送数据
printf("please input:");
fgets(p,1024,stdin);
semctl(semid,DATA,SETVAL,1);//发送数据后,将data设置为1
semctl(semid,SPACE,SETVAL,0);//发送数据后,将space设置为0
if(!strncmp(p,"quit",4))
{
break;
}
}
}
shmdt(p);
return 0;
}
recv.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define SPACE 0
#define DATA 1
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
int main(int argc,char const *argv[])
{
key_t key =ftok(".",1);
//create shm
int shmid=shmget(key,1024,IPC_CREAT|0666);
if(-1==shmid)
{
perror("shmget erro");
exit(1);
}
//create 2 sem
int semid=semget(key,2,IPC_CREAT|0666);
if(-1==semid)
{
perror("semget erro");
exit(1);
}
union semun a;
//init sem
//a.val=1;
semctl(semid,SPACE,SETVAL,1);//space设置信号量0为1
//a.val=0;
semctl(semid,DATA,SETVAL,0);//data设置信号量1为0
char *p=shmat(shmid,NULL,0);
if(NULL==p)
{
perror("shmat erro");
exit(1);
}
while(1)
{
if(semctl(semid,SPACE,GETVAL)==0 && semctl(semid,DATA,GETVAL)==1)
{
//如果空间为0,且数据为1,说明有数据所以接受数据
printf("recv:%s",p);
if(!strncmp(p,"quit",4))
{
break;
}
semctl(semid,DATA,SETVAL,0);//接受数据后,将data设置为0
semctl(semid,SPACE,SETVAL,1);//接受数据后,将space设置为1
}
}
shmdt(p);
return 0;
}