Linux Programming Learning Notes----process management and program development (i)

Source: Internet
Author: User
Tags bit set session id

Reprint please indicate the source, http://blog.csdn.net/suool/article/details/38406211, thank you!


Linux process storage structure and process structure executable file structure

Such as:

As you can see, this elf executable file is stored (not into memory) into the code area, the data area, and the non-out data area three parts.

Code area: A machine instruction that holds the execution of the CPU.

Data area: Contains static variables that have been initialized in the program, as well as global variables that have already been initialized.

Uninitialized data area: An uninitialized global variable and an uninitialized static variable are stored.

Now add a static variable of int to the program code above, and the result is as follows:

The Code section adds 4 bytes, which is the size of int.

Process structure

If you load an elf-formatted executable file into memory, it will be transformed into one or more processes. A process is the basic unit of Linux transaction management, and all processes have an independent environment and resources.

Shows a comparison of the storage structure of an elf executable with the basic structure of the Linux process:

Only the code area, initialization data area, uninitialized data area, heap area, and stack area five parts of a process are requested in memory.

To better manage the resources accessed by Linux, the system defines the Process Control block (PCB) structure to manage each process resource.

And the process resources are divided into:

Kernel space process resources and user space process resources

Kernel space process resources are PCB-related information. User-space process resources include: memory space mapped by member Mm_struct.

Process status

Kernel Process Architecture # TASK_RUNNING0   //Ready # Task_interruptible1   //Interrupt Wait # Task_uninterruptible2   // Non-interruptible wait # __task_stopped4   //Zombie # __task_traced8   //Pause/       * in Tsk->exit_state */#define Exit_ Zombie16#define exit_dead32/* in Tsk->state again */#define Task_dead64#define Task_wakekill128#define TASK_ WAKING256


Each process in the system must be in one of the process states listed above.

Task_running indicates that the process is either executing or is preparing to execute.

Task_interruptible indicates that the process is blocked (sleep) until a condition becomes true. Once the condition is reached, the status of the process is set to task_running.

The meaning of task_uninterruptible is similar to that of task_interruptible, except that it cannot be awakened by accepting a signal.

__task_stopped indicates that the process was stopped execution.

__task_traced indicates that the process is being monitored by processes such as debugger.

Exit_zombie indicates that the execution of the process is terminated, but its parent process has not yet used a system called wait () to learn about its termination.

Exit_dead represents the final state of the process.

Exit_zombie and Exit_dead can also be stored in exit_state members. Process state switching process and causes are roughly the same (picture from "Linux Kernel development")

Process basic attribute Process number (PID)

PID is the only positive integer that the system maintains to indicate a process, and the process number cannot be modified at the user level. In the Linux system, the first user process of the system is the INIT process, the PID is 1, the other process PID in turn, the following:


In application programming, you can use the Getpid () function to get the PID of the current process. See also: http://pubs.opengroup.org/onlinepubs/009695399/functions/getpid.html

Parent process Number (PPID)

All processes except the INIT process are created by another process, which becomes the parent process and the process being created is a child process. Ppid cannot be modified at the user level. Use the Getppid () function to get the parent process number of the current process. See also: http://pubs.opengroup.org/onlinepubs/009695399/functions/getppid.html

Process group number (PGID)

As with user management, processes also have their own process number (PID) and process group number (PGID). A process group is a collection of one or more processes. They are associated with the same job and can accept various signals from the same terminal, but the process group number can be modified at the user level.

Use the Getpgid () function to get the process group number for the specified process. See: http://pubs.opengroup.org/onlinepubs/009695399/functions/getpgid.html

The following program is an example of the sum of the above three functions used:

#include <stdio.h> #include <unistd.h>int main (int Argc,char *argv[]) {int i;printf ("\tpid\t ppid \ t pgid\n") ;             Hint Information printf ("parent\t%d\t%d\t%d\n", Getpid (), Getppid (), Getpgid (0));  Current process information for (i=0;i<2;i++) if (fork () ==0) printf ("child\t%d\t%d\t%d\n", Getpid (), Getppid (), Getpgid (0)); Child process information return 0;}


