From 6adde4e8b6d5f867cb91ab8ab865891818c65f73 Mon Sep 17 00:00:00 2001 From: fadhelrahmawan Date: Wed, 20 Apr 2022 18:08:37 +0700 Subject: [PATCH] Quis 1 Sistem Tersebar Fadhel Rahmawan --- QUIS SISTER 1/quis_fadhelrahmawan/Barrier.py | 26 +++++++ .../quis_fadhelrahmawan/Condition.py | 70 +++++++++++++++++ QUIS SISTER 1/quis_fadhelrahmawan/Event.py | 38 ++++++++++ .../quis_fadhelrahmawan/MyThreadClass.py | 67 +++++++++++++++++ .../quis_fadhelrahmawan/MyThreadClass_lock.py | 74 ++++++++++++++++++ .../MyThreadClass_lock_2.py | 75 +++++++++++++++++++ QUIS SISTER 1/quis_fadhelrahmawan/Rlock.py | 27 +++++++ .../quis_fadhelrahmawan/Semaphore.py | 37 +++++++++ .../quis_fadhelrahmawan/Thread_definition.py | 17 +++++ .../Thread_name_and_processes.py | 31 ++++++++ .../Threading_with_queue.py | 53 +++++++++++++ QUIS SISTER 1/quis_fadhelrahmawan/queue.py | 55 ++++++++++++++ 12 files changed, 570 insertions(+) create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/Barrier.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/Condition.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/Event.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock_2.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/Rlock.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/Semaphore.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/Thread_definition.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/Thread_name_and_processes.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/Threading_with_queue.py create mode 100644 QUIS SISTER 1/quis_fadhelrahmawan/queue.py diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/Barrier.py b/QUIS SISTER 1/quis_fadhelrahmawan/Barrier.py new file mode 100644 index 0000000..05d4d2e --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/Barrier.py @@ -0,0 +1,26 @@ +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +num_runners = 3 +finish_line = Barrier(num_runners) +runners = ['Ragil kuncoro', 'dimana', 'Alamat rumahmu'] + +def runner(): + name = runners.pop() + sleep(randrange(3, 10)) + print('%s reached the barrier at: %s \n' % (name, ctime()))# + finish_line.wait() + +def main(): + threads = [] + print('Ayo Pergi jalan sama aku?!!!!') + for i in range(num_runners): + threads.append(Thread(target=runner)) + threads[-1].start() + for thread in threads: + thread.join() + print('Ayo berangkat!') + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/Condition.py b/QUIS SISTER 1/quis_fadhelrahmawan/Condition.py new file mode 100644 index 0000000..828ceec --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/Condition.py @@ -0,0 +1,70 @@ +import logging +import threading +import time + +LOG_FORMAT = '%(asctime)s %(threadName)-17s %(levelname)-8s %(message)s' +logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) + +items = [] +condition = threading.Condition() + + +class Consumer(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def consume(self): + + with condition: + + if len(items) == 0: + logging.info('no items to consume') + condition.wait() + + items.pop() + logging.info('consumed 1 item') + + condition.notify() + + def run(self): + for i in range(20): + time.sleep(2) + self.consume() + + +class Producer(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def produce(self): + + with condition: + + if len(items) == 10: + logging.info('items produced {}. Stopped'.format(len(items))) + condition.wait() + + items.append(1) + logging.info('total items {}'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(20): + time.sleep(0.5) + self.produce() + + +def main(): + producer = Producer(name='Producer') + consumer = Consumer(name='Consumer') + + producer.start() + consumer.start() + + producer.join() + consumer.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/Event.py b/QUIS SISTER 1/quis_fadhelrahmawan/Event.py new file mode 100644 index 0000000..d22288e --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/Event.py @@ -0,0 +1,38 @@ +import threading +import time +import logging + +logging.basicConfig(level=logging.DEBUG, + format='(%(threadName)-9s) %(message)s',) + +def jalan(e): + logging.debug('sebentar') + event_is_set = e.wait() + logging.debug('kamu ada dimana?: %s', event_is_set) + +def pergi(e, t): + while not e.isSet(): + logging.debug('ayo kita pergi, kemana yaa?') + event_is_set = e.wait(t) + logging.debug('ke bandung yok: %s', event_is_set) + if event_is_set: + logging.debug('terimakasih waktunya') + else: + logging.debug('mobilnya bagus juga?') + +if __name__ == "__main__": + e = threading.Event() + t1 = threading.Thread(name='bintang', + target=jalan, + args=(e,)) + t1.start() + + t2 = threading.Thread(name='bulan', + target=pergi, + args=(e, 2)) + t2.start() + + logging.debug('Waiting before calling Event.set()') + time.sleep(3) + e.set() + logging.debug('melelahkan juga ya') \ No newline at end of file diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass.py b/QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass.py new file mode 100644 index 0000000..45e6fff --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass.py @@ -0,0 +1,67 @@ +import time +import os +from random import randint +from threading import Thread + +class MyThreadClass (Thread): + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + def run(self): + print ("---> " + self.name + \ + " running, belonging to process ID "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print ("---> " + self.name + " over\n") + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = MyThreadClass("Thread#1 ", randint(1,10)) + thread2 = MyThreadClass("Thread#2 ", randint(1,10)) + thread3 = MyThreadClass("Thread#3 ", randint(1,10)) + thread4 = MyThreadClass("Thread#4 ", randint(1,10)) + thread5 = MyThreadClass("Thread#5 ", randint(1,10)) + thread6 = MyThreadClass("Thread#6 ", randint(1,10)) + thread7 = MyThreadClass("Thread#7 ", randint(1,10)) + thread8 = MyThreadClass("Thread#8 ", randint(1,10)) + thread9 = MyThreadClass("Thread#9 ", randint(1,10)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + thread4.start() + thread5.start() + thread6.start() + thread7.start() + thread8.start() + thread9.start() + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + thread4.join() + thread5.join() + thread6.join() + thread7.join() + thread8.join() + thread9.join() + + # End + print("End") + + #Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock.py b/QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock.py new file mode 100644 index 0000000..ad2f64c --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock.py @@ -0,0 +1,74 @@ +import threading +import time +import os +from threading import Thread +from random import randint + +# Lock Definition +threadLock = threading.Lock() + +class MyThreadClass (Thread): + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + def run(self): + #Acquire the Lock + threadLock.acquire() + print ("---> " + self.name + \ + " running, belonging to process ID "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print ("---> " + self.name + " over\n") + #Release the Lock + threadLock.release() + + +def main(): + start_time = time.time() + # Thread Creation + thread1 = MyThreadClass("Thread#1 ", randint(1,10)) + thread2 = MyThreadClass("Thread#2 ", randint(1,10)) + thread3 = MyThreadClass("Thread#3 ", randint(1,10)) + thread4 = MyThreadClass("Thread#4 ", randint(1,10)) + thread5 = MyThreadClass("Thread#5 ", randint(1,10)) + thread6 = MyThreadClass("Thread#6 ", randint(1,10)) + thread7 = MyThreadClass("Thread#7 ", randint(1,10)) + thread8 = MyThreadClass("Thread#8 ", randint(1,10)) + thread9 = MyThreadClass("Thread#9 ", randint(1,10)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + thread4.start() + thread5.start() + thread6.start() + thread7.start() + thread8.start() + thread9.start() + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + thread4.join() + thread5.join() + thread6.join() + thread7.join() + thread8.join() + thread9.join() + + # End + print("End") + + #Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock_2.py b/QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock_2.py new file mode 100644 index 0000000..2b44f98 --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock_2.py @@ -0,0 +1,75 @@ +import threading +import time +import os +from threading import Thread +from random import randint + +# Lock Definition +threadLock = threading.Lock() + +class MyThreadClass (Thread): + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + def run(self): + #Acquire the Lock + threadLock.acquire() + print ("---> " + self.name + \ + " running, belonging to process ID "\ + + str(os.getpid()) + "\n") + threadLock.release() + time.sleep(self.duration) + print ("---> " + self.name + " over\n") + #Release the Lock + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = MyThreadClass("Thread#1 ", randint(1,10)) + thread2 = MyThreadClass("Thread#2 ", randint(1,10)) + thread3 = MyThreadClass("Thread#3 ", randint(1,10)) + thread4 = MyThreadClass("Thread#4 ", randint(1,10)) + thread5 = MyThreadClass("Thread#5 ", randint(1,10)) + thread6 = MyThreadClass("Thread#6 ", randint(1,10)) + thread7 = MyThreadClass("Thread#7 ", randint(1,10)) + thread8 = MyThreadClass("Thread#8 ", randint(1,10)) + thread9 = MyThreadClass("Thread#9 ", randint(1,10)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + thread4.start() + thread5.start() + thread6.start() + thread7.start() + thread8.start() + thread9.start() + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + thread4.join() + thread5.join() + thread6.join() + thread7.join() + thread8.join() + thread9.join() + + # End + print("End") + + #Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/Rlock.py b/QUIS SISTER 1/quis_fadhelrahmawan/Rlock.py new file mode 100644 index 0000000..55675fa --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/Rlock.py @@ -0,0 +1,27 @@ +# program to illustrate the use of RLocks + +# importing the module +from csv import list_dialects +import threading + +# initializing the shared resource +Lidiya = 18 + +# creating an RLock object instead +# of Lock object +lock = threading.RLock() + +# the below thread is trying to access +# the shared resource +lock.acquire() +Lidiya = Lidiya + 19 + +# the below thread is trying to access +# the shared resource +lock.acquire() +Lidiya = Lidiya - 24 +lock.release() +lock.release() + +# displaying the value of shared resource +print(Lidiya) \ No newline at end of file diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/Semaphore.py b/QUIS SISTER 1/quis_fadhelrahmawan/Semaphore.py new file mode 100644 index 0000000..234d606 --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/Semaphore.py @@ -0,0 +1,37 @@ +# importing the modules +from threading import * +import time + +# creating thread instance where count = 3 +obj = Semaphore(2) + +# creating instance +def display(name): + + # calling acquire method + obj.acquire() + for i in range(7): + print('M.Ihsan, ', end = ' Diyaa') + time.sleep(1) + print(name) + + # calling release method + obj.release() + +# creating multiple thread +t1 = Thread(target = display , args = ('Thread-1',)) +t2 = Thread(target = display , args = ('Thread-2',)) +t3 = Thread(target = display , args = ('Thread-3',)) +t4 = Thread(target = display , args = ('Thread-4',)) +t5 = Thread(target = display , args = ('Thread-5',)) +t6 = Thread(target = display , args = ('Thread-6',)) +t7 = Thread(target = display , args = ('Thread-7',)) + +# calling the threads +t1.start() +t2.start() +t3.start() +t4.start() +t5.start() +t6.start() +t7.start() \ No newline at end of file diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/Thread_definition.py b/QUIS SISTER 1/quis_fadhelrahmawan/Thread_definition.py new file mode 100644 index 0000000..080c33a --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/Thread_definition.py @@ -0,0 +1,17 @@ +import threading + + +def my_func(thread_number): + return print('ayank dimana? {}'.format(thread_number)) + + +def main(): + threads = [] + for i in range(7): + t = threading.Thread(target=my_func, args=(i,)) + threads.append(t) + t.start() + t.join() + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/Thread_name_and_processes.py b/QUIS SISTER 1/quis_fadhelrahmawan/Thread_name_and_processes.py new file mode 100644 index 0000000..8d5034f --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/Thread_name_and_processes.py @@ -0,0 +1,31 @@ +from threading import Thread +import time +import os + +class MyThreadClass (Thread): + def __init__(self, name): + Thread.__init__(self) + self.name = name + + def run(self): + print("jangan menunggu yang tidak pasti {}".format(self.name)) #, " is {} \n".format(os.getpid())) + +def main(): + from random import randint + # Thread Creation + thread1 = MyThreadClass("Thread#1 ") + thread2 = MyThreadClass("Thread#2 ") + + # Thread Running + thread1.start() + thread2.start() + + thread1.join() + thread2.join() + + # End + print("lebih baik rebahan dari pada menunggu") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/Threading_with_queue.py b/QUIS SISTER 1/quis_fadhelrahmawan/Threading_with_queue.py new file mode 100644 index 0000000..5cebee6 --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/Threading_with_queue.py @@ -0,0 +1,53 @@ +""""Thread synchronisation with queue""" + +from threading import Thread +from queue import Queue +import time +import random + + +class Producer(Thread): + + def __init__(self, queue): + Thread.__init__(self) + self.queue = queue + + def run(self): + for i in range(5): + item = random.randint(0, 256) + self.queue.put(item) + print('Producer notify : item N°%d appended to queue by %s\n'\ + % (item, self.name)) + time.sleep(1) + + +class Consumer(Thread): + + def __init__(self, queue): + Thread.__init__(self) + self.queue = queue + + def run(self): + while True: + item = self.queue.get() + print('Consumer notify : %d popped from queue by %s'\ + % (item, self.name)) + self.queue.task_done() + +if __name__ == '__main__': + queue = Queue() + + t1 = Producer(queue) + t2 = Consumer(queue) + t3 = Consumer(queue) + t4 = Consumer(queue) + + t1.start() + t2.start() + t3.start() + t4.start() + + t1.join() + t2.join() + t3.join() + t4.join() diff --git a/QUIS SISTER 1/quis_fadhelrahmawan/queue.py b/QUIS SISTER 1/quis_fadhelrahmawan/queue.py new file mode 100644 index 0000000..5e05272 --- /dev/null +++ b/QUIS SISTER 1/quis_fadhelrahmawan/queue.py @@ -0,0 +1,55 @@ +def queue(): + s = [] + return s +def enqueue(s,i): + s.insert(0,i) + return s +def dequeue(s): + return s.pop() +def rear(s): + return (s[0]) +def front(s): + return (s[len(s)-1]) +def size(s): + return len(s) +def isEmpty(s): + return s==[] + +def No2(): + s = queue() + k='' + while True: + banyak = int(input('Masukan banyak orang yang ingin ada di permainan = ')) + for j in range(banyak): + orang = input('Masukan nama orang ke %i yang masuk di antrian = '%(j+1)) + enqueue(s,orang) + s.reverse() + print('Orang yang berada di Antrian %s'%s) + s.reverse() + o = input('Masukan nama orang yang ingin ditemukan = ') + ditemukan = 't' + itung = 0 + while ditemukan=='t': + if o == front(s): + print('Congrast! Orang ditemukan') + ditemukan = 'y' + elif o != front(s): + masukan = dequeue(s) + enqueue(s,masukan) + ditemukan = 't' + s.reverse() + print('Looping %i = %s'%((itung+1),s)) + s.reverse() + itung+=1 + if itung > len(s): + print('Maaf! Orang yang dimaksud tidak ada') + ditemukan = 'y' + print('Total looping yang perlukan adalah',str(itung-1)) + k = input('ingin melanjutkan permainan (y/n) ? ') + if k != 'y': + break + else: + print('OKE LANJUT') + +No2() + \ No newline at end of file