Getting Started with Linux system programming details of the process



1, the concept of process

Linux operating system for multi-user. At the same time, many users can issue various commands to the operating system. Then the operating system how to achieve multi-user environment? Which in modern operating systems have procedures and processesconcept. So what is the procedure, what is the process? popular talk program is a file that contains the code can perform, is a static file. The process is a beginning but no end of the implementation of the program instance. is an executable filethe specific implementation. a program may have many processes, but each process and can have many sub-processes. followed by the cycle continues, and the process of generating offspring. When the program is the system call to memory, the system will allocate a certain program resources (memory,, equipment, etc.) and then a series of complex operations, making the program into a process for the system call. in the system which only

There is no procedure process, in order to distinguish between different processes, the system assigns to each process, a ID (like our ID cards) for identification. In order to fully utilize resources, the system also distinguish between the different process states. The processinto new, running, blocking, ready and complete the five states. New said that the process is being created, run a process is running, blocking is the process is waiting for a certain event occurs, the system is in place is waiting for CPU to execute the command, andcompletion of that process has concluded the system is recycled resources. on the process, a detailed explanation of the five states that we can see the "Operating System" a detailed explanation of the above.

2, the process of mark

We know that the process above has a ID, then how do we get the process ID it? System call getpid can get the process ID, and getppid get parent process (the process of creating the process of calling the function) of the ID.

# Include

pid_t getpid (void);

pid_t getppid (void);

Process for program services, and program services to users. The system processes in order to find the user name, but also for processes and users to establish contact. The user called the process owner. Corresponding each user has a user ID.getuid system call can get through the process owner ID. due process to use some resources, but Linux is the protection of system resources, in order to access certain resources in the process there is a valid user ID. The use of ID and system resourcesrelated to the rights related to the process. geteuid system call through the process we can get the effective user ID. and the user ID and a corresponding process group ID and effective group ID system calls getgid and getegid can get group ID, respectively, and the effective group ID.

# Include

# Include

uid_t getuid (void);

uid_t geteuid (void);

gid_t getgid (void);

git_t getegid (void);

Sometimes we would be interested in additional information on the user (login name, etc.), this time we can call getpwui

d to get.

struct passwd {

char * pw_name; / bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var/ vmware login name backup / bin / conf / data / log / maint / svn / tmp /

char * pw_passwd; / bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var/ vmware login password backup / bin / conf / data / log / maint / svn / tmp /

uid_t pw_uid; / bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var /vmware user ID backup / bin / conf / data / log / maint / svn / tmp /

gid_t pw_gid; / bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var /vmware user group ID backup / bin / conf / data / log / maint / svn / tmp /

char * pw_gecos; / bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var/ vmware user's real name backup / bin / conf / data / log / maint / svn / tmp /

char * pw_dir; / bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var/ vmware user's directory backup / bin / conf / data / log / maint / svn / tmp /

char * pw_shell; / bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var/ vmware user's SHELL backup / bin / conf / data / log / maint / svn / tmp /

};

# Include

# Include

struct passwd * getpwuid (uid_t uid);

Here we learn to practice an example of what we learn here is that several functions:

# Include

# Include

# Include

# Include

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, the process of creating

System call to create a very simple process. We call the fork as long as the function on it.

# Include

pid_t fork ();

When a process calls fork, the system creates a child process. The child process and the different places the parent process ID, and only his parent process ID, the other is the same. Just as the process of cloning operator (clone) as their own. of course, create two identical process is not meaningless. In order to distinguish between parent and child process, we must follow the return value of fork. When the failure when using fork out (of memory or the maximum number of processes the user has to) fork returned-1, otherwise the return value of fork play an important role. For the parent fork returns child process ID, and for the child fork returns 0. We return value is based on this process to distinguish between father and son. Why should we create a parent process child processit? we have said before Linux is a multi-user operating system, at the same time there will be many users competing for system resources. Sometimes the process early in order to create a child process to complete the task to compete for resources. Once the child process is created, together with his son from the fork at the process to continue, competing system resources. Sometimes we want the child process to continue, and the parent process blocks until the child process to complete the task. this time we can call wait or waitpid system calls.

# Include

# Include

pid_t wait (int * stat_loc);

pid_t waitpid (pid_t pid, int * stat_loc, int options);

