Linux Program Design entry system process details

Source: Internet
Author: User
Article Title: a detailed introduction to the system process for getting started with Linux programming. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

1. Process Concept

The Linux operating system is intended for multiple users. At the same time, many users can issue various commands to the operating system. How does the operating system implement multi-user environments? In modern operating systems, there are procedures and processes. What are programs and processes? In general, a program is a file that contains executable code and is a static file. A process is an instance of a program that has started execution but has not ended. is the specific implementation of executable files. A program may have many processes, and each process may have many sub-processes. and generate child processes. after a program is called to the memory by the system, the system allocates certain resources (memory, device, and so on) to the program and then performs a series of complex operations to turn the program into a process for the system to call. in the system, only

A process has no program. to distinguish different processes, the system assigns an ID (like our ID card) to each process for identification. to make full use of resources, the system also differentiates processes from each other. processes are divided into five states: new, running, blocking, ready, and completed. "New" indicates that the process is being created, "running" means that the process is running, "blocking" means that the process is waiting for an event, and "ready" means that the system is waiting for the CPU to execute the command, completion indicates that the process has ended and the system is recycling resources. for a detailed explanation of the five States of a process, see operating system.

2. Process flag

As we know above that all processes have an ID, how can we get the ID of the process? The system calls getpid to get the ID of the process, while getppid can get the ID of the parent process (the process that creates the process that calls this function.

       
        #include <unistd> pid_t getpid(void);             pid_t getppid(void);
       

The process serves the program, and the program serves the user. in order to find the User Name of the process, the system also establishes contact with the user for the process. this user is called the owner of the process. each user also has a user ID. the system calls getuid to obtain the ID of the process owner. because the process uses some resources, and Linux protects system resources, there is a valid user ID to obtain certain resource processes. this ID is related to the usage of system resources and involves process permissions. the system calls geteuid to obtain the valid user ID of the process. the process corresponding to the user ID has a group ID and a valid group ID. The system calls getgid and getegid to obtain the group ID and the valid group ID respectively.

       
        #include <unistd> #include <sys/types.h> uid_t getuid(void); uid_t geteuid(void); gid_t getgid(void);             git_t getegid(void);
       

Sometimes we are also interested in other user information (login name, etc.). At this time, we can call getpwui

D.

       
        
Struct passwd {char * pw_name;/* logon name */char * pw_passwd;/* logon password */uid_t pw_uid;/* User ID */gid_t pw_gid; /* User Group ID */char * pw_gecos;/* User's real name */char * pw_dir;/* User's directory */char * pw_shell; /* User's SHELL */}; # include <pwd. h> # include <sys/types. h> struct passwd * getpwuid (uid_t uid );
       

Next we will learn an example to practice the functions we have learned above:

       
        #include <unistd.h> #include <pwd.h> #include <sys/types.h> #include <stdio.h> int main(int argc,char **argv) { pid_t my_pid,parent_pid; uid_t my_uid,my_euid; gid_t my_gid,my_egid; struct passwd *my_info; my_pid=getpid(); parent_pid=getppid(); my_uid=getuid(); my_euid=geteuid(); my_gid=getgid(); my_egid=getegid(); my_info=getpwuid(my_uid); printf("Process ID:%ld ",my_pid); printf("Parent ID:%ld ",parent_pid); printf("User ID:%ld ",my_uid); printf("Effective User ID:%ld ",my_euid); printf("Group ID:%ld ",my_gid); printf("Effective Group ID:%ld ",my_egid): if(my_info) { printf("My Login Name:%s " ,my_info->pw_name); printf("My Password :%s " ,my_info->pw_passwd); printf("My User ID :%ld ",my_info->pw_uid); printf("My Group ID :%ld ",my_info->pw_gid); printf("My Real Name:%s " ,my_info->pw_gecos); printf("My Home Dir :%s ", my_info->pw_dir); printf("My Work Shell:%s ", my_info->pw_shell); }             }
       

3. Process Creation

It is easy to create a system call for a process. You only need to call the fork function.

       
        #include <unistd.h>             pid_t fork();
       

After a process calls fork, the system creates a sub-process. this sub-process is different from the parent process only by its process ID and parent process ID. Others are the same. just like clone itself. of course it makes no sense to create two identical processes. to distinguish Parent and Child processes, we must track the return values of fork. when the fork fails to be used (the memory is insufficient or the maximum number of processes has reached), fork returns-1. Otherwise, the return value of fork plays an important role. for the parent process fork, return the child process ID, and for the fork sub-process, return 0. the Parent and Child processes are differentiated based on the returned values. why should a parent process create a child process? We have already mentioned that Linux is a multi-user operating system, and many users compete for system resources at the same time. sometimes a process creates a sub-process to compete for resources to complete the task earlier. once a child process is created, the Parent and Child processes run from the fork together to compete for system resources. sometimes we want the sub-process to continue execution, and the parent process is blocked until the sub-process completes the task. in this case, we can call wait or waitpid.

       
        #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *stat_loc);             pid_t waitpid(pid_t pid,int *stat_loc,int options);
       

[1] [2] [3] Next page

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.