Shared Memory application example

Source: Internet
Author: User

1. Example of parent-child process communication. The source code of SHM. C is as follows:

# 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;

}

Compile gcc shm. C-o SHM.

Run./SHM. The execution result is as follows:

Shm_segsz = 1024 bytes

Shm_cpid = 9503

Shm_lpid = 1, 9504

Hi, I am child process!

2. multi-process read/write example

Multi-process read/write refers to one process write shared memory, and one or more processes read shared memory. The following example implements a process write shared memory and a process read shared memory.

(1) The following program creates shared memory and writes messages.

The source code of shmwrite. C is as follows:

# 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) The following program reads messages from the shared memory.

The source code of shmread. C is as follows:

# 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) Compilation and execution

① Compile GCC shmwrite. C-o shmwrite.

② Execute./shmwrite. The execution result is as follows:

Key = 1, 50453281

Shm_id = 688137

③ Compile GCC shmread. C-o shmread.

④ Run./shmread. The execution result is as follows:

Key = 1, 50453281

Shm_id = 688137

Name: test1

Age 0

Name: Test2

Age 1

Name: test3

Age 2

⑤ Run./shmwrite again. The execution result is as follows:

Key = 1, 50453281

Shmget error: file exists

⑥ Use ipcrm-M 688137 to delete the shared memory.

Shared Memory application example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.