LinuxQuestions.org
Visit Jeremy's Blog.
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 04-09-2012, 09:48 AM   #1
mcy
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Rep: Reputation: Disabled
signals


hello everyone, i am trying to understand signals and i wrote that little program:

int main(){

pid1=fork();
pid2=fork();

if(pid1==0){
printf("pid1: I am child %d\n",getpid());
}

else if(pid2==0){
printf("pid2: I am child %d\n",getpid());
}

else
printf("I am parent %d\n",getpid());
}

and the output is:

pid1: I am child 8702
pid1: I am child 8701
pid2: I am child 8703
I am father 8700

my question is why there are two pid1 and not two pid2?
thanks
 
Old 04-09-2012, 10:20 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
That is because the child of the first fork will run the fork for pid2, so there are two processes running the second fork.
Code:
pid1=fork()
  child1
    pid2=fork()
       child 3

pid2=fork()
  child2
and the parent processes is 4 total
 
Old 04-09-2012, 10:23 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Please use [CODE] [/CODE] tags around your code to make it more readable.

Quote:
Originally Posted by mcy View Post
Code:
pid1=fork();
pid2=fork();

if(pid1==0){
   printf("pid1: I am child %d\n",getpid());
}

else if(pid2==0){
   printf("pid2: I am child %d\n",getpid());
}
my question is why there are two pid1 and not two pid2?
You obviously have four processes:
  1. The original process, let's call it A
  2. The child B created by the first fork in the original process
  3. The child C created by the second fork in the original process
  4. The child D created by the second fork in process B
The values of pid1 and pid2 before the if clauses are:
  1. pid1==nonzero, pid2==nonzero
  2. pid1==zero, pid2==nonzero
  3. pid1==nonzero, pid2==zero
  4. pid1==zero, pid2==zero
The first if clause will match in both B and D. This is why you get two pid1 outputs: one by B, the first child created by the original process, and one by D, the child created by the second child created by the original process.

Because the second clause is else if, it is only evaluated when the first one did not match; i.e., only for A, the original process, and for C, the second child created by the original process. Because pid2 is zero only for the latter, you only get one pid2 output, printed by C, the second child created by the original process.

This is a very good example of why you should not do forks in succession: it is too easy to miss that all processes created by the first fork will execute the following forks, unless you add the check that makes sure only the original process forks.

Last edited by Nominal Animal; 04-09-2012 at 10:26 AM. Reason: typos
 
Old 04-09-2012, 10:56 AM   #4
mcy
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
These were very helpful! Thanks for your explanation! I've got one more question if u could help me.. I cant understand how and when to use kill(pid,SIGUSR1) and signal(SIGUSR1,handler);
Thank u again!
 
Old 04-09-2012, 11:22 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by mcy View Post
These were very helpful! Thanks for your explanation! I've got one more question if u could help me.. I cant understand how and when to use kill(pid,SIGUSR1) and signal(SIGUSR1,handler);
Thank u again!
I believe the use of signal() has been deprecated in favor of using sigaction(), which offers more features.

Here's a simple example:
Code:
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

void sigHandler(int signo)
{
    printf("Signal Handler called with signo = %d\n", signo);
}


int main()
{
    struct sigaction act;

    memset(&act, 0, sizeof(struct sigaction));

    act.sa_handler = sigHandler;

    sigaction(SIGUSR1, &act, 0);

    sleep(2);

    kill(getpid(), SIGUSR1);

    return 0;
}
To compile the program above:
Code:
gcc -D_POSIX_SOURCE signal.c
 
Old 04-09-2012, 01:16 PM   #6
mcy
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
ok thank u very much!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Signals Khaj.pandey Programming 7 06-19-2010 07:54 AM
signals sx6 Programming 2 09-12-2009 03:10 PM
signals lamtab Programming 1 11-17-2007 11:51 AM
!!! about signals !!! b2na Programming 4 02-04-2005 12:34 AM
!! about signals !!! b2na General 1 01-03-2005 04:37 PM

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

All times are GMT -5. The time now is 03:48 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