要变优秀的科啊啊啊啊啊啊!!!

要变优秀的科啊啊啊啊啊啊!!!

IPC(Inter-Process Communication,进程间通信)

        常用的 IPC 对象包括管道(pipe)、消息队列(message queue)、信号量(semaphore)和共享内存(shared memory)等

    1.ipcs 

         查看系统重的消息队列、共享内存、信号灯的信息
   

    2.ipcrm

       删除消息队列、共享内存、信号灯
      ipcrm -Q/-M/-S key
      ipcrm -q/-m/-s 消息队列ID/共享内存ID/信号灯ID
      
   

    3.操作流程:


        创建消息队列 -> 发送消息 -> 接收消息

   4.函数接口:


        1.ftok

        2.msgget 

        3.msgsnd 

        4.msgrcv 

        5.msgctl 

练习:    利用消息队列实现clientA和clientB两个进程任务的全双工聊天功能

#ifndef __HEAD_H__
#define __HEAD_H__


#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct msgbuf 
{
	long mtype;
	char mtext[256];
};

#endif

 Client A

#include "head.h"

pthread_t tid_send;
pthread_t tid_recv;
int msgid = 0;

void *SendThread(void *arg)
{
	struct msgbuf sendmsg;
	int ret = 0;

	while (1)
	{
		memset(&sendmsg, 0, sizeof(sendmsg));
		sendmsg.mtype = 100;
		gets(sendmsg.mtext);
		ret = msgsnd(msgid, &sendmsg, sizeof(sendmsg) - sizeof(long), 0);
		if (-1 == ret)
		{
			perror("fail to msgsnd");
			return NULL;
		}
		if (!strcmp(sendmsg.mtext, ".quit"))
		{
			break;
		}
	}
	pthread_cancel(tid_recv);

	return NULL;
}

void *RecvThread(void *arg)
{
	struct msgbuf recvmsg;
	ssize_t nsize = 0;

	while (1)
	{
		memset(&recvmsg, 0, sizeof(recvmsg));
		nsize = msgrcv(msgid, &recvmsg, sizeof(recvmsg) - sizeof(long), 200, 0);
		if (-1 == nsize)
		{
			perror("fail to msgrcv");
			return NULL;
		}

		if (!strcmp(recvmsg.mtext, ".quit"))
		{
			break;
		}

		printf("RECV:%s\n", recvmsg.mtext);
	}
	
	pthread_cancel(tid_send);

	return NULL;
}

int main(void)
{
	key_t key;

	key = ftok(".", 'a');
	if (-1 == key)
	{
		perror("fail to ftok");
		return -1;
	}

	msgid = msgget(key, IPC_CREAT | 0664);
	if (-1 == msgid)
	{
		perror("fail to msgget");
		return -1;
	}

	pthread_create(&tid_send, NULL, SendThread, NULL);
	pthread_create(&tid_recv, NULL, RecvThread, NULL);

	pthread_join(tid_send, NULL);
	pthread_join(tid_recv, NULL);
	
	msgctl(msgid, IPC_RMID, NULL);

	return 0;
}

clientB

#include "head.h"

pthread_t tid_send;
pthread_t tid_recv;
int msgid = 0;

void *SendThread(void *arg)
{
	struct msgbuf sendmsg;
	int ret = 0;

	while (1)
	{
		memset(&sendmsg, 0, sizeof(sendmsg));
		sendmsg.mtype = 200;
		gets(sendmsg.mtext);
		ret = msgsnd(msgid, &sendmsg, sizeof(sendmsg) - sizeof(long), 0);
		if (-1 == ret)
		{
			perror("fail to msgsnd");
			return NULL;
		}
		if (!strcmp(sendmsg.mtext, ".quit"))
		{
			break;
		}
	}
	pthread_cancel(tid_recv);

	return NULL;
}

void *RecvThread(void *arg)
{
	struct msgbuf recvmsg;
	ssize_t nsize = 0;

	while (1)
	{
		memset(&recvmsg, 0, sizeof(recvmsg));
		nsize = msgrcv(msgid, &recvmsg, sizeof(recvmsg) - sizeof(long), 100, 0);
		if (-1 == nsize)
		{
			perror("fail to msgrcv");
			return NULL;
		}

		if (!strcmp(recvmsg.mtext, ".quit"))
		{
			break;
		}

		printf("RECV:%s\n", recvmsg.mtext);
	}
	
	pthread_cancel(tid_send);

	return NULL;
}

int main(void)
{
	key_t key;

	key = ftok(".", 'a');
	if (-1 == key)
	{
		perror("fail to ftok");
		return -1;
	}

	msgid = msgget(key, IPC_CREAT | 0664);
	if (-1 == msgid)
	{
		perror("fail to msgget");
		return -1;
	}

	pthread_create(&tid_send, NULL, SendThread, NULL);
	pthread_create(&tid_recv, NULL, RecvThread, NULL);

	pthread_join(tid_send, NULL);
	pthread_join(tid_recv, NULL);
	
	msgctl(msgid, IPC_RMID, NULL);

	return 0;
}

2.共享内存:


          它是进程间通信最高效的形式

    1.操作方式:


        创建共享内存 -> 映射到共享内存中 -> 共享内存操作 -> 解除映射 -> 删除共享内存 

    2.函数接口:


        1.ftok 


        
        2.shmget 


        3.shmat

        4.shmdt 

        5.shmctl

3.信号灯(有名信号量) 

1.创建

    2.销毁 

    3.申请信号量


    4.释放信号量

 

#include "head.h"

union semun {
	int              val;    /* Value for SETVAL */
	struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
	unsigned short  *array;  /* Array for GETALL, SETALL */
	struct seminfo  *__buf;  /* Buffer for IPC_INFO
								(Linux-specific) */
};


