Solved

cleaning after programs crash.

Posted on 2000-04-03
2
231 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Named range not carried over 10 63
windows 10 how make picture as desktop background 2 63
java ^ examples 8 67
Work with App store 7 52
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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