LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2008, 07:31 AM   #1
namanhams
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Rep: Reputation: 0
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 ?

Hope to hear some explain here. Thanks.
 
Old 03-23-2008, 07:40 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I am not sure, but what I think is this:

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 agree it is interesting...
 
Old 03-23-2008, 07:55 AM   #3
ophirg
Member
 
Registered: Jan 2008
Location: Israel
Distribution: Kubuntu 13.10
Posts: 134

Rep: Reputation: 34
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);
...
 
Old 03-23-2008, 01:12 PM   #4
embesil
LQ Newbie
 
Registered: Mar 2008
Posts: 5

Rep: Reputation: 0
....edit....

Last edited by embesil; 03-23-2008 at 01:21 PM. Reason: mis undertanding
 
Old 03-24-2008, 01:02 AM   #5
namanhams
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
@ 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 ?

@ophirg : Yes, you'r correct. Thanks.


Any more explaination ?
 
Old 03-24-2008, 05:42 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by namanhams View Post
@ 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...
 
Old 03-24-2008, 07:59 AM   #7
namanhams
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Hko View Post
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 ?
 
Old 03-24-2008, 10:07 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by namanhams View Post
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().

Last edited by Hko; 03-24-2008 at 10:10 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about Linux Process Stopping bjdea1 Linux - General 2 06-25-2007 05:37 AM
Interesting C Question ar1 Programming 2 05-07-2005 10:24 AM
One interesting question palanisaravanan Linux - Networking 2 04-21-2004 09:39 AM
Some Question on linux process management.. ttianhuat Linux - Newbie 4 01-29-2004 10:35 PM
A Question .. Linux boot process pyramid Linux - General 3 03-23-2001 06:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration