Interesting question regarding the process in Linux
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.
Interesting question regarding the process in Linux
Hi all,
I have a question about process in Linux.
First of all, i have a C program, that run fork() every 1 seconds, and the child processes also do that recursively.
Here's the code :
Code:
#include <stdio.h>
#include <unistd.h>
int main()
{
setlinebuf(stdout);
printf("ORIGINAL PID = %d\n", getpid());
while (1) {
if (!fork()) printf("NEW PID = %d\n", getpid());
sleep(1); // wait 1s, so forking is slowed down
}
}
If i just run the program, the system hangs. It may be because of too many process running. So i set the limit number of processes to , says 1000, by :
ulimit -u 1000
Then i run the program again. Ok, now it works.
The problems is, when i set the time to be smaller than 1 second, then the system runs very very slowly. Even when i set the time to 0.99 second, it's still the same.
The question is : how come does it happen ? Why the difference between 1 and 0.99 can make such a big change ?
With one second timer, the system/kernel checks each of your 1000 processes once a second. But when you specify 0.99 second, it uses high resolution timers and check all the processes each 0.01 secs, and the system load gets 100 times larger.
I think your program doesn't start a new process every second.
The number of processes over time is not linear, it's exponential.
Each second, every process starts it's own program. So in second N you'll have about 2^N processes.
If you want linear number of processes you should use:
...
if (fork()==0) {
printf("NEW PID = %d\n", getpid());
return 0;
}
else sleep(1);
...
@ Hko : Can you explain in more detailed ? So if i set the time to 0.1 second, then the system check all the processes each 0.9 second or 0.01 second ?
@ Hko : Can you explain in more detailed ? So if i set the time to 0.1 second, then the system check all the processes each 0.9 second or 0.01 second ?
Any more explaination ?
No, that is not what I meant. I thought the kernel might check your processes every 1 sec when you pause/sleep for 1 sec. But when you pause for 0.99 it starts to use high-precision timing. Like when you pause for 0.9 maybe it checks every 0.1 second or so.
But now I had a look at your program a second time, I think it is different. How did you sleep for 0.99 ? With sleep(0.99)? If so, then there is a much simpler explanation: The sleep() function only takes an integer, so your 0.99 maybe rounded down to zero...
But now I had a look at your program a second time, I think it is different. How did you sleep for 0.99 ? With sleep(0.99)? If so, then there is a much simpler explanation: The sleep() function only takes an integer, so your 0.99 maybe rounded down to zero...
I look at the "man sleep" page but it doesn't say that the time should be integer. How do you know ?
I look at the "man sleep" page but it doesn't say that the time should be integer. How do you know ?
Actually the man page really does say the time should be integer:
Quote:
Code:
SYNOPSIS
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
If yours did not, you were probably looking at the man page of the sleep shell command. Try "man 3 sleep" to be sure you the man page about the C-library function sleep().
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.