|
发表于 2003-9-9 18:00:34
|
显示全部楼层
1 /* -------------------------------------------------------------------- *
2 * master_pvm.c *
3 * *
4 * This is the master program for the simple PVM demonstration. *
5 * -------------------------------------------------------------------- */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <pvm3.h> /* declares PVM constants and functions */
9 #include <string.h>
10 int main()
11 {
12 int mytid; /* our task ID */
13 int slave_tid; /* task ID of the slave */
14 int result;
15 char message[] = "hello pvm";
16
17 /* enroll ourselves into the PVM system and get our ID */
18 mytid = pvm_mytid();
19 /* spawn the slave */
20 result = pvm_spawn("slave_pvm", (char**)0, PvmTaskDefault,
21 "", 1, &slave_tid);
22 /* check if the slave was spawned successfully */
23 if(result != 1)
24 {
25 fprintf(stderr, "Error: Cannot spawn slave.\n");
26 /* clean up and exit from the PVM system */
27 pvm_exit();
28 exit(EXIT_FAILURE);
29 }
30 /* initialize the data buffer to send data to slave */
31 pvm_initsend(PvmDataDefault);
32 /* ``pack'' the string into the data buffer */
33 pvm_pkstr(message);
34 /* send the string to the slave with a message tag of 0 */
35 pvm_send(slave_tid, 0);
36 /* wait and receive the result string from the slave */
37 pvm_recv(slave_tid, 0);
38
39 /* ``unpack'' the result from the slave */
40 pvm_upkstr(message);
41 /* show the result from the slave */
42 printf("Data from the slave : %s\n", message);
43 /* clean up and exit from the PVM system */
44 pvm_exit();
45
46 exit(EXIT_SUCCESS);
47 } /* end main() */
48 /* end master_pvm.c */
--------------------------------------------------------------------------------
1 /* -------------------------------------------------------------------- *
2 * slave_pvm.c *
3 * *
4 * This is the slave program for the simple PVM demonstration *
5 * -------------------------------------------------------------------- */
6 #include <stdio.h>
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <pvm3.h>
10 #define MSG_LEN 20
11 void convert_to_upper(char*);
12 int main()
13 {
14 int mytid;
15 int parent_tid;
16 char message[MSG_LEN];
17 /* enroll ourselves into the PVM system */
18 mytid = pvm_mytid();
19 /* get the task ID of the master */
20 parent_tid = pvm_parent();
21 /* receive the original string from master */
22 pvm_recv(parent_tid, 0);
23 pvm_upkstr(message);
24 /* convert the string to upper case */
25 convert_to_upper(message);
26 /* send the converted string to the master */
27 pvm_initsend(PvmDataDefault);
28 pvm_pkstr(message);
29 pvm_send(parent_tid, 0);
30 /* clean up and exit from the PVM system */
31 pvm_exit();
32
33 exit(EXIT_SUCCESS);
34 } /* end main() */
35 /* function to convert the given string into upper case */
36 void convert_to_upper(char* str)
37 {
38 while(*str != '\0')
39 {
40 *str = toupper(*str);
41 str++;
42 }
43 } /* end convert_to_upper() */
44 /* end slave_pvm.c */
--------------------------------------------------------------------------------
1 # Make file for the demo PVM program
2 .SILENT :
3 # paths fro PVM include files and libraries
4 INCDIR=-I/usr/share/pvm3/include
5 LIBDIR=-L/usr/share/pvm3/lib/LINUX
6 # link the PVM library
7 LIBS=-lpvm3
8 CFLAGS=-Wall
9 CC=gcc
10 TARGET=all
11 # this is where the PVM executables go
12 PVM_HOME=$(HOME)/pvm3/bin/LINUX
13 all : $(PVM_HOME)/master_pvm $(PVM_HOME)/slave_pvm
14 $(PVM_HOME)/master_pvm : master_pvm.c
15 $(CC) -o $(PVM_HOME)/master_pvm master_pvm.c $(CFLAGS) $(LIBS) \
16 $(INCDIR) $(LIBDIR)
17 $(PVM_HOME)/slave_pvm : slave_pvm.c
18 $(CC) -o $(PVM_HOME)/slave_pvm slave_pvm.c $(CFLAGS) $(LIBS) \
19 $(INCDIR) $(LIBDIR)
--------------------------------------------------------------------------------
Once your programs have been compiled, you must copy them into the ~/pvm3/bin/LINUX directory. (The makefile does it by default). Now to run the programs, you must first start the PVM system. To do this give the command pvm to start the PVM Console. Now at the pvm> prompt, type quit. The output will be as follows:
pvm> quit
quit
Console: exit handler called
pvmd still running.
Notice the last line, indicating that the PVM daemon (pvmd) is still running. To run the PVM programs, you need to run the PVM daemon which manages the exchange of messages and that what we are doing here. Once the PVM daemon is running, you can run the program by the following commands:
[rahul@joshicomp rahul]$ cd ~/pvm3/bin/LINUX/
[rahul@joshicomp LINUX]$ ./master_pvm
Data from the slave : HELLO PVM |
|