473,785 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing default Signal handling in C (linux)

3 New Member
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 4819
ashitpro
542 Recognized Expert Contributor
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
3936
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 we are registering for, and a second parameter that is the function to call. What I want to do is specify the function to call as a function of an instance of "this" class e.g.
2
2847
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 manual for signals and tried the program to check whether child inherits the signal handler intalled by parent befor fork.
5
2613
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
2214
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 similiar to the concept of try-catch to me.) It seems to relate more to nix OS. Are signals handling part of Windows too?
10
4035
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 finished, control does not return to the printf statement in main() which is present after the strcpy statement. Instead Segmentation violation error message is printed in Redhat Linux and the program is aborted. In VC++, the SIGSEGV_handler is...
2
1488
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 main/only thread. It works but I want to know if it is legal / what type of synchro API I have the right to use in a signal handler.
2
3861
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 exception handling in C++. if this is possible then please provide me some code example to handle such situation. Thanks and Regards Ali Muhammad kaim khani
2
5482
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, timeoutHandler); // set alarm signal handler 2. alarm (timeout); I am not sure what the best method is to handle those? Are there any constructs in Windows that will make the translation as painless as
9
5838
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
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.