You are on page 1of 7

-------------------------------array.py:---------------------------------------#!

/usr/bin/python
import threading
import time
numbers = range(0,20)
numThreads = 6
class myThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.kill = False
def run(self):
print "Starting thread " + self.name
while self.kill == False:
#i = self.threadID
#print "threadID = " + str(self.threadID)
compute(self.name, self.threadID)

def compute(threadName, threadID):
i = threadID
while i < 19:
#print "i = " + str(i)
global numbers
numbers[i] += 1
i += numThreads
threads = []

print numbers
for t in range(0, numThreads):
threads.append(myThread(t,"Thread-" + str(t)))
threads[t].start()
#for t in range(0, numThreads):
#
print numbers
#
threads[t].join()
while len(threads) > 0:
#print numbers
try:
threads = [thread.join(1) for thread in threads if thread is not None an
d thread.isAlive()]
print threads[0].name
except KeyboardInterrupt:
print "Killing threads..."
for thread in threads:
thread.kill = True

print numbers
print "Exiting Main Thread"

-------------------------array_barrier.py-------------------------------------------from threading import *
numbers = range(0,20)
numThreads = 3
class ReusableBarrier():
def __init__(self, num_threads):
self.num_threads = num_threads
self.count_threads1 = [self.num_threads]
self.count_threads2 = [self.num_threads]
self.count_lock = Lock()
# protejam accesarea/modificare
a contoarelor
self.threads_sem1 = Semaphore(0)
# blocam thread-urile in prima
etapa
self.threads_sem2 = Semaphore(0)
# blocam thread-urile in a doua
etapa
def wait(self):
self.phase(self.count_threads1, self.threads_sem1)
self.phase(self.count_threads2, self.threads_sem2)
def phase(self, count_threads, threads_sem):
with self.count_lock:
count_threads[0] -= 1
if count_threads[0] == 0:
# a ajuns la bariera si ultimul
thread
for i in range(self.num_threads):
threads_sem.release()
# incrementarea semaforului va
debloca num_threads thread-uri
count_threads[0] = self.num_threads # reseteaza contorul
threads_sem.acquire()
# num_threads-1 threaduri se bl
ocheaza aici
# contorul semaforului se decre
menteaza de num_threads ori
class MyThread(Thread):
def __init__(self, tid, barrier):
Thread.__init__(self)
self.tid = tid
self.barrier = barrier
def run(self):
for i in xrange(10):
compute(self.tid)
self.barrier.wait()
print "I'm Thread " + str(self.tid) + " after barrier, in step " + s
tr(i) + "\n",
def compute(threadID):
i = threadID

while i < 20:
#print "i = " + str(i)
global numbers
numbers[i] += 1
i += numThreads
barrier = ReusableBarrier(3)
thread1 = MyThread(0, barrier)
thread2 = MyThread(1, barrier)
thread3 = MyThread(2, barrier)
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()
print numbers
print "Main thread exit"
------------------------------------------barrier.py---------------------------------from threading import *
class SimpleBarrier():
def __init__(self, num_threads):
self.num_threads = num_threads
self.count_threads = self.num_threads
d-uri ramase
self.count_lock = Lock()
rea contorului
self.threads_sem = Semaphore(0)

# contorizeaza numarul de threa
# protejeaza accesarea/modifica
# blocheaza thread-urile ajunse

def wait(self):
with self.count_lock:
self.count_threads -= 1
if self.count_threads == 0:
#
thread
for i in range(self.num_threads):
print "i = " + str(i)
self.threads_sem.release() #
debloca num_threads thread-uri
self.threads_sem.acquire()
#
ocheaza aici
#
menteaza de num_threads ori
class MyThread(Thread):
def __init__(self, tid, barrier):
Thread.__init__(self)
self.tid = tid
self.barrier = barrier
def run(self):

a ajuns la bariera si ultimul

incrementarea semaforului va
num_threads-1 threaduri se bl
contorul semaforului se decre

print "I'm Thread " + str(self.tid) + " before\n",
self.barrier.wait()
print "I'm Thread " + str(self.tid) + " after barrier\n",
barrier = SimpleBarrier(3)
thread1 = MyThread(1, barrier)
thread2 = MyThread(2, barrier)
thread3 = MyThread(3, barrier)
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()
print "Main thread exit"
----------------------------------rbarrier.py--------------------------------------from threading import *
class ReusableBarrier():
def __init__(self, num_threads):
self.num_threads = num_threads
self.count_threads1 = [self.num_threads]
self.count_threads2 = [self.num_threads]
self.count_lock = Lock()
# protejam accesarea/modificare
a contoarelor
self.threads_sem1 = Semaphore(0)
# blocam thread-urile in prima
etapa
self.threads_sem2 = Semaphore(0)
# blocam thread-urile in a doua
etapa
def wait(self):
self.phase(self.count_threads1, self.threads_sem1)
self.phase(self.count_threads2, self.threads_sem2)
def phase(self, count_threads, threads_sem):
with self.count_lock:
count_threads[0] -= 1
if count_threads[0] == 0:
# a ajuns la bariera si ultimul
thread
for i in range(self.num_threads):
threads_sem.release()
# incrementarea semaforului va
debloca num_threads thread-uri
count_threads[0] = self.num_threads # reseteaza contorul
threads_sem.acquire()
# num_threads-1 threaduri se bl
ocheaza aici
# contorul semaforului se decre
menteaza de num_threads ori
class MyThread(Thread):
def __init__(self, tid, barrier):
Thread.__init__(self)
self.tid = tid

self.barrier = barrier
def run(self):
for i in xrange(10):
self.barrier.wait()
print "I'm Thread " + str(self.tid) + " after barrier, in step " + s
tr(i) + "\n",
barrier = ReusableBarrier(3)
thread1 = MyThread(1, barrier)
thread2 = MyThread(2, barrier)
thread3 = MyThread(3, barrier)
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()
print "Main thread exit"
-----------------------------------read_file.py------------------------------------#!/usr/bin/python
with open('barrier.py') as f:
lines = f.readlines()
print lines
-------------------------------------simple_client.py-----------------------------#!/usr/bin/python

# This is client.py file

import socket

# Import socket module

s = socket.socket()
# Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345
# Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
s.close

# Close the socket when done

--------------------------------------simple_server.py-----------------------------#!/usr/bin/python

# This is server.py file

import socket

# Import socket module

s = socket.socket()

# Create a socket object

host = socket.gethostname() # Get local machine name
port = 12345
# Reserve a port for your service.
s.bind((host, port))
# Bind to the port
s.listen(5)
# Now wait for client connection.
while True:
c, addr = s.accept()
# Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
# Close the connection
------------------------------------threads.py--------------------------------------#!/usr/bin/python
import
import
import
import

threading
time
sys
signal

class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.kill = False
self.counter = counter
def run(self):
while self.kill == False:
print "Starting " + self.name
# Get lock to synchronize threads
threadLock.acquire()
print_time(self.name, self.counter, 3)
# Free lock to release next thread
threadLock.release()
def print_time(threadName, delay, counter):
while counter:
time.sleep(0.1)
print "%s: %s" % (threadName, time.ctime(time.time()))
counter -= 1
threadLock = threading.Lock()
threads = []
# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# Start new Threads
thread1.start()
thread2.start()
# Add threads to thread list
threads.append(thread1)
threads.append(thread2)

# Wait for all threads to complete
#for t in threads:
#
t.join()
while len(threads) > 0:
try:
#
for t in threads:
#
t.join()
threads = [thread.join() for thread in threads if thread is not
None and thread.isAlive()]
except KeyboardInterrupt:
print "Killing threads..."
for thread in threads:
thread.kill = True
print "Exiting Main Thread"