Linux shared memory and nginx shared memory

The implementation of shared memory is divided into two steps:

1. Create shared memory and use the shmget function.

Second, map the shared memory, map the created shared memory to the specific process space, and use the shmat function.

Create shared memory

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

key identifies the key value of shared memory: 0/IPC_PRIVATE. When the value of key is IPC_PRIVATE, the function shmget will create a new shared memory; if the value of key is 0 and the IPC_PRIVATE flag is set in the parameter, a new shared memory will also be created.

Return Value: Returns the shared memory indicator on success, -1 on failure.

map shared memory

int shmat(int shmid,char *shmaddr,int flag)

parameter:

shmid: Shared storage identifier returned by the shmget function

flag: decides how to determine the mapped address (usually 0)

return value:

Returns the address in the process where the shared memory is mapped on success, or -1 on failure.

Shared memory unmap

When a process no longer needs shared memory, it needs to be removed from the process address space.

int shmdt(char *shmaddr)

Examples of contributed memory are as follows:

Experiment requirements: Create two processes, create a shared memory in the A process, write data to it, and read data from the shared memory through the B process.

chm_com.h function

#define TEXT_SZ 2048   
  
struct shared_use_st  
{  
    int written_by_you;  
    char some_text[TEXT_SZ];  
};  
#define TEXT_SZ 2048  
  
struct shared_use_st  
{  
    int written_by_you;  
    char some_text[TEXT_SZ];  
};  

Read process:

/**********************************************************
*Experimental requirements: Create two processes to communicate through shared memory.
*Function description: This program applies for and allocates shared memory, and then rotates and reads the data in the shared memory until
* Read "end".
* Date: 2010-9-17
*Author: Guo Chi
**********************************************************/  
#include <unistd.h>   
#include <stdlib.h>   
#include <stdio.h>   
#include <string.h>   
#include <sys/types.h>   
#include <sys/ipc.h>   
#include <sys/shm.h>   
#include "shm_com.h"   
  
/*
 * Program entry
 * */  
int main(void)  
{  
    int running=1;  
    void *shared_memory=(void *)0;  
    struct shared_use_st *shared_stuff;  
    int shmid;  
    /* create shared memory */  
    shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);  
    if(shmid==-1)  
    {  
        fprintf(stderr,"shmget failed\n");  
        exit(EXIT_FAILURE);  
    }  
  
    /* map shared memory */  
    shared_memory=shmat(shmid,(void *)0,0);  
    if(shared_memory==(void *)-1)  
    {  
        fprintf(stderr,"shmat failed\n");  
        exit(EXIT_FAILURE);  
    }  
    printf("Memory attached at %X\n",(int)shared_memory);  
  
    /*Let the struct pointer point to this shared memory*/  
    shared_stuff=(struct shared_use_st *)shared_memory;  
  
    /* Control read and write order */  
    shared_stuff->written_by_you=0;  
    /* Read data from shared memory in a loop until "end" is read */  
    while(running)  
    {  
       if(shared_stuff->written_by_you)  
       {  
           printf("You wrote:%s",shared_stuff->some_text);  
           sleep(1); //The reading process sleeps for one second, and at the same time will cause the writing process to sleep for one second, and then write after reading   
           shared_stuff->written_by_you=0;  
           if(strncmp(shared_stuff->some_text,"end",3)==0)  
           {  
               running=0; //end the loop   
           }  
       }  
    }  
    /* delete shared memory */  
    if(shmdt(shared_memory)==-1)  
    {  
        fprintf(stderr,"shmdt failed\n");  
        exit(EXIT_FAILURE);  
    }  
       exit(EXIT_SUCCESS);  
}  
/**********************************************************
*Experimental requirements: Create two processes to communicate through shared memory.
*Function description: This program applies for and allocates shared memory, and then rotates and reads the data in the shared memory until
* Read "end".
* Date: 2010-9-17
*Author: Guo Chi
**********************************************************/  
#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/ipc.h>  
#include <sys/shm.h>  
#include "shm_com.h"  
  
/*
 * Program entry
 * */  
int main(void)  
{  
    int running=1;  
    void *shared_memory=(void *)0;  
    struct shared_use_st *shared_stuff;  
    int shmid;  
    /* create shared memory */  
    shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);  
    if(shmid==-1)  
    {  
        fprintf(stderr,"shmget failed\n");  
        exit(EXIT_FAILURE);  
    }  
  
    /* map shared memory */  
    shared_memory=shmat(shmid,(void *)0,0);  
    if(shared_memory==(void *)-1)  
    {  
        fprintf(stderr,"shmat failed\n");  
        exit(EXIT_FAILURE);  
    }  
    printf("Memory attached at %X\n",(int)shared_memory);  
  
    /*Let the struct pointer point to this shared memory*/  
    shared_stuff=(struct shared_use_st *)shared_memory;  
  
    /* Control read and write order */  
    shared_stuff->written_by_you=0;  
    /* Read data from shared memory in a loop until "end" is read */  
    while(running)  
    {  
       if(shared_stuff->written_by_you)  
       {  
           printf("You wrote:%s",shared_stuff->some_text);  
           sleep(1); //The reading process sleeps for one second, and at the same time will cause the writing process to sleep for one second, and then write after reading  
           shared_stuff->written_by_you=0;  
           if(strncmp(shared_stuff->some_text,"end",3)==0)  
           {  
               running=0; //end the loop  
           }  
       }  
    }  
    /* delete shared memory */  
    if(shmdt(shared_memory)==-1)  
    {  
        fprintf(stderr,"shmdt failed\n");  
        exit(EXIT_FAILURE);  
    }  
       exit(EXIT_SUCCESS);  
}  

