Premise: This article is based on the study of Linux system
How is the process of user-state organized?
All the user states are formed into a tree. The process tree.
The root of the process tree is init. That is process number 1th. Is the ancestor process of the user-state process.
How do I view the process tree?
Pstree
Relationship between processes parent-child process and sibling process
View information about a process
Ps-aux
Real-time view of process usage top
How do I create a process?
Fork (2) You can create a child process.
#include <unistd.h>
pid_t fork (void);
Function: Creates a child process. The caller of the function is the parent process, and the newly created process is a child process
Parameter: void
return value:
PID of a child process returned in a successful parent process
Returns 0 in a child process
The failed parent process returns-1. Errno is set
Child process creation failed
Termination of the process
Return and exit (3)
Return is just returned from the function call. The execution of the function is ended. Returned in the main function with no end process
Exit (3) When this function is called, ends the current process.
Nameless Pipes and famous pipes
Nameless Pipe--applied to inter-process communication with kinship
Pipe (2) Create nameless pipe
Pipefd[0] Pointing to the read end;Pipefd[1] pointing to the write end
1 //The nameless pipe must be a father-son or brother with kinship.2 //nameless pipes for interprocess communication3#include <stdio.h>4#include <unistd.h>5#include <string.h>6#include <stdlib.h>7 intMain () {8 Charbuf[ -];9 Char* msg="hello,father\n";Ten intpfd[2]; One //Create a nameless pipe A intP=pipe (PFD);//Pfd[0] read; PFD[1] Write - if(p==-1){ -Perror ("Pipe"); the return-1; - } - //Create Child process -pid_t pid=fork (); + if(pid==-1){ -Perror ("Fork"); + return-1; A } at if(pid==0){//son -Close (pfd[0]);//Turn off Read -Write (pfd[1],msg,strlen (msg)); -Close (pfd[1]); -Exit0); - } in Else{//Father -Close (pfd[1]); to intR=read (pfd[0],buf, -); +Write1, buf,r); -Close (pfd[0]); the Wait (NULL); * } $ return 0;Panax Notoginseng}
Famous pipeline: is actually a file, this file does not have the content, just plays the role of a bridge
Mkfifo (3)
1 //mkfifo.c2 //Create a pipeline file3#include <stdio.h>4#include <sys/types.h>5#include <sys/stat.h>6 7 intMainintargcChar*argv[]) {8 intMk=mkfifo (argv[1],0644);9 if(mk==-1){TenPerror ("Mkfifo"); One return-1; A } - return 0; -}
//fifo_w.c#include <stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<string.h>intMainintargcChar*argv[]) { Char* msg="hello.read\n"; //Open Pipeline File intFd=open (argv[1],o_wronly); if(fd==-1) {perror ("Open"); return-1; } //write to the pipeline fileWrite (Fd,msg,strlen (msg)); Close (FD); return 0;}
1 //fifo_r.c2#include <stdio.h>3#include <sys/types.h>4#include <sys/stat.h>5#include <fcntl.h>6 7 intMainintargcChar*argv[]) {8 Charbuf[ -];9 intFd=open (argv[1],o_rdonly);Ten if(fd==-1){ OnePerror ("Open"); A return-1; - } - intR=read (Fd,buf, -); theWrite1, buf,r); - Close (FD); - return 0; -}
Inter-process communication----pipelines