Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 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); }

一般情況下使用管道即可。更多內容可參考:https://www.cnblogs.com/linshui91/archive/2010/09/29/1838770.html

Copyright © Linux教程網 All Rights Reserved