Solved

cleaning after programs crash.

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now