Code für Teil21 Interrupts

This commit is contained in:
2023-12-11 07:49:21 +01:00
parent c04c88e2af
commit 278d79f6d0
5 changed files with 63 additions and 3 deletions

41
teil21/interrupts.py Executable file
View File

@@ -0,0 +1,41 @@
#! /usr/bin/python3
import time
import signal
import sys
#Behandlung von SIGINT (CTRL-C)
def handle_sigint(signum, frame) :
print(f'Handling signal {signum} ({signal.Signals(signum).name}).')
if signum == signal.SIGINT:
print(f'SIGINT wird behandelt. {frame}')
time.sleep(1)
sys.exit(0)
#Behandlung von SIGTSTP (CTRL-Z)
def handle_sigtstp(signum,frame):
print(f'Behandle signal {signum} ({signal.Signals(signum).name}).')
print('Programm in Hintergrund')
# Behandlung von SiGCONT
def handle_sigcont(signum,frame):
print(f'Behandle signal {signum} ({signal.Signals(signum).name}).')
print('Programm im Vordergrund')
if __name__ == '__main__':
# Interrupt Handler registrieren
signal.signal(signal.SIGINT, handle_sigint)
signal.signal(signal.SIGTSTP, handle_sigtstp)
signal.signal(signal.SIGCONT, handle_sigcont)
for i in range(0,10000000):
print(f'Schleife: {i}')
time.sleep(0.5)
print('Schleifenende')