First, get the ID
#include <sys/types.h>
#include <unistd.h>
pid_t getpid (void) Get this process ID
pid_t getppid (void) Gets the parent process ID
Parent process: A new process is created in the existing process.
Example: GETPID.C
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main ()
{
printf ("pid=%d\n", Getpid ());
printf ("ppid=%d\n", Getppid ());
return 0;
}
Ii. Process Creation-fork
#include <unistd.h>
pid_t fork (void)
Features: Creating child processes
The magic of Fork is that it is called once, but returns two times, and it may have three different return values:
1. In the parent process, fork returns the PID of the newly created child process.
2, in the sub-process, fork returns 0.
3. If an error occurs, fork returns a negative value.
Example: fork1.c
#include <sys/types.h>
#include <unistd.h>
int main ()
{
pid_t pid;
/* There is only one process at this time */
Pid=fork ();
/* There are already two processes running at the same time */
if (pid<0)
printf ("Error in fork!");
else if (pid==0)
printf ("I am the child process,id is%d\n", getpid ());
Else
printf ("I am the parent process, ID is%d\n", getpid ());
return 0;
}
Example two:
#include <unistd.h>
#include <stdio.h>
int main ()
{
pid_t pid;
int count=0;
Pid=fork ();
count++;
printf ("count=%d\n", count);
return 0;
}
Execution Result:
Summary: After the child process is created, the parent-child process shares the code, but the data portion (data space, stack space), is copied. Each operation.
Iii. Process Creation-vfork
#include <sys/types.h>
#include <unistd.h>
pid_t vfork (void)
Features: Creating child processes
Note: The difference between fork and vfork:
1, Fork: The child process copies the data segment of the parent process.
Vfork: Child processes and parent processes share data segments.
2. Fork: The order of execution of parent and child process is uncertain.
Vfork: The child process runs first, and then runs after the parent process.
Four, exec function family
exec replaces the program that called it with the executed program.
Difference:
Fork creates a new process that produces a new PID.
EXEC starts a new program and replaces the original process, so the PID does not change.
Common functions:
1, int execl (const char* path,const char* arg1,...)
Parameters:
Path: Executed program name (with full path)
ARG1-ARGN: command-line arguments required by the executing program, including the program name. Ends with a null pointer (NULL).
#include <unistd.h>
Main ()
{
Execl ("/bin/ls", "ls", "-al", "/etc/passwd", NULL);
}
2, int execlp (const char* path,const char* arg1,...)
Parameters:
Path: Executed program name (without path, the program will be looked up from the PATH environment variable)
ARG1-ARGN: command-line arguments required by the executing program, including the program name. Ends with a null pointer (NULL).
#include <unistd.h>
Main ()
{
EXECL ("ls", "ls", "-al", "/etc/passwd", (char*) 0);
}
Execution Result:
3, EXECV
int execv (const char* path,char* const argv[])
Parameter: path is executed with the program name (with full path).
Argv[]: An array of command-line arguments required by the executing program.
Example: EXECVC
#include <unistd.h>
int main ()
{
char* argv[]={"ls", "-al", "/etc/passwd", (char*) 0};
EXECV ("/bin/ls", argv);
return 0;
}
4. System
#include <stdlib.h>
int system (const char* String)
Function: Call fork to produce a child process that calls /bin/sh-c string to execute the command represented by the argument string.
Example: SYSTEM.C
#include <stdio.h>
void Main ()
{
System ("ls-al/etc/passwd");
}
Difference: System is called execution in a child process. That is, the statement after the statement can also be executed.
The EXEC function family is to replace the process with execution. That is, the statement after the statement cannot be executed.
V. Process waiting
#include <sys/types.h>
#include <sys/wait.h>
pid_t Wait (int* status)
Function: Blocks the process until one of its child processes exits.
Example: wait.c
#include <stdio.h>
#include <sys/wait.h>
void Main ()
{
pid_t PC,PR;
Pc=fork ();
if (pc==0)/* If it is a child process */
{
printf ("I am Child process:%d\n", Getpid ());
Sleep (10);/* Sleeps 10 seconds */
}
else if (pc>0) {/* If it is a parent process */
Pr=wait (NULL);/* Wait */
printf ("I am child process,and i catched a child process:%d\n", PR);
}
Exit (0);
}
Execution Result: