Linux Process Control (i)

Source: Internet
Author: User

1. Overview of the Linux process

A process is a process that a program executes at a time, and it differs fundamentally from a program. The program is static, it is an ordered set of instructions stored on disk, and the process is a dynamic concept, it is a running program, contains the process of dynamic creation, scheduling and extinction process, is the basic Linux scheduling unit. So how do you describe and represent the changes from a system perspective? Here, it is described by the Process Control block (PCB). The Process Control block contains the process description information, control information, and resource information, which is a static description of the process.

The kernel uses processes to control access to the CPU and other system resources, and uses processes to determine which program to run on the CPU, how long to run, and what characteristics to run it. The kernel Scheduler is responsible for allocating CPU execution time between all processes, called time slice, which takes turns taking back control from the process after the time slices that each process has been allocated.

1.1. Process identification

The OS assigns each process a unique integer ID, which is the identification number (PID)of the process. The process, in addition to its own ID, has the parent process ID (ppid), the ancestor process of all processes is the same process, it is called the init process , the ID of the 1,init process is a boot process after the kernel bootstrap. The INIT process is responsible for booting the system, starting the daemon (background) process, and running the necessary programs.

The PID and ppid of the process can be obtained by function Getpid () and getppid () respectively.

Example:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main ()

{

printf ("pid:%d ppid:%d\n", Getpid (), Getppid ());

return 0;

}

1.2. The user ID of the process and the group ID (the running identity of the process)

While the process is running, it must have a user-like identity in order to control the permissions of the process, by default, which logged-on user runs the program, and the program process has the user's identity. For example, assuming that the current logged-on user is Gotter, and he runs the LS program, LS has a gotter identity during the run, and the user ID and group ID of the LS process are gotter and gotter, respectively. This type of ID is called the real user ID of the process and the real group ID. Real user IDs and real group IDs can be obtained through functions getuid () and Getgid () .

corresponding to the real ID, the process also has a valid user ID and a valid Group ID property, and when the kernel accesses the process, it examines the process's valid user ID and valid group ID, not the real user ID and the real group ID. By default, the user (valid user ID and valid group ID) is the same as (real user ID and real group ID). Valid user IDs and valid group IDs are obtained through functions geteuid () and Getegid () .

Example

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main ()

{

printf ("uid:%d gid:%d euid:%d egid:%d\n", Getuid (), Getgid (), Geteuid (), Getegid ());

return 0;

}

Shell>id

uid=500 (Ghaha) gid=500 (Ghaha) groups=500 (Ghaha)

Compile to build the executable file A.out, the properties of the program file may be:

-rwxrwxr-x 1 Ghaha ghaha 12132 Oct 7 09:26 a.out

The execution results may be:

Shell>./a.out

uid:500 gid:500 euid:500 egid:500

Now change the owner executable property of A.out to S

Shell>chmod U+s a.out

Shell>ll

-RWsrwxr-x 1 ghaha ghaha 12132 Oct 7 09:26 a.out

At this point change another user Gotter log in and run the program a.out

Shell>id

uid=502 (Gotter) gid=502 (gotter) groups=502 (Gotter)

Shell>./a.out

uid:502 gid:502 euid:500 egid:502

As you can see, the valid user identity of the process becomes ghaha, not Gotter, because the owner of the file a.out access permission is executable for the property set for S, and after the user runs a.out, The valid user identity of the a.out process will no longer be the user running a.out, but the owner of the a.out file.

The most common examples of S permissions are

The/USR/BIN/PASSWD program, which has a permission bit of

shell>ll/usr/bin/passwd

-r-s--x--x 1 root root 16336 Feb 2003/usr/bin/passwd

