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 06-29-2010, 04:03 AM   #1
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Rep: Reputation: 17
Question 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
 
Old 06-29-2010, 04:08 AM   #2
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Original Poster
Rep: Reputation: 17
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.
 
Old 06-29-2010, 04:16 AM   #3
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Original Poster
Rep: Reputation: 17
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:~$
 
Old 06-29-2010, 05:26 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by BlueSkull View Post
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.
 
1 members found this post helpful.
Old 06-29-2010, 07:23 AM   #5
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Original Poster
Rep: Reputation: 17
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
 
  


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
Needing a few SSH clarifications vendtagain Linux - Server 1 11-21-2009 09:22 PM
memory usage few clarifications pudhiyavan Linux - Server 9 06-25-2008 11:50 AM
Some clarifications Gins Programming 2 08-28-2006 05:20 AM
initrd clarifications titopoquito Slackware 2 10-05-2005 09:17 AM
gpl clarifications paulr1984 Linux - General 2 08-14-2005 09:47 AM

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

All times are GMT -5. The time now is 06:47 PM.

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