LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   clarifications about fork() (https://www.linuxquestions.org/questions/programming-9/clarifications-about-fork-816940/)

BlueSkull 06-29-2010 04:03 AM

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

BlueSkull 06-29-2010 04:08 AM

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.

BlueSkull 06-29-2010 04:16 AM

Output on my system:

Code:

aduait@aduait-laptop:~$ cat ls
cat: ls: No such file or directory
aduait@aduait-laptop:~$ cat fork.c >
bash: syntax error near unexpected token `newline'
aduait@aduait-laptop:~$ cat > fork.c
#include <stdio.h>
main()
{
    int pid;
    printf("I'm the original process with PID %d and PPID %d.\n",
          getpid(),getppid());
    pid=fork();  /* Duplicate. Child and parent continue from here.*/
    if (pid!=0)  /* pid is non-zero, so I must be the parent  */
        {
          printf("I'm the parent process with PID %d and PPID %d.\n",
                  getpid(),getppid());
          printf("My child's PID is %d.\n", pid);
        }
    else  /* pid is zero, so I must be the child. */
        {
          printf("I'm the child process with PID %d and PPID %d.\n",
                  getpid(),getppid());
        }
    printf("PID %d terminates.\n",pid);  /* Both processes execute this */
}
aduait@aduait-laptop:~$ gcc -o fork fork.c
aduait@aduait-laptop:~$ ./fork
I'm the original process with PID 17004 and PPID 16794.
I'm the parent process with PID 17004 and PPID 16794.
My child's PID is 17005.
PID 17005 terminates.
I'm the child process with PID 17005 and PPID 17004.
PID 0 terminates.
aduait@aduait-laptop:~$


johnsfine 06-29-2010 05:26 AM

Quote:

Originally Posted by BlueSkull (Post 4017982)
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.

Neither. Some other factor is causing the difference. Does one of those systems have only one core?

Quote:

Just wana clear my doubt and also do child and parent execute simultaneously in multitasking mode or whosoever gets CPU first completes first.
If you have two or more cores, the user mode code will really execute simultaneously.

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.

BlueSkull 06-29-2010 07:23 AM

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.