共享內存函數(shmget、shmat、shmdt、shmctl)及其範例

 

共享內存函數由shmget、shmat、shmdt、shmctl四個函數組成。下面的表格列出了這四個函數的函數原型及其具體說明。

1.   shmget函數原型

shmget(得到一個共享內存標識符或創建一個共享內存對象)

所需頭文件

#include <sys/ipc.h>

#include <sys/shm.h>

函數說明

得到一個共享內存標識符或創建一個共享內存對象並返回共享內存標識符

函數原型

int shmget(key_t key, size_t size, int shmflg)

函數傳入值

key

0(IPC_PRIVATE):會建立新共享內存對象

大於0的32位整數:視參數shmflg來確定操作。通常要求此值來源於ftok返回的IPC鍵值

size

大於0的整數:新建的共享內存大小,以字節爲單位

0:只獲取共享內存時指定爲0

shmflg

0:取共享內存標識符,若不存在則函數會報錯

IPC_CREAT:當shmflg&IPC_CREAT爲真時,如果內核中不存在鍵值與key相等的共享內存,則新建一個共享內存;如果存在這樣的共享內存,返回此共享內存的標識符

IPC_CREAT|IPC_EXCL:如果內核中不存在鍵值與key相等的共享內存,則新建一個消息隊列;如果存在這樣的共享內存則報錯

函數返回值

成功:返回共享內存的標識符

出錯:-1,錯誤原因存於error中

附加說明

上述shmflg參數爲模式標誌參數,使用時需要與IPC對象存取權限(如0600)進行|運算來確定信號量集的存取權限

錯誤代碼

EINVAL:參數size小於SHMMIN或大於SHMMAX

EEXIST:預建立key所指的共享內存,但已經存在

EIDRM:參數key所指的共享內存已經刪除

ENOSPC:超過了系統允許建立的共享內存的最大值(SHMALL)

ENOENT:參數key所指的共享內存不存在,而參數shmflg未設IPC_CREAT位

EACCES:沒有權限

ENOMEM:核心內存不足

在Linux環境中,對開始申請的共享內存空間進行了初始化,初始值爲0x00。

如果用shmget創建了一個新的消息隊列對象時,則shmid_ds結構成員變量的值設置如下:

        shm_lpidshm_nattachshm_atime、shm_dtime設置爲0。

        msg_ctime設置爲當前時間。

        shm_segsz設成創建共享內存的大小。

        shmflg的讀寫權限放在shm_perm.mode中。

        shm_perm結構的uid和cuid成員被設置成當前進程的有效用戶ID,gid和cuid成員被設置成當前進程的有效組ID。

2.   shmat函數原型

shmat(把共享內存區對象映射到調用進程的地址空間)

所需頭文件

#include <sys/types.h>

#include <sys/shm.h>

函數說明

連接共享內存標識符爲shmid的共享內存,連接成功後把共享內存區對象映射到調用進程的地址空間,隨後可像本地空間一樣訪問

函數原型

void *shmat(int shmid, const void *shmaddr, int shmflg)

函數傳入值

msqid

共享內存標識符

shmaddr

指定共享內存出現在進程內存地址的什麼位置,直接指定爲NULL讓內核自己決定一個合適的地址位置

shmflg

SHM_RDONLY:爲只讀模式,其他爲讀寫模式

函數返回值

成功:附加好的共享內存地址

出錯:-1,錯誤原因存於error中

附加說明

fork後子進程繼承已連接的共享內存地址。exec後該子進程與已連接的共享內存地址自動脫離(detach)。進程結束後,已連接的共享內存地址會自動脫離(detach)

錯誤代碼

EACCES:無權限以指定方式連接共享內存

EINVAL:無效的參數shmid或shmaddr

ENOMEM:核心內存不足

3.   shmdt函數原型

shmat(斷開共享內存連接)

所需頭文件

#include <sys/types.h>

#include <sys/shm.h>

函數說明

與shmat函數相反,是用來斷開與共享內存附加點的地址,禁止本進程訪問此片共享內存

函數原型

int shmdt(const void *shmaddr)

函數傳入值

shmaddr:連接的共享內存的起始地址

函數返回值

成功:0

出錯:-1,錯誤原因存於error中

附加說明

本函數調用並不刪除所指定的共享內存區,而只是將先前用shmat函數連接(attach)好的共享內存脫離(detach)目前的進程

錯誤代碼

EINVAL:無效的參數shmaddr

4.   shmctl函數原型

shmctl(共享內存管理)

所需頭文件

#include <sys/types.h>

#include <sys/shm.h>

函數說明

完成對共享內存的控制

函數原型

int shmctl(int shmid, int cmd, struct shmid_ds *buf)

函數傳入值

msqid

共享內存標識符

cmd

IPC_STAT:得到共享內存的狀態,把共享內存的shmid_ds結構複製到buf中

IPC_SET:改變共享內存的狀態,把buf所指的shmid_ds結構中的uid、gid、mode複製到共享內存的shmid_ds結構內

IPC_RMID:刪除這片共享內存

buf

共享內存管理結構體。具體說明參見共享內存內核結構定義部分

函數返回值

成功:0

出錯:-1,錯誤原因存於error中

錯誤代碼

EACCESS:參數cmd爲IPC_STAT,確無權限讀取該共享內存

EFAULT:參數buf指向無效的內存地址

EIDRM:標識符爲msqid的共享內存已被刪除

EINVAL:無效的參數cmd或shmid

