Linux Study Notes-Process

Source: Internet
Author: User
1. process definition process: the process is Program The execution process of a program is essentially different from that of a program. The program is static. It is an ordered set of commands stored on a disk and has no concept of execution; A process is a dynamic concept. It is a process of program execution, including the entire process of dynamic creation, scheduling, and extinction. It is the smallest unit of program execution and resource management. 2. Process Control Block processes are the basic scheduling units of the Linux system. process control blocks contain the process description, control information, and resource information, which is a static description of the process. In Linux, each item in the process control block is a task_struct structure defined in include/Linux/sched. h. 3 in Linux, the most important process identifiers are the process id pid and its parent process ID ppid. Both PID and ppid are non-negative. # Include <stdio. h> # include <unistd. h> # include <stdlib. h> int Mian () {printf ("the PID of this process is % d \ n", getpid ()); printf ("The ppid of this process is % d \ n", getppid ();} 4 processes in Linux contain three segments: "data segment" and" Code And Stack segments ". Data Segment: The data segment that stores global variables, constants, and dynamic data distribution: The data stack segment that stores program code: it stores the return address of the subroutine, the parameters of the subroutine, and the local variables of the program. 5 in Linux, the process execution mode is divided into user mode and kernel mode. If you are running a user program, application program, or a system program other than the kernel, the corresponding process is performed in user mode; if a system call or interruption occurs during user program execution, run the operating system program and change the process mode to the kernel mode. User processes can run either in user mode or in kernel mode. 6. Scheduling processes: including interrupting processes, changing priorities, and viewing process statuses. in Linux, you can use related system commands to perform operations. PS: view the top processes in the system: nice Process in dynamic reality: Run renice based on the user-specified priority: change the priority of the running process kill: Terminate the process crontab: run the following command to install, delete, or list the task BG used to drive cron background processes: run the suspended process in the background 7. The only way to create a process in Linux is to use the fork () function. Two values are returned after one execution. The fork function is used to create a new process from an existing process. The new process is called a child process, and the original process is called a parent process. These two return values respectively. The return value of the parent process is the process number of the child process, and the return value of the child process is 0. therefore, the return value can be used to determine whether the process is a child process or a parent process. 8 perror () is used to output the cause of the previous function error to the standard error (stderr ). The string referred to by parameter S is printed first, followed by the error cause string. The cause of this error is determined based on the value of the global variable errno. 9 The exec function family provides a method to start execution of another program in the process. It can find executable files based on the specified file name or directory name, use it to replace the data segment, code segment, and stack segment of the original calling process. After execution, all the content of the remote calling process except the process number is replaced by the new process. In addition, the executable file can be either a binary file or any executable script file in Linux. 10 exec function family member functions: int execl (const char * path, const char * Arg ,.....) int execv (const char * path, char * const argv []) int execle (const char * path, const char * Arg ,....., char * const envp []) int execve (const char * path, char * const argv [], char * const envp []) int execlp (const char * file, const char * Arg ,.....) int execvp (const char * file, char * const argv []) when using the exec function family, you must add an error judgment statement. Because exec is easy to execute and fails, the most common cause is that the file or path cannot be found. In this case, errno is set to the enoent array argv and envp and it is forgotten to end with null, in this case, errno is set to efault and does not have the corresponding Executable File Permission. In this case, errno is set to eacces11 exit and the _ exit function are used to terminate the process. When the program executes exit or _ exit, the process will unconditionally stop all the remaining operations, clear various data structures including PCB, and terminate the operation of the process. _ Exit () directly stops the process, clears its memory space, and clears various data structures in the kernel. 12 The wait waitpidwait function is used to block the parent process, knowing that a child process ends or the process accepts a specified signal. If the parent process has no child process or its child process has ended, wait will return immediately. 13 daemon: it is a background service process in Linux. It is a long-lived process, usually independent of the control terminal and periodically executes a task or waits for processing certain events. Write a daemon: 1) create a sub-process and the parent process exits. 2) create a new session process group in the sub-process. The process group is a collection of one or more processes. A process group is uniquely identified by the Process Group ID. Each process group has a leader process, and the process ID of the leader process is equal to the Process Group ID. Session group: A session Group is a collection of one or more Process Groups. Generally, a session starts when the user logs on and ends when the user exits, during this period, all processes run by the user belong to this session period. The setsid function is used to create a new session and act as the group leader. Calling setsid has three functions: getting the process out of control of the original session, getting the process out of control of the original process group, and getting the process out of control of the original control terminal. 3) change the current directory to the root directory. 4) reset the File Permission mask.

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.