You must understand the basic concepts before learning:
process, thread, process 3 states, process scheduling and 4 algorithms, process synchronization, deadlock, can't remember to find Niang.
Get ID
#include <sys/types.h>
#include <unistd.h>
Get this process ID:
pid_t getpid (void)
Get Parent Process ID:
pid_t getppid (void)
Example:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main (void)
{
printf ("pid=%d\n", Getpid ());
printf ("pid=%d\n", Getppid ());
return 0;
}
Process creation:
#include <unistd.h>
pid_t fork (void)
Create a child process, be called once, return two times, possibly 3 kinds of values;
1. In the parent process, the child process PID is returned;
2. In the sub-process, return 0;
3. Error returns a negative value;
The data space for the child process, and the stack space is copied from the parent process, not shared.
-vfork
pid_t vfork (void)
Difference: The Vfork child process shares the data segment with the parent process.
The Vfork child process executes first, after the parent process.
The order of Fork is uncertain;
exec function Family:
The program being executed replaces the program that called it:
Difference:
Fork creates a new process that produces a new PID;
EXEC starts a new program, replaces the original process, and the PID does not change.
#include <unistd.h>
int execl (const char*path,const CHAR*ARG1,...);
Path: The name of the program being executed
ARGN: command-line arguments, with parameter names, ending with a null pointer (NULL)
Example:
#include <unistd.h>
Main ()
{
Execl ("/bin/ls", "ls", "-al", "/etc/passwd", (char*) 0);
}
#include <unistd.h>
int EXECP (const char*path,const CHAR*ARG1,...);
Path: The name of the program being executed (without the path, as found in the PATH environment variable).
Exmple
EXECP ("ls", "ls", "-al", "/etc/passwd", (char*) 0);
#include <unistd.h>
int Execv (const char*path, char*const argv[]);
Example
#include <unistd.h>
Main
{
char *argvl[]={"ls", "-al", "/etc/passwd", (char*) 0};
EXECV ("/bin/ls", argv);
}
#include <stdlib.h>
int system (const char*sring);
Call fork to produce a child process that calls/bin/sh-c string to execute the command represented by the argument string.
E:
System ("ls-al/etc/passwd");
Process waits:
#include <sys/types.h>
#include <sys/wait.h>
pid_t Wait (int*status)
Block the process until a process exits.
Linux application Development-process programming