Write process:

/**********************************************************
*Experimental requirements: Create two processes to communicate through shared memory.
*Function description: This program applies for the same shared memory block as the previous program, and then loops to the shared memory block.
* Write data until "end" is written.
* Date: 2010-9-17
*Author: Guo Chi
**********************************************************/  
#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/ipc.h>  
#include <sys/shm.h>  
#include "shm_com.h"  
  
/*
 * Program entry
 * */  
int main(void)  
{  
    int running=1;  
    void *shared_memory=(void *)0;  
    struct shared_use_st *shared_stuff;  
    char buffer[BUFSIZ];  
    int shmid;  
    /* create shared memory */  
    shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);  
    if(shmid==-1)  
    {  
        fprintf(stderr,"shmget failed\n");  
        exit(EXIT_FAILURE);  
    }  
  
    /* map shared memory */  
    shared_memory=shmat(shmid,(void *)0,0);  
    if(shared_memory==(void *)-1)  
    {  
        fprintf(stderr,"shmat failed\n");  
        exit(EXIT_FAILURE);  
    }  
    printf("Memory attached at %X\n",(int)shared_memory);  
  
    /*Let the struct pointer point to this shared memory*/  
    shared_stuff=(struct shared_use_st *)shared_memory;  
    /*Write data to the shared memory cyclically until the write is "end"*/  
    while(running)  
    {  
        while(shared_stuff->written_by_you==1)  
        {  
            sleep(1);//Wait until the reading process finishes reading before writing  
            printf("waiting for client...\n");  
        }  
        printf("Ener some text:");  
        fgets(buffer,BUFSIZ,stdin);  
        strncpy(shared_stuff->some_text,buffer,TEXT_SZ);  
        shared_stuff->written_by_you=1;  
        if(strncmp(buffer,"end",3)==0)  
        {  
            running=0; //end the loop  
        }  
    }  
    /* delete shared memory */  
    if(shmdt(shared_memory)==-1)  
    {  
        fprintf(stderr,"shmdt failed\n");  
        exit(EXIT_FAILURE);  
    }  
    exit(EXIT_SUCCESS);  
}  
3. Run in a terminal

/**********************************************************
*Experimental requirements: Create two processes to communicate through shared memory.
*Function description: This program applies for the same shared memory block as the previous program, and then loops to the shared memory block.
* Write data until "end" is written.
* Date: 2010-9-17
*Author: Guo Chi
**********************************************************/  
#include <unistd.h>   
#include <stdlib.h>   
#include <stdio.h>   
#include <string.h>   
#include <sys/types.h>   
#include <sys/ipc.h>   
#include <sys/shm.h>   
#include "shm_com.h"   
  
/*
 * Program entry
 * */  
int main(void)  
{  
    int running=1;  
    void *shared_memory=(void *)0;  
    struct shared_use_st *shared_stuff;  
    char buffer[BUFSIZ];  
    int shmid;  
    /* create shared memory */  
    shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);  
    if(shmid==-1)  
    {  
        fprintf(stderr,"shmget failed\n");  
        exit(EXIT_FAILURE);  
    }  
  
    /* map shared memory */  
    shared_memory=shmat(shmid,(void *)0,0);  
    if(shared_memory==(void *)-1)  
    {  
        fprintf(stderr,"shmat failed\n");  
        exit(EXIT_FAILURE);  
    }  
    printf("Memory attached at %X\n",(int)shared_memory);  
  
    /*Let the struct pointer point to this shared memory*/  
    shared_stuff=(struct shared_use_st *)shared_memory;  
    /*Write data to the shared memory cyclically until the write is "end"*/  
    while(running)  
    {  
        while(shared_stuff->written_by_you==1)  
        {  
            sleep(1);//Wait until the reading process finishes reading before writing   
            printf("waiting for client...\n");  
        }  
        printf("Ener some text:");  
        fgets(buffer,BUFSIZ,stdin);  
        strncpy(shared_stuff->some_text,buffer,TEXT_SZ);  
        shared_stuff->written_by_you=1;  
        if(strncmp(buffer,"end",3)==0)  
        {  
            running=0; //end the loop   
        }  
    }  
    /* delete shared memory */  
    if(shmdt(shared_memory)==-1)  
    {  
        fprintf(stderr,"shmdt failed\n");  
        exit(EXIT_FAILURE);  
    }  
    exit(EXIT_SUCCESS);  
}  

3. Run shm1 in one terminal and shm2 in another terminal. When shm1 runs, it will be in a waiting state because there is no data to read in the shared memory

[root@localhost 2-4-4]# ./shm1

Memory attached at B7F9A000

/***block***/

Then enter the string into the terminal where shm2 is running

[root@localhost 2-4-4]# ./shm2

Memory attached at B7FD8000

Enter some text:Impossible is nothing

waiting for client。。。

waiting for client。。。

Enter some text:Anything is possible

waiting for client。。。

Ener some text:end

[root@localhost 2-4-4]#

shm1 was able to read them one by one from the shared memory, and both programs exited until both sides fainted the string "end".

[root@localhost 2-4-4]# ./shm1

Memory attached at B7F9A000

You write:Impossible is nothing

You write:Anything is possible

You write:end

[root@localhost 2-4-4]#

In the above running process, red indicates the result of running in terminal 1, and blue indicates the result of running in terminal 2.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324775342&siteId=291194637