clarifications about fork()
Hi,
well I was writing a code in my class for forking a child process on RedHat and I read that fork after creating child process grant 1st time stamp of CPU to newly created child process. At home I write the same program on Ubuntu but this time parent is running first. Is there any difference using fork in different linux distros (BSD/SystemV) or I'm misinterpreting it. Just wana clear my doubt and also do child and parent execute simultaneously in multitasking mode or whosoever gets CPU first completes first. This question can really be very foolish ... but I was not having this confusion till I dint tried the same program on ubuntu 10.04 and also one friend of mine is running ubuntu 9.04 and child process is running first So just a little confused . Thanks |
I read http://www.cs.cityu.edu.hk/~lwang/fork and found that in example parent is executing first (not mentioned the OS) but had some confusions.
|
Output on my system:
Code:
aduait@aduait-laptop:~$ cat ls |
Quote:
Quote:
Most of the actual CPU time in your example is kernel code copying each line of text to the screen. Since it is the same scree, that code can't run simultaneously even if you have two cores. After that kernel mode completes for one process in a multi core machine, the other process would have already finished the user mode code to create the next line of text, so the lines would alternate. In a single core machine, the scheduler portion of the kernel must decide which user mode code to run after each kernel operation completes. That might also make the lines alternate. |
thanks .. thanks a lot
Now its making sense .. yeah but there was one more thing : I was searching for the answer after posting here and I was reading from "The McGraw-Hill Companies - Unix Concepts and applications - 4 e - by Sumithabha Das" and while giving example of simple child creation using fork there it was mentioned "In this program run on Solaris, the child runs first,But on linux , the parent runs before the child. POSIX doesn't specify any sequence, so you can't write programs based on the assumption that one runs before the other." Here one after one his statements are like complimentry. Any way thanks for reply. It really helped. Thanks :) |
All times are GMT -5. The time now is 03:33 PM. |