wait system call will block until a parent or a parent child process to finish the process received a signal. If there is no parent or no child of his child back over the wait to return immediately. successful (the end result of a child process) wait to return to the child process ID, otherwise it returns -1 and sets the global variable errno.stat_loc a child's exit status. child process calls exit, _exit, or return to set this value. To get the value of the Linux defines severalmacro to test the return value.

WIFEXITED: to determine the value of the child process to exit non-0

WEXITSTATUS: determine the child's exit value (the time when the child process to exit non-0).

WIFSIGNALED: child process did not get a signal due to the exit.

WTERMSIG: child did not get the signal number (in the true sense when WIFSIGNALED).

waitpid to wait until the child process specified by the child process to return. If pid is positive then the wait for the specified process (pid). If any one group of 0 to wait for the caller ID and the same process group ID. is equal to -1 whenwait calls. is less than -1 when the wait for any one group ID is equal to the absolute value of the process pid. stat_loc and wait the same meaning. options can determine the status of the parent process. You can take two values WNOHANG: parent process child process to return immediately when there is noexist. WUNTACHED: waitpid end of the process when the child returns, but the child's exit status should not be. parent process creates a child process, the child process to perform different procedures generally. In order to call system procedures, we can use the system

Call the exec family of system calls. Exec family of calls has five functions.

# Include

int execl (const char * path, const char * arg ,...);

int execlp (const char * file, const char * arg ,...);

int execle (const char * path, const char * arg ,...);

int execv (const char * path, char * const argv []);

int execvp (const char * file, char * const argv []):

exec family of calls to perform a given procedure. on the exec family of calls detailed explanation can refer to system manual (man exec

Here we learn an example. Note compile time in order to connect to add-lm math library.

# Include

# Include

# Include

# Include

# Include

# Include

void main (void)

{

pid_t child;

int status;

printf ("This will demostrate how to get child status");

if ((child = fork ())==- 1)

{

printf ("Fork Error:% s", strerror (errno));

exit (1);

}

else if (child == 0)

{

int i;

printf ("I am the child:% ld", getpid ());

for (i = 0; i <1000000; i + +) sin (i);

i = 5;

printf ("I exit with% d", i);

exit (i);

}

while (((child = wait (& status ))==- 1) & (errno == EINTR));

if (child ==- 1)

printf ("Wait Error:% s", strerror (errno));

else if (! status)

printf ("Child% ld terminated normally return status is zero",

child);

else if (WIFEXITED (status))

printf ("Child% ld terminated normally return status is% d",

child, WEXITSTATUS (status));

else if (WIFSIGNALED (status))

printf ("Child% ld terminated due to signal% d znot caught",

child, WTERMSIG (status));

}

strerror function returns a specified number of error messages the error string.

4, the creation of the daemon

If you are ever written in the DOS era program, then you probably know that in DOS, memory resident to write a program we want to write much code. Conversely, if prepared in Linux a "permanent memory" of the program is veryeasy.As long as we can do a few lines of code. In fact due to Linux is a multitasking operating system, we do not write code that can be put into the background to the implementation of a program. We simply SHELL command will be followed by the & symbolour program to run in the background. Here we "develop" a background check e-mail program. The program for a specified time each go back and check our mailbox, if we find we have a message, and will continue to alarm(through a small speaker on the chassis to sound.) behind the enhanced version of this function version of the background to enhance the process of creating ideas: First, the parent process creates a child process. and then kill the parent process child process (is not it cruel?).signal processing all the work handled by the child process.

# Include

# Include

# Include

# Include

# Include

# Include

# Include

/ * Linux's default of any individual's email address is / var / spool / mail / user login name backup / bin / conf / data / log / maint / svn / tmp /

# Define MAIL "/ var / spool / mail / hoyt"

/ * Sleep 10 seconds to backup / bin / conf / data / log / maint / svn / tmp /

# Define SLEEP_TIME 10

main (void)

{

pid_t child;

if ((child = fork ())==- 1)

{

printf ("Fork Error:% s", strerror (errno));

exit (1);

}

else if (child> 0)

while (1);

if (kill (getppid (), SIGTERM) ==- 1)

{

printf ("Kill Parent Error:% s", 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 create the default path to your mailbox file, and then test this procedure. Of course, this process there are many places to improve. We will later improve this small program, look at me you can try to improve themselves beforeimprove it. For example let the user specify the path and Post-phase sleep and so on. believe that they can do. Go ahead, brave explorers.

Well, we process the contents of the section to be learned here. The process is a very important concept, and many of the program will use the child process. To create a child process is the basic requirement of every programmer!