Lightweight alternative to fork() in POSIX C?


Kyle

From the man pages I'm reading, it seems that popen, system, etc. tend to call fork(). In turn, fork() copies the entire memory state of the process. This does seem like a lot of work, especially in many cases where the child calling fork() barely has any memory allocated to the parent.

So my question is, is it possible to get fork()-like behavior without copying the entire memory state of the parent process? Or am I missing something, e.g. fork() is not as heavy as it seems (e.g. maybe inclined to optimize calls to avoid unnecessary memory duplication)?

Basil Starinkevich

From the point of view of a user-space application, fork(2), like all system calls , is a primitive operation (but some C libraries use clone(2) for this ). It's mostly a machine instruction, SYSCALLor a SYSENTERswitch from user mode to kernel mode, so the Linux kernel (latest versions) is doing quite a bit of processing.

In practice, it is very efficient (e.g. less than a millisecond, sometimes even less than a tenth) because the kernel uses the lazy copy-on-write technique extensively to share pages between parent and child processes. The actual copying will happen later due to a page fault when overwriting the shared page .

And forking has huge advantages, since the initiation of some other program is delegated to execve(2) : this is conceptually simple: the only difference between parent and child processes is the resultfork

BTW on POSIX systems like Linux, fork(2) or a suitable clone(2) is the only way to create a process (some weird exception should generally be ignored: the kernel is executing some process like that ...) because vfork (2) Obsolete./sbin/init

Related


Lightweight alternative to fork() in POSIX C?

Kyle From the man pages I'm reading, it seems that popen, system, etc. tend to call fork(). In turn, fork() copies the entire memory state of the process. This does seem like a lot of work, especially in many cases where the child calling fork() barely has any

Lightweight alternative to fork() in POSIX C?

Kyle From the man pages I'm reading, it seems that popen, system, etc. tend to call fork(). In turn, fork() copies the entire memory state of the process. This does seem like a lot of work, especially in many cases where the child calling fork() barely has any

Lightweight alternative to fork() in POSIX C?

Kyle From the man pages I'm reading, it seems that popen, system, etc. tend to call fork(). In turn, fork() copies the entire memory state of the process. This does seem like a lot of work, especially in many cases where the child calling fork() barely has any

Are posix pipes lightweight?

Nils Pipenbrinck : In a Linux application, I use pipes to pass information between threads. The idea behind using pipes is that I can wait on multiple pipes at once using poll(2). This works well in practice, and my thread sleeps most of the time. They only wa

Are posix pipes lightweight?

Nils Pipenbrinck : In a Linux application, I use pipes to pass information between threads. The idea behind using pipes is that I can wait on multiple pipes at once using poll(2). This works well in practice, and my thread sleeps most of the time. They only wa

Are posix pipes lightweight?

Nils Pipenbrinck : In a Linux application, I use pipes to pass information between threads. The idea behind using pipes is that I can wait on multiple pipes at once using poll(2). This works well in practice, and my thread sleeps most of the time. They only wa

Lightweight alternative to Hibernate?

Jared: I have a user Java program that wants to store data in a lightweight database such as Derby or Sqlite. I want to use a data abstraction layer in my program. Hibernate seems to require a lot of configuration and is overkill for what I need. What is a lig

Lightweight alternative dock

Alex: Generally , Jetty is known as a lightweight alternative when it comes to servlet containers like Tomcat and App Servers like Glassfish . I want to run a RESTful service on CloudFoundry. use jetty java -jar target/dependency/jetty-runner.jar target/*.war

Lightweight GNU readline alternative

punekr12 : I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I w

Lightweight multilingual alternative to JMX?

ROM1: I have a software real-time application project that requires extensive monitoring. JMX seems to be a good fit for the task, it's just that the application project is based on c++. Is there a lightweight alternative to JMX (with a c/c++ support library)

Lightweight alternative dock

Alex: Generally , Jetty is known as a lightweight alternative when it comes to servlet containers like Tomcat and App Servers like Glassfish . I want to run a RESTful service on CloudFoundry. use jetty java -jar target/dependency/jetty-runner.jar target/*.war

Lightweight alternative to Hibernate?

Jared: I have a user Java program that wants to store data in a lightweight database such as Derby or Sqlite. I want to use a data abstraction layer in my program. Hibernate seems to require a lot of configuration and is overkill for what I need. What is a lig

Lightweight GNU readline alternative

punekr12 I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I wan

Lightweight GNU readline alternative

punekr12 : I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I w

A clean, lightweight alternative to Python?

jkp : One (a long time ago) I wrote a web spider and I multithreaded the thread to allow concurrent requests to happen at the same time. That was my Python youth, before I learned about the GIL and its associated troubles for multithreaded code (IE, most of th

Lightweight multilingual alternative to JMX?

ROM1: I have a software real-time application project that requires extensive monitoring. JMX seems to be a good fit for the task, it's just that the application project is based on c++. Is there a lightweight alternative to JMX (with a c/c++ support library)

Lightweight zxcvbn alternative?

Greg Blass Wondering if there are any lightweight zxcvbn alternatives out there. I don't need my password strength meter to be incredible - rather, I need to reduce the size of my assets. Can someone say 80% of the content? Maybe not the whole dictionary in th

Lightweight alternative dock

Alex Generally , Jetty is known as a lightweight alternative when it comes to servlet containers like Tomcat and App Servers like Glassfish . I want to run a RESTful service on CloudFoundry. use jetty java -jar target/dependency/jetty-runner.jar target/*.war

Lightweight alternative to Hibernate?

Jared: I have a user Java program that wants to store data in a lightweight database such as Derby or Sqlite. I want to use a data abstraction layer in my program. Hibernate seems to require a lot of configuration and is overkill for what I need. What is a lig

Lightweight alternative dock

Alex: Generally , Jetty is known as a lightweight alternative when it comes to servlet containers like Tomcat and App Servers like Glassfish . I want to run a RESTful service on CloudFoundry. use jetty java -jar target/dependency/jetty-runner.jar target/*.war

Lightweight GNU readline alternative

punekr12 : I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I w

A clean, lightweight alternative to Python?

jkp : One (a long time ago) I wrote a web spider and I multithreaded the thread to allow concurrent requests to happen at the same time. That was my Python youth, before I learned about the GIL and its associated troubles for multithreaded code (IE, most of th

Lightweight multilingual alternative to JMX?

ROM1: I have a software real-time application project that requires extensive monitoring. JMX seems to be a good fit for the task, it's just that the application project is based on c++. Is there a lightweight alternative to JMX (with a c/c++ support library)

A clean, lightweight alternative to Python?

jkp : One (a long time ago) I wrote a web spider and I multithreaded the thread to allow concurrent requests to happen at the same time. That was my Python youth, before I learned about the GIL and its associated troubles for multithreaded code (IE, most of th

Is there a (lightweight) alternative to `rxvt-unicode`?

Buffalo I am currently rxvt-unicodeusing as a terminal emulator. Since I also like the configurability of GNOME and KDE's terminal emulators, I'm wondering if there is some sort of alternative that can replace GNOME or KDE Terminal, but rxvt-unicodewith more f

Is there a (lightweight) alternative to `rxvt-unicode`?

Buffalo I am currently rxvt-unicodeusing as a terminal emulator. Since I also like the configurability of GNOME and KDE's terminal emulators, I'm wondering if there is some sort of alternative that can replace GNOME or KDE Terminal, but rxvt-unicodewith more f

Is there a (lightweight) alternative to `rxvt-unicode`?

Buffalo I am currently rxvt-unicodeusing as a terminal emulator. Since I also like the configurability of GNOME and KDE's terminal emulators, I'm wondering if there is some sort of alternative that would replace GNOME or KDE Terminal, but rxvt-unicodewith more

Is there a POSIX shell alternative to read -e?

Sokob How can I achieve the effect read -e(online editing via this turn readlineand offer bash) from a general (POSIX) shell? I don't want to lose POSIX compatibility just because of this command. Stephane Chazelas readlineis a GNU project (developed with bash

Is there a POSIX shell alternative to read -e?

Sokob How can I achieve the effect read -e(online editing via this turn readlineand offer bash) from a general (POSIX) shell? I don't want to lose POSIX compatibility just because of this command. Stephane Chazelas readlineis a GNU project (developed with bash