Release shared memory semaphore when process killedHow do I recover a semaphore when the process that decremented it to zero crashes?How to measure actual memory usage of an application or process?What killed my process and why?Peak memory usage of a linux/unix processLocking mechanisms for shared-memory consistencySemaphores, processes and incrementing variables in shared memoryHow to kill all processes with a given partial name?Is linux release the spinlock/semaphore when it kill a process?Shared memory synchronization with semaphoresHow to kill a process running on particular port in Linux?ipython using python2 although python3 is defined in the PATH

Team goes to lunch frequently, I do intermittent fasting but still want to socialize

Different inverter (logic gate) symbols

Best gun to modify into a monsterhunter weapon?

In a topological space if there exists a loop that cannot be contracted to a point does there exist a simple loop that cannot be contracted also?

Can I call myself an assistant professor without a PhD?

What game uses dice with sides powers of 2?

Is the initial ㅎ not pronounced sometimes?

Replace value with variable length between double quotes

What does Apple mean by "This may decrease battery life"?

Dropdowns & Chevrons for Right to Left languages

Withdrew when Jimmy met up with Heath

What costs less energy? Roll or Yaw?

Why did the RAAF procure the F/A-18 despite being purpose-built for carriers?

Author changing name

MinionPro is erroneous

Left switch on gang light pair not getting power

Can you castle with a "ghost" rook?

How can you evade tax by getting employment income just in equity, then using this equity as collateral to take out loan?

Why is transplanting a specific intact brain impossible if it is generally possible?

How can I solve for the intersection points of two ellipses?

Help evaluating integral (anything simple that I am missing?)

Should I ask for permission to write an expository post about someone's else research?

How should an administrative assistant reply to student addressing them as "Professor" or "Doctor"?

Is Calculus necessary for computer science student?



Release shared memory semaphore when process killed


How do I recover a semaphore when the process that decremented it to zero crashes?How to measure actual memory usage of an application or process?What killed my process and why?Peak memory usage of a linux/unix processLocking mechanisms for shared-memory consistencySemaphores, processes and incrementing variables in shared memoryHow to kill all processes with a given partial name?Is linux release the spinlock/semaphore when it kill a process?Shared memory synchronization with semaphoresHow to kill a process running on particular port in Linux?ipython using python2 although python3 is defined in the PATH






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have short python script that is supposed to:



  • be used by many users and threads concurrently,

  • call another program (/usr/bin/ld)

  • call this other program not more than x times concurrently (e.g. 2 concurrent calls to ld)

  • handle being interrupted / killed

I managed to achieve most of this using shared semaphore from python module posix_ipc. It handles SIGTERM and ctrl+c - semaphore is released, but it doesn't handle SIGKILL - semaphore stays acquired and needs to be reset manually. This means that doing kill -9 on it twice disables it permanently (until manual fix is applied).



How can I release semaphore when script is killed? If not possible - is there different method to achieve similar result?



I looked into file locks (with assumption that number of concurrent uses will always be 2) - maybe I can have 2 files, try to lock 1, if failed lock the other and wait until available. But I couldn't figure how to do "try to lock, if sb else already locked it, do sth else".



Full code of script:



#!/usr/bin/env python3 
# -*- coding: utf-8 -*-

import posix_ipc
import subprocess
import sys
import signal

SEM_NAME = '/serialize_ld'
MAX_CONCURRENT = 1
PROGRAM = '/usr/bin/ld'


def main():
import os
os.umask(0)
sem = posix_ipc.Semaphore(SEM_NAME, posix_ipc.O_CREAT, mode=0o666, initial_value=MAX_CONCURRENT)
sem.acquire()

def release_semaphore(signum, frame):
print("exiting due to signal " + str(signum))
sem.release()
sem.close()
sys.exit(1)
signal.signal(signal.SIGTERM | signal.SIGINT | signal.SIGKILL, release_semaphore)

try:
subprocess.call([PROGRAM, *sys.argv[1:]])
finally:
sem.release()
sem.close()


if __name__ == "__main__":
main()









share|improve this question
























  • From the Wiki on signals: The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal. There's nothing you can do from inside your process.

    – shmee
    Mar 27 at 9:18











  • @shmee yes, I know. So I am open to other mechanisms that don't have problem with this.

    – MateuszL
    Mar 27 at 9:21











  • This is why people should not use sigkill as first choice

    – geckos
    Mar 27 at 10:05











  • You can try to apply the manual intervention in the program, land have some means to determine if the program was killed abruptly

    – geckos
    Mar 27 at 10:09












  • What are the manual means?

    – geckos
    Mar 27 at 10:09

















0















I have short python script that is supposed to:



  • be used by many users and threads concurrently,

  • call another program (/usr/bin/ld)

  • call this other program not more than x times concurrently (e.g. 2 concurrent calls to ld)

  • handle being interrupted / killed

I managed to achieve most of this using shared semaphore from python module posix_ipc. It handles SIGTERM and ctrl+c - semaphore is released, but it doesn't handle SIGKILL - semaphore stays acquired and needs to be reset manually. This means that doing kill -9 on it twice disables it permanently (until manual fix is applied).



How can I release semaphore when script is killed? If not possible - is there different method to achieve similar result?



I looked into file locks (with assumption that number of concurrent uses will always be 2) - maybe I can have 2 files, try to lock 1, if failed lock the other and wait until available. But I couldn't figure how to do "try to lock, if sb else already locked it, do sth else".



Full code of script:



#!/usr/bin/env python3 
# -*- coding: utf-8 -*-

import posix_ipc
import subprocess
import sys
import signal

SEM_NAME = '/serialize_ld'
MAX_CONCURRENT = 1
PROGRAM = '/usr/bin/ld'


def main():
import os
os.umask(0)
sem = posix_ipc.Semaphore(SEM_NAME, posix_ipc.O_CREAT, mode=0o666, initial_value=MAX_CONCURRENT)
sem.acquire()

def release_semaphore(signum, frame):
print("exiting due to signal " + str(signum))
sem.release()
sem.close()
sys.exit(1)
signal.signal(signal.SIGTERM | signal.SIGINT | signal.SIGKILL, release_semaphore)

try:
subprocess.call([PROGRAM, *sys.argv[1:]])
finally:
sem.release()
sem.close()


if __name__ == "__main__":
main()









share|improve this question
























  • From the Wiki on signals: The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal. There's nothing you can do from inside your process.

    – shmee
    Mar 27 at 9:18











  • @shmee yes, I know. So I am open to other mechanisms that don't have problem with this.

    – MateuszL
    Mar 27 at 9:21











  • This is why people should not use sigkill as first choice

    – geckos
    Mar 27 at 10:05











  • You can try to apply the manual intervention in the program, land have some means to determine if the program was killed abruptly

    – geckos
    Mar 27 at 10:09












  • What are the manual means?

    – geckos
    Mar 27 at 10:09













0












0








0








I have short python script that is supposed to:



  • be used by many users and threads concurrently,

  • call another program (/usr/bin/ld)

  • call this other program not more than x times concurrently (e.g. 2 concurrent calls to ld)

  • handle being interrupted / killed

I managed to achieve most of this using shared semaphore from python module posix_ipc. It handles SIGTERM and ctrl+c - semaphore is released, but it doesn't handle SIGKILL - semaphore stays acquired and needs to be reset manually. This means that doing kill -9 on it twice disables it permanently (until manual fix is applied).



How can I release semaphore when script is killed? If not possible - is there different method to achieve similar result?



I looked into file locks (with assumption that number of concurrent uses will always be 2) - maybe I can have 2 files, try to lock 1, if failed lock the other and wait until available. But I couldn't figure how to do "try to lock, if sb else already locked it, do sth else".



Full code of script:



#!/usr/bin/env python3 
# -*- coding: utf-8 -*-

import posix_ipc
import subprocess
import sys
import signal

SEM_NAME = '/serialize_ld'
MAX_CONCURRENT = 1
PROGRAM = '/usr/bin/ld'


def main():
import os
os.umask(0)
sem = posix_ipc.Semaphore(SEM_NAME, posix_ipc.O_CREAT, mode=0o666, initial_value=MAX_CONCURRENT)
sem.acquire()

def release_semaphore(signum, frame):
print("exiting due to signal " + str(signum))
sem.release()
sem.close()
sys.exit(1)
signal.signal(signal.SIGTERM | signal.SIGINT | signal.SIGKILL, release_semaphore)

try:
subprocess.call([PROGRAM, *sys.argv[1:]])
finally:
sem.release()
sem.close()


