[SOLVED] Why is my C program output not in the same order as when I redirect it to a file ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Yeah, fork() – in both Unix® and Linux® – is a lot more expansive than the users of many other systems expect it to be, if they're accustomed to a mere "CreateProcess()" system function!
Just curious, how (or where?) can it be much better/faster?
When you "fork" a process, you replicate everything about its context. A simple "create process" call in most OSes does not undertake to do nearly so much.
I read a bit about semaphores and I think that the problem is that you need to define the semaphore in shared memory so that parent and childs can use it.
When you "fork" a process, you replicate everything about its context. A simple "create process" call in most OSes does not undertake to do nearly so much.
In modern processors, fork() isn't nearly as expensive as one might think. That replication is done by having the parent and child processes share the same memory pages and marking them "copy on write" in the MMU. The most common use for fork() is for the child process to do a little cleaning up and then call exec(), which discards almost all of the pages, so very little actual copying is performed.
@rknichols: Acknowledging that your assessment is (of course) technically correct.
While the Unix/Linux fork() procedure is not necessarily expensive, it is quite extensive. In most OSes, there is simply no corollary at all to fork().
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.