编程知识 cdmana.com

Linux process related functions

Process dependent functions ( Process primitives )

1.fork

	#include <unistd.h>
	
	pid_t fork(void);            pid_t      It can be seen as an unsignalized plastic 
	#include <sys/types.h>
	#include <unistd.h>
	
	id_t vfork(void);
	 success , Subprocess's PID Returns in the parent process , Return... In child process 0, If it fails, it is returned in the parent process  -1, Do not create child processes , And set up errno
	 because fork Function has the feature of one call and two returns , You can take advantage of different return values , Make the parent and child processes achieve different functions 
	 When creating a subprocess , All the code of the parent process is shared with the child process through mapping , Only when the child process writes data 
	 Will actually replicate for the child process , Also known as " Share while reading , When writing copy ", This saves memory space and reduces replication overhead 
	
	vfork:  Subprocesses run first , The parent process waits until the child exits or the program is replaced , The parent process will run , Otherwise it runs at the same time , Because the parent and child processes share the virtual address space , This will cause confusion in the call stack , So block the parent process ,vfork Child processes if return sign out , Releasing resources can cause the parent process to fall into confusion or error 

2.getpid

	#include <sys/types.h>
	#include <unistd.h>
	
	pid_t getpid(void);       // Returns the process that called the process ID.( This is usually used to generate a unique temporary routine file name )
	pid_t getppid(void);      // Returns the process of the parent process that called the process ID

3.getuid

	#include <unistd.h>
	#include <sys/types.h>
	
	uid_t getuid(void);       // Return to the actual user id
	uid_t geteuid(void);      // Return valid users id

4.getgid

	#include <unistd.h>
	#include <sys/types.h>
	
	gid_t getgid(void);       // Return to the actual user group id
	gid_t getegid(void);      // Return valid user groups id

5.exec family

	#include <unistd.h>
	
	extern char **environ;
	
	// Find... In the current path path,  command ,  Command parameter , NULL
	int execl( const char *path, const char *arg, ...);
	
	// stay path Environment variables file,  command ,  Command parameter , NULL
	int execlp( const char *file, const char *arg, ...);
	
	// Find... In the current path path,  command ,  Command parameter , NULL, envp[] = { environment variable }
	int execle( const char *path, const char *arg , ..., char * const envp[]);
	
	// Find... In the current path path, argv[] = { command ,  Command parameter ..., NULL}
	int execv( const char *path, char *const argv[]);
	
	// stay path Environment variables file, argv[] = { command ,  Command parameter ..., NULL}
	int execvp( const char *file, char *const argv[]);
	
	// Find in the set environment variable filename,  command ,  Command parameter , NULL, envp[] = { environment variable }
	int execve (const char *filename, char *const argv [], char *const envp[]);
	
	 If the function is called successfully, the new program is loaded and executed from the startup code , Don't go back , Return if failed -1
	 On , Only execve It's a real system call , All of the above five functions eventually call execve, therefore execve Function in man Manual No 2 section 
	 Other functions are in man Manual No 3 section 
	exec function , The first parameter must be a valid path , Then the second parameter is equivalent to a place holder , It makes no sense , But you can't delete , The third parameter is what the function really cares about 

6.wait

	#include <sys/types.h>
	#include <sys/wait.h>
	
	pid_t wait(int *status);
	pid_t waitpid(pid_t pid, int *status, int options);
	int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
	wait It's a blocking function ,  call wait The parent process of will always wait for the child process resource to be reclaimed , If there are no child processes ,wait return -1, And then it goes down 
	wait If the successful return of the dead child process id Number , Failure to return -1
	
	waitpid Is a blocking function, but can be set to non blocking , pid Set the recycling range , status Accept exit value , options Options 
	waitpid If it works , The state of the child process is changed id, If you set WNOHANG And one or more processes are specified pid The recycling of , But if the state doesn't change, it returns 0
	 Failure to return -1
		pid The value of is :
		< -1					 Reclaim any child process within the specified process group 
		-1						 You can recycle any child process 
		0						 Recovery and current invocation waitpid All the child processes of a group 
		> 0					 Recycle subprocess , Of this subprocess pid You set it up pid.
	option The value of is one or one or more of the following constants :
	WNOHANG			 If no child process ends , Return immediately , Non blocking 
	WUNTRACED		 If the child process has stopped , Then return to 
	WCONTINUED		 If by delivery SIGCONT The stopped child process was resumed , Then return to 

版权声明
本文为[Ran1366]所创,转载请带上原文链接,感谢

Scroll to Top