int main(void)
{
	key_t key;
	int semid = 0;
	union semun myun;
	struct sembuf mybuf;
	int ret = 0;

	key = ftok(".", 'a');
	if (-1 == key)
	{
		perror("fail to ftok");
		return -1;
	}

	semid = semget(key, 2, IPC_CREAT | 0664);
	if (-1 == semid)
	{
		perror("fail to semget");
		return -1;
	}
	
	/* 对信号灯中的0号信号量初始化为0 */
	myun.val = 0;
	semctl(semid, 0, SETVAL, myun);

	/* 对信号灯中的1号信号量初始化为1 */
	myun.val = 1;
	semctl(semid, 1, SETVAL, myun);

	/* 申请1号信号量 */
	mybuf.sem_num = 1;
	mybuf.sem_op = -1;
	mybuf.sem_flg = SEM_UNDO;
	ret = semop(semid, &mybuf, 1);
	if (-1 == ret)
	{
		perror("fail to semop");
		return -1;
	}

	printf("申请到写信号量!\n");
	
	/* 释放0号信号量 */
	mybuf.sem_num = 0;
	mybuf.sem_op = +1;
	mybuf.sem_flg = SEM_UNDO;
	ret = semop(semid, &mybuf, 1);
	if (-1 == ret)
	{
		perror("fail to semop");
		return -1;
	}
	printf("释放了读信号量!\n");

	/* 申请0号信号量 */
	mybuf.sem_num = 0;
	mybuf.sem_op = -1;
	mybuf.sem_flg = SEM_UNDO;
	ret = semop(semid, &mybuf, 1);
	if (-1 == ret)
	{
		perror("fail to semop");
		return -1;
	}
	printf("申请了读信号量!\n");
	
	

	semctl(semid, 0, IPC_RMID);

	return 0;
}

练习:使用共享内存和信号量实现同步通信

#ifndef __HEAD_H__
#define __HEAD_H__

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>

union semun {
	int              val;    /* Value for SETVAL */
	struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
	unsigned short  *array;  /* Array for GETALL, SETALL */
	struct seminfo  *__buf;  /* Buffer for IPC_INFO
								(Linux-specific) */
};

extern int init_sem(int semid, int *parray, int len);
extern int sem_p(int semid, int num);
extern int sem_v(int semid, int num);

#endif

发送

#include "head.h"

int main(void)
{
	key_t key;
	int shmid = 0;
	int semid = 0;
	char *pshmaddr = NULL;
	int val[2] = {0, 1};

	key = ftok(".", 'a');
	if (-1 == key)
	{
		perror("fail to ftok");
		return -1;
	}

	semid = semget(key, 2, IPC_CREAT | 0664);
	if (-1 == semid)
	{
		perror("fail to semget");
		return -1;
	}

	init_sem(semid, val, 2);

	shmid = shmget(key, 4096, IPC_CREAT | 0664);
	if (-1 == shmid)
	{
		perror("fail to shmget");
		return -1;
	}

	pshmaddr = shmat(shmid, NULL, 0);
	if (NULL == pshmaddr)
	{
		perror("fail to shmat");
		return -1;
	}

	while (1)
	{
		sem_p(semid, 1);
		gets(pshmaddr);
		sem_v(semid, 0);
		if (!strcmp(pshmaddr, ".quit"))
		{
			break;
		}
	}

	shmdt(pshmaddr);

	shmctl(shmid, IPC_RMID, NULL);

	return 0;
}

接收

#include "head.h"

int main(void)
{
	key_t key;
	int shmid = 0;
	int semid = 0;
	char *pshmaddr = NULL;
	int val[2] = {0, 1};

	key = ftok(".", 'a');
	if (-1 == key)
	{
		perror("fail to ftok");
		return -1;
	}

	semid = semget(key, 2, IPC_CREAT | 0664);
	if (-1 == semid)
	{
		perror("fail to semget");
		return -1;
	}

	init_sem(semid, val, 2);

	shmid = shmget(key, 4096, IPC_CREAT | 0664);
	if (-1 == shmid)
	{
		perror("fail to shmget");
		return -1;
	}

	pshmaddr = shmat(shmid, NULL, 0);
	if (NULL == pshmaddr)
	{
		perror("fail to shmat");
		return -1;
	}

	while (1)
	{
		sem_p(semid, 0);
		printf("SHMADDR:%s\n", pshmaddr);
		if (!strcmp(pshmaddr, ".quit"))
		{
			break;
		}
		sem_v(semid, 1);
	}

	shmdt(pshmaddr);

	shmctl(shmid, IPC_RMID, NULL);

	return 0;
}

信号量控制

#include "head.h"

int init_sem(int semid, int *parray, int len)
{
	union semun myun;
	int i = 0;
	int ret = 0;

	for (i = 0; i < len; i++)
	{
		myun.val = parray[i];
		ret = semctl(semid, i, SETVAL, myun);
		if (-1 == ret)
		{
			perror("fail to semctl");
			return -1;
		}
	}

	return 0;
}

int sem_p(int semid, int num)
{
	int ret = 0;
	struct sembuf mybuf;

	mybuf.sem_num = num;
	mybuf.sem_op = -1;
	mybuf.sem_flg = SEM_UNDO;

	ret = semop(semid, &mybuf, 1);
	if (-1 == ret)
	{
		perror("fail to semop");
		return -1;
	}

	return 0;
}

int sem_v(int semid, int num)
{
	int ret = 0;
	struct sembuf mybuf;

	mybuf.sem_num = num;
	mybuf.sem_op = +1;
	mybuf.sem_flg = SEM_UNDO;

	ret = semop(semid, &mybuf, 1);
	if (-1 == ret)
	{
		perror("fail to semop");
		return -1;
	}

	return 0;
}
03-06 01:54