interprocess communication IPC: Message Queuing, semaphore, shared memory

Source: Internet
Author: User
Tags message queue semaphore

2015.3.4
Wednesday Cloudy

interprocess communication: IPC

File object: Record file descriptor, file switch, etc.

IPC Identifier: System global serial number
Two processes to communicate, open is the only object to communicate, through the key operation

XSI IPC: Message Queuing, semaphore, shared memory.

IPCS view IP object Shared memory, semaphore, message queue, etc.
IPCRM Deleting an IP object

Linux provides users with complete and powerful network functions.
Complete built-in network: Other operating systems do not contain such tight network parts that are combined with the kernel

There are two ways to obtain a shared memory identifier: Ftok (Pathname,id) and the other is key = Ipc_create

Create, map, read state of shared memory operations: (The following program is very short, but basically contains most of the shared memory functions of the application, the program is very good to get started)

Instance program for Message Queuing;

#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define KEY 1234
#define TEXT_SIZE 48

struct Msgbuffer
{
Long Mtype;
Char Mtext[text_size];
}MSGP;

int mian (int argc, char **argv)
{
int msqid;
Msqid = Msgget (KEY, ipc_cteat | 0600); Establish Message Queuing

if (fork () = = 0)
{
Msgp.mtype = 1;
strcpy (Msgp.mtext, "hi! I am Child process!\n ");
MSGSND (msqid,&msgp,text_size,0); Send Message to queue
return;
}

Else
{
Sleep (3); Wait for the child process to finish executing
MSGRCV (msqid,&msgp,text_size,0,0); Receive Queue information
printf ("Parent Receive Mtext:%s", msgp.mtext);
Msgctl (msqid,ipc_rmid,null): Delete Message Queuing
}
}


#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define KEY 1234
#define SIZE 1024

int main (int argc, char **argv)
{
int shmid;
Char *shmaddr;
struct Shmid_ds buf;

Shmid = Shmget (key,size,ipc_creat | 0600); Establish shared memory

if (fork () = = 0) Child process
{
SHMADDR = (char *) Shmat (shmid,null,0); Child process Mapping shared memory
strcpy (shmaddr, "hi! I AN-child process!\n "); Write data to Shared memory
SHMDT (SHMADDR); Free child process virtual memory
Return
}

{Parent Process
Sleep (3); Wait for the child process to finish executing
Shmctl (SHMID,IPC_STAT,&BUF); Get the state of shared memory
printf ("Shm_segsz =%d bytes\n", SHM_SEGSZ);
printf ("Shm_cpid =%d bytes\n", shm_cpid);
printf ("Shm_lpid =%d bytes\n", shm_lpid);

SHMADDR = (char *) Shmat (shmid,null,0); Parent process Mapping Shared memory
printf ("%s", shmaddr); displaying shared memory content
SHMDT (SHMADDR); Releasing the parent process virtual memory
Shmctl (Shmid,ipc_rmid,null); Delete Shared memory
}
}

Another program that shares memory:

#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#define BUFSZ 2048

int main (int argc, char **argv)
{
int shmid;
Char *shmaddr;

if ((Shmid = Shmget (ipc_private,bufsz,0666)) < 0); Ipc_private is the specified key, which guarantees that a new IPC can be created, and the disadvantage is that the obtained identifier is written
{The file, the user needs to enter the file to read
Perror ("Shmget");
Exit (-1);
}

printf ("# # #create shared-memory:%d\n", shmid);

System ("Ipcs-m | grep 666 "); Equivalent to executing ipcs-m

if ((shmaddr = (char *) Shmat (shmid,null,0)) < (char *) 0)
{
Perror ("Shmat");
Exit (-1);
}


printf ("# # #create shared-memory\n");

System ("Ipcs-m | grep 666 ");

if ((SHMDT (shmaddr)) < 0)
{
Perror ("Shmdt");
Exit (-1);
}

printf ("# # #create shared-memory\n");

System ("Ipcs-m | grep 666 ");

Shmctl (Shmid, Ipc_rmid, NULL); Delete a shared memory segment
printf ("# # #create shared-memory\n");

System ("Ipcs-m | grep 666 ");

return 0;

}


Create the shared memory and semaphore and initialize, create the child process, the parent process reads the standard input and writes the shared memory, the child process prints the contents of the shared memory, and the two parties are synchronized by the semaphore;

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>

#define READ 0
#define WRITE 1

Union Semun This Union needs its own definition.
{
int Val;
struct Semid_ds *buf;
unsigned short *array;
struct Seminfo *_buf;
};

int shmid; Some of the global variables defined:
int semid;
pid_t pid;

int main ()
{
key_t key;
Char *shmaddr;
Union Semun Myun;
struct SEMBUF buf;

if (key = Ftok ("./", ' a ')) = =-1) Get key value
{
Perror ("Faile to Ftok");
Exit (-1);
}

if ((Shmid = Shmget (key,64,ipc_creat|0666)) < 0) Establish shared memory and take memory ID if it exists
{
Perror ("fail to Shmget");
Exit (-1);
}

Shmaddr = (char*) shmat (shmid,null,0); Mapping virtual memory, calling fork () below, will inherit the mapping into the child process, which is a way to create the same mapping between processes

if ((Semid = Semget (key,2,ipc_creat|0666)) < 0) Create a signal set with two signals in the signal set
{
Perror ("fail to Semget");
Exit (-1);
}

Myun.val = 0; This is an initialization operation that represents the number of semaphores
Semctl (Semid,read,setval,myun);
Myun.val = 1;
Semctl (Semid,write,setval,myun);


if ((PID = fork ()) < 0) Create child process
{
Perror ("fail to fork");
Exit (-1);
}
else if (PID = = 0)
{
Buf.sem_num = READ; Number of signals 0
Buf.sem_op =-1; P operation, the semaphore is less than or equal to 0 block (sem_wait () function) 3. Start execution after 2
BUF.SEM_FLG = 0;
Semop (semid,&buf,1);
printf ("read from SHM:%s", shmaddr);
Buf.sem_num = WRITE; Number of Signals 1
Buf.sem_op = 1; The V operation (Sem_post () function) will signal 4. Set the semaphore number to 0, and the sub-process waits.
BUF.SEM_FLG = 0;
Semop (semid,&buf,1);
}
Else
{
Buf.sem_num = WRITE; Number of signals
Buf.sem_op =-1; P Operation (Sem_post () function) 1. Execution
BUF.SEM_FLG = 0;
Semop (semid,&buf,1);
printf ("Write to SHM:");
Fgets (Shmaddr,64,stdin);
Buf.sem_num = READ; Number of signals 0
Buf.sem_op = 1; V Operation (sem_wait () function) 2. Wake-up of the child process waiting program
BUF.SEM_FLG = 0;
Semop (semid,&buf,1);
}

return 0;
}

*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************

interprocess communication IPC: Message Queuing, semaphore, shared memory

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.