-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmw2.py
More file actions
35 lines (27 loc) · 1.11 KB
/
mw2.py
File metadata and controls
35 lines (27 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import math
#Simple MasterWorker Application using fork().
#Given string s, the program reverses the first two char in string
#Continue doing so on until it reaches end of string.
#If the len(s) is odd, then the last character's position remains the same.
#No need for locking mechanism
def worker(prev,next):
rev = next+prev; #Reverse the two char string
return rev;
def master(s):
final=[]; #Array to collect processes' work
iter = int(math.floor(len(s)/2));
for i in range(0,iter):
nworker = os.fork(); #Forks a new process
pids = (os.getpid(), nworker) #Gets PID of the created process
print "parent: %d, child: %d" % pids
final.append(worker(s[i*2],s[i*2+1])); #Collects the result
if(len(s)%2==1):
final.append(s[len(s)-1]); #Appends the last char is len(s) is odd
return final;
if __name__ == "__main__":
s="";
stringarr = master("ONPNRATIEIS");
for i in range(0,len(stringarr)): #Techically master's work. Turns the collected work into printable answer.
s+=stringarr[i];
print s;