In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
The C code we usually write is compiled by the compiler, and eventually it becomes an executable program, and when the executable program runs (before it ends), it becomes a process. I. Overview of the process
The C code we usually write is compiled by the compiler, and eventually it becomes an executable program, and when the executable program runs (before it ends), it becomes a process.
A program is an executable file stored on a storage medium, and a process is the process of program execution. The state of a process changes, including its creation, scheduling, and demise. The program is static and the process is dynamic.
In the Linux system, the operating system completes a task through the process, and the process is the basic unit of managing transactions. The process has its own independent processing environment (such as which environment variables are currently needed, where the program is running, which user is currently running the program, etc.) and system resources (such as processor CPU occupancy, memory, Imax O devices, data, programs). We can understand that the company is equivalent to the operating system, the department is equivalent to the process, the company manages through the department (the system is managed through the process), and for each department, each department has its own resources, such as personnel, computer equipment, printers and so on.
II. Process status
Our computers are basically multitasking, and when we talk about QQ, we can watch video at the same time, which is equivalent to both QQ and video programs running at the same time (two processes). In the early days, the CPU of a computer was single-core (a single core theoretically ran only one task), so how did it achieve multitasking? This involves the scheduling strategy of the process. Now let me give you an example. There are three processes of AMagneBMagneC. In the case of our single CPU, there is only one process running at a time. If A runs out, B runs, B finishes, C runs, C finishes, A runs, and CPU is fast enough, and the interval between An and B is short enough. Macroscopically, we can see that Amine BMague C seems to be running at the same time. This is the core principle of running multiple tasks in a single CPU, which is implemented through a time slice polling scheduling strategy (for more details, see "Linux process scheduling Analysis").
From the above example, we can know that for the A process, sometimes it is running, sometimes it is not running, and the two states are different, so the process is stateful, and at the same time, the states can be converted to each other from the executed state to the unexecuted state, here. We can simply divide the entire life cycle of a process into three states (not actually these three states): ready state, execution state, and waiting state.
Ready:
The process already has all the conditions for execution and is waiting for processing time to be allocated for CPU.
Executive state:
The process is using CPU to run.
Waiting state:
The state in which a process cannot continue execution temporarily because it does not have certain conditions for execution.
It should be noted here that the ready state and the waiting state are not executed, but they are different. The ready state means that the condition is satisfied, the time is not up, and the waiting state does not meet the condition.
Similarly, the three states of the process can be switched to each other:
Execute state-- > wait state:
When an executing process cannot continue execution because it is waiting for an event to occur, it changes from the execution state to the waiting state.
Waiting state-> ready state:
A process in a waiting state. If the event it is waiting for occurs, the process changes from a waiting state to a ready state.
Ready state-- > execution state:
As soon as the cpu time slice waiting for the ready process arrives, the process changes from ready state to executive state.
Executive state-- > ready state:
In the course of execution, a process in the execution state has to give up cpu because one of the time slices allocated to it has been used up, so the process changes from the execution state to the ready state.
In order to give you a clearer understanding of the transition of the three states, give you an example of lh buying a train ticket.
Lh hurried to the railway station to buy a train ticket. He was in such a hurry that he found that he had forgotten his ID card when he arrived at the ticket office. At this time, even the lh queue is useless, because lh does not have the conditions to buy a ticket (without ID card). At this time, lh belongs to the waiting state.
Lh calls its object and asks her to bring her ID card. Wait a minute, the ID card is delivered. At this time, lh can wait in line to buy a ticket, but when time is up, lh can buy a ticket. At this time, lh belongs to the ready state. This process is changed from waiting state to ready state.
After waiting for 10 minutes, finally arrived at lh, lh began to buy tickets, at this time, lh belongs to the executive state. This process is changed from the ready state to the executive state.
In the process of buying a ticket, lh's object called him and asked lh to buy a train ticket for her, but lh did not have her object's ID card, and then lh continued to wait for his object to send an ID card. At this time, lh changed from the executive state to the waiting state.
If this is the case, lh buy train tickets for colleagues in the company (need to buy more than 100 tickets), in the process of buying tickets (executive state), there are many people in the queue, the people behind the queue must be unhappy, then the conductor said, 20 minutes later, if you have not finished processing, please wait in the back of the queue. As a result, it took lh 20 minutes to finish processing, so he obediently went to the back and re-queued, and at this time, the lh changed from the executive state to the ready state.
3. Process control block
For the operating system, it needs to control a lot of processes, at the same time, each process has a different state, how does the system know that An executes to B instead of C? How does the system coordinate the control process?
When we run a program to make it a process, the system opens up a memory space to store the data information related to the process, which is stored through the structure (open / usr/src/linux-headers-3.2.0-23/include/linux/sched.h in task_struct,ubuntu12.04 to find the definition of task_struct). We call this structure for storing process-related data information a process control block.
The operating system operates and controls the process through this process control block. For more details, see "Linux process Management".
The process control block is the most important recording data structure in the operating system. The process control block records all the information needed to describe the progress of the process and control the operation of the process. It is the only sign of the existence of the process. There is a lot of information in the process control block, of which the more important is the process number, as for some other information we will not discuss in detail here.
IV. Process number
Each process is identified by a process number, the type of which is pid_t (unsigned integer). The range of process number is: 0room32767. Process numbers are always unique, but process numbers can be reused. When a process terminates, its process number can be used again.
Therefore, all processes under Linux are created directly or indirectly by init processes.
Next, I will introduce you to three different process numbers.
Process number (PID):
Identifies a non-negative integer number of the process.
Parent process number (PPID):
Any process (except the init process) is created by another process, which is called the parent process of the created process, and the corresponding process is called the parent process number (PPID). For example, process A creates process B, and the process number of process An is the parent process number of process B.
Process group number (PGID):
A process group is a collection of one or more processes. They are related to each other, the process group can receive all kinds of signals from the same terminal, and the associated process has a process group number (PGID). This process is somewhat similar to the QQ group, the group is equivalent to the QQ group, and each process is equivalent to each friend. Pulling all the friends into this QQ group is mainly convenient for management, especially when notifying something, as long as you yell in the group, everyone receives it simply and rudely. However, this process group number is a little different from the QQ group number, and by default, the current process number is used as the current process group number.
Fifth, process number operation function
The Linux operating system provides three functions to obtain the process number: getpid (), getppid (), and getpgid ().
Required header file:
Includeincludepid_t getpid (void)
Features:
Get the process number (PID)
Parameter: none
Return value: current process number
Pid_t getppid (void)
Function: get the parent process number (PPID) of the process calling this function
Parameter: none
Return value:
Pid_t getpgid (pid_tpid)
Function: get process group number (PGID)
Parameter: pid: process number
Return value: the current process group number is returned when the parameter is 0, otherwise the process group number of the process specified by the parameter is returned
The sample code is as follows:
Includeincludeincludeint main (int argc, char * argv []) {pid_t pid, ppid, pgid;pid = getpid (); printf ("pid =% d", pid); ppid = getppid (); printf ("ppid =% d", ppid); pgid = getpgid (pid); printf ("pgid =% d", pgid); return 0;}
Running result:
Original link: https://www.linuxprobe.com/linux-system-programming.html
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.