LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 03-04-2011, 06:19 PM   #1
monicojr84
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
Signal Handling problem


I have a program that creates and uses a shared memory segment. I am trying to find out how to detach and delete this shared memory segment when I hit crtl-C, and I still need the process to terminate.

shmdt() and shmctl() have variables that are local to the main passed to them
(shared and shmid)
Code:
//Prototype
void leave(int sig);

//part of code trying to use signal handling
if(signal(SIGINT, leave))
{ 
  //Deallocate shared memory segment
  shmdt(shared);
	  
  //remove shared memory segment
  shmctl(shmid, IPC_RMID, NULL);
}

//leave declaration
void leave(int sig)
{ 
  sleep(10);
}
 
Old 03-05-2011, 04:49 AM   #2
JimmyKrak
LQ Newbie
 
Registered: Feb 2011
Posts: 13

Rep: Reputation: 0
It's been a while since I've messed with signals, but it seems that your "signal" call is only to set up your handler association with the specified event. Put your detach and remove within your handler (i.e. "leave"). These actions will happen once the event is captured.
 
Old 03-06-2011, 07:33 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 03-06-2011, 07:55 AM   #4
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 JimmyKrak View Post
It's been a while since I've messed with signals, but it seems that your "signal" call is only to set up your handler association with the specified event. Put your detach and remove within your handler (i.e. "leave"). These actions will happen once the event is captured.
For simple/quick tasks, this may be appropriate, however typically one sets a semaphore (a simple counter or flag) in the signal handler, and then checks the state of this semaphore elsewhere.

Something like this:
Code:
int all_done = 0;

void leave(int signo)
{
   if (signo == SIGINT)
   {
      all_done = 1;
   }
}

int main()
{
   /* This style is obsolete; should be avoided! */
   /* signal(SIGINT, leave); */

   struct sigaction usr_action;
   sigset_t         block_mask;

   sigfillset(&block_mask);

   usr_action.sa_handler = leave;
   usr_action.sa_mask    = block_mask;
   usr_action.sa_flags   = 0;

   sigaction(SIGINT, &usr_action, NULL);

   while (!all_done)
   {
      /* handle task of application... */

      /* perhaps have a delay... */
      usleep(10000);
   }

   /* clean up... */

   return 0;
}
 
  


Reply



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
Signal handling in c++ harishisnow Programming 2 02-01-2011 03:49 AM
How to reinstall a signal handler after exec ( Strange behaviour in signal handling ) lali.p Programming 0 09-20-2008 12:11 PM
Signal handling in C scoban Programming 2 12-18-2006 12:54 AM
Signal handling......... rajsun Programming 2 06-28-2005 08:10 AM
Signal handling Mohsen Programming 2 07-30-2003 06:55 AM

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

All times are GMT -5. The time now is 06:11 PM.

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