System functions, fork functions, exec Functions

Source: Internet
Author: User

How to rename an object?

If you need to copy or delete an object, the fastest way is to rename the object. The time required is in a CPU cycle and is an atomic operation.

For example, to log on to and log out of the log file management system, you need to count the daily log file information. At this time, you need to copy the System Log File wtmpx, however, if you copy one piece of data or one byte, the efficiency will be very low. In this case, users may log on to or log out of the system and need to use Log File records, at this time, because you are using this log file, it will become a conflict. At this time, file locks cannot be used to solve the problem. Because the log file is used by the system, you cannot negotiate with the kernel to use the lock protocol. The best way is to rename the log file.

 

# Include <stdio. h>
# Include <unistd. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <sys/types. h>
# Include <sys/Wait. H>
# Include <stdlib. h>
Int main ()
{
Char P [20] = "tTwo. c ";
Char PT [20] = "Two. c ";

Int pid = fork ();

If (PID <0)
{
Return-1;
}
Else if (pid = 0)
{
Execl ("/bin/mv", "mv", PT, P, null );
Exit (0 );
}
Int status;
Waitpid (pid, & status, 0 );
Printf ("open ttwo. c \ n ");
Int fd = open (p, O_RDONLY );
If (fd> 0)
Printf ("open success \ n ");
Close (fd );
Return 0;
}

 

 

(1) exec function family description
The fork function is used to create a sub-process. The sub-process copies almost all the content of the parent process.
How to execute the newly created process? This exec function family provides
Method. It can locate the executable file based on the specified file name or directory name and use it to replace the data of the original calling Process
After the execution, all the content of the original calling process except the process number is added to

. In addition, the executable file can be either a binary file or any executable file in Linux.
Script file.
In Linux, there are two main scenarios for using the exec function family:
· When a process deems itself unable to make any contribution to the system and users, it can call any exec function family.
Let yourself be reborn;
· If a process wants to execute another program, it can call the fork function to create a new process, and then
Call any exec, which looks like a new process is generated by executing the application. (In this case
Very common) (2) exec function family syntax
In fact, there is no exec () function in Linux, but there are 6 Function Families starting with exec.
There are nuances in the method, which will be explained in detail below. Header file required for the exec function family member function syntax # include <unistd. h> function prototype int execl (const char * path, const char * Arg ,...)
Int execv (const char * path, char * const argv [])
Int execle (const char * path, const char * Arg,..., char * const envp [])
Int execve (const char * path, char * const argv [], char * const envp [])
Int execlp (const char * file, const char * arg ,...)
Int execvp (const char * file, char * const argv []) function return value-1: The six functions have minor differences in function names and syntax rules, the following describes how to find executable files,
The parameter table transmission mode and environment variables are compared.
· Search methods
You can note that the first four functions in Table 7.3 are searched in a complete file directory path, and the last
Two functions (two functions ending with p) can only provide the file name, and the system automatically starts from the environment variable "$ PATH"
Find the path.
· Parameter transmission mode
There are two ways to pass parameters in the exec function family: one is to list them one by one, and the other is to list all parameters.
The entire structure pointer array is passed.
It is distinguished by the 5th-bit letters of the function name. the letter "l" (list) indicates listing one by one,
Its syntax is char * arg; the letter "v" (vertor) indicates that all parameters are transmitted in an array of constructed pointers. Its Syntax
It is * const argv []. Readers can observe the differences between the syntax of execl, execle, and execlp and that of execv, execve, and execvp.
Their specific usage will be illustrated in the subsequent examples.
The parameters here are actually all the Command Option strings required by the user to use this executable file (including
The executable program command itself ). Note that these parameters must end with null. If you use columns one by one
For example, force convert it into a character pointer. Otherwise, exec will interpret it as an integer parameter. If the length of an integer char * is different, the exec function returns an error.
· Environment Variables
The exec function family can default the system environment variables or pass in the specified environment variables. Here we use "e"
The two functions execle and execve at the end of enviromen can specify the environment used by the current process in envp [].
Variable. Exec function name
The first four bits are: execl: 5th bits. The parameters are passed to the list-by-list method: execl, execle, and execlp.
V: The parameter is passed to execv, execve, and execvp 6th-bit E for constructing the pointer array. The new process environment variables execle and execve can be passed.
P: Find executable files by file name execlp, execvp

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.