IPC Process Communication

Source: Internet
Author: User
Tags sprintf strlen

These two days to learn something a little more, feel completely coping but come, have been digesting these days to learn things so 2 ,3 ,4 of the study content did not summarize in a timely manner, In this expressed deep guilt, today to learn these days to summarize the knowledge points.

The main study of IPC interprocess Communication, I understand the IPC mainly includes pipelines, shared memory, signal volume.

Review the multi-process before summing up the IPC :

First, Process creation:

1.system Function

By calling the Shell program /bin/sh –c to execute the command specified by the string , the function is internally by calling Execve ("/ Bin/sh ",..) function to implement the. After the child process is created through the system, the original and child processes run each other, with fewer associations. If the system Call succeeds, 0 is returned .

2.Fork function

A function that is very important when you fork a function in Linux, which creates a new process from an existing process. The new process is a child process, and the original process is the parent process. It differs from other functions in that it performs a return of two values at a time. Where the return value of the parent process is the process number of the child process, and the return value of the child process is 0. If there is an error, return -1. The return value can therefore be used to determine whether the parent process or the child process.

Forkthe procedure for creating a child process is: using theForkthe child process that the function gets is a replica of the parent process that inherits the address space of the process from the parent process, including the process context, process stack, memory information, open file descriptor, signal control settings, process priority, process group number, current working directory, root directory, resource limit, control terminal, The child process is unique only to its process number, resource usage, and timers. After the child process is created by this replication, both the original process and the child process are from the functionForkreturn, each continues to run down, but the original processForkreturns the value of the child process.ForkThe return value is different, in the original process,Forkreturns the child process'sPID,in the child process,Forkreturn0,ifForkreturns a negative value indicating that the child process creation failed. (vfork function)

3. exec function Family

Exec* consists of a set of functions

int execl (const char *path, const char *arg, ...)

the EXEC function family's work process is completely differentfrom fork ,fork is copying a copy of the original process, and the exec function is exec the first parameter of the specified program overwrites the existing process space (that is, the execution exec after the family function, all code behind it is not executing).

Path is the full path name that includes the execution file name

Arg is the command-line argument for the executable, multiple uses, and the last parameter must be NULL for the split note .


Second, shared memory and signal volume

The shared memory mechanism mainly uses the shmget shmat shmdt shmctl function;

Principle and implementation: The nature of shared memory under thesystem V IPC Mechanism is a special area of memory where the data that needs to be shared between processes is placed in the shared memory area, and all processes that need to access the shared zone map the shared area to the address space of the process. Such a process using shared memory can write information to that space, while another process that uses shared memory can get the information just written by using a simple memory read operation, which enables the exchange of information between two different processes, enabling interprocess communication. Shared memory allows one or more processes to communicate through memory that appears in their virtual address space at the same time, and this virtual memory page is referenced by the page table entry for each shared process, and does not require that the virtual memory of all processes have the same address. The process object's access to the shared memory is controlled by key(key) while the access is checked by key.

Shmget Create shared memory through keywords;

Shmat Return shared memory address

SHMDT disconnecting a link to shared memory

Shmctl Reclaim Shared Memory


Iii. Application of IPC process communication:Select Multi-Process Server

Header file:

#ifndef __clinet_h__

#define __clinet_h__

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/sem.h>

typedef struct TAG

{

int M_flag;

Char m_buf[1024];

}mbuf, *pmbuf;

void P (int semid);

void V (int semid);

#endif

Client-side input:

/*************************************************************************

> File name:client_in.c

> Author:comst

> Mail:[email protected]

> Created time:wed 03:04:39 PM CST

************************************************************************/

#include "Client.h"

#include <sys/stat.h>

#include <sys/types.h>

#include <sys/fcntl.h>

#define PATH "/home/comst/pipe"

#define SERVER "Server.fifo"

int main (int argc, char* argv[])//shm_key Sem_key

{

Char server_name[128]= "";

Char read_file[128], write_file[128];

Char msg[32] = "";

int Fd_r, Fd_w;

sprintf (server_name, "%s/%s", PATH, server);

int fd_server = open (server_name, o_wronly);

memset (read_file, 0, 128);

memset (write_file, 0, 128);

sprintf (Read_file, "%s/%d_r.fifo", PATH, Getpid ());

sprintf (Write_file, "%s/%d_w.fifo", PATH, Getpid ());

Mkfifo (Read_file, 0666);

Mkfifo (Write_file, 0666);

sprintf (msg, "%d\n", Getpid ());

Write (Fd_server, MSG, strlen (msg));

Fd_r = open (Read_file, o_rdonly);

Fd_w = open (Write_file, o_wronly);

key_t Shm_key, Sem_key;

int My_shm, My_sem;

Char line[1024];

Pmbuf p;

Shm_key = (key_t) atoi (argv[1]);

Sem_key = (key_t) atoi (argv[2]);

MY_SHM = Shmget (Shm_key, sizeof (MBUF), 0666| Ipc_creat);

My_sem = Semget (Sem_key, 1, 0666 | Ipc_creat);

Semctl (My_sem, 0, Setval, 1);

p = (pmbuf) shmat (MY_SHM, NULL, 0);

memset (p, 0, sizeof (MBUF));

while (memset (line, 0, 1024x768), fgets (line, 1024x768, stdin)! = NULL)

{

Write (Fd_w, line, strlen);

memset (line, 0, 1024);

Read (Fd_r, line, 1024);

while (P (My_sem), p-, M_flag = = 1)

{

V (My_sem);

Sleep (1);

}

strcpy (P->m_buf, line);

P->m_flag = 1;

V (My_sem);

}

while (P (My_sem), p-, M_flag = = 1)

{

V (My_sem);

Sleep (1);

}

strcpy (P->m_buf, "over");

P->m_flag = 1;

V (My_sem);

Sleep (3);

SHMDT (P);

Shmctl (My_shm, Ipc_rmid, NULL);

Semctl (My_sem, 0, Ipc_rmid);

}