if __name__ == "__main__":
main()









share|improve this question














I have short python script that is supposed to:



  • be used by many users and threads concurrently,

  • call another program (/usr/bin/ld)

  • call this other program not more than x times concurrently (e.g. 2 concurrent calls to ld)

  • handle being interrupted / killed

I managed to achieve most of this using shared semaphore from python module posix_ipc. It handles SIGTERM and ctrl+c - semaphore is released, but it doesn't handle SIGKILL - semaphore stays acquired and needs to be reset manually. This means that doing kill -9 on it twice disables it permanently (until manual fix is applied).



How can I release semaphore when script is killed? If not possible - is there different method to achieve similar result?



I looked into file locks (with assumption that number of concurrent uses will always be 2) - maybe I can have 2 files, try to lock 1, if failed lock the other and wait until available. But I couldn't figure how to do "try to lock, if sb else already locked it, do sth else".



Full code of script:



#!/usr/bin/env python3 
# -*- coding: utf-8 -*-

import posix_ipc
import subprocess
import sys
import signal

SEM_NAME = '/serialize_ld'
MAX_CONCURRENT = 1
PROGRAM = '/usr/bin/ld'


def main():
import os
os.umask(0)
sem = posix_ipc.Semaphore(SEM_NAME, posix_ipc.O_CREAT, mode=0o666, initial_value=MAX_CONCURRENT)
sem.acquire()

def release_semaphore(signum, frame):
print("exiting due to signal " + str(signum))
sem.release()
sem.close()
sys.exit(1)
signal.signal(signal.SIGTERM | signal.SIGINT | signal.SIGKILL, release_semaphore)

try:
subprocess.call([PROGRAM, *sys.argv[1:]])
finally:
sem.release()
sem.close()


if __name__ == "__main__":
main()






python linux ipc semaphore filelock






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 8:24









MateuszLMateuszL

8899 silver badges19 bronze badges




8899 silver badges19 bronze badges















  • From the Wiki on signals: The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal. There's nothing you can do from inside your process.

    – shmee
    Mar 27 at 9:18











  • @shmee yes, I know. So I am open to other mechanisms that don't have problem with this.

    – MateuszL
    Mar 27 at 9:21











  • This is why people should not use sigkill as first choice

    – geckos
    Mar 27 at 10:05











  • You can try to apply the manual intervention in the program, land have some means to determine if the program was killed abruptly

    – geckos
    Mar 27 at 10:09












  • What are the manual means?

    – geckos
    Mar 27 at 10:09

















  • From the Wiki on signals: The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal. There's nothing you can do from inside your process.

    – shmee
    Mar 27 at 9:18











  • @shmee yes, I know. So I am open to other mechanisms that don't have problem with this.

    – MateuszL
    Mar 27 at 9:21











  • This is why people should not use sigkill as first choice

    – geckos
    Mar 27 at 10:05











  • You can try to apply the manual intervention in the program, land have some means to determine if the program was killed abruptly

    – geckos
    Mar 27 at 10:09












  • What are the manual means?

    – geckos
    Mar 27 at 10:09
















From the Wiki on signals: The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal. There's nothing you can do from inside your process.

– shmee
Mar 27 at 9:18





From the Wiki on signals: The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal. There's nothing you can do from inside your process.

– shmee
Mar 27 at 9:18













@shmee yes, I know. So I am open to other mechanisms that don't have problem with this.

– MateuszL
Mar 27 at 9:21





@shmee yes, I know. So I am open to other mechanisms that don't have problem with this.

– MateuszL
Mar 27 at 9:21













This is why people should not use sigkill as first choice

– geckos
Mar 27 at 10:05





This is why people should not use sigkill as first choice

– geckos
Mar 27 at 10:05













You can try to apply the manual intervention in the program, land have some means to determine if the program was killed abruptly

– geckos
Mar 27 at 10:09






You can try to apply the manual intervention in the program, land have some means to determine if the program was killed abruptly

– geckos
Mar 27 at 10:09














What are the manual means?

– geckos
Mar 27 at 10:09





What are the manual means?

– geckos
Mar 27 at 10:09












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372646%2frelease-shared-memory-semaphore-when-process-killed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372646%2frelease-shared-memory-semaphore-when-process-killed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해