Author : Hoyt Email:hoytluo@21cn.com
Foreword : This article is to introduce the concept of Linux and related to the process. We will learn : The process concept The identity process The creation process Guardian of the creation process
--------------------------------------------------------------------------------
1. The process concept Linux is designed for multiple users. At the same time many users to the OS can be issued various orders. So how is the multi-user operating system environment? In modern operating systems which have the concept of procedures and processes. What is the procedure. What is the process? Speakers included is a simple procedure code can be implemented in the paper is a static document. and the implementation process is a beginning but no end of examples of the procedure. Executable File is a concrete realization. a procedure may be many process, and each process may have many elements of a process. followed by the cycle continues. process and have children and grandchildren. When the procedure call system to memory, the system will process the allocation of certain resources (memory, equipment, etc.) and then carried out a series of complicated operations, procedures into the process for the system call. in the process of not only the system procedures, in order to distinguish between the various process a distribution system to a process ID (as our ID cards) for the identification. In order to fully utilize resources, the system also in the process of the distinction between the different state. will be divided into the new process, running, blocking, and completed five for the state. , said the new process is being established, and operation of process is running. the process is blocked waiting for a case, are waiting for the CPU system is said to implement the order, said the process has been completed is the end of the recovery system resources. five state the details of the process we can look at the "operating system" above a detailed explanation. 2. Signs process We know there is a process ID of the above, then how we get the process ID? Getpid system can be deployed to the process ID, and the father can be getppid process (process of the process of creating the function call) ID.
#include "Unistd>
Pid_t getpid (void); Pid_t getppid (void);
Procedures for the process and procedures for users of services. To find the process of system users, and and the process for users to establish links. The users call the owners of the process. corresponding to each user has a user ID. getuid system can be deployed by the owner of the ID process. Due process had to use some resources on the Linux system and the protection of resources, To have an effective user access to certain resources for the process ID. The ID system and the use of resources. involved in the process to the authority. System Call geteuid we can get through the process of effective user ID. There should be a process relatively user ID and group ID and group ID System Call getgid efficient and effective getegid will receive group ID and group ID #include "Unistd> #include "Sys/types.h>
Uid_t getuid (void); Uid_t geteuid (void);
Gid_t getgid (void); Git_t getegid (void);
Sometimes, we will provide the information to other interested users (who download, etc.), this time we have been able to redeploy to be getpwuid. Passwd struct ( */ Char name posted *pw_name; LEAVES OF 13 SPECIES OF LAURACEAE LEAVES OF 13 SPECIES OF LAURACEAE download password */ char *pw_passwd; Uid_t user ID */ pw_uid; LEAVES OF 13 SPECIES OF LAURACEAE Gid_t pw_gid; LEAVES OF 13 SPECIES OF LAURACEAE User Group ID */ LEAVES OF 13 SPECIES OF LAURACEAE users real name */ char *pw_gecos; Contents */ char users *pw_dir; LEAVES OF 13 SPECIES OF LAURACEAE Shell */ char users *pw_shell; LEAVES OF 13 SPECIES OF LAURACEAE };
#include "Pwd.h> #include "Sys/types.h>
*getpwuid Passwd struct (uid_t uid);
Now we learn about a typical practice above our study of several functions : #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:%ldn" my_pid); Printf ( "Parent ID:%ldn" parent_pid); Printf ( "User ID:%ldn" my_uid); Printf ( "Effective User ID:%ldn" my_euid); Printf ( "Group ID:%ldn" my_gid); Printf ( "Effective Group ID:%ldn" my_egid) :
If (my_info) {
Printf ( "My Login Name:%sn" my_info->pw_name); Printf ( "Password : My %sn" my_info->pw_passwd); Printf ( "My %ldn User ID :" my_info->pw_uid); Printf ( "My Group ID : %ldn" my_info->pw_gid); Printf ( "My Real Name:%sn" my_info->pw_gecos); Printf ( "My Home Dir : %sn" my_info->pw_dir); Printf ( "My Work Shell:%sn" my_info->pw_shell); }
}
3. The creation process The process of creating a system call is very simple. If we can call it a fork function.
#include "Unistd.h>
Pid_t fork ();
Calling the process when a fork, the system will establish a sub-process. Father and son of the process only difference is that the process of his father in the process ID and ID process. the other is the same. As at cloning process (Clone) himself. Of course, the creation of two identical process is meaningless. To distinguish between father and son process process We must fork tracking the return value. When the fork away with a time of failure (or lack of memory is the largest user has to process several) return -1 fork, the fork has an important role to return to duty. Father fork process for the return of the ID process son. return 0 fork process for the son. This is in accordance with our duty to return the process to distinguish between father and son. Why is the process of founding father to son process? As we have said Linux is a multi-user operating system, At the same time there will be a lot of system resources for users. Sometimes the process of creating sub-tasks to finish the process early to scramble for resources. Once the process son was established, together with his father from the fork to continue the process of implementation of the competitive system resources. Sometimes, we hope to continue the process son. Father and son process until the process is complete obstruction task. this time, we are able to redeploy or waitpid wait system call. #include "Sys/types.h> #include "Sys/wait.h>
Pid_t wait (int *stat_loc); Pid_t waitpid (pid_t pid, *stat_loc int, int options);
Calling the process of blocking system will wait until a father or son is the father of process arising from the end of the process a signal. If there is no process or sub-process did not father the son he wait to return immediately to put an end to the process. Successful (by the end of a sub-process) wait son will return to the ID process, or else return -1, errno.stat_loc son is variable and set up the overall process from the state. Calling the process son exit, or return to set up the _exit value. To get the definition of the value of Linux to test the return value of several Acer. WIFEXITED : Judging from the value of non-process elements 0 WEXITSTATUS : Judging from the value of the sub-process (process from the time when the sub-0). WIFSIGNALED : son of the process because there was no signal from. WTERMSIG not process the signal elements : (meaningful only in WIFSIGNALED true). Waitpid wait until the designated sub-sub-process back on the process. If it was waiting for the specified process pid (pid). 0 if no one is waiting and caller ID, group ID of the same group in the process. 1 pm to wait for the same call. Wait any ID when a group of less than 1 equivalent to the absolute value of pid process. Stat_loc and wait in the same sense. Father of the state process can decide options. WNOHANG can take two values : When the father did not immediately return process son process exists. WUNTACHED waitpid end of the process : When the son returned, but the process elements can not get from the state. Father of the process of creating sub-process, the implementation of the different elements to the process of general procedures. To transfer system procedures, we can use the nation Call exec system call. Calling the nation have five exec function. #include "Unistd.h>
Int execl (const char *path, const char *arg that. . . ); Int execlp (const char *file, const char *arg that. . . ); Int execle (const char *path, const char *arg that. . . ); Int execv (const char *path, char *const argv[]); Int execvp (const char *file, *const argv[] char) :
Calling procedures can be implemented to exec nation. Calling on the nation exec detailed explanation can reference manual (man execl). Here is an example of learning. -lm Attention to the translation when necessary to connect Math Library.
#include "Unistd.h> #include "Sys/types.h> #include "Sys/wait.h> #include "Stdio.h> #include "Errno.h> #include "Math.h>
Void main (void) {
Pid_t child; Int status;
Printf ( "This will demostrate how to get child statusn"); If ((child=fork ()) ==-1) {
Printf ( "Error : %sn Fork", strerror (errno)); Exit (1); }
Else if (child==0) {
Int Rifa Printf ( "I am the child:%ldn", getpid ()); For (i=0;i<1000000;i++) sin (i); I=5; Printf ( "I exit with %dn", i); Exit (i); }
While (((child=wait (&status)) ==-1) & (errno==EINTR)); If (child==-1) Printf ( "Wail Error:%sn", strerror (errno)); Else if (!status) Printf ( "Child %ld terminated normally return status is zeron" Child); Else if (WIFEXITED (status)) Printf ( "Child %ld %dn status is terminated normally return." Child, WEXITSTATUS (status)); Else if (WIFSIGNALED (status)) Printf ( "Child %ld terminated due to signal %d znot caughtn" Child, WTERMSIG (status)); }
Strerror function will return a specific issue of the wrong message to the wrong string. 4. Guardian of the creation process If you are in the preparation process is DOS era. DOS then you may be aware of procedures for the preparation of a permanent memory we must prepare a code number. If Linux contrary to prepare a "permanent memory" of the process is very easy. It can be done as long as we have a few lines of code. Because Linux is actually multi-task operating system. We can code is not prepared to carry out a procedure put in the background. If we sign the order, insert & Shell will bring to our operating procedures into the background. Here we have the "development" of a background check mail procedures. This procedure each for a specified time we go back and check boxes. If we have found the mail, the alarm will continue (through a loudspeaker to voice box). behind a strengthened version of this strengthened version of the function The founding father of the process of thinking : First, the background process to create a sub-process. Son killed his father and then the process of the process (which is not very harsh? ). Signal processing by the sub-process to handle all the work.
#include "Unistd.h> #include "Sys/types.h> #include "Sys/stat.h> #include "Stdio.h> #include "Errno.h> #include "Fcntl.h> #include "Signal.h>
LEAVES OF 13 SPECIES OF LAURACEAE Linux-mail address is a tacit term /var/spool/mail/ individual users who download */
# MAIL "/var/spool/mail/hoyt"
LEAVES OF 13 SPECIES OF LAURACEAE sleep 10 seconds */
# 10 SLEEP_TIME
Main (void) {
Pid_t child;
If ((child=fork ()) ==-1) {
Printf ( "Fork Error:%sn", strerror (errno)); Exit (1); }
Else if (child>0) While (1); If (kill (getppid (), SIGTERM) ==-1) {
Printf ( "Kill Parent Error:%sn", strerror (errno)); Exit (1); }
{
Int mailfd;
While (1) {
If ((mailfd=open (MAIL, O_RDONLY)) !=-1) {
Fprintf (stderr, "%s", "07"); Close (mailfd); }
Sleep (SLEEP_TIME); }
}
}
You can build your mail acquiescence under the path, and then to test this procedure. Of course, this process there are many areas to improve. We will be behind the small improvement in the process. I see the improvement before you can try to improve. For example, enable users to designated trails and the Post-phase sleep schedules. believe they can do so. gets right, brave explorer. The well as a section of the process we learn here first. Process is a very important concept, and many of the procedures used to process son. The creation of a sub-process is the basic requirement of every programmer!
|