Client-side output:

/*************************************************************************

> File name:client_out.c

> Author:comst

> Mail:[email protected]

> Created time:wed 03:04:39 PM CST

************************************************************************/

#include "Client.h"

int main (int argc, char* argv[])//shm_key Sem_key

{

key_t Shm_key, Sem_key;

int My_shm, My_sem;

Char line[1024];

Pmbuf p;

Shm_key = Atoi (argv[1]);

Sem_key = Atoi (argv[2]);

MY_SHM = Shmget (Shm_key, sizeof (MBUF), 0666);

My_sem = Semget (Sem_key, 1, 0666);

Semctl (My_sem, 0, Setval, 1);

p = (pmbuf) shmat (MY_SHM, NULL, 0);

memset (p, 0, sizeof (MBUF));

while (1)

{

while (P (My_sem), p-, M_flag = = 0)

{

V (My_sem);

Sleep (1);

}

printf ("%d:%s\n", Getpid (), P-, m_buf);

if (strcmp (P->m_buf, "over") = = = 0)

{

V (My_sem);

break;

}

P--m_flag = 0;

V (My_sem);

}

SHMDT (P);

}

function function:

/*************************************************************************

> File Name:./func.c

> Author:comst

> Mail:[email protected]

> Created time:wed 03:17:06 PM CST

************************************************************************/

#include "Client.h"

void P (int semid)

{

struct SEMBUF my_buf;

memset (&my_buf, 0, sizeof (MY_BUF));

My_buf.sem_num = 0;

My_buf.sem_op =-1;

MY_BUF.SEM_FLG = Sem_undo;

Semop (Semid, &my_buf, 1);

}

void V (int semid)

{

struct SEMBUF my_buf;

memset (&my_buf, 0, sizeof (MY_BUF));

My_buf.sem_num = 0;

My_buf.sem_op = 1;

MY_BUF.SEM_FLG = Sem_undo;

Semop (Semid, &my_buf, 1);

}

Server:

/*************************************************************************

> File name:server.c

> Author:comst

> Mail:[email protected]

> Created time:wed 04:40:58 PM CST

************************************************************************/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <fcntl.h>

#include <signal.h>

#define PATH "/home/comst/pipe"

#define NAME "Server.fifo"

void Child_handle (int sig_num)

{

printf ("A Child exit!\n");

Wait (NULL);

}

void reverse (char* str)

{

int BG, END;

char tmp;

BG = 0;

end = strlen (str)-1;

while (BG < end)

{

TMP = STR[BG];

STR[BG] = Str[end];

Str[end] = tmp;

BG + +;

End--;

}

}

void Child_main (int fd_rd, int fd_wr)

{

Char buf[1024];

while (memset (buf, 0, 1024x768), read (FD_RD, buf, 1024)! = 0)

{

Reverse (BUF);

Write (FD_WR, buf, strlen (BUF));

}

}

int main (int argc, char* argv[])

{

Signal (Child_handle);

Char file_name[128] = "";

Char client_r[128], client_w[128];

Char line[32];

int fd_read;

int client_id;

int FD_CR, FD_CW;

file* FP;

sprintf (file_name, "%s/%s", PATH, name);

Mkfifo (file_name, 0666);

Fd_read = open (file_name, o_rdonly);

Open (file_name, o_wronly);

fp = Fdopen (Fd_read, "R");

while (memset (line, 0, +), fgets (line, p, fp) = NULL)//"pid\n"

{//CR CW Pid_r.fifo Pid_w.fifo

SSCANF (line, "%d", &client_id);

printf ("Client:%d request!\n", client_id);

memset (client_r, 0, 128);

memset (client_w, 0, 128);

sprintf (Client_r, "%s/%d_r.fifo", PATH, client_id);

sprintf (Client_w, "%s/%d_w.fifo", PATH, client_id);

FD_CW = open (Client_r, o_wronly);

FD_CR = open (Client_w, o_rdonly);

if (fork () = = 0)

{

Child_main (FD_CR, FD_CW);

Close (FD_CR);

Close (FD_CW);

Exit (1);

}

Close (FD_CR);

Close (FD_CW);

}

memset (file_name, 0, 128);

sprintf (file_name, "%s/%s", PATH, name);

Unlink (file_name);

return 0;

}

IPC Process Communication

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.