From 8fa1a1dc377c936ad0aa725dab6a0a8cbd8a7be7 Mon Sep 17 00:00:00 2001 From: waldek Date: Tue, 23 Aug 2022 09:59:09 +0200 Subject: [PATCH] adds missing python scripts --- advanced/learning_processes.md | 6 +++--- assets/processes_ex_01.py | 13 +++++++++++++ assets/processes_ex_02.py | 23 +++++++++++++++++++++++ assets/processes_ex_03.py | 18 ++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 assets/processes_ex_01.py create mode 100644 assets/processes_ex_02.py create mode 100644 assets/processes_ex_03.py diff --git a/advanced/learning_processes.md b/advanced/learning_processes.md index cb11598..aa66f0b 100644 --- a/advanced/learning_processes.md +++ b/advanced/learning_processes.md @@ -405,6 +405,6 @@ TODO To help you understand what happens to running and stopped processes I made a few python scripts you can download below. Run them either with `python3 $SCRIPT_NAME` or `./$SCRIPT_NAME`. -* [simple timer](./assets/processes_ex_01.py) -* [timer with random keyboard prompt](./assets/processes_ex_02.py) -* [custom callback function for SIGALRM](./assets/processes_ex_03.py) +* [simple timer](../assets/processes_ex_01.py) +* [timer with random keyboard prompt](../assets/processes_ex_02.py) +* [custom callback function for SIGALRM](../assets/processes_ex_03.py) diff --git a/assets/processes_ex_01.py b/assets/processes_ex_01.py new file mode 100644 index 0000000..f827e55 --- /dev/null +++ b/assets/processes_ex_01.py @@ -0,0 +1,13 @@ +import time +import os + +start_ts = time.time() +loops = 0 + +while True: + time.sleep(1) + loops += 1 + tick_ts = time.time() + delta = int(tick_ts - start_ts) + pid = os.getpid() + print("I'm {} and I've been running for {} seconds and did {} loops".format(pid, delta, loops)) diff --git a/assets/processes_ex_02.py b/assets/processes_ex_02.py new file mode 100644 index 0000000..b61f27e --- /dev/null +++ b/assets/processes_ex_02.py @@ -0,0 +1,23 @@ +import time +import os +import random + +start_ts = time.time() +loops = 0 + +while True: + time.sleep(1) + loops += 1 + tick_ts = time.time() + delta = int(tick_ts - start_ts) + pid = os.getpid() + print("I'm {} and I've been running for {} seconds and did {} loops".format(pid, delta, loops)) + chance = random.randint(0, 20) + if chance == 5: + response = input("I need some input...") + if len(response) == 0: + exit(0) + else: + print("thanks for the response, I'll reset my counter and continue now...") + start_ts = time.time() + loops = 0 diff --git a/assets/processes_ex_03.py b/assets/processes_ex_03.py new file mode 100644 index 0000000..3c4ac96 --- /dev/null +++ b/assets/processes_ex_03.py @@ -0,0 +1,18 @@ +import time +import os +import signal + +def alarm_handler(signum, frame): + print("I'm an alarm hear me ring! (my ID is {})".format(signum)) + +def main(): + print("I'm {}".format(os.getpid())) + while True: + time.sleep(1) + +if __name__ == "__main__": + signal.signal(signal.SIGALRM, alarm_handler) + try: + main() + except Exception as e: + print(e)