A process is an address space running one or more threads and the system resources required by these threads, and the PS-AJX command is used to print all current processes, where the state is expressed as: s for Sleep, R for run, D for wait, T for stop, Z for zombie process, PID for process number, Ppid represents the parent process number;
One: PID, ppid can be obtained by Function getpid (), Getppid (), the following is the get code
1#include <stdio.h>2#include <sys/types.h>3#include <unistd.h>4 5 intMain ()6 {7pid_t Myself_pid =getpid ();8pid_t Parent_pid =getppid ();9 Tenprintf"Myself_pid is%d\n", myself_pid); Oneprintf"Parent_pid is%d\n", parent_pid); A - return 0; -}
Two: Create a new process
There are three ways to create a new process: fork (); system (); Exel () family;
(1) system ();
The system () function calls "/bin/sh-c command" to execute a specific command, blocking the current process until command execution is complete
The following is the Execute LS-A command through System ()
1#include <stdio.h>2 3 intMain ()4 {5 //Create a new process6System"ls-a");7printf"over\n");8 9 Ten return 0; One}
(2) The Exel family contains members (EXECL,EXECLP,EXECLE,EXECV,EXEVP,EXECVE);
The Exel series function is a replacement creation process (the child process replaces the original process, and the parent process does something different), and only the PID remains the same (and the exec process PID is called);
Some of its prototypes are as follows:
int EXELC (const char *path, const char * arg, ...);
int EXECLP (const char *file, const char * arg, ...);
int execv (const char *path, char * const argv[]);
1#include <stdio.h>2#include <unistd.h>3#include <stdlib.h>4 5 intMain ()6 {7 intRET =0;8 Char*argv[3] = {"ls"," -A", NULL};9 Ten //Create a new process Oneret = EXECV ("/bin/ls", argv); A if(Ret <0) { -Perror ("Execv"); - exit (exit_failure); the } - -printf"Hello world\n");//Run program can see, no output "Hello World" (replaced) - + return 0; -}
(3) fork ();
An existing process can call the fork () function to create a new process, the process created by Fork is called a child process, the fork function is called once but returns two times, and the only difference between two returns is:
A 0 value is returned in the child process, and the PID of the child process is returned in the second parent process
1#include <stdio.h>2#include <unistd.h>3#include <stdlib.h>4 5 intMain ()6 {7pid_t PID =0;8 9PID =fork ();Ten if(PID <0) { OnePerror ("Fork"); A exit (exit_failure); - } - the //Child Process - if(PID = =0) { -printf"This is a child PID is:%d, Ppid is:%d\n", Getpid (), Getppid ());
- + } - //Parent Process + if(PID >0) { Aprintf"This is the parent PID is:%d, Ppid is:%d\n", Getpid (), Getppid ()); at } - -printf"Hello world\n");//you can see that "Hello World" has been printed two times (parent process, child process) -}
Three: The following program is the parent process to file a read lock, and then go to sleep, the child process first sleep 1 seconds, and then go to verify the file lock, verify that the child process see the lock file process is the parent process
1#include <stdio.h>2#include <fcntl.h>3#include <stdlib.h>4 5 intMain ()6 {7 8pid_t PID =fork ();9 if(PID >0) { Tenprintf"This is father\n"); One A //Open File - intFD =0; -FD = open ("txt", O_RDWR); the if(FD <0) { -Perror ("Open"); - exit (exit_failure); - } + - intLen = Lseek (FD,0, seek_end);//Get File Size +Lseek (FD,0, seek_set); A at //the parent process reads the file lock - structFlockLock; - Lock. L_type =F_rdlck; - Lock. l_whence =Seek_set; - Lock. L_start =0; - Lock. L_len =Len; in intret = Fcntl (FD, F_SETLK, &Lock); - if(Ret <0) { toPerror ("Child Fcntl"); + exit (exit_failure); - } theSleep3); * exit (exit_success); $ }Panax Notoginseng - //Child Process theprintf"child\n"); +Sleep1); A intFD =0; theFD = open ("txt", O_RDWR); + if(FD <0) { -Perror ("Open"); $ exit (exit_failure); $ } - intLen = Lseek (FD,0, seek_end); -Lseek (FD,0, seek_set); the - //child process attempts to write lockWuyi structFlockLock; the Lock. L_type =F_wrlck; - Lock. l_whence =Seek_set; Wu Lock. L_start =0; - Lock. L_len =Len; About intret = Fcntl (FD, F_GETLK, &Lock); $ if(Ret <0) { -Perror ("Parent Fcntl"); - exit (exit_failure); - } A + theprintf"parent is:%d\n", Getppid ()); -printf"Lock.l_pid is:%d\n",Lock. l_pid); $ the if(Lock. L_type = =F_rdlck) { theprintf"Read lock\n"); the } theprintf"lock.l_whence is:%d\n",Lock. l_whence); -printf"Lock.l_len is:%d\n",Lock. L_len); in the Close (FD); the exit (exit_success); About}
Process creation and inter-process communication