LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 11-05-2006, 12:02 PM   #1
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
A process-related question


Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
   pid_t pid;
   char *message;
   int n;
   printf(“fork program starting\n”);
   pid = fork();
   switch(pid)
   {
     case -1:
             perror(“fork failed”);
             exit(1);
     case 0:
             message = “This is the child”;
             n = 5;
             break;
     default:
             message = “This is the parent”;
             n = 3;
             break;
}
   for(; n > 0; n--) {
   puts(message);
   sleep(1);
   }
   exit(0);
}
As you can see from code, the parent process finishes before the child. In this case, the child process should be terminated too, but it's still running. I checked with htop and it seems that when the father-process finishes, the child "gets" another parent:init. What I don't understand is why the child process doesn't terminate simultaneously with the father( Yes, I know what's a Program Counter )

LE: Thanks, introuble

Last edited by BiThian; 11-05-2006 at 12:37 PM.
 
Old 11-05-2006, 12:15 PM   #2
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Quote:
What I don't understand is why the child process doesn't terminate simultaneously with the father
Why would it?

If the father dies, the child's parent becomes it's "grandparent" [in your case, init, which you can compare to "adam".] It's quite normal behaviour. If the child would die when the parent dies, you wouldn't be able to have "daemons".
 
Old 11-07-2006, 06:39 AM   #3
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Original Poster
Rep: Reputation: 15
I don't open another thread...Could you recommend me (based on what you've read already) a book (regarding Linux/Unix) that explains you detailed every low-level aspect of this OS? Thanks!
 
Old 11-07-2006, 10:47 AM   #4
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
The definitive tome on unix system calls is W. Richard Stevens' Advanced Programming in the Unix Environment, ISBN 0201433079. An excellent book, which was updated last year to cover recent OS developments (including Linux).

It doesn't cover "every" aspect (no single book can do that), but it's invaluable for understanding how unix works.
 
Old 11-07-2006, 12:51 PM   #5
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Also, try out: advancedlinuxprogramming.com
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet-Process Id,ports related jared78 Linux - Networking 6 05-19-2005 02:44 AM
bash related question Menestrel Linux - Newbie 2 01-25-2005 10:03 AM
process related issue LinuxRam Linux - Networking 1 09-12-2004 04:24 PM
c related question. blackzone Programming 1 07-24-2004 08:55 AM
Vmware Related Question jon_k Linux - Software 2 07-22-2003 06:16 PM


All times are GMT -5. The time now is 02:46 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration