Linux Process Control theory and several common inter-process communication mechanisms

Source: Internet
Author: User

1. Linux Process Control theory

The ① process is a running activity (dynamic, concurrency, Independence, asynchrony) of a program with a certain independent function.

  Four elements of a process:

(1) There is a procedure for its execution (not necessarily a process-specific), as a play must have its own script.
(2) has its own dedicated system stack space (private property)
(3) with Process Control block (task_struct) ("id, PID")
(4) There is a separate storage space.

Missing fourth is called a thread , and if there is no user space at all called a kernel thread , the shared user space is called the user thread .

② Classic Process Tri-state: block, Ready, execute

Note: At the same time, the cup runs with only one process

③ process ID (PID): The unique number that identifies the process, the parent process ID (PPID), the user ID (UID) to start the process

④ process Mutex: when there are a number of processes that want to use a shared resource, a maximum of one process is allowed at any time, and other processes that want to use the resource must wait until the process that consumes the resource frees the resource

⑤ Critical Resource: A resource that is accessed by only one process at a time in the operating system is called a critical resource

⑥ critical section: the code that accesses critical resources in a process is called a critical section. In order to achieve mutually exclusive access to critical resources, the process of mutual exclusion should be ensured to enter the respective critical areas.

⑦ process synchronization: A sequence of processes performed by a set of concurrent processes is called interprocess synchronization

⑧ process scheduling: According to a certain algorithm, from a set of processes to run to select one to occupy the CPU run

(1) Scheduling mode: Preemptive (A is running, b ready, B priority High, direct running B) and non-preemptive (A is running, b ready, B priority high, etc. a run out and then run B)

(2) Scheduling algorithm: First come first service, short process first, high priority priority, time slice rotation

⑨ deadlock: A deadlock in multiple processes due to competitive critical resources

⑩ Get ID

pid_t getpid (void); Get this process ID

pid_t getppid (void); Get Parent Process ID

? Process creation:

pid_t fork (void); Create Child process

Fork is called once, but returns two times, he 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

Note:the code after the fork is run by two processes, that is, two processes share a piece of code, but copies a copy of the data, that is, the child process's data space, the stack space will get a copy from the parent process, rather than sharing .

? Another function created by the process:

pid_t vfork (void)

? exec function Family: Replace the program that called it with the executed program

? system function: Run a command

(1) System prototype: int system (const char* command);

(2) System simple implementation:

intSystemConst Char*cmdstring)    {pid_t pid; intstatus; if(Cmdstring = =NULL) {        return(1);//if cmdstring is empty, returns a value other than 0, typically 1    }        if(PID = fork ()) <0) {Status= -1;//Fork failed, return-1    }    Else if(PID = =0) {execl ("/bin/sh","SH","- C", Cmdstring, (Char*)0); _exit (127);//Exec Execution Failure Returns 127, note that EXEC only returns to the current process if it fails, and if successful, the current process does not exist.    }    Else //Parent Process    {         while(Waitpid (PID, &status,0) <0)        {            if(errno! =eintr) {Status= -1;//returns 1 if the Waitpid is interrupted by a signal                 Break; }        }    }        returnStatus//returns the return status of the child process if the waitpid succeeds}

? process waits

(1) pid_t wait (int* status);

(2) pid_t waitpid ((pid_t pid, int * status, int options);

2. Linux interprocess communication (nameless pipes and known pipes, signals, message queues, shared memory, semaphores, sockets)

Linux Process Control theory and several common inter-process communication mechanisms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.