We know that the user's username and password are saved in/etc/passwd (later specifically the password is saved in/etc/shadow, it is based on the/etc/passwd file to generate/etc/shadow, it moves all passwords from the/etc/passwd to the/ The Etc/shadow. Here is the shadow password, it divides the password file into two parts:/etc/passwd and/etc/shadow, at this time/etc/shadow is the shadow password file, it is to save the encrypted password, and/etc/passwd password all become x) under. Through the Ls–l view/etc/passwd This file, you will find that this file is not writable by ordinary users, then we do passwd when the password can be changed, then what is this? That is, any user running the program, the effective identity of the program will be root (in normal status to perform this operation, it will temporarily get the file owner root permissions), and so the passwd program has permission to read the/etc/passwd file information.

We also implement the following passwd functions, the steps are as follows:

1. Use touch to create a a.txt input "hello" similar to/etc/passwd

2. Write a program 1.c as follows:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <stdlib.h>

#include <errno.h>

int main ()

{

printf ("UID:%d gid:%d\n", Getuid (), Getgid ());

printf ("Eudi:%d Egid:%d\n", Geteuid (), Getegid ());

file* fp = fopen ("A.txt", "a");//Note this is opened in append form, indicating a.txt to have writable permissions

if (fp = = NULL)

{

Perror ("fopen error");

Exit (-1);

}

Fputs ("World", FP);

Fclose (FP);

return 0;

}

3. Compile Gcc–o 1 1.c generate executable program 1 at this time 1 similar to/usr/bin/passwd file

4. Use ll to view a.txt Discovery permissions are-rw-r-r-permissions (as with/etc/passwd permissions), stating that ordinary users do not have writable permissions, if you switch directly to a normal user execution./1 will error.

5. Use root to modify the 1 permissions to-rwsr-xr-x (Operation command: chmod u+s 1, at this time with the same permissions as/USR/BIN/PASSWD), then switch to the normal user Wangxiao, execute./1 Discovery can be performed successfully, because at this time. The valid user ID of the 1 process becomes root, which means that the normal user is implemented with root identity.

1.3. Status of the process

Process is the process of execution of a program, according to its life cycle can be divided into 3 states.

L Execution state: The process is running, that is, the process is consuming CPU.

L Ready state: The process already has all the conditions for execution and is waiting to allocate CPU processing time slices.

L Wait state: The process cannot use the CPU and can wake up if it waits for an event to occur (the resource that is waiting to be allocated).

1.4. The process structure under Linux

Linux system is a multi-process system, and its process has the characteristics of parallelism and non-interference. In other words, processes are separate tasks, with their own rights and responsibilities. Each of these processes runs in its own separate virtual address space, so even if one process has an exception, it does not affect other processes in the system.

The process in Linux consists of 3 segments, the data segment, the code snippet, and the stack segment, respectively.

· The data segment puts global variables, constants, and data spaces for dynamic Data allocation . Data segments are divided into ordinary data segments (including readable writable / read-only data segments that hold static initialized global variables or constants),BSS data segments (which hold uninitialized global variables) , and Heap (storing dynamically allocated data).

· " Code Snippet " The data that is stored in the program code .

· " Stack segment " The return address of the subroutine, the parameters of the subroutine, and the local variables of the program are stored .

1.5. Process management under Linux

Start process: Manually start the scheduled start

Note:

Process: Is the OS's smallest unit OS allocates 4g of virtual memory space for each process, where 1g gives the kernel space 3g to the user space {code area data area stack area}

PS View Active Process Ps–aux View all processes ps-aux| grep ' AA ' lookup specifies (AA) process PS–EF can show parent-child process relationship top show Top 20 processes, dynamic change pgrep ' VI ' lookup process

Process state: Execution readiness Wait state

Ps-aux See%CPU (CPU Usage)%MEM (memory usage) stat Status {s sleep t pause R run Z Zombie}

VI A.C & (& for background operation), a dead loop, press CTRL + Z to pause the process, and then execute [BG Job ID] to bring the process into the background. Jobs can be used to view background tasks, FG 1 brings the background task to the foreground, where 1 indicates the job ID

kill-9 process number è means sending signal number 9th to a process to kill a process that uses pkill A to kill a process called a process

Linux Process Control (i)

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.