File: task0.c

package info (click to toggle)
pvm 3.4.6-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,284 kB
  • sloc: ansic: 72,074; makefile: 1,198; fortran: 631; sh: 285; csh: 74; asm: 37
file content (227 lines) | stat: -rw-r--r-- 6,299 bytes parent folder | download | duplicates (4)
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

static char rcsid[] =
	"$Id: task0.c,v 1.7 2009/01/23 01:12:52 pvmsrc Exp $";

/*
 *         PVM version 3.4:  Parallel Virtual Machine System
 *               University of Tennessee, Knoxville TN.
 *           Oak Ridge National Laboratory, Oak Ridge TN.
 *                   Emory University, Atlanta GA.
 *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
 *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
 *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
 *                   (C) 1997 All Rights Reserved
 *
 *                              NOTICE
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted
 * provided that the above copyright notice appear in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * Neither the Institutions (Emory University, Oak Ridge National
 * Laboratory, and University of Tennessee) nor the Authors make any
 * representations about the suitability of this software for any
 * purpose.  This software is provided ``as is'' without express or
 * implied warranty.
 *
 * PVM version 3 was funded in part by the U.S. Department of Energy,
 * the National Science Foundation and the State of Tennessee.
 */

/*
 *	Filename:	task0.c
 *
 * This program will place a "well known" service into a mailbox
 * along with a communication context.  It will then wait for another
 * task to communicate via that context.
 *
 * task1.c is designed as the "other" communicating task.
 * task_end.c tells task0 to terminate itself and quit waiting.
 *
 * Note that you may load a number of these into the mailbox providing
 * they have unique <service_name> key.
 *
 * The task_end.c program will cause this service to gracefully exit
 * pvm.
 *
 *	usage: task0 [service_name] <flags>
 *
 *				PvmMboxDefault		d
 *				PvmMboxPersistent	p
 *				PvmMboxMultiInstance	m
 *				PvmMboxOverWritable	o
 *
 *  files used:  task0.c taskf.c
 */

#include <stdio.h>
#ifdef HASSTDLIB
#include <stdlib.h>
#endif
#ifndef WIN32
#include <unistd.h>		/* for gethostname */
#else
#include "pvmwin.h"
#endif
#ifdef	SYSVSTR
#include <string.h>
#else
#include <strings.h>
#endif
#include "pvm3.h"

main( argc, argv )
int argc;
char *argv[];
{
	char *me = "task0";
	char machine[25];
	int mytid;
	int context0, context1;
	char *service_name;
	int service_ctr = 0;	/* count of the # of service requests */
	int i;
	int index;				/* returned index for mailbox entry */

	char flagc[10];			/* hold characters for flags */
	int flags;				/* PvmMbox flags set */
	char *flagstring;		/* pointer to string of flag settings */

	int their_tid;			/* tid of task sending service request */
	char msg_txt[100];		/* message received as service request */
	int msg_buf;			/* message buffer to receive on */

	/* display_incomming_parameters( me, argc, argv ); */

	/*
	 *  validate input parameters
	 */
	if ( argc == 3 ) {
		/* we got what we wanted -- all three */
		strcpy( flagc, argv[2] );		/* copy flags to local */
	}
	else {
		if ( argc == 2 ) {
			/* assume flags left off */
			/*   - so force flags to PvmMboxDefault */
			strcpy( flagc, "d" );
		}
		else {
			/* unknown input sequence - complain and exit */
			printf( "\n\nusage: task0 [service_name] <flags>\n" );
			printf( "\tPvmMboxDefault\t\td\n" );
			printf( "\tPvmMboxPersistent\tp\n" );
			printf( "\tPvmMboxMultiInstance\tm\n" );
			printf( "\tPvmMboxOverWritable\to\n" );
			exit( -1 );
		}
	}

	printf( "%s: sending <%s> to set_flags.\n", me, flagc );
	if ( ( flags = set_flags( flagc, &flagstring ) ) < 0 ) {
		exit( -1 );	/* something failed - abort! */
	}

	service_name = argv[1];
	printf( "%s: service name <%s> with flags <%d> : <%s>.\n",
			me, service_name, flags, flagstring );

	if ( ( mytid = pvm_mytid() ) == PvmSysErr ) {
		printf( "\nPVM not up!\n" );
		exit( -1 );
	}

	gethostname( machine, 25 );

	printf( "%s: t%x on machine <%s> with context %d.\n",
			me, mytid, machine, pvm_getcontext() );

	context1 = pvm_newcontext();			/* create new context1 */
	context0 = pvm_setcontext( context1 );	/* activate new context1 */

	printf( "%s: t%x on machine <%s> with context %d.\n",
			me, mytid, machine, pvm_getcontext() );

	/*
	 *  create message to place in mailbox
	 */
	pvm_initsend( PvmDataDefault );
	pvm_packf( "%+ %d %d %s",
			PvmDataDefault, mytid, context1, machine );
	if ( (index = pvm_putinfo( service_name, pvm_getsbuf(), flags ))
			< 0 ) {
		/*
		 *  did not find mailbox - tell user why and exit...
		 */
		switch ( index ) {
			case PvmExists:
				printf( "\n%s: Names service already running\n", me );
				break;
			case PvmNotFound:
				printf( "\n%s: no service running\n", me );
				break;
			case PvmBadParam:
				printf( "\n%s: Invalid argument to pvm_recvinfo().\n",
						me );
				break;
			case PvmNoSuchBuf:
				printf( "\n%s: Message buffer id does not exist.\n",
						me );
				break;
			case PvmDenied:
				printf(
					"\n%s: Key locked by another task, can't delete.\n",
						me );
				break;
			default:
				lpvmerr( "\n%s: task0.c: error %d", me, index );
				break;
		} /* end_switch */
		exit( -1 );
	}

	printf( "%s: just inserted service name <%s> at index %d.\n",
			me, service_name, index );

	for ( ; ; )
	{
		/*
		 *  wait for service requests
		 */
		printf( "%s: t%x on machine <%s> with context %d ",
				me, mytid, machine, pvm_getcontext() );
		printf( "- waiting for service request.\n" );
		msg_buf = pvm_recv( -1, -1 );
		pvm_bufinfo( msg_buf, (int *) 0, (int *) 0, &their_tid );
		pvm_upkstr( msg_txt );
		printf( "%s: t%x %s  Request number %d.\n",
				me, mytid, msg_txt, ++service_ctr );

		if ( strncmp( msg_txt, "END", 3 ) == 0 )
			break;

		/*
		 *  send a greeting message to service requestor
		 */
		sprintf( msg_txt,
			"welcome to my server - you are service request number %d.",
				service_ctr );
		pvm_initsend( PvmDataDefault );
		pvm_pkstr( msg_txt );
		pvm_send( their_tid, 1 );
	}

	/*
	 *  reset back to my original context
	 */
	(void) pvm_setcontext( context0 );
	printf( "%s: t%x on machine <%s> with context %d.\n",
			me, mytid, machine, pvm_getcontext() );
	printf( "%s: handled %d service requests.\n", me, service_ctr );

	pvm_exit();
	exit( 0 );
}