[Linux & amp; Unix -- exec and fock System Call], linux operating system

Source: Internet
Author: User

[Linux & Unix -- exec and fock System Call], linux operating system

If you have any shortcomings, please kindly advise. Reprinted Please note: @ CSU-Max


Series of blog posts:

Linux and Unix -- file descriptors and permissions

  The second bullet in Linux and Unix Learning -- file descriptor and permissions


Processes and programs

Simply put, the difference between a process and an application is that an application is stored as a static file in a hard disk or other storage space of a computer system, A process is a system resource management entity maintained by the operating system under dynamic conditions.

(1) The process can describe concurrency more realistically, but the program cannot.
(2) A process is composed of two parts: a program and data. A process is the basic unit for competing with the limited resources of a computer system and scheduling of a process processor.
(3) A program is a static concept; a process is a process of executing a program on a processing machine and a dynamic concept.
(4) A process has a life cycle and has its birth and death. Is short, and the program is relatively long.
(5) One program can be used as the running program of multiple processes. One process can also run multiple programs.
(6) The process has the function of creating other processes, but the program does not.


Exec and fock

The exec system calls to reinitialize the process from the specified program. Although the process is still running, the program has changed.

The fock system call only creates a new process cloned from an existing process by copying commands, user data, and system data segments. The new process is not obtained from program initialization, therefore, the old and new processes execute the same commands.

In addition to starting the UNIX kernel itself, exec is the only method for the program to obtain execution on UNIX. Not only does shell use exec to execute the program, but also the shell and its ancestor shell will be called by exec. Fock is the only way to create a new process.

 

Details about exec system calls

In fact, there is no system call called "exec". The so-called "exec" system call is actually six system calls named in the form of execAB. The matching method is shown in:


