Solved

cleaning after programs crash.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 85
VB.NET how to use the Vertical ScrollBar 5 90
Whole sheet autoscrub still needed 19 48
Window placement 17 74
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

912 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

16 Experts available now in Live!

Get 1:1 Help Now