进程间通信机制(IPC)
简述
IPC:Inter Process Communication
进程和进程之间的用户空间相互独立,但是4G内核空间共享,进程间的通信就是通过这4G的内核空间
分类
传统的进程间通信机制
无名管道(pipe)
有名管道(fifo)
信号(signal)
System V中的IPC对象和IPC的区别
消息队列(message queue)
共享内存(shared memory)
信号灯集(semaphore)
可用于主机传输的通信机制
套接字(socket)
消息队列
概念
在内核内存中创建一个队列,进程需要将数据打包成结点,添加到队尾中,或者从队列中读取结点,可以通过消息类型进行消息分类
特点
需要打包,有特定的格式以及消息类型
按照先进先出原则,但是也可以限制消息类型读取
独立于进程,灯进程结束后,消息队列以及其中的内容不会删除,除非中期操作系统或者手动删除
在Linux系统下,可以通过命令ipcs查看消息队列,通过ipcrm -q msqid删除消息队列
函数
ftok()函数,获得一个Key值用来创建消息队列,通过相同的key值可以访问对应的消息队列
msgget()函数创建一个消息队列获得起id号
msgsnd()发送消息到指定消息队列
msgrcv()获取对应消息队列中对应的消息
msgctl()控制消息队列,常用于删除消息队列
#include<myhead.h>
#include<sys/msg.h>
#include<sys/ipc.h>
struct msgbuf
{
	long mtype;      //消息类型
	char mtext[128]; //消息内容
};
//线程1函数
void *task1(void *arg)
{
	int msqid = *(int *)arg;//获取消息队列id号
	struct msgbuf msbuf;//声明消息结构体
	printf("A:\n\t");
	fflush(stdout);
	while (1)
	{
		msbuf.mtype = 1;//消息类型
		fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);
		msbuf.mtext[strlen(msbuf.mtext)-1]='\0';
		if(msgsnd(msqid,&msbuf,sizeof(msbuf)-sizeof(long),0) == -1)
		{
			perror("msgsnd");
			return NULL;
		}
		//printf("线程%d:发送成功\n",getpid());
		printf("A:\n\t");
		fflush(stdout);
		if (!strcmp(msbuf.mtext,"quit"))
		{
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}
//线程2函数
void *task2(void *arg)
{
	int msqid = *(int *)arg;
	struct msgbuf buf;
	ssize_t num = 0;
	while (1)
	{
		bzero(&buf,sizeof(buf));
		if ((num = msgrcv(msqid,&buf,sizeof(buf.mtext),2,0))<0)
		{
			//perror("msgrcv");
			return NULL;
		}
		printf("\nB:\n\t%s\n",buf.mtext);
		printf("A:\n\t");
		fflush(stdout);
		if (!strcmp(buf.mtext,"quit"))
		{
			msgctl(msqid,IPC_RMID,NULL);
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	key_t key = ftok("./",0);
	if (key == -1)
	{
		perror("ftok");
		return -1;/* code */
	}
	umask(0);
	int msqid = msgget(key,IPC_CREAT|0664);
	if (msqid == -1)
	{
		perror("msgget");
		return -1;
	}
	pthread_t tid1,tid2;
	if (pthread_create(&tid1,NULL,task1,&msqid) != 0)
	{
		printf("线程1创建失败\n");
	}
	if (pthread_create(&tid2,NULL,task2,&msqid) != 0)
	{
		printf("线程1创建失败\n");
	}
	
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	
	return 0;
}
#include<myhead.h>
//#include<sys/msg.h>
//#include<sys/ipc.h>
struct msgbuf
{
	long mtype;      //消息类型
	char mtext[128]; //消息内容
};
//线程1函数
void *task1(void *arg)
{
	int msqid = *(int *)arg;
	struct msgbuf msbuf;
	printf("B:\n\t");
	fflush(stdout);
	while (1)
	{
		msbuf.mtype = 2;
		fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);
		msbuf.mtext[strlen(msbuf.mtext)-1]='\0';
		if(msgsnd(msqid,&msbuf,sizeof(msbuf)-sizeof(long),0) == -1)
		{
			perror("msgsnd");
			return NULL;
		}
		//printf("线程%d:发送成功\n",getpid());
		printf("B:\n\t");
		fflush(stdout);
		if (!strcmp(msbuf.mtext,"quit"))
		{
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}
//线程2函数
void *task2(void *arg)
{
	int msqid = *(int *)arg;
	struct msgbuf buf;
	ssize_t num = 0;
	while (1)
	{
		bzero(&buf,sizeof(buf));
		if ((num = msgrcv(msqid,&buf,sizeof(buf.mtext),1,0))<0)
		{
			//perror("msgrcv");
			return NULL;
		}
		printf("\nA:\n\t%s\n",buf.mtext);
		printf("B:\n\t");
		fflush(stdout);
		if (!strcmp(buf.mtext,"quit"))
		{
			msgctl(msqid,IPC_RMID,NULL);
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	key_t key = ftok("./",0);
	if (key == -1)
	{
		perror("ftok");
		return -1;/* code */
	}
	umask(0);
	int msqid = msgget(key,IPC_CREAT|0664);
	if (msqid == -1)
	{
		perror("msgget");
		return -1;
	}
	pthread_t tid1,tid2;
	if (pthread_create(&tid1,NULL,task1,&msqid) != 0)
	{
		printf("线程1创建失败\n");
	}
	if (pthread_create(&tid2,NULL,task2,&msqid) != 0)
	{
		printf("线程1创建失败\n");
	}
	
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	
	return 0;
}


![[架构之路-232]:操作系统 - 文件系统存储方法汇总](https://img-blog.csdnimg.cn/681537916b3a4072b9dec59cc6071ffd.png)










![[极客大挑战 2019]FinalSQL - 异或盲注](https://img-blog.csdnimg.cn/85c921ec7b414127b7ff7a691c76c142.png)



![[CISCN 2019华北Day2]Web1 - 布尔盲注](https://img-blog.csdnimg.cn/4f322eb6651d4f81a554ab475e74e5cd.png)


