自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux系統(tǒng)編程—共享內(nèi)存之mmap

系統(tǒng) Linux
共享內(nèi)存是通信效率最高的IPC方式,因?yàn)檫M(jìn)程可以直接讀寫內(nèi)存,而無需進(jìn)行數(shù)據(jù)的拷備。但是它沒有自帶同步機(jī)制,需要配合信號(hào)量等方式來進(jìn)行同步。

共享內(nèi)存概念

共享內(nèi)存是通信效率最高的IPC方式,因?yàn)檫M(jìn)程可以直接讀寫內(nèi)存,而無需進(jìn)行數(shù)據(jù)的拷備。但是它沒有自帶同步機(jī)制,需要配合信號(hào)量等方式來進(jìn)行同步。

[[345034]]

共享內(nèi)存被創(chuàng)建以后,同一塊物理內(nèi)存被映射到了多個(gè)進(jìn)程地址空間,當(dāng)有一個(gè)進(jìn)程修改了共享內(nèi)存的數(shù)據(jù),其余的進(jìn)程均可看見所修改的內(nèi)容,反之亦然。

mmap函數(shù)

函數(shù)原型:

  1. void mmap(void adrr, size_t length, int prot, int flags, int fd, off_t offset); 

返回值:

  • 成功:返回創(chuàng)建的映射區(qū)首地址;
  • 失?。悍祷豈AP_FAILED

具體參數(shù)含義:

  • addr:指向映射區(qū)的首地址,這是由系統(tǒng)內(nèi)核所決定的,一般設(shè)為NULL;
  • length:欲創(chuàng)建的映射區(qū)大小;
  • prot:映射區(qū)的權(quán)限,一般有如下幾種:
  • PROT_EXEC 映射區(qū)域可被執(zhí)行
  • PROT_READ 映射區(qū)域可被讀取
  • PROT_WRITE 映射區(qū)域可被寫入
  • PROT_NONE 映射區(qū)域不能存取
  • flags:指映射區(qū)的標(biāo)志位,MAP_FIXED與MAP_PRIVATE必須選擇一個(gè):
  • MAP_FIXED:對(duì)映射區(qū)所作的修改會(huì)反映到物理設(shè)備,但需要調(diào)用msync()或者munmap();
  • MAP_PRIVATE:對(duì)映射區(qū)所作的修改不會(huì)反映到物理設(shè)備。
  • fd:創(chuàng)建的映射區(qū)的文件描述符;
  • offset:被映射文件的偏移量,一般設(shè)為0,表示從頭開始映射。

mumap函數(shù)

函數(shù)原型:

  1. int munmap(void *addr, size_t length); 

函數(shù)作用:

如同malloc之后需要free一樣,mmap調(diào)用創(chuàng)建的映射區(qū)使用完畢之后,需要調(diào)用munmap去釋放。

例程

