From e227b234c03a2ed855487c70dccc3c55927753bc Mon Sep 17 00:00:00 2001 From: waldek Date: Mon, 5 Jul 2021 15:48:42 +0200 Subject: [PATCH] completes the processes md and adds the exercises --- modules/qualifying/assets/processes_ex_01.py | 13 +++++++++++ modules/qualifying/assets/processes_ex_02.py | 23 ++++++++++++++++++++ modules/qualifying/assets/processes_ex_03.py | 18 +++++++++++++++ 3 files changed, 54 insertions(+) create mode 100755 modules/qualifying/assets/processes_ex_01.py create mode 100755 modules/qualifying/assets/processes_ex_02.py create mode 100755 modules/qualifying/assets/processes_ex_03.py diff --git a/modules/qualifying/assets/processes_ex_01.py b/modules/qualifying/assets/processes_ex_01.py new file mode 100755 index 0000000..f827e55 --- /dev/null +++ b/modules/qualifying/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/modules/qualifying/assets/processes_ex_02.py b/modules/qualifying/assets/processes_ex_02.py new file mode 100755 index 0000000..b61f27e --- /dev/null +++ b/modules/qualifying/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/modules/qualifying/assets/processes_ex_03.py b/modules/qualifying/assets/processes_ex_03.py new file mode 100755 index 0000000..3c4ac96 --- /dev/null +++ b/modules/qualifying/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)