⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dirtest.c

📁 MPICH是MPI的重要研究,提供了一系列的接口函数,为并行计算的实现提供了编程环境.
💻 C
字号:
#include "p4.h"#include "sr_user.h"    main(argc,argv)int argc;char **argv;{        p4_initenv(&argc,argv);    p4_create_procgroup();    if (p4_get_my_id() == 0)    {	master();    }    else    {	worker();    }    p4_wait_for_end();}master(){    char msg[200];    int i, my_id, size, type, from;    char *incoming, *cp;    int start_time,end_time;        my_id = p4_get_my_id();        printf("enter a string:\n");    while (fgets(msg, sizeof(msg), stdin) != NULL)    {        for(cp=msg, i=1; *cp; i++, cp++)	    if(*cp == '\n')	    {		*cp = 0;		break;	    }	start_time = p4_clock();	p4_send(100, 1, msg, i);	type = -1;	from = -1;	incoming = NULL;	p4_recv(&type, &from, &incoming, &size);	end_time = p4_clock();	printf("total time=%d \n",end_time-start_time);	printf("master received :%s: from %d\n", incoming, from);	p4_msg_free(incoming);	printf("enter a string:\n");    }        p4_send(END, 1, msg, 0);    type = -1;    from = -1;    incoming = NULL;    p4_recv(&type, &from, &incoming, &size);    p4_msg_free(incoming);        printf("master exiting normally\n");}worker()	{    int done;    int my_id, next, size;    int num_total_slaves, type, from;    char *incoming;    FILE *fp;    char filename[20];    char dirname[200];    my_id = p4_get_my_id();    getcwd(dirname,200);    p4_dprintf("directory is %s\n",dirname);    sprintf(filename,"dirtest%d",my_id);    fp = fopen(filename,"w");    if (fp == NULL)     {	p4_dprintf("cannot open %s\n",filename);	p4_error("file open failure",my_id);    }    num_total_slaves = p4_num_total_slaves();    if (my_id == num_total_slaves)        next = 0;    else	next = my_id + 1;    done = FALSE;    while (!done)    {	type = -1;	from = -1;	incoming = NULL;	p4_recv(&type,&from, &incoming, &size);	fprintf(fp,":%s:\n",incoming);	if (type == END)	    done = TRUE;	p4_send(type, next, incoming, size);	p4_msg_free(incoming);    }    fclose(filename);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -