Solved

cleaning after programs crash.

Posted on 2000-04-03
2
236 Views
Last Modified: 2013-12-26
Hi,

I have a c program that uses sem_open() to create a semaphore and releases it before it exits.  But when my program crashes in the middle, the semaphore is hanging there since the program didn't get to the point where it releases the semaphore.

Can someone please tell me how I can handle this? How do I release the semaphore(or other type of resources) when programs abnormally stops?

Thanks in advance.

0
Comment
Question by:samantha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 2682039
You might could reduce the incidence of hanging semaphores by installing signal handlers for the common crash events (SIGABRT, SIGSEGV, SIGPIPE, etc). If the crash isn't so bad as to have destroyed the handler code, it can release the semaphore. Take a look at the manpages for signal(2) & signal(7).
0
 
LVL 3

Accepted Solution

by:
ufolk123 earned 100 total points
ID: 2682435
You can not do that if program is killed using SIGSTOP.So there is not a 100% failsafe method available to ensure the cleanup of hanging resources as Unix resources are not process wide.Ther generally do not reference counting associated with them.
If resources like semaphore are in use by only your application may be you can do the following.

Create a cleanup function.
Include it in shared library startup routine for your app ( please lokk compiler help for specification about how to add shared library init routine in your code).
Now this cleanup routine will get called before entry func of your app ( generally main) and can do the cleanup for your prev instance( For this to work use a pre-defined key in semget).

This way you can delay the cleanup up to next invokation of your application.
Otherwise create a starter program for your application running only the cleanup func and use it like this.


Starter program
{
create the main application
wait for app to exit using waitpid
call semctl(delete semaphore).
}


Pls get back for futher clarification.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question