LinuxC programming-process

Source: Internet
Author: User
Article title: LinuxC programming-process. 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.
I. process
  
1. what is a process?
  
When running any UNIX command, shell will create at least one process to run this command, so any program running in UNIX systems can be called a process; but the process is not a program, processes are dynamic, while programs are static, and multiple processes can concurrently call the same program.
  
Every process in the system contains a task_struct data structure. all pointers to the data structure form a process vector array. the default process vector data size is 512, indicates that the system can accommodate 512 processes at the same time. The task_struct data structure of a process includes the status, scheduling information, process identifier, and other information of the process.
  
Because a UNIX system is a multi-process operating system, every process is independent and has its own permissions and tasks. Therefore, when a process fails, no other process will fail. The system uses process identifiers to identify different processes. process identifiers are non-negative positive numbers and are unique at any time. when a process ends, the process identifier can be assigned to another new process. The system assigns identifier 0 to the scheduling process, and identifier 1 to the initialization process.
  
A process uses many resources, including the most valuable CPU resources, during running. when a process occupies CPU resources, other processes must wait for the idle CPU of the running process to run, because many processes are waiting, the kernel uses the scheduling algorithm to determine which process the CPU is allocated.
  
The system runs in the kernel mode when it is just started. at this time, only one initialization process is running. First, it initializes the system and then executes the initialization program (usually/sbin/init ). The initialization process is the first process of the system. all subsequent processes are subprocesses of the initialization process.
  
2. process creation
  
The process is created by the fock function and defined in the unistd. h library as follows:
  
# Include
Pid_t fock (void );
  
The fock function is called once but returns twice. the ID of the child process is returned in the parent process, and 0 is returned in the process. this is because the parent process may have many child processes, therefore, the returned child process ID must be used to track the child process. The child process has only one parent process, and its ID can be obtained through getppid. First, the program creates a sub-process:
  
# Include
# Include
Main (){
Pid_t pid;
Pid = fork ();
If (! Pid)
Printf ("this is child ");
Else if (pid> 0)
Printf ("this is parent, child has pid % d", pid );
Else
Printf ("fork fail ");
}
  
After fock is called to create a child process, all the opened descriptions in the parent process are shared in the child process. this feature is widely used in network servers, for example, if the parent process returns a socket through the socket function and then calls the fock function to create a word process, the child process can directly operate on the existing socket. Another typical application of fock is to create a sub-process to call the exec function to execute new programs instead of itself.
  
3. process execution
  
The only way to run executable programs stored as files on disks in UNIX systems is to use an existing process to call one of the six exec functions. The six exec functions are defined as follows:
  
# Include
Int execl (const char * pathname, const char * arg0,.../* (char *) 0 */);
Int execv (const char * pathname, char * const argv []);
Int execle (const char * pathname, const char * arg0,.../(char *) 0,
Char * const envp [] */);
Int execve (const char * pathname, char * const argv [], char * const envp []);
Int execlp (const char * filename, const char * arg0,.../* (char *) 0 */);
Int execvp (const char * filename, char * const argv []);
  
In the preceding six exec functions, if the first parameter is pathname, it indicates that the program to be executed is specified by the path name. if it is filename, it indicates that the program is specified by the file name; if the second parameter is an array, it indicates that the parameters of the program to be executed are indexed by an array (the array must contain a null pointer to indicate the end); otherwise, the parameters must be listed one by one; the envp pointer array of execle () and execve () indicates that the two functions indicate that an environment table is specified (this array must end with a null pointer ), the other four functions use the current value of the external variable environ to create an environment table and pass it to the new program.
  
The exec function loads the new program into the memory space of the calling process to change the execution code of the calling process. When the exec function is successfully executed, the calling process will be overwritten, which is equivalent to generating a new process. However, the process table identifier of the new process is the same as that of the calling process, therefore, exec does not create a new process like the fock function, but replaces the calling process with a new process. The following is a fork () program combined with exec:
  
# Include
# Include
Main (){
Int pid;
Pid = fork ();
Switch (pid ){
Case-1:
Perror ("fork failed ");
Exit (1 );
Case 0:
Execl ("/bin/ls", "ls", "-l", NULL );
Perror ("execl failed ");
Exit (1 );
}
}
  
  
2. daemon
  
1. what is a daemon?
  
A Daemon is a process that runs in the background and is out of the terminal. The daemon is separated from the terminal to prevent information in the process from being displayed on any terminal and the process is not interrupted by terminal information generated by any terminal.
  
2. daemon startup
  
Daemon can be started in the following ways:
  
A: The startup scripts are usually stored in/etc/rc. d directory B: start with inetd super server. most network services are started like this, such as ftp and telnet. C: In addition, the process started by cron and started by nohup on the terminal is also a daemon process.
3. daemon error output
  
Because the daemon is isolated from any terminal, no information of the daemon can be directly output to the standard output device or the standard error output device. syslog () is provided in Linux () the system calls to solve this problem. Syslog () is defined in shslog. h as follows:
  
# Include
Void syslog (int priority, char * format ,...);
  
The parameter priority indicates the level and purpose of information to be written by the process. The level is shown in Table 1. the purpose is shown in Table 2:
   
  
The second parameter is a format string that specifies the record output format. at the end of the string, you must specify a % m, which corresponds to the errno error code.
  
4. daemon creation
  
First, fock a sub-process and shut down the parent process. if the process is executed on the console as a shell command, when the parent process is terminated, shell considers that the command has ended, at this time, the child process is automatically converted to the backend operation. because the child process inherits the group identifier and owns its own process identifier in the parent process, it ensures that the child process is not the first process in the process Group; call setsid () to create a new process Group, and call the process to become the first process of the process Group. this leaves the process from the original terminal. then call fock () again (), make the process no longer the first process in the process Group, and ignore the SIGHUP signal to avoid re-connecting to the terminal when the terminal is opened outside the process in some cases; and finally change the working directory, close all opened file handles and open the log system. This daemon is successfully created.
  
  
  
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.