In addition, GETPGRP () can also be used to get the process group number of the current process. See also: http://pubs.opengroup.org/onlinepubs/009695399/functions/getpgrp.html

Session

A session is a collection of one or more processes. The system call Function GetSID () is used to get the SID of a process session.

See also: http://pubs.opengroup.org/onlinepubs/009695399/functions/getsid.html

The SID of a process can be modified, and the function Setsid () is used to create a new session. Here:http://pubs.opengroup.org/onlinepubs/009695399/functions/setsid.html

Control Terminal

Sessions and process groups have the following characteristics:

In order for the terminal device driver to send the signal to the process, you can call TCGETPGRP () to get the process group number of the foreground process group. Returns the foreground process group number associated with the open terminal. Http://pubs.opengroup.org/onlinepubs/009695399/functions/tcgetpgrp.html

The TCSETPGRP () function is used to set whether a process group is a foreground or a background process group. Http://pubs.opengroup.org/onlinepubs/009695399/functions/tcsetpgrp.html

If the process has a control terminal, the foreground process group ID is set to Pgrpid, which should be in the ID of a process group in a session, and the parameter fileds is the control terminal file descriptor.

The TCGETSID (*) function gets the session ID of the session first process of the current control terminal. Http://pubs.opengroup.org/onlinepubs/009695399/functions/tcgetsid.html


Here is an example of using the above function:

#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h>int main () {    int FD;    pid_t pid;    Pid=fork ();     Create a new process    if (pid==-1) perror ("fork");    else if (pid>0)    {       wait (NULL);       Exit (exit_failure);    }    else    {        if (Fd=open ("/dev/pts/0", O_RDWR) ==-1)//Because it is a network terminal, open the terminal here to confirm        {           perror ("open");        } printf ("pid=%d,ppid=%d\n", Getpid (), Getppid ());         Get process number & Parent process        printf ("sid=%d,tcgetsid=%d\n", GetSID (Getpid ()), Tcgetsid (FD));//read session SID and Terminal Sid        printf ( "Tcgetpgrp=%d\n", Tcgetpgrp (FD));        Read terminal foreground process        printf ("pigd=%d\n", Getpgid (Getpid ()));      Read the ID of the process group   }}  

Operation Result:

Process User Properties

Linux is the right to have a tightly controlled operating system, a process has a real user number (RUID), Real user group number (rgid), valid user number (EUID), valid user group number (egid) information.

In a Linux system, the creator of the file is the owner of the file, that is, the real user number of the file is the owner of the file and is viewed using the ls-l command.

Process Real user number (RUID)

For a process, the user UID that created the process is the real user number for this process. Use the Getuid (0 function to get the ruid of the current process. The function is defined in unistd.h.

See also: http://pubs.opengroup.org/onlinepubs/009695399/functions/getuid.html

Process Valid user group number (EUID)

Used primarily for permission checks. In most cases, the Euid and UID are the same, but if the setuid bit of the executable is valid, a user other than the owner of the file runs the program, EUID and UID are different, that is, when an executable file has the Setgid bit set, any user (including root) runs the program. Its effective user group Euid is the owner of the file. Sunch as:

Pay attention to the particularity of setuid, this will be specially explained later.

Process user group number (GID)

The group number of the user who created the process is the user group number (GID) of the process, and you can call the Getgid () function to get the real user group number for the current process.

See also: http://pubs.opengroup.org/onlinepubs/009695399/functions/getgid.html

The following is a sample program that reads the uid/gid/euid/egid of the current process:

#include <stdio.h> #include <unistd.h>int main (int Argc,char *argv[]) {printf ("\tuid\tgid\teuid\tegid\n") ;p rintf ("parent\t%d\t%d\t%d\t%d\n", Getuid (), Getgid (), Geteuid (), Getegid ()), if (fork () ==0) {printf ("child\t%d\t%d\ T%d\t%d\n ", Getuid (), Getgid (), Geteuid (), Getegid ());} return 0;}


Next

Process Management and control

Linux Special Processes


Reprint please indicate the source, http://blog.csdn.net/suool/article/details/38406211, thank you!

Related Article

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.