Here, A can be l or v, depending on whether the parameter is directly in the call (list) or in the array (vector); B either does not exist or is p, p indicates to use the path environment variable to find the program, or e indicates to use the specific environment. (The user cannot obtain both feature p and feature e in the same call. Therefore, these six calls are execl, execv, execlp, execvp, execle, and execve respectively.

 

Function prototype and interpretation

// Execl -- execute the int execl file with the parameter list (const char * path,/* program pathname */const char * arg0,/* first arg (file name) */const char * arg1,/* second arg (if needed )*/..., (char *) NULL/* arg list terminator */);

The path parameter is the path name of a program file that can be executed by a valid user ID (for example, mode 755). The content of the executable program must be correct.

The group of parameters following the path parameter is included in a character pointer array, and the value of the last parameter is NULL, which is used to stop the income and terminate the array, the first of these parameters is the name of the program file (not the entire path ).

By re-initializing the stack, the commands from the program overwrite the command segment of the process, and the data from the program overwrites the user data of the process, and then the process executes the new program from the top (that is, call its mian function ).

 

Other 5 exec system calls

// Execv -- execute the file int execv with parameter vectors (const char * path,/* program pathname */char * const argv []/* argument vector */); // execlp -- execute the int execlp (const char * file,/* program file name */const char * arg0, /* first arg (file name) */const char * arg1,/* second arg (if needed )*/..., (char *) NULL/* arg list terminator */); // execvp -- execute the file int execvp (const char * file with parameter vectors and path searches, /* program file name */char * const argv []/* argument vector */); // execle -- execute the int execle (const char * path,/* program pathname */const char * arg0,/* first arg (file name) file with the parameter list and Environment Variables) */const char * arg1,/* second arg (if needed )*/..., (char *) NULL/* arg list terminator */char * const envv []/* environment vector */); // execve -- execve the int execve (const char * path, /* program pathname */char * const argv []/* argument vector */char * const envv []/* environment vector */);

Note: execlp and execvp. We can see from the previous introduction that when B is p, it indicates TO USE THE PATH environment variable to find the program. Let's take a simple look.

If the file parameter of execlp or execvp has no diagonal lines, compare the strings listed in the PATH environment variables one by one until a common file with the result PATH is located, the result path has the execution permission.

Scenario 1: If the file contains an executable program (indicated by a code number in its first word), it will be executed;

Scenario 2: if a program is not included, it is assumed as a script file. Generally, to run this script file, the path is used as the first parameter of shell to execute shell.

If the PATH search result proves that nothing is executable, exec fails.

If the file parameter contains "/", you do not need to perform the search. That is, the path search is completed, but it may still be a script file.

 

Detailed description of fock system calls

To some extent, we can think of fock and exec as the opposite: fock can create a new process, but it does not initialize the process in the new program. On the contrary, commands of new processes, user data segments, and system data segments are almost full copies of old processes.

 

Function prototype and interpretation

// Fock -- create a new process pid_t fock (void );

After a fock call returns a result, both processes (child and parent processes) receive the returned value, but the returned value is different. The parent process accepts the ID of the child process, the returned value received by the sub-process is 0. if "-1" is returned, it indicates an error. However, because the fock does not have a parameter, there are very few errors. the only cause of the error is resource depletion, such as insufficient swap space or too many processes.

After the fock call is successful, the child process generally executes exec, and the parent process either waits for the child process to terminate or leaves to do other things.

 

Parent and Child processes under the fock call

The child process created by fork inherits most of the attributes of the parent process, because its system data segment is copied from the parent process. However, there are also very few properties that are not inherited: child process IDs are different from parent process IDs, they are different processes; if the parent process is running multiple threads, only the thread that executes the fock will exist in the child process, and multiple threads in the parent process will not be affected. The cumulative execution time of the Child process will be reset to zero, because it is at the beginning of its life cycle.



**************************************** ***********************************

* Reprinted please indicate the source: @ CSU-Max http://blog.csdn.net/csu_max *

**************************************** ***********************************







In Linux, how do I switch directories?

Today, I saw the "quick query manual for Linux commands", which is exactly the same. Copy it.
[Syntax] cd [Directory]
[Function description] the cd command changes the current directory to the specified directory. The HOME variable is the default directory path.
[Execution example]
[Example 15] change the current directory.
[Root @ localhost root] # pwd // display the current directory
/Root
[Root @ localhost root] # cd/home/kk // change the directory to/home/kk
[Root @ localhost kk] # pwd // display the current directory
/Home/kk
[Root @ localhost kk] #
When the current directory is displayed for the first time, it is the/root directory. Run the command cd/home/kk to change the current path to/home/kk. When the command pwd is used to display the current directory, it is/home/kk.
[Related commands]
Pwd

Linux and embedded linux

When it comes to embedded Linux, I always think of "cropping" and "castrated? If you understand this, it means that you still have some misunderstandings about the embedded system, which is not conducive to the improvement of your knowledge structure...

What is embedded? Although there is no standard definition at present, it is widely believed that computer systems specifically implemented for a specific application scenario are all called embedded systems. Four vrouters, mobile phones, tablets, and even PC boards are embedded systems. It can also be said that apart from the PCs, workstations, and servers you use, they can all be summarized into fields such as embedded systems.

In embedded systems, some have operating systems, and some do not. Although Single-Chip Microcomputer still plays a very important role and share in embedded applications, from the perspective of employment development of software engineers, embedded systems with operating systems undoubtedly have a wider space for development.

In embedded operating systems, there are various RTOS (the most famous one is VxWorks ), there are also some OS with good real-time performance (the newer version 2.6 and later versions of Linux can be planned here), and some OS with poor real-time performance (version 2.4 of Linux kernel, windows and so on can be included here ).

Therefore, according to the application scenario, the operating system may not be very complex. Therefore, embedded Linux may not be an "incomplete" or "simplified" Linux that "caws major functions...

After you correct the concept and unify your understanding, you will certainly not ask again: Is it true to learn "pure" Linux or to learn embedded Linux? This problem is meaningless. In fact, whether it is the kernel, the libc library, or various applications, PC and embedded knowledge are common and there is no strict boundary. The difference between embedded Linux and Linux on PC lies more in the selection of drivers, local compilation, cross compilation, and UI interface. There is no difference in their essential technologies.

You may need to crop a small amount of data on the embedded system. You can also Crop Data on the PC. This does not mean to caster important functions, because the work you do on the PC will also be achieved on mobile phones, tablets, and other complex mobile terminals ~~~
(If you only want to implement a simple function, I suggest you do not study or use Linux )~~

Therefore, the final conclusion is: If you are proficient in Linux, you are proficient in both Linux on PC and embedded Linux. Embedded and PC are only different application scenarios (more reflected in the differences between various programs in the user space ).

To become a kernel developer:
If you are interested in the kernel, we recommend two classic books: LInux kernel and LInux driver. These two books are the core developers who read them once, 10 times, and 100 times. They will still read them carefully and try to understand the classic of each word.
In addition, I personally recommend an in-depth Linux kernel architecture. This book is written by a German. It is a good self-learning material for those with weak foundations.
If it looks hard, we recommend that you start with "Linux kernel design and implementation. You can refer to the three books mentioned above without a thorough explanation in this book. If this book is hard to read, we suggest you read "Linux kernel 0.11 full note (Zhao Xiao)". If this book is hard to use, I can't help it. I suggest you read some popular articles on Linux first.
In addition, reading any book will never replace reading the kernel source code ~~~

If you want to develop applications, you can't read the advanced programming in UNIX environment. The rest depends on your application.

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.