You are on page 1of 7

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

/usr/bin/python
import time
numbers = range(0,20)
self.name = name
self.kill = False
def run(self):
print "Starting thread " + self.name
while self.kill == False:

while i < 19:
#print "i = " + str(i)
global numbers
numbers[i] += 1

print numbers
#
print numbers
#
#print numbers
try:
except KeyboardInterrupt:

print numbers

numbers = range(0,20)
class ReusableBarrier():
self.count_lock = Lock()
# protejam accesarea/modificare
a contoarelor
etapa
# blocam thread-urile in a doua
etapa
def wait(self):
with self.count_lock:
# a ajuns la bariera si ultimul
# incrementarea semaforului va
ocheaza aici
# contorul semaforului se decre
def __init__(self, tid, barrier):
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",

while i < 20:
#print "i = " + str(i)
global numbers
numbers[i] += 1
barrier = ReusableBarrier(3)
print numbers
class SimpleBarrier():
d-uri ramase
self.count_lock = Lock()
rea contorului

# contorizeaza numarul de threa
# protejeaza accesarea/modifica

def wait(self):
with self.count_lock:
#
print "i = " + str(i)
#
ocheaza aici
#
def __init__(self, tid, barrier):
self.tid = tid
self.barrier = barrier
def run(self):

a ajuns la bariera si ultimul

incrementarea semaforului va
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)
class ReusableBarrier():
self.count_lock = Lock()
# protejam accesarea/modificare
a contoarelor
etapa
# blocam thread-urile in a doua
etapa
def wait(self):
with self.count_lock:
# a ajuns la bariera si ultimul
# incrementarea semaforului va
ocheaza aici
# contorul semaforului se decre
def __init__(self, tid, barrier):
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)
with open('barrier.py') as f:
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
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
s.bind((host, port))
# Bind to the port
s.listen(5)
# Now wait for client connection.
while True:
# Establish connection with client.
c.send('Thank you for connecting')
c.close()
# Close the connection
import
import
import
import

time
sys
signal

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
print_time(self.name, self.counter, 3)
# Free lock to release next thread
while counter:
time.sleep(0.1)
print "%s: %s" % (threadName, time.ctime(time.time()))
counter -= 1

# Wait for all threads to complete
#
t.join()