Linux-Multi-process Summary 1

Source: Internet
Author: User

Reference:

Operatingfocus.bokee.com/3327857.html

www.oschina.net/code/snippet_237505_8646

Www.cnblogs.com/techdoc/archive/2010/12/22/1913521.html

I. Functions commonly used in multi-process programming.

1.pid_t Fork ();

Create Child process

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

Path is the executable path , the header file #include <unistd.h>, and the file passes past argv (0), argv[1] ..., the last argument must end with a null pointer (NULL).

3.int EXECLP (const char * file,const char * arg,......) ;

EXECLP () looks for a file name that conforms to the parameter in the directory referred to by the PATH environment variable, and executes the file when it is found.

4.int execv (const char * path, char * const argv[]);

Path is the file path

5.pid_t getpid (void);

Getpid () The process identification code used to obtain the current process

6.pid_t getppid (void);

getppid () is used to obtain the parent process ID of the current process.

7.void setsid (void);

If the process calling this function is not the leader of a process group, this function creates a new session, which becomes the first process of the conversation, and then the process becomes the lead process for the group of newly created processes, which does not control the terminal. Because the session first process is a single process with a unique process ID, you can treat the process ID of the session's first process as the session ID. This function mates with umask () to turn a process into a daemon.

8.pid_t Wait (int * status);

Function: Blocks the process until one of its child processes exits.

9.pid_t Wait (int * status);

#include <sys/types.h>, #include <sys/wait.h>. Wait () temporarily stops execution of the current process until a signal arrives or the child process ends. Recycling Zombie Processes

10.pid_t waitpid (pid_t pid,int * status,int options);

Parameter PID is to wait for the child process identification code, the other numerical significance is as follows:

Pid<-1 waits for the process group identifier to be the PID absolute value of any child processes.                 Pid=-1 waits for any child process, equivalent to wait ().            Pid=0 waits for the process group identifier to be the same as the current process for any child processes. Pid>0 waits for any child process identification code to be a child of PID.

The parameter option can be Wnohang, and if there is no end of the child process immediately returns, not to wait.

Two. Inter-process communication

1. Signal Volume

  1)  int sem_wait(sem_t *sem);

  //P操作,若是信号量大于零则减一,否则阻塞在该函数位置等待.

  2)  int sem_post(sem_t *sem);

  //V操作,信号量加一

  3)  sem_t *sem_open(const char *name, int oflag);

  //打开信号量,flag参数与打开普通文件的标记一样

  4)  sem_t *(const char *name, int oflag,mode_t mode, unsigned int value);

  //创建并打开信号量,value参数指的是信号量的初始值oflag参数可以是0、O_CREAT(创建一个信号灯)或O_CREAT|O_EXCL(如果没有指定的信号灯就创建),

  其中mode参数指定权限位(00777).

  5)  int sem_unlink(const char *name);

  //删除系统创建的信号量

  6)  int sem_close(sem_t *sem);

  //关闭彻底销毁信号量

 2.管道

int pipe (int filedis[2]);

Header file # include <unistd.h>, parameter Filedis returns two file descriptor: filedes[0] Open for Read, filedes[1] for write. The output of filedes[1] is the input of filedes[0].

3. Memory sharing

#include <sys/mman.h>

void *mmap (void *start, size_t length, int prot, int flags,int fd, off_t offset);

int Munmap (void *start, size_t length);

Example:

P_map = (people*) mmap (null,sizeof (people) *10,prot_read| prot_write,map_shared,fd,0);

P_map= (people*) mmap (null,sizeof (people) *10,prot_read| prot_write,map_shared| map_anonymous,-1,0);//anonymous, suitable for parent-child processes

Munmap (p_map,sizeof (people) *10);

Linux-Multi-process Summary 1

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.