Advertisement
Bkmz

Untitled

Mar 19th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from copy import deepcopy, copy
  3. from pdb import set_trace as st
  4.  
  5. from os import fork, pipe, read, write, close, fdopen
  6. from sys import exit
  7.  
  8. import sysv_ipc
  9.  
  10. import sys
  11. reload(sys)
  12. sys.setdefaultencoding('utf8')
  13.  
  14. QUEUE = False
  15. # if True is Message Queue Mechanism
  16. # another, if False use pipes
  17.  
  18.  
  19. THREADS = 4
  20. LEN = 6
  21. STEP = 1
  22.  
  23. pids = []
  24.  
  25.  
  26. def list_to_str(lst):
  27.     out = ""
  28.     for i in lst:
  29.         out += str(i)
  30.     return out
  31.  
  32.  
  33. STR = list()
  34.  
  35.  
  36. def generate_str():
  37.     global STR, STEP
  38.     STR[len(STR)-1] += STEP
  39.  
  40.     while STR[len(STR)-1] > 9:
  41.         # for i in reversed(xrange(STR))
  42.         i=len(STR)-1
  43.  
  44.         while STR[i]>9 and i>0:
  45.             STR[i] -= 10
  46.             STR[i-1] += 1
  47.             i -= 1
  48.  
  49.  
  50.     # return copy(STR)
  51.     return STR
  52.  
  53.  
  54. def check_str_loop():
  55.     global STR
  56.     flag1=0
  57.     flag2=0
  58.     res  =0
  59.  
  60.     for i in xrange(len(STR)-1):
  61.         if STR[i] > STR[i+1]:
  62.             flag1+=1
  63.  
  64.         if STR[i] < STR[i+1]:
  65.             flag2+=1
  66.  
  67.     if flag1 == len(STR)-1:
  68.         res+=1
  69.  
  70.     if flag2 == len(STR)-1:
  71.         res+=1
  72.  
  73.     return res
  74.  
  75.  
  76. def main():
  77.     global STR, STEP, ID, pids
  78.     def truth(str):
  79.         if int(str[0]) > 9:
  80.             return 0
  81.         else:
  82.             return 1
  83.  
  84.     for i in xrange(THREADS):
  85.        
  86.         if QUEUE:
  87.             q = sysv_ipc.MessageQueue(sysv_ipc.IPC_PRIVATE, sysv_ipc.IPC_EXCL| sysv_ipc.IPC_CREAT)
  88.         else:
  89.             r, w = pipe()
  90.  
  91.         p = fork()
  92.  
  93.         if p == 0:
  94.             str = STR
  95.             avg = 0.0
  96.             index = 0
  97.             STEP = THREADS
  98.  
  99.             for j in xrange(LEN):
  100.                 STR.append(0)
  101.  
  102.             buf = copy(i)
  103.             iter = len(STR)-1
  104.  
  105.             while not buf == 0 and not iter == 0:
  106.                 STR[iter] = buf%10
  107.                 buf /= 10
  108.                 iter -= 1
  109.  
  110.             # print list_to_str(STR)
  111.  
  112.  
  113.             while truth(str):
  114.                 str = generate_str()
  115.                 status = check_str_loop()
  116.  
  117.                 # print "cur: %s\r" % (list_to_str(str)),
  118.                 if status > 0:
  119.                     # print "%s\r" % (" "*20),
  120.                     # print list_to_str(str)
  121.                     avg += float(list_to_str(str))
  122.                     index += 1
  123.  
  124.             # print "sum: %.2f" % (avg)
  125.             # print "ind: %i" % (index)
  126.             # print "avg: %.2f" % (avg/index)
  127.  
  128.             # close(r)
  129.             # write(w, "%i;%.2f" % (index, avg))
  130.  
  131.             if QUEUE:
  132.                 q.send("%i;%.2f"%(index, avg))
  133.             else:
  134.                 close(r)
  135.                 write(w, "%i;%.2f" % (index, avg))
  136.  
  137.  
  138.             sys.exit(0)
  139.         else:
  140.             pids.append([])
  141.             pids[i].append(p)
  142.  
  143.             if QUEUE:
  144.                 pids[i].append(q)
  145.             else:
  146.                 pids[i].append([r,w])
  147.  
  148.     # now working in parent thread:
  149.  
  150.     result = {"avg":0.0, "i":0}
  151.  
  152.     for i in xrange(THREADS):
  153.         # pids[i][1][1].close()
  154.         # close(pids[i][1][1])
  155.         # str = read(pids[i][1][0], 4096).split(";")
  156.         if QUEUE:
  157.             str = pids[i][1].receive()[0].split(";")
  158.         else:
  159.             close(pids[i][1][1])
  160.             str = read(pids[i][1][0], 4096).split(";")
  161.  
  162.         result['avg'] += float(str[1])
  163.         result['i'] += int(str[0])
  164.  
  165.  
  166.  
  167.     print "sum: %.2f" % (result['avg'])
  168.     print "ind: %i" % (result['i'])
  169.     print "avg: %.2f" % (result['avg']/result['i'])
  170.  
  171.  
  172.  
  173.  
  174.  
  175. if __name__ == '__main__':
  176.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement