From 632189c22e4795a9370a21f0089d3c6337081647 Mon Sep 17 00:00:00 2001 From: nabilla2 Date: Wed, 20 Apr 2022 18:01:02 +0700 Subject: [PATCH] Quis 1 Sistem Tersebar Nabilla --- QUIS SISTER 1/Nabilla 1184075/Barrier.py | 31 +++++++++ QUIS SISTER 1/Nabilla 1184075/Event.py | 38 +++++++++++ .../Nabilla 1184075/MythreadClass.py | 61 +++++++++++++++++ .../Nabilla 1184075/MythreadClass_lock_2.py | 63 +++++++++++++++++ QUIS SISTER 1/Nabilla 1184075/Rlock.py | 26 +++++++ QUIS SISTER 1/Nabilla 1184075/Semaphore.py | 33 +++++++++ .../Nabilla 1184075/Thread_definition.py | 17 +++++ .../Thread_name_and_processes.py | 38 +++++++++++ .../Nabilla 1184075/Threading_with_queue.py | 68 +++++++++++++++++++ QUIS SISTER 1/Nabilla 1184075/queue.py | 55 +++++++++++++++ 10 files changed, 430 insertions(+) create mode 100644 QUIS SISTER 1/Nabilla 1184075/Barrier.py create mode 100644 QUIS SISTER 1/Nabilla 1184075/Event.py create mode 100644 QUIS SISTER 1/Nabilla 1184075/MythreadClass.py create mode 100644 QUIS SISTER 1/Nabilla 1184075/MythreadClass_lock_2.py create mode 100644 QUIS SISTER 1/Nabilla 1184075/Rlock.py create mode 100644 QUIS SISTER 1/Nabilla 1184075/Semaphore.py create mode 100644 QUIS SISTER 1/Nabilla 1184075/Thread_definition.py create mode 100644 QUIS SISTER 1/Nabilla 1184075/Thread_name_and_processes.py create mode 100644 QUIS SISTER 1/Nabilla 1184075/Threading_with_queue.py create mode 100644 QUIS SISTER 1/Nabilla 1184075/queue.py diff --git a/QUIS SISTER 1/Nabilla 1184075/Barrier.py b/QUIS SISTER 1/Nabilla 1184075/Barrier.py new file mode 100644 index 0000000..965bbae --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/Barrier.py @@ -0,0 +1,31 @@ + + +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +jumlah_menu = 4 +hasilnya = Barrier(jumlah_menu) +menunya = ['BAKSO', 'BATAGOR', 'SIOMAY', 'MIE AYAM', 'KUE BERAS', 'SEBLAK'] + + + +def menu(): + cobain = menunya.pop() + sleep(randrange(2, 3 )) + print('%s JUGA TERMASUK DALAM MENU \n' % (cobain)) + hasilnya.wait() + + +def main(): + threads = [] + print('Yang ada disini') + for i in range(jumlah_menu): + threads.append(Thread(target=menu)) + threads[-1].start() + for thread in threads: + thread.join() + print('okeh beres') + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Nabilla 1184075/Event.py b/QUIS SISTER 1/Nabilla 1184075/Event.py new file mode 100644 index 0000000..d22288e --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/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/Nabilla 1184075/MythreadClass.py b/QUIS SISTER 1/Nabilla 1184075/MythreadClass.py new file mode 100644 index 0000000..efad835 --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/MythreadClass.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Apr 20 13:53:13 2022 + +@author: Hp +""" + +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 + \ + " mulai presentasi, NPM "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print ("---> " + self.name + " melakukan presentasi\n") + + +def main(): + start_time = time.time() + + # Thread Creation + Audry1 = MyThreadClass("Audry ", randint(6,10)) + Artha2 = MyThreadClass("Artha ", randint(6,10)) + Lina3 = MyThreadClass("Lina ", randint(6,10)) + Jimin4 = MyThreadClass("Jimin ", randint(6,10)) + Wooyoung5 = MyThreadClass("Wooyoung ", randint(6,10)) + Haruto6 = MyThreadClass("Haruto ", randint(6,10)) + + # Thread Running + Audry1.start() + Artha2.start() + Lina3.start() + Jimin4.start() + Wooyoung5.start() + Haruto6.start() + + # Thread joining + Audry1.join() + Artha2.join() + Lina3.join() + Jimin4.join() + Wooyoung5.join() + Haruto6.join() + + # End + print("End") + + #Execution Time + print("--- waktu presentasi %s seconds ---" % (time.time() - start_time)) + + +if _name_ == "_main_": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Nabilla 1184075/MythreadClass_lock_2.py b/QUIS SISTER 1/Nabilla 1184075/MythreadClass_lock_2.py new file mode 100644 index 0000000..7e1d873 --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/MythreadClass_lock_2.py @@ -0,0 +1,63 @@ +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 games, ID games "\ + + 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 + Audry1 = MyThreadClass("Audry ", randint(1,5)) + Artha2 = MyThreadClass("Artha ", randint(1,5)) + Lina3 = MyThreadClass("Lina ", randint(1,5)) + Jimin4 = MyThreadClass("Jimin ", randint(1,5)) + Wooyoung5 = MyThreadClass("Wooyoung ", randint(1,5)) + Haruto6 = MyThreadClass("Haruto ", randint(1,5)) + + + # Thread Running + Audry1.start() + Artha2.start() + Lina3.start() + Jimin4.start() + Wooyoung5.start() + Haruto6.start() + + # Thread joining + Audry1.join() + Artha2.join() + Lina3.join() + Jimin4.join() + Wooyoung5.join() + Haruto6.join() + + # End + print("End") + + #Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if _name_ = "_main_": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Nabilla 1184075/Rlock.py b/QUIS SISTER 1/Nabilla 1184075/Rlock.py new file mode 100644 index 0000000..0ba5cd8 --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/Rlock.py @@ -0,0 +1,26 @@ +# program to illustrate the use of RLocks + +# importing the module +import threading + +# initializing the shared resource +VickySafira = 0 + +# creating an RLock object instead +# of Lock object +lock = threading.RLock() + +# the below thread is trying to access +# the shared resource +lock.acquire() +VickySafira = VickySafira + 25 + +# the below thread is trying to access +# the shared resource +lock.acquire() +VickySafira = VickySafira + 32 +lock.release() +lock.release() + +# displaying the value of shared resource +print("VickySafira = 57") diff --git a/QUIS SISTER 1/Nabilla 1184075/Semaphore.py b/QUIS SISTER 1/Nabilla 1184075/Semaphore.py new file mode 100644 index 0000000..fd3bcf1 --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/Semaphore.py @@ -0,0 +1,33 @@ +# importing the modules +from threading import * +import time + +# creating thread instance where count = 3 +obj = Semaphore(1) + +# creating instance +def display(name): + + # calling acquire method + obj.acquire() + for i in range(1): + print('Jenis Sate, ', end = '') + time.sleep(1) + print(name) + + # calling release method + obj.release() + +# creating multiple thread +t1 = Thread(target = display , args = ('Taichan-1',)) +t2 = Thread(target = display , args = ('Maranggi-2',)) +t3 = Thread(target = display , args = ('Kambing-3',)) +t4 = Thread(target = display , args = ('Ayam-4',)) +t5 = Thread(target = display , args = ('Lilit-5',)) + +# calling the threads +t1.start() +t2.start() +t3.start() +t4.start() +t5.start() diff --git a/QUIS SISTER 1/Nabilla 1184075/Thread_definition.py b/QUIS SISTER 1/Nabilla 1184075/Thread_definition.py new file mode 100644 index 0000000..080c33a --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/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/Nabilla 1184075/Thread_name_and_processes.py b/QUIS SISTER 1/Nabilla 1184075/Thread_name_and_processes.py new file mode 100644 index 0000000..1469250 --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/Thread_name_and_processes.py @@ -0,0 +1,38 @@ +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("Bakso Bakar {}".format(self.name)) # , " is {} \n".format(os.getpid())) + + +def main(): + from random import randint + # Thread Creation + thread1 = MyThreadClass("mantappu ") + thread2 = MyThreadClass("mantappu") + + # Thread Running + thread1.start() + thread2.start() + + # Thread joining + thread1.join() + thread2.join() + + # End + print("Siap Dihidangakan") + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/Nabilla 1184075/Threading_with_queue.py b/QUIS SISTER 1/Nabilla 1184075/Threading_with_queue.py new file mode 100644 index 0000000..d4cced9 --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/Threading_with_queue.py @@ -0,0 +1,68 @@ +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('Dari angka 1-10, seberapa siap kamu? = ')) + for j in range(banyak): + orang = input('undang teman anda %i yang akan masuk dalam games ini = ' % (j + 1)) + enqueue(s, orang) + s.reverse() + print('maka teman anda masuk dalam permainan %s' % s) + s.reverse() + o = input('mencari id patner musuh anda = ') + ditemukan = 't' + itung = 0 + while ditemukan == 't': + if o == front(s): + print('Congrats! id musuh ditemukan') + ditemukan = 'ok' + 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 anda kurang beruntung, Coba lagi ya') + ditemukan = 'ya' + print('Total peserta yang di perlukan', str(itung - 5)) + k = input('apakah anda ingin melanjutkan permainannya? (y/t) ? ') + if k != 'y': + break + else: + print('Semangat, jangan menyerah' ) + + +No2() \ No newline at end of file diff --git a/QUIS SISTER 1/Nabilla 1184075/queue.py b/QUIS SISTER 1/Nabilla 1184075/queue.py new file mode 100644 index 0000000..5e05272 --- /dev/null +++ b/QUIS SISTER 1/Nabilla 1184075/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