-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshare.c
More file actions
76 lines (67 loc) · 1.88 KB
/
share.c
File metadata and controls
76 lines (67 loc) · 1.88 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
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
/* DSM Assignment: Shared memory test
*
* Author : Manuel M T Chakravarty
* Created: 25 March 2000
*
* Version $Revision: 1.4 $ from $Date: 2003/03/30 14:31:21 $
*
* Copyright (c) 2000 MMT Chakravarty & University of New South Wales
*
* DESCRIPTION ---------------------------------------------------------------
*
* Test shared use of memory
*
* DOCU ----------------------------------------------------------------------
*
* language: ANSI C
*
* TODO ----------------------------------------------------------------------
*
*/
#include <stdio.h>
#include "sm.h"
void fatal (int nid, char *msg)
{
printf ("node %d: Fatal internal error:\n%s\n", nid, msg);
exit (1);
}
int main (int argc, char *argv[])
{
int nodes, nid;
char *sharedChar, *sharedChar2;
if (sm_node_init (&argc, &argv, &nodes, &nid))
fatal (nid, "share: Cannot initialise!");
/* first, node #0 allocates a shared variable and uses it to communicate
* the letter `A' to node #1
*/
if (0 == nid) {
sharedChar = (char *) sm_malloc (sizeof (char));
*sharedChar = 'A';
}
sm_bcast ((void **) &sharedChar, 0);
/* Checkpoint A */
printf ("node %d: 1st shared variable is at %p.\n", nid, sharedChar);
if (0 != nid)
printf ("node %d: Value in 1st shared variable is %d\n",
nid, *sharedChar);
/* Checkpoint B */
sm_barrier ();
/* next, node #1 allocates a shared variable and uses it to allow node #0
* to communicate the letter `B' to it
*/
if (1 == nid)
sharedChar2 = (char *) sm_malloc (sizeof (char));
sm_bcast ((void **) &sharedChar2, 1);
/* Checkpoint C */
if (0 == nid)
*sharedChar2 = 'B';
/* Checkpoint D */
sm_barrier (); /* make sure node #0 wrote the character */
if (0 != nid)
printf ("node %d: Value in 2nd shared variable is %d\n",
nid, *sharedChar2);
/* Checkpoint E */
sm_barrier ();
sm_node_exit ();
return 0;
}