Copy from: http://blog.sina.com.cn/s/blog_759dc36b0100p8y4.html
When running any UNIX Command, shell will create at least one process to run this command, so any program running in UNIX systems can be called a process; but the process is not a program, processes are dynamic, while programs are static, and multiple processes can concurrently call the same program.
Every process in the system contains a task_struct data structure. All pointers to the data structure form a process Vector Array. The default process vector data size is 512, indicates that the system can accommodate 512 processes at the same time. The task_struct data structure of a process includes the status, scheduling information, process identifier, and other information of the process.
Because a UNIX system is a multi-process operating system, every process is independent and has its own permissions and tasks. Therefore, when a process fails, no other process will fail. The system uses process identifiers to identify different processes. Process identifiers are non-negative Positive numbers and are unique at any time. When a process ends, the process identifier can be assigned to another new process. The system assigns identifier 0 to the scheduling process, and identifier 1 to the initialization process.
A process uses many resources, including the most valuable CPU resources, during running. When a process occupies CPU resources, other processes must wait for the idle CPU of the running process to run, because many processes are waiting, the kernel uses the scheduling algorithm to determine which process the CPU is allocated.
The system runs in the kernel mode when it is just started. At this time, only one initialization process is running. First, it initializes the system and then executes the initialization program (usually/sbin/init ). The initialization process is the first process of the system. All subsequent processes are subprocesses of the initialization process.
2. process identifier Management
UNIX systems use process identifiers to manage processes in the current system. To manage processes with similar features in a unified manner, the system introduces the concept of Process Groups to identify whether processes are in the same group with group identifiers. The Group identifier of a process is inherited from the parent process. Therefore, generally, the group identifier of a process is the identifier of the registration process associated with it. Process identifiers are assigned by the system and cannot be modified. Group identifiers can be modified through the setpgrp system call.
The system call format is as follows:
# Include <sys/types. h>
# Include <unistd. h>
Pid_t getpid (void );
Pid_t getpgrp (void );
Pid_t getppid (void );
Pid_t getpgid (pid_t PID );
Note: The first three system calls return the process identifier, Process Group Identifier, and parent process identifier of the calling process. They always return successfully. The fourth call sets the Process Group Identifier. It changes the Process Group Identifier of the calling process to the process identifier of the calling process to make it the first process in the process group, and return the New Process Group Identifier.
Let's look at an example:
|
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
printf( "PID
= %dn", getpid() );
printf( "PPID
= %dn", getppid() );
return 0;
}
|
[Root @ localhost SRC] # GCC getpid. c
[Root @ localhost SRC] #./A. Out
PID = 1, 4408
Ppid = 3128
[Root @ localhost SRC] # |