Explain output


 
Thread Tools Search this Thread
Top Forums Programming Explain output
# 1  
Old 04-13-2010
Explain output

Code:
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
sigcatcher()
{
    printf("pid=%d",getpid());
    signal(SIGINT,sigcatcher);     //line1
}
main()
{
    int ppid;
    signal(SIGINT,sigcatcher);
    if(fork()==0)
    {
        sleep(5);
        ppid=getppid();
        while(1)
            if(kill(ppid,SIGINT)==-1)
                exit(1);
    }
    nice(10);
    while(1){}
}

why this program is printing pid infinite times if line1 is eliminated????

Last edited by Franklin52; 04-13-2010 at 03:40 PM.. Reason: Please use code tags!
# 2  
Old 04-14-2010
Quote:
Originally Posted by gol007
Code:
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
sigcatcher()
{
    printf("pid=%d",getpid());
    signal(SIGINT,sigcatcher);     //line1
}
main()
{
    int ppid;
    signal(SIGINT,sigcatcher);
    if(fork()==0)
    {
        sleep(5);
        ppid=getppid();
        while(1)
            if(kill(ppid,SIGINT)==-1)
                exit(1);
    }
    nice(10);
    while(1){}
}

why this program is printing pid infinite times if line1 is eliminated????
You mean this does NOT print the pid over and over if line1 IS present? Although this depends on the signal handling on your system, I would expect it to either always print your PID again and again, or to print it once if you deleve line1, not the other way around.

Either way, this is where you tell the program to send itself SIGINT (and hence call sigcatcher()) again and again:

Code:
while (1)
    if(kill(ppid,SIGINT)==-1)
                exit(1);

If you want some other behaviour, change it.
# 3  
Old 04-16-2010
i was expecting the same output as you said but on running the program..its output is independent of line1 means it has no effect and pid is being printed again and again...
# 4  
Old 04-16-2010
Quote:
Originally Posted by gol007
i was expecting the same output as you said but on running the program..its output is independent of line1 means it has no effect and pid is being printed again and again...
Ah, I see what you mean - so it prints the PID indefinitely, even if line1 is commented out.

Simply put, some systems reset the handler for a signal to the "default" once it's been called, some don't - yours obviously doesn't, so your signal handler is always the one called.

Portable programs should assume the default handler is reinstated, so:

  1. If you want your signal handler to remain in-place, keep line1 there "just in case".
  2. If you want the default signal handler reinstated, do so explicitly.
# 5  
Old 04-17-2010
thanks i got it...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Explain the output of swap -s and swap -l

Hi Solaris Folks :), I need to calculate the swap usage on solaris server, please let me understand the output of below swap -s and swap -l commands. $swap -s total: 1774912k bytes allocated + 240616k reserved = 2015528k used, 14542512k available $swap -l swapfile dev swaplo... (6 Replies)
Discussion started by: seenuvasan1985
6 Replies

2. UNIX for Dummies Questions & Answers

netstat -an output, pls. explain..

Hi, I have old SCO O/S. System keeps crashing. I made lot of changes to kernel but so for nothing helped. I wrote a script which takes netstat -an output every one minute. I saw some thing right before the system crashed. Not sure if this means anything.. uname -a SCO_SV djx2 3.2... (2 Replies)
Discussion started by: samnyc
2 Replies

3. Solaris

Help Explain the output of probe-scsi

Can anyone explain the output of probe-scsi command below? ok probe-scsi Target 0 Unit 0 Disk SEAGATE ST373207LSUN72G 045A Target 1 Unit 0 Disk SEAGATE ST373207LSUN72G 045A I have no idea what it means. I tried to read online but I still did not understand. I appreciate... (6 Replies)
Discussion started by: cjashu
6 Replies

4. Shell Programming and Scripting

need explain

Dear unix team i'm user for a system build on unix system ,so we need to run a lot of scripts not in one sission but every script on the associated terminal , so the script name = the name of the terminal which will run this script on it . and someone create a batch that make as below : 1- but... (4 Replies)
Discussion started by: fofatoti
4 Replies

5. Shell Programming and Scripting

Can anyone please explain this??

cur_fy=`grep "CONSOL" $GLDATA/parms/cur_fiscalyear.lis | awk '{print $2}' Here i don't understand "CONSOL" and awk'{print$2) Please help me out cur_fiscalyear.lis contents : DL 2011 MOL 2011 MV 2011 SF 2010 CONSOL 2011 MVU 2011 (3 Replies)
Discussion started by: Diddy
3 Replies

6. Shell Programming and Scripting

can any one explain this example

hi all i have an example i want one help me to understand cause i tried to test it but almost fail and i don't know how can i solve this problem " the main idea to read from two files and replace something from one to another " but i don't understand why it fail all time $ cat main.txt... (4 Replies)
Discussion started by: maxim42
4 Replies

7. UNIX for Dummies Questions & Answers

Please explain this

if then echo "Syntax: $0 <sid> <COLD/HOT> <DEST>" exit fi if --------------what does this mean??? echo "Syntax: $0 <sid> <COLD/HOT> <DEST>"---pls explain this as well (2 Replies)
Discussion started by: appsdba.nitin
2 Replies

8. Shell Programming and Scripting

Please can any one explain this ${0##/}

I did not understand what is ${0##/} PGM=${0##/} TMP=/tmp/${PGM}.$$ Please explain me. (2 Replies)
Discussion started by: gadege
2 Replies

9. Programming

Please Explain me the output

#include<stdio.h> char *def={"pqrs","rstu","tuvw","vwxyz","xyzab"}; char abc={"abc","def","ghi","jkl","mno"}; void main() { char *p=(char *)def; p=p+40; printf("%s\n",p); } the output of the abve code snippet is mno... HOW??? beats me.. please help (10 Replies)
Discussion started by: vikashtulsiyan
10 Replies

10. UNIX for Dummies Questions & Answers

Explain the output of the command....

Explain the output of the command “sort -rfn file1 | more” (1 Reply)
Discussion started by: wickbc
1 Replies
Login or Register to Ask a Question