aimode.news
Published on

Going beyond fork() + exec()

Authors

Continue with the article

Since the beginning of Unix, two of the core processes are oriented systems

Calls were fork(), which created a subordinate process as a copy of

the superordinate element and exec(), which performs instead of a new program

the current. In Linux- These system calls are better known than

Cloning()

and execve(),

However, the core functionality remains the same. There is elegance

There are also shortcomings in this process generation model. A current proposal from

Adding “spawn templates” to the kernel is not accepted by Li Chen

in the present form, but it can point the way to a new process creation

primitive in the future. fork() is a relatively expensive system call; it must copy

entire process status (including memory) for the subordinate process. Many

Over the years optimizations have been made, but a fork is still a

in principle costly operation. To make things worse, a fork()

The call often follows an exec(), which is the case

Discard all the memories that have been copied so carefully for the child. Tests (e.g. vfork())

Over the years, work has been done to optimize the pattern in this case

is still more expensive than it could be. Spawn templates

Chen's patch set follows an interesting approach to optimization

fork() and exec() patterns. The focus is on applications

restart the processes on which the same executable file is executed; imagine, for

For example, a program that Git must perform repeatedly to obtain information about it

the content of a repository. In such cases, the programme could set up

template to speed up these calls and distribute the installation costs

several operations. This template would be created with

spawn template create() System call:

struct spawn template create args {

aligned u64 Flags;

s32 exefd;

u32 exec flags;

aligned u64 file name;

/* Some fields have been omitted */

};

int spawn template create(struct spawn template create args *args, size t args size);

This call returns a file descriptor that represents a template for the

executable file that can be specified either as a file descriptor

(execfd) or an absolute path (filename), but not both. To create the template, the kernel opens the specified file and saves one between

A number of information that allows a process to perform this file more frequently

quickly into the future. The application may run a specific executable file several times, but each time

The call differs in many ways. The details of a particular

The call must be placed in an instance of this structure:

struct spawn template spawn args {

aligned u64 Flags;

aligned u64 pidfd;

aligned u64 argv;

aligned u64 envp;

aligned u64 actions;

aligned u64 actions len;

aligned u64 reserved[4];

};

The argv field is a pointer to the argument list to be passed to

the program, while envp refers to its surroundings. Changes

Instead, file descriptors and signal processing are transmitted

Actions that are a pointer to an array of:

struct spawn template action

u32 type;

u32 flags;

s32

s32 newfd;

aligned u64 arg.

};

If, for example, the file descriptor 4 is to be closed in the child, the

The associated spawn template action structure would have

Type set to SPAWN TEMPLATE ACTION CLOSE and fd

set to four. There are other actions to duplicate file descriptors and open

Change files, change work directory and change signal processing. Once spawn template spawn args has been completed,

The new process can be carried out with:

int spawn template spawn(int template fd,

struct spawn template spawn args *args, int args size);

Internally, this system call follows something that comes close to normal

fork()/exec() Path. Chen carefully points out

All normal checks applied when running a new file remain. But the intermediately stored information in the template accelerates the entire process

when it was before. How much faster? The benchmark results indicated in the letter show a

improvement of about 2%, which may not seem to be much, but is quite possible

Make a difference for applications that match the expected pattern. In direction posix spawn()

The most detailed review of this work has been published

of Mateusz Guzik, who said: “This problem is my heart and my heart.”

I've been thinking about it for some time. The entire Fork + Exec

The phrase is terrible and must be abolished

". He pointed out that the

The focus of the patch set was somewhat strange as he left it

fork() is a part of the problem that remains unaffected. There are most

cost lies, he said, so optimization efforts should aim to eliminate them

the picture. Instead of copying the current process, “a” is created

A flawless process is the right way

". Christian Brauner was

positive to the goal and said: “The idea of having a builder API.”

for exec is not so crazy

". His proposal, however, was that it was a new one.

API should build on the existing pidfd abstraction. Without doing so

Until the level of detail he said that the right approach would be to create a

Option for pidfd open()

to create an empty process. A series of calls for a new

The system call pidfd config() would then configure this new process

If desired, setting up the environment, the image to be executed and more. pidfd config() would thus be analogous to fsconfig(). An important goal for a new interface, said Brauner, would be the

Ability to support an implementation of posix spawn()

in the user area. posix spawn() is well suited as a replacement for

the fork()/exec() pattern; Developers would probably

Welcome a native implementation that is not present (as opposed to current)

Implementation) hidden fork() and exec() under the ceiling. Chen agreed

that the API roughly outlined by Brauner seemed better, and said

that future work would go in this direction. So there will be no spawn

templates in the Linux kernel, but if Chen's future work bears fruit,

Linux could finally get a proper posix spawn() implementation instead. Did you like this article?? Request your

Get a free sample subscription now to get even more of it.

Going beyond fork() + exec() | aimode.news