寫進(jìn)程:

  1.  #include <stdio.h> 
  2.  #include <sys/mman.h> 
  3.  #include <sys/types.h> 
  4.  #include <sys/stat.h> 
  5.  #include <fcntl.h> 
  6.  #include <unistd.h> 
  7.  #include <string.h> 
  8.   
  9.  typedef struct 
  10. {11    int id; 
  11.     char name[20]; 
  12.     char gender; 
  13. }stu; 
  14.  
  15. int main(int argc, char *argv[]) 
  16.     stu *p = NULL
  17.     int fd = 0
  18.     stu student = {10, "harry", 'm'}; 
  19.  
  20.    if (argc < 2) { 
  21.         printf("useage: ./a.out file\n"); 
  22.         return -1; 
  23.     } 
  24.  
  25.     fd = open(argv[1], O_RDWR | O_CREAT, 0664); 
  26.     if (fd == -1) { 
  27.         printf("ERROR: open failed!\n"); 
  28.         return -1; 
  29.     } 
  30.     ftruncate(fd, sizeof(stu)); 
  31.  
  32.     p = mmap(NULL, sizeof(stu), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 
  33.     if (p == MAP_FAILED) { 
  34.         printf("ERROR: mmap failed!\n"); 
  35.         return -1; 
  36.     } 
  37.  
  38.     close(fd); 
  39.  
  40.     while (1) { 
  41.         memcpy(p, &student, sizeof(stu)); 
  42.         student.id++; 
  43.         sleep(2); 
  44.     } 
  45.     munmap(p, sizeof(stu)); 
  46.  
  47.     return 0; 

讀進(jìn)程:

  1.  #include <stdio.h> 
  2.  #include <sys/mman.h> 
  3.  #include <sys/types.h> 
  4.  #include <sys/stat.h> 
  5.  #include <fcntl.h> 
  6.  #include <unistd.h> 
  7.   
  8.  typedef struct 
  9.  { 
  10.     int id; 
  11.     char name[20]; 
  12.     char gender; 
  13. }stu; 
  14.  
  15. int main(int argc, char *argv[]) 
  16.     stu *p = NULL
  17.     int fd = 0
  18.  
  19.     if (argc < 2) { 
  20.         printf("useage: ./a.out file\n"); 
  21.         return -1; 
  22.     } 
  23.  
  24.     fd = open(argv[1], O_RDONLY); 
  25.     if (fd == -1) { 
  26.         printf("ERROR: open failed!\n"); 
  27.         return -1; 
  28.     } 
  29.  
  30.     p = mmap(NULL, sizeof(stu), PROT_READ, MAP_SHARED, fd, 0); 
  31.     if (p == MAP_FAILED) { 
  32.         printf("ERROR: mmap failed!\n"); 
  33.         return -1; 
  34.     } 
  35.  
  36.     close(fd); 
  37.  
  38.     while (1) { 
  39.         printf("id = %d, name = %s, gender = %c\n", p->id, p->name, p->gender); 
  40.         sleep(2); 
  41.     } 
  42.  
  43.     munmap(p, sizeof(stu)); 
  44.  
  45.     return 0; 

本文授權(quán)轉(zhuǎn)載自公眾號(hào)「良許Linux」。良許,世界500強(qiáng)外企Linux開發(fā)工程師,公眾號(hào)里分享大量Linux干貨,歡迎關(guān)注!

 

責(zé)任編輯:趙寧寧 來源: 今日頭條
相關(guān)推薦

2020-10-05 22:01:02

Linux系統(tǒng)編程線程屬性

2020-09-26 21:43:59

Linux系統(tǒng)編程條件變量

2020-10-10 07:18:14

Linux系統(tǒng)編程管道

2020-10-18 07:13:44

Linux系統(tǒng)編程信號(hào)捕捉

2020-09-25 07:34:40

Linux系統(tǒng)編程信號(hào)量

2020-10-05 22:05:10

Linux系統(tǒng)編程時(shí)序競(jìng)態(tài)

2020-09-28 06:49:50

Linux系統(tǒng)編程互斥量mutex

2020-09-26 23:09:00

Linux系統(tǒng)編程讀寫鎖

2020-09-22 07:35:06

Linux線程進(jìn)程

2021-03-04 20:11:09

Linux內(nèi)存編程

2020-10-08 10:10:51

Linux系統(tǒng)編程信號(hào)集

2017-02-28 18:26:09

Linuxinput子系統(tǒng)編程

2009-12-14 17:13:04

Linux系統(tǒng)修改共享

2010-03-05 13:34:54

2019-03-15 09:30:09

Linux系統(tǒng)CPU

2009-07-03 11:57:18

系統(tǒng)編程安全linux

2010-02-02 13:26:53

Linux內(nèi)核

2009-10-23 16:35:44

linux Debia

2009-12-23 16:16:57

Linux操作系統(tǒng)

2017-08-14 13:35:36

Linux共享內(nèi)存tmpfs文件系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)