Linux网络编程 - 【IPC】进程间通信

IPC:Inter-Process Commucation



目录

  • 进程信号量
    • 头文件
    • 自定义semun联合体(semun.h)
    • 封装信号量初始化操作、删除操作、P操作、V操作
    • 测试
  • 共享内存
    • 头文件(
    • 创建共享内存空间并获得该空间地址(将shm地址与当前进程关联)
    • 测试
      • 向共享内存中写入数据
      • 从共享内存中读取数据

<一> 进程信号量

1> 头文件

#include 
#include 
#include 
#include 
#include 
#include "semun.h"

2> 自定义semun联合体(semun.h)

#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)    /* union semun is defined by including  */
#else    /* according to X/OPEN we have to define it ourselves */   
union semun {        
   int val;                     /* value for SETVAL */      
   struct semid_ds *buf;        /* buffer for IPC_STAT, IPC_SET */   
   unsigned short int *array;   /* array for GETALL, SETALL */     
   struct seminfo *__buf;       /* buffer for IPC_INFO */    
};
#endif

3> 封装信号量初始化操作、删除操作、P操作、V操作

/*
 * 为信号量组中第0个信号量设置值
 * 参数:
 * 		信号量组+信号量的值
 * */
int init_sem(int semid,int val){
	union semun myun;
	myun.val = val;
	//semid信号量组中,第一个,设置值
	if(semctl(semid,0,SETVAL,myun)==-1){
		return 0;
	}else{
		return 1;
	}
}
 
/*
 * 删除一个信号量组
 * */
int del_sem(int semid){
	union semun myun;
	if(semctl(semid,0,IPC_RMID,myun)==-1){
		return 0;
	}else{
		return 1;
	}
}
 
/*
 * P operation
 * */
int p_op(int semid){
	struct sembuf mysembuf;
	mysembuf.sem_num = 0;
	mysembuf.sem_op = -1;
	mysembuf.sem_flg = SEM_UNDO;
	if(semop(semid,&mysembuf,1)==-1){
		return 0;
	}else{
		return 1;
	}
}
 
/*
 * V operation
 * */
int v_op(int semid){
	struct sembuf mysembuf;
	mysembuf.sem_num = 0;
	mysembuf.sem_op = 1;
	mysembuf.sem_flg = SEM_UNDO;
	if(semop(semid,&mysembuf,1)==-1){
		return 0;
	}else{
		return 1;
	}
}

4> 测试

int main(){
	int semid;
	//信号量组标志+其中有几个信号量
	//返回信号量组标识符
	semid = semget((key_t)10001,1,0666|IPC_CREAT);
	//刚创建并不会立刻执行(未初始化),在此处阻塞
	printf("anchor\n");
 
	p_op(semid);
	printf("world\n");
	
	del_sem(semid);
	return 0;
}
 
 
int main(){
	int semid;
	//信号量组标志+其中有几个信号量
	//返回信号量组标识符
	semid = semget((key_t)10001,1,0666|IPC_CREAT);
	init_sem(semid,0);
	printf("hello\n");
	v_op(semid);
	return 0;
}

<二> 共享内存

Linux网络编程 - 【IPC】进程间通信_第1张图片

1> 头文件

#include 
#include 
#include 
#include 
#include 
#include 
#include "semun.h"

2> 创建共享内存空间并获得该空间地址(将shm地址与当前进程关联)

int shmid;

//创建256B共享内存空间
shmid=shmget((key_t)1002,256,0666|IPC_CREAT);

//返回指向共享内存空间的地址
void *pshm_res;
pshm_res = shmat(shmid,0,0);

//将void指针转换为char类型指针
char *pshm_res_char;
pshm_res_char = (char*)pshm_res;

3> 测试

  • 向共享内存中写入数据
#include 
#include 
#include 
#include 
#include 
#include 
#include "semun.h"

/*
 * 为信号量组中第0个信号量设置值
 * 参数:
 * 		信号量组+信号量的值
 * */
int init_sem(int semid,int val){
	union semun myun;
	myun.val = val;
	//semid信号量组中,第一个,设置值
	if(semctl(semid,0,SETVAL,myun)==-1){
		return 0;
	}else{
		return 1;
	}
}

/*
 * 删除一个信号量组
 * */
int del_sem(int semid){
	union semun myun;
	if(semctl(semid,0,IPC_RMID,myun)==-1){
		return 0;
	}else{
		return 1;
	}
}

/*
 * P operation
 * */
int p_op(int semid){
	struct sembuf mysembuf;
	mysembuf.sem_num = 0;
	mysembuf.sem_op = -1;
	mysembuf.sem_flg = SEM_UNDO;
	if(semop(semid,&mysembuf,1)==-1){
		return 0;
	}else{
		return 1;
	}
}

/*
 * V operation
 * */
int v_op(int semid){
	struct sembuf mysembuf;
	mysembuf.sem_num = 0;
	mysembuf.sem_op = 1;
	mysembuf.sem_flg = SEM_UNDO;
	if(semop(semid,&mysembuf,1)==-1){
		return 0;
	}else{
		return 1;
	}
}

int main(){
	int semid;
	int semid1;
	semid1 = semget((key_t)10001,1,0666|IPC_CREAT);
	init_sem(semid1,1);
	semid = semget((key_t)10000,1,0666|IPC_CREAT);
	init_sem(semid,0);


	int shmid;

	//创建256B共享内存空间
	shmid=shmget((key_t)1002,256,0666|IPC_CREAT);

	//返回指向共享内存空间的地址
	void *pshm_res;
	pshm_res = shmat(shmid,0,0);

	//将void指针转换为char类型指针
	char *pshm_res_char;
	pshm_res_char = (char*)pshm_res;

	char buff[256] = {'\0'};
	
	while(1){
		memset(buff,0,sizeof(buff));
		scanf("%s",buff);
		p_op(semid1);
		//向共享内存空间写数据
		strcpy(pshm_res_char,buff);
		v_op(semid);
		//退出机制
		if(strcmp(buff,"exit")==0){
			break;
		}
	}

	return 0;
}
  • 从共享内存中读取数据
#include 
#include 
#include 
#include 
#include 
#include 
#include "semun.h"

/*
 * 为信号量组中第0个信号量设置值
 * 参数:
 * 		信号量组+信号量的值
 * */
int init_sem(int semid,int val){
	union semun myun;
	myun.val = val;
	//semid信号量组中,第一个,设置值
	if(semctl(semid,0,SETVAL,myun)==-1){
		return 0;
	}else{
		return 1;
	}
}

/*
 * 删除一个信号量组
 * */
int del_sem(int semid){
	union semun myun;
	if(semctl(semid,0,IPC_RMID,myun)==-1){
		return 0;
	}else{
		return 1;
	}
}

/*
 * P operation
 * */
int p_op(int semid){
	struct sembuf mysembuf;
	mysembuf.sem_num = 0;
	mysembuf.sem_op = -1;
	mysembuf.sem_flg = SEM_UNDO;
	if(semop(semid,&mysembuf,1)==-1){
		return 0;
	}else{
		return 1;
	}
}

/*
 * V operation
 * */
int v_op(int semid){
	struct sembuf mysembuf;
	mysembuf.sem_num = 0;
	mysembuf.sem_op = 1;
	mysembuf.sem_flg = SEM_UNDO;
	if(semop(semid,&mysembuf,1)==-1){
		return 0;
	}else{
		return 1;
	}
}

int main(){
	int semid;
	int semid1;
	semid1 = semget((key_t)10001,1,0666|IPC_CREAT);
	init_sem(semid1,1);
	semid = semget((key_t)10000,1,0666|IPC_CREAT);
	init_sem(semid,0);


	int shmid;

	//创建256B共享内存空间
	shmid=shmget((key_t)1002,256,0666|IPC_CREAT);

	//返回指向共享内存空间的地址
	void *pshm_res;
	pshm_res = shmat(shmid,0,0);

	//将void指针转换为char类型指针
	char *pshm_res_char;
	pshm_res_char = (char*)pshm_res;
	
	while(1){
		p_op(semid);
		//从共享内存空间中读数据
		char buff[256] = {'\0'};
		strcpy(buff,pshm_res_char);
		//退出机制
		if(strcmp(buff,"exit")==0){
			break;
		}
		printf("Read %d char from shared memory:%s\n",strlen(buff),buff);
		v_op(semid1);
	}

	return 0;
}

 

 

你可能感兴趣的:(Linux)