473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Changing default Signal handling in C (linux)

Hi, first post here.

First off, this IS a homework assignment for an operating systems class, but the question isn't "how do i do the assignment" but "why is my particular implementation not working".


I've searched the forums here, but could not find anything particular to my problem, Googling and the linux manual pages haven't been much help either.

What the program is supposed to do is create a very simple shell (that sits atop the already running shell) to parse commands and run them via a child process, with an option to run in the background. The program is also to implement a history feature that displays the last 10 commands, and provide a shortcut for the user to quickly select one of these commands and execute it.

Here is the problem, the assignment specifically requires that the recent command list be displayed with <ctrl><c>. the code i am using to accomplish this is roughly


Expand|Select|Wrap|Line Numbers
  1.  struct sigaction sigint_handler;      //global
  2.  
  3.     void display_history()
  4.     {  
  5.       // msg is a globally defined c-string that says "caught <ctrl><c>"
  6.       write(STDOUT_FILENO,msg,strlen(msg));  //signal safe ouput
  7.     }
  8.  
  9.  
  10.  int main(void)
  11.  {
  12.     sigint_handler.sa_handler = display_history;   
  13.  
  14.     //register display_history() to replace default SIGINT action
  15.     sigaction(SIGINT,&sigint_handler,NULL); 
  16.  
  17.         while(1)
  18.        {   //parser detects C - d  to exit program normally
  19.         parser(iBuffer,args);     //parses commands and arguments, storing 
  20.                                          //this in the two memories passed by ref.
  21.               switch(fork())
  22.               {
  23.               case 0: execvp(iBuffer,args);
  24.                           break;
  25.               case -1: handle_error();
  26.                           break;
  27.               default:
  28.                           if(background == 0) //if "run in background" option not        
  29.                              wait(NULL);        //selected, wait for child to complete
  30.  
  31.               }
  32.         }
  33.  
  34.  }
  35.  
The behavior i am having a hard time figuring out is what happens once the SIGINT signal is sent. As you can see the display history logic isnt there, only a stub to show that ctrl-c was handled correctly. When running the program,
the first time C- c is pressed, the message displays correctly. The second time C-c is pressed (either immediately or after executing a few commands) the message is printed twice, the third time C- c is pressed the message is printed 3 times etc.
This multiple calling of the display_hist function when C - c is pressed is what has me confused. To make matters worse, gdb seems to catch the SIGINT itself sometimes, exiting the debugger, making debugging this problem difficult. Perhaps the most confusing bit is that the problem doesnt always act the same way, sometimes gdb catches sigint, sometimes it doesnt. Sometimes the program catches sigint and displays the stub message more then once on the very first C -c. Sometimes the number of times that the stub message is displayed is twice the C-c press, i.e on the nth usage of C - c, the stub is printed 2*n times.

I have tried:
-setting sa_flags on the sigaction struct to SA_RESTART

-using sigprocmask() to Block sigint signals, set default SIGINT action
to ignore, then unblock sigint so that any enqueued signals will be
de-queued and ignored, (since the error looks like some queue of signal
is being dequeued). This blocking was performed in the display_hist
(signal handling) function - i would have like to have tried this in the main
code, but am not sure how to use it since i don't think you can be sure
at what point the process will recieve the signal.

ANY insight into signal handling would be most appreciated, as this problem has
plagued me for a few days now. I'm hoping that the problem is glaringly obvious.

thanks.
Oct 6 '07 #1
1 4779
ashitpro
542 Expert 512MB
Basic reason behind your scenario is..
Whenever parent gets the signal it gets delivered to it's child also.
More precisely..
In your code at the time of ctrl-c,if you have three child running then this signal will get delivered to them too.
And each of them will call display_history() function.
Nov 12 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Lionel van den Berg | last post by:
Hi all, I'm trying to do some signal handling using the csignal library but I can't find and specific examples for C++. The signal function as defined in C takes a parameter that is the signal...
2
by: gnutuxy | last post by:
Hi, I am newbie in the Unix system programming and in learning phase. I usually read the libc manual and then try to implement small programs to test/check the learnt thing. I read the libc...
5
by: Sontu | last post by:
Hi all, Consider the following code: #include<signal.h> #include<stdio.h> #include<sys/mman.h> void handler(int sig) {
7
by: Stanley S | last post by:
Hi, Are Signal Handling part of ANSI C? I am not able to find any reference of Sig Handling in Stephen Prata's "C Primer Plus". The usage of signals is to trap errors I guess. (It looks...
10
by: subramanian | last post by:
Consider the following code: segment violation is deliberately generated to catch the SIGSEGV signal. The handler for this signal, namely SIGSEGV_handler, is called. However after the handler is...
2
by: hg | last post by:
Hi, I posted an equivalent question earlier ... but am still not sure: I currently (under Linux) have a program that uses Queue.put (raw_input('')) in a signal handler and Queue.get() in the...
2
by: amohammad | last post by:
Hi , All I am a linux user. As we all know using exception handling we can catch like divide by zero, floating point exception etc in C++. Can we catch a segmentation fault and kill signal using...
2
by: responsible | last post by:
Hi, I am trying to convert a small piece of source code that was initially written for Linux to build under Windows. My main problem though is in two lines... 1. signal(SIGALRM,...
9
by: thagor2008 | last post by:
Is the behaviour of throwing exceptions in a unix signal handler defined? eg: void sighandler(int sig) { ... do something throw myobj; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.