?使用消息队列完成两个进程之间相互通信
?
#include<my_head.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
#define SIZE (sizeof(struct msgbuf) - sizeof(long))
//分支线程从消息队列中读取类型2的消息
void *task(void *arg)
{
int msgid = *((int *)arg);
struct msgbuf buf;
while(1)
{
msgrcv(msgid,&buf,SIZE,2,0);
printf("收到消息:%s\n",buf.mtext);
if(strcmp(buf.mtext,"quit") == 0)
break;
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建key值
key_t key = ftok("/",'k');
if(key == -1)
{
perror("ftok error");
return -1;
}
//用key值创建消息队列
int msgid = msgget(key,IPC_CREAT|0664);
if(msgid == -1)
{
perror("msgget error");
return -1;
}
//创建分支线程
pthread_t tid = -1;
if(pthread_create(&tid,NULL,task,&msgid) != 0)
{
printf("create tid error\n");
return -1;
}
//主线程向消息队列中存放类型1的消息
struct msgbuf buf;
while(1)
{
printf("请输入消息类型1>>>");
scanf("%ld",&buf.mtype);
printf("请输入消息正文:");
scanf("%s",buf.mtext);
msgsnd(msgid,&buf,SIZE,0);
if(strcmp(buf.mtext,"quit") == 0)
break;
}
//删除消息队列
if(msgctl(msgid,IPC_RMID,NULL) == -1)
{
perror("msgctl error");
return -1;
}
//阻塞回收分支线程资源
pthread_join(tid,NULL);
return 0;
}
?
#include<my_head.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
#define SIZE (sizeof(struct msgbuf) - sizeof(long))
//分支线程从消息队列中读取类型1的消息
void *task(void *arg)
{
int msgid = *((int *)arg);
struct msgbuf buf;
while(1)
{
msgrcv(msgid,&buf,SIZE,1,0);
printf("收到消息:%s\n",buf.mtext);
if(strcmp(buf.mtext,"quit") == 0)
break;
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建key值
key_t key = ftok("/",'k');
if(key == -1)
{
perror("ftok error");
return -1;
}
//用key值创建消息队列
int msgid = msgget(key,IPC_CREAT|0664);
if(msgid == -1)
{
perror("msgget error");
return -1;
}
//创建分支线程
pthread_t tid = -1;
if(pthread_create(&tid,NULL,task,&msgid) != 0)
{
printf("create tid error\n");
return -1;
}
//主线程向消息队列中存放类型2的消息
struct msgbuf buf;
while(1)
{
printf("请输入消息类型2>>>");
scanf("%ld",&buf.mtype);
printf("请输入消息正文>>>");
scanf("%s",buf.mtext);
msgsnd(msgid,&buf,SIZE,0);
if(strcmp(buf.mtext,"quit") == 0)
break;
}
//阻塞回收分支线程资源
pthread_join(tid,NULL);
return 0;
}
?将信号通信相关代码重新实现一遍
#include<my_head.h>
//信号处理函数
void handler(int signo)
{
if(signo == SIGCHLD)
{
waitpid(-1,NULL,WNOHANG);
kill(getpid(),SIGKILL);
}
if(signo == SIGUSR1)
{
printf("逆子,何至于此!!\n");
}
}
int main(int argc, const char *argv[])
{
//创建一个子进程
int pid = fork();
if(pid > 0)
{
//父进程通过绑定信号函数,将子进程发送的信号捕获后用函数处理
if(signal(SIGCHLD,handler) == SIG_ERR)
{
perror("signal error");
return -1;
}
if(signal(SIGUSR1,handler) == SIG_ERR)
{
perror("signal error");
return -1;
}
//父进程在运行中
while(1)
{
printf("我真的还想再活500年\n");
sleep(1);
}
}
else if(pid == 0)
{
//子进程在运行中,发送一个信号给父进程
printf("来来来,喝酒喝酒!!!\n");
sleep(5);
printf("我已看破红尘,父亲陪我走吧\n");
kill(getppid(),SIGUSR1);
//子进程退出
sleep(2);
exit(EXIT_SUCCESS);
}
else
{
perror("fork error");
return -1;
}
return 0;
}
将共享内存相关代码重新实现一遍
发送端:
#include<my_head.h>
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
//创建key值
int key = ftok("/",'k');
if(key == -1)
{
perror("");
return -1;
}
//创建共享内存段
int shmid = shmget(key,PAGE_SIZE,IPC_CREAT|0664);
if(shmid == -1)
{
perror("shmget error");
return -1;
}
//将共享内存段映射到用户空间
char *addr = (char *)shmat(shmid,NULL,0);
if(addr == (void *)-1)
{
perror("");
return -1;
}
printf("addr = %p\n",addr);
//操作共享内存段
while(1)
{
fgets(addr,PAGE_SIZE,stdin);
addr[strlen(addr)-1] = 0;
if(strcmp(addr,"quit") == 0)
break;
}
//取消映射
if(shmdt(addr) == -1)
{
perror("shmdt error");
return -1;
}
//删除共享内存
if(shmctl(shmid,IPC_RMID,NULL) == -1)
{
perror("shmctl error");
return -1;
}
return 0;
}
#include<my_head.h>
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
//创建key值
int key = ftok("/",'k');
if(key == -1)
{
perror("");
return -1;
}
//创建共享内存段
int shmid = shmget(key,PAGE_SIZE,IPC_CREAT|0664);
if(shmid == -1)
{
perror("shmget error");
return -1;
}
//将共享内存段映射到用户空间
char *addr = (char *)shmat(shmid,NULL,0);
if(addr == (void *)-1)
{
perror("");
return -1;
}
printf("addr = %p\n",addr);
//操作共享内存段
while(1)
{
printf("共享内存中的数据为:%s\n",addr);
sleep(1);
if(strcmp(addr,"quit") == 0)
break;
}
//取消映射
if(shmdt(addr) == -1)
{
perror("");
return -1;
}
return 0;
}