EPERM:參數cmd爲IPC_SET或IPC_RMID,卻無足夠的權限執行

 

共享內存應用範例

5.   父子進程通信範例

父子進程通信範例,shm.c源代碼如下:

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <error.h>

#define SIZE 1024

int main()

{

    int shmid ;

    char *shmaddr ;

    struct shmid_ds buf ;

    int flag = 0 ;

    int pid ;

 

    shmid = shmget(IPC_PRIVATE, SIZE, IPC_CREAT|0600 ) ;

    if ( shmid < 0 )

    {

            perror("get shm  ipc_id error") ;

            return -1 ;

    }

    pid = fork() ;

    if ( pid == 0 )

    {

        shmaddr = (char *)shmat( shmid, NULL, 0 ) ;

        if ( (int)shmaddr == -1 )

        {

            perror("shmat addr error") ;

            return -1 ;

 

        }

        strcpy( shmaddr, "Hi, I am child process!\n") ;

        shmdt( shmaddr ) ;

        return  0;

    } else if ( pid > 0) {

        sleep(3 ) ;

        flag = shmctl( shmid, IPC_STAT, &buf) ;

        if ( flag == -1 )

        {

            perror("shmctl shm error") ;

            return -1 ;

        }

 

        printf("shm_segsz =%d bytes\n", buf.shm_segsz ) ;

        printf("parent pid=%d, shm_cpid = %d \n", getpid(), buf.shm_cpid ) ;

        printf("chlid pid=%d, shm_lpid = %d \n",pid , buf.shm_lpid ) ;

        shmaddr = (char *) shmat(shmid, NULL, 0 ) ;

        if ( (int)shmaddr == -1 )

        {

            perror("shmat addr error") ;

            return -1 ;

 

        }

        printf("%s", shmaddr) ;

        shmdt( shmaddr ) ;

        shmctl(shmid, IPC_RMID, NULL) ;

    }else{

        perror("fork error") ;

        shmctl(shmid, IPC_RMID, NULL) ;

    }

 

    return 0 ;

}

編譯 gcc shm.c –o shm。

執行 ./shm,執行結果如下:

shm_segsz =1024 bytes

shm_cpid = 9503

shm_lpid = 9504

Hi, I am child process!

6.   多進程讀寫範例

多進程讀寫即一個進程寫共享內存,一個或多個進程讀共享內存。下面的例子實現的是一個進程寫共享內存,一個進程讀共享內存。

(1)下面程序實現了創建共享內存,並寫入消息。

shmwrite.c源代碼如下:

#include <stdio.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/types.h>

#include <unistd.h>

#include <string.h>

typedef struct{

    char name[8];

    int age;

} people;

int main(int argc, char** argv)

{

    int shm_id,i;

    key_t key;

    char temp[8];

    people *p_map;

    char pathname[30] ;

 

    strcpy(pathname,"/tmp") ;

    key = ftok(pathname,0x03);

    if(key==-1)

    {

        perror("ftok error");

        return -1;

    }

    printf("key=%d\n",key) ;

    shm_id=shmget(key,4096,IPC_CREAT|IPC_EXCL|0600); 

    if(shm_id==-1)

    {

        perror("shmget error");

        return -1;

    }

    printf("shm_id=%d\n", shm_id) ;

    p_map=(people*)shmat(shm_id,NULL,0);

    memset(temp, 0x00, sizeof(temp)) ;

    strcpy(temp,"test") ;

    temp[4]='0';

    for(i = 0;i<3;i++)

    {

        temp[4]+=1;

        strncpy((p_map+i)->name,temp,5);

        (p_map+i)->age=0+i;

    }

    shmdt(p_map) ;

    return 0 ;

}

(2)下面程序實現從共享內存讀消息。

shmread.c源代碼如下:

#include <stdio.h>

#include <string.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/types.h>

#include <unistd.h>

typedef struct{

    char name[8];

    int age;

} people;

int main(int argc, char** argv)

{

    int shm_id,i;

    key_t key;

    people *p_map;

    char pathname[30] ;

 

    strcpy(pathname,"/tmp") ;

    key = ftok(pathname,0x03);

    if(key == -1)

    {

        perror("ftok error");

        return -1;

    }

    printf("key=%d\n", key) ;

    shm_id = shmget(key,0, 0);   

    if(shm_id == -1)

    {

        perror("shmget error");

        return -1;

    }

    printf("shm_id=%d\n", shm_id) ;

    p_map = (people*)shmat(shm_id,NULL,0);

    for(i = 0;i<3;i++)

    {

        printf( "name:%s\n",(*(p_map+i)).name );

        printf( "age %d\n",(*(p_map+i)).age );

    }

    if(shmdt(p_map) == -1)

    {

        perror("detach error");

        return -1;

    }

    return 0 ;

}

(3)編譯與執行

①    編譯gcc shmwrite.c -o  shmwrite。

②    執行./shmwrite,執行結果如下:

key=50453281

shm_id=688137

③    編譯gcc shmread.c -o shmread。

④    執行./shmread,執行結果如下:

key=50453281

shm_id=688137

name:test1

age 0

name:test2

age 1

name:test3

age 2

⑤    再執行./shmwrite,執行結果如下:

key=50453281

shmget error: File exists

⑥    使用ipcrm -m 688137刪除此共享內存。

 

      摘錄自《深入淺出Linux工具與編程》

發佈了47 篇原創文章 · 獲贊 61 · 訪問量 44萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章