-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvectorclock.cpp
108 lines (90 loc) · 2.28 KB
/
vectorclock.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>
#include<conio.h>
#define SIZE 10
using namespace std;
class node {
public:
int data[SIZE];
node *next;
node() {
for(int p=0; p<SIZE; p++) {
data[p] = 0;
}
next = NULL;
}
node(int v[], int n1) {
for(int s = 0; s < n1; s++) {
data[s] = v[s];
}
next = NULL;
}
friend class process;
}*start=NULL;
int main() {
int n, events, sent, receive, sentE, recE, commLines = 0;
node *temp;
node *proc[SIZE]; //array of processes
cout<<"Enter no. of processes: ";
cin>>n;
int vector[n] = {0}; //representation of data
/*----------------INITIALIZATION LOOP-------------------------*/
for(int i = 0; i < n; i++) { //number of processes
for(int v = 0; v < n; v++) {
vector[v] = 0;
}
cout<<"Enter no. of events in process "<<i+1<<": ";
cin>>events;
for(int j = 1; j <= events; j++) {
vector[i] = j;
node *newnode = new node(vector,n);
if(start == NULL) {
start = newnode;
temp = start;
} else {
temp->next = newnode;
temp = temp->next;
}
}
proc[i] = start;
start = NULL;
}
/*-------------------DATA GATHERING--------------------*/
cout<<"\nEnter the number of communication lines: ";
cin>>commLines;
node *tempS, *tempR;
for(int i = 0; i < commLines; i++) {
cout<<"\nEnter the sending process: ";
cin>>sent;
cout<<"\nEnter the receiving process: ";
cin>>receive;
cout<<"\nEnter the sending event number: ";
cin>>sentE;
cout<<"\nEnter the receiving event number: ";
cin>>recE;
tempS = proc[sent - 1];
tempR = proc[receive - 1];
for(int j = 1; j < sentE; j++)
tempS = tempS->next;
for(int j = 1; j < recE; j++)
tempR = tempR->next;
for(int j = 0; j < n; j++) {
tempR->data[j] = (tempR->data[j] < tempS->data[j]) ? tempS->data[j] : tempR->data[j];
}
}
/*-------------------DISPLAYING------------------------*/
cout<<"\nThe resulting vectors are:\n\n";
for(int k = 0; k < n; k++) {
cout<<"Process "<<k + 1<<": ";
node *temp1 = proc[k];
while(temp1) {
cout<<"(";
for(int f = 0; f < n - 1; f++)
cout<<temp1->data[f]<<",";
cout<<temp1->data[n-1];
cout<<")";
temp1 = temp1->next;
}
cout<<endl;
}
return 0;
}