Solved

cleaning after programs crash.

Posted on 2000-04-03
2
233 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
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

839 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