Linux进程通信例子

Linux进程通信例子
1.共享内存
使用:mmap
/*-------------map_normalfile1.c-----------*/
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct{
  char name[4];
  int  age;
}people;
main(int argc, char** argv) // map a normal file as shared mem:
{
  int fd,i;
  people *p_map;
  char temp;
  
  fd=open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777);
  lseek(fd,sizeof(people)*5-1,SEEK_SET);
  write(fd,"",1);
  
  p_map = (people*) mmap( NULL,sizeof(people)*10,PROT_READ|PROT_WRITE,
        MAP_SHARED,fd,0 );
  close( fd );
  temp = 'a';
  for(i=0; i<10; i++)
  {
    temp += 1;
    memcpy( ( *(p_map+i) ).name, &temp,2 );
    ( *(p_map+i) ).age = 20+i;
  }
  printf(" initialize over \n ");
  sleep(10);
  munmap( p_map, sizeof(people)*10 );
  printf( "umap ok \n" );
}


/*-------------map_normalfile2.c-----------*/
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct
{
  char name[4];
  int  age;
}people;


void main(int argc, char** argv)  // map a normal file as shared mem:
{
  int fd,i;
  people *p_map;
  fd=open( argv[1],O_CREAT|O_RDWR,00777 );
  p_map = (people*)mmap(NULL,sizeof(people)*10,PROT_READ|PROT_WRITE,
       MAP_SHARED,fd,0);
  for(i = 0;i<10;i++)
  {
  printf( "name: %s age %d;\n",(*(p_map+i)).name, (*(p_map+i)).age );
  }
  munmap( p_map,sizeof(people)*10 );
}


3.管道
mkfifo


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
	int fd;
	char buf[20] = "hello world!!!/n";
	if((mkfifo("my_fifo", O_CREAT|O_RDWR|0666)) < 0)
	{
		perror("mkfifo");
		exit(1);
	}
	if((fd = open("my_fifo" , O_WRONLY)) < 0)
	{
		perror("open");
		exit(1);
	}
	if((write(fd,buf,strlen(buf)-1)) < 0)
	{
		perror("write");
		exit(1);
	}
	return 0;
			
}




#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
	int fd;
	char buf[20] = "";
	if((fd = open("my_fifo" , O_RDONLY)) < 0)
	{
		perror("open");
		exit(1);
	}
	if((read(fd,buf,20)) < 0)
	{
		perror("read");
		exit(1);
	}
	printf("%s" , buf);
	return 0;
			
}




Makefile
overpass:main.o 
	g++ -g -o  overpass main.o -L. -lhiredis -lpthread -lcurl -lrt -lcrypto -lidn -lssl
main.o:main.cpp
	g++ -g -c main.cpp -o main.o
clean:
	rm -f *.o overpass



4.信号量
#include <stdio.h>
#include <linux/sem.h>
#define MAXNUMS 15




int get_sem_val(int sid,int semnum)//取得当前信号量
{
    return(semctl(sid,semnum,GETVAL,0));
}


int main(void)
{
    int sem_id;
    int pid;
    int rc;
    struct sembuf sem_op;//信号集结构
    union semun sem_val;//信号量数值
    
    
    //建立信号量集,其中只有一个信号量
    sem_id=semget(IPC_PRIVATE,1,IPC_CREAT|0600);//IPC_PRIVATE私有,只有本用户使用,如果为正整数,则为公共的;1为信号集的数量;
    if (sem_id==-1){
        printf("create sem error!\n");
        exit(1);    
    }
    printf("create %d sem success!\n",sem_id);    
    //信号量初始化
    sem_val.val=1;
    rc=semctl(sem_id,0,SETVAL,sem_val);//设置信号量,0为第一个信号量,1为第二个信号量,...以此类推;SETVAL表示设置
    if (rc==-1){
        printf("initlize sem error!\n");
        exit(1);    
    }
    //创建进程
    pid=fork();
    if (pid==-1){
        printf("fork error!\n");
        exit(1);           
    }
    else if(pid==0){//一个子进程,消费者
        for (int i=0;i<MAXNUMS;i++){
            sem_op.sem_num=0;
            sem_op.sem_op=-1;
            sem_op.sem_flg=0;
            semop(sem_id,&sem_op,1);//操作信号量,每次-1                
            printf("%d 消费者: %d\n",i,get_sem_val(sem_id,0));
        }     
    }
    else{//父进程,生产者
        for (int i=0;i<MAXNUMS;i++){
            sem_op.sem_num=0;
            sem_op.sem_op=1;
            sem_op.sem_flg=0;
            semop(sem_id,&sem_op,1);//操作信号量,每次+1                 
            printf("%d 生产者: %d\n",i,get_sem_val(sem_id,0));
            
        }     
    }
    exit(0);
}




Makefile
overpass:main.o 
	g++ -g -o  overpass main.o -L. -lhiredis -lpthread -lcurl -lrt -lcrypto -lidn -lssl
main.o:main.cpp
	g++ -g -c main.cpp -o main.o
clean:
	rm -f *.o overpass




一般是用管道即可
更多参考:http://www.cnblogs.com/linshui91/archive/2010/09/29/1838770.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值