rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Nov 14
2002

Distributed Shared Memory – Read Replication Model digg

Sample input would be of the form:

read(10)
writ(100)
read(20)
writ(200)

Download the report.

assign2.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* Distributed Shared Memory - Read Replication Model
 * Pravin Paratey (October 31, 2002)
 * DS Programming Assignment 2
*/
 
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
 
#define MEMSIZE 100
 
pthread_t thChild[5]; /* Stores the child descriptor returned by pthread_create */
char childStack[5]; /* Space for the child stack */
 
 
void *daChild (void *arg);
void SendWriteMessage(int id, int memorynum, int memory[]);
void SendReadMessage(int id, int memorynum, int memory[]);
void PurgeMemory (int id, int memorynum, int memory[]);
 
int main(int argc, char *argv[])
{
	int i;
	/* We have 5 nodes with memory addresses 0-499 statically distributed among them as
	 * node1 -> 0 - 99
	 * node2 -> 100 - 199
	 * node3 -> 200 -299
	 * node4 -> 300 - 399
	 * node5 -> 400 - 499
	*/
 
	for (i=1; i<=5; i++)
	{
		if(pthread_create(&thChild[i], NULL, daChild, (void *) i) != 0)
		{
			fprintf(stderr,"Error: Unable to create thread");
			return 1;
		}
	}
 
sleep(10);
 
	return 0;
}
 
// int daChild (int arg) SB
void *daChild (void *arg)
{
	int num;
	FILE *fp;
	char filename[10];
	int eof;
	char inbuf[15];
	int innum, i;
 
 
	/* This is the block of memory. I am storing other details here.
	 * Actually these details should be stored elsewhere, but wth
	 * The other details are who has the read block and who has write lock
	 * the first 8 bits tell the node(s) which have the lock
	 * the next 8 the type of lock,
	 * 	0 - read block
	 * 	1 - write block
	 */
	int memory[100];
//	struct sockaddr_in sa1, sa2;
//	int sock1, sock2; /* Listening and sending sockets */
 
	num = (int) arg;
	printf ("[%i] I am %i\n", pthread_self(), num);
	/* Generate the filename */
	sprintf(filename, "%i", num);
	printf("Opening %s\n", filename);
	/* Open the file which has instructions */
	fp = fopen (filename, "r");
	if (!fp)
	{
		perror("Terminating Thread");
		pthread_exit((void *)1);
	}
	/* Read instructions and execute them */
	while (1)
	{
		eof = fscanf(fp, "%5s%i%*c", &inbuf, &innum);
//		printf("DEBUG %s %i\n",inbuf, innum);
		if (eof == -1)
			break;
		if (strcmp(inbuf, "read(") == 0)
		{
			/* Determine which node to send message to
			 * The procedure is as follows -
			 * If the memory being addressed isn't ours,
			 * 	send message and wait for OK or NEG
			 * Otherwise you know what to do (look at report)
			 */
 
			printf("[-%i-] Reading block %i\n",num, innum);
 
			/* Send READ(innum) broadcast */
//			for (i=1;i<=5;i++)
//				SendReadMessage(i, innum, memory);
 
			/* If this is our node */
			if (innum % 100 == num-1)
			{
				/* Send READ(innum) message to self server */
//				printf("SELF\n");
				SendReadMessage(num, innum, memory);
			}
			else
			{
				/* if we don't have block in our cache */
				/* Send READ(innum) message to other server */
//				printf("OTHER\n");
				SendReadMessage(innum%100 + 1, innum, memory);
			}
			/* Get block and cache it */
			printf("[-%i-] Reading %i done\n",num, innum);
 
		}
		else if (strcmp(inbuf, "writ(") == 0)
		{
			printf("[-%i-] Writing block %i\n", num, innum);
			/* Send WRITE (innum) message to other nodes */
 
			for (i=1; i<=5; i++)
				SendWriteMessage(i,innum, memory);
 
			printf("[-%i-] Writing %i done\n", num, innum);
		}
	}
	/* Close file */
	fclose(fp);
 
//	sleep(10);
 
	pthread_exit(NULL);
}
 
void SendReadMessage(int id, int memorynum,int memory[])
{
	int thenum;
//	printf("DEBUG-READ id=%i memorynum=%i\n",id,memorynum);
 
	thenum = memorynum % id;
 
	if (memorynum % 100 == id-1)
	{
		/* If memory is not being written */
		if (memory[thenum] == 0 || memory[thenum] == 1)
		{
			/* Mark memory as being read */
			memory[memorynum % id] = 1;
 
			/* Send block back here */
			printf ("[%i] Sending block %i...\n", id, memorynum);
 
			/* Send message back to calling node SB DITCH*/
		}
		else
		{
			while (memory[thenum] == 2)
			{
				printf("[%i] sleeping\n", id);
				sleep(1);
			}
			/* Mark memory as being read */
			memory[memorynum % id] = 1;
 
			/* Send block back here */
			printf ("[%i] Sending block %i...\n", id, memorynum);
 
			/* Send message back to calling node SB DITCH*/
		}
	}
	else
	{
		/* Do nothing, not our node */
		return;
	}
}
 
void SendWriteMessage(int id, int memorynum, int memory[])
{
	int thenum;
	int i;
 
//	printf("DEBUG-WRITE id=%i memorynum=%i mod=%i\n",id,memorynum,memorynum%100);
 
	thenum = memorynum % id;
 
	if (memorynum % 100 == id-1)
	{
		/* If memory is not being written */
		if (memory[thenum] == 0 || memory[thenum] == 1)
		{
			/* Send to all nodes PurgeMemory(thenum) SB */
			for (i=1;i<=5;i++)
				PurgeMemory(i, thenum, memory);
			/* Set the writing flag */
			memory[thenum] = 2;
			/* Do actual writing */
			printf("[%i] Writing to location %i ...\n", id, memorynum);
//			sleep(4);
			printf("\t... done[%i] \n",id);
			/* Unset the writing flag */
			memory[thenum] = 0;
		}
		else
		{
			/* Wait till somebody finishes with the block */
			while (memory[thenum] == 2)
			{
				printf("[%i] sleeping\n", id);
				sleep(1);
			}
			/* Send to all nodes PurgeMemory(thenum) SB */
			for (i=1;i<=5;i++)
				PurgeMemory(i, thenum, memory);
 
			/* Set the writing flag */
			memory[thenum] = 2;
			/* Do actual writing */
			printf("[%i] Writing to location %i ...\n", id, memorynum);
//			sleep(1);
			printf("\t... done[%i] \n",id);
			/* Unset the writing flag */
			memory[thenum] = 0;
		}
	}
	else
	{
		/* Do nothing, not our node */
		return;
	}
 
}
 
void PurgeMemory (int id, int memorynum, int memory[])
{
	int thenum;
//	printf("DEBUG id=%i memorynum=%i\n",id,memorynum);
 
	thenum = memorynum % id;
 
	if (memorynum % 100 == id-1)
	{
		printf("[%i] Memory block %i purged ...\n", id, memorynum);
	}
	else
	{
		/* Do nothing, not our node */
		return;
	}
}

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Latest Articles

Apr
07

Palindromic sub-sequences in python

This bit of python code returns all palindromic subsequences in the input string.

[Read More]
Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it!

[Read More]

Featured Projects

NLP classes for PHP

NLP classes for PHP

This is an ongoing project to develop a set of classes for Natural Language Processing. Some code would be ported from the NLTK project.

[Read More]

Deebot

Deebot

Deeb0t is an IRC chat bot capable of making meaningful conversation with other users. It also responds to commands issued by its owner.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.