集群节点列表编辑程序
在集群上跑程序的人都知道要编辑一个节点列表文件来说明在哪些机器上启动进程,以及在某个机器上启动的进程的数量,最典型的情况是mpi。但是如果机器的数量过大,比如有500个节点的机群,这样要维护这个文件也是一件比较头疼的事情。 维护节点列表文件,主要是进行三个操作:创建列表,删除某些节点,修改某些节点。接下来就可以用C或者Python实现一个具备这三种功能的小程序了。今天上午实现了一个,代码贴在这里。[*]/**************************************************************************************************************************
[*]* Name: md *
[*]* Author: Chaos Lee *
[*]* Date: 2012-06-07 *
[*]* Description: This program is used to create and edit a host file. It provides three simple functions to accomplish this.*
[*]* (1) To create a host file: mh -p prefix_name -f file_name -l num1-num2 num3 num4-num5 *
[*]* Above command will create a host file whose name is file_name,containing a machine name list in format *
[*]* as bellow: *
[*]* prefix_namenum1 *
[*]* ... *
[*]* prefix_namenum2 *
[*]* prefix_namenum3 *
[*]* prefix_namenum4 *
[*]* ... *
[*]* prefix_namenum5 *
[*]* (2) To delete some machine names from the host file: mh -f file_name -d num1-num2 num3 num4-num5. *
[*]* Above command will delete prefix_namenum1,...,prefix_namenume2,prefix_namenum3,prefix_namenum4,... *
[*]* ,prefix_namenum5 from the file indicated by the file_name *
[*]* (3) To modify some machine names from the host file: mh -f file_name -m num1 num2 num3 num4 *
[*]* Above command will change prefix_namenum1 to prefix_namenum2,prefix_namenum3 to prefix_namenum4. *
[*]**************************************************************************************************************************/
[*]#include<stdio.h>
[*]#include<string.h>
[*]#include<ctype.h>
[*]#include<stdlib.h>
[*]
[*]typedef enum com_type_tag {create,delete,modify} com_type;
[*]typedef int status;
[*]
[*]#define false 0
[*]#define true 1
[*]#define EXIT_SUCCESS 0
[*]#define BAD_FORMAT-1
[*]#define MAX_NODE_NUMBER 999
[*]char machine_names;
[*]int names_number = 0;
[*]#define XXXXdo{\
[*] printf("FILE:%s LINE:%d\n",__FILE__,__LINE__); \
[*] fflush(stdout); \
[*] }while(0);
[*]int find_minus(char *nums)
[*]{
[*] int len = strlen(nums);
[*] int i;
[*] for(i=0;i<len;i++)
[*] {
[*] if(nums=='-')
[*] {
[*] return i;
[*] }
[*] }
[*] return -1;
[*]}
[*]status check_cd_num(char * nums)
[*]{
[*] int minus_pos = find_minus(nums);
[*] int i = 0;
[*] if(-1 == minus_pos)
[*] return check_m_num(nums);
[*] nums = '\0';
[*] if(false == check_m_num(nums) || false == check_m_num(nums+i+1) )
[*] return false;
[*] nums = '-';
[*] return true;
[*]}
[*]
[*]status check_m_num(char * num)
[*]{
[*] int i=0;
[*] int len = strlen(num);
[*] if(len>3 || len==0)
[*] return false;
[*] for(i=0;i<len;i++)
[*] {
[*] if( !isdigit(num) )
[*] return false;
[*] }
[*] return true;
[*]}
[*]
[*]status check_num_list(int type,int argc,char *argv[])
[*]{
[*] int i;
[*] int start=4;
[*] if(type == create || type == delete)
[*] {
[*] if(type == create)
[*] start = 6;
[*] for(i=start;i<argc;i++)
[*] {
[*] status res = check_cd_num(argv);
[*] if(!res)
[*] return res;
[*] }
[*] }
[*] else if(type == modify)
[*] {
[*] int counter = 0;
[*] for(i=start;i<argc;i++)
[*] {
[*] status res = check_m_num(argv);
[*] if(!res)
[*] return res;
[*] counter++;
[*] }
[*] if(counter & 1)
[*] return false;
[*] }
[*] else
[*] {
[*] printf("command type is wrong.\n");
[*] return false;
[*] }
[*] return true;
[*]}
[*]
[*]com_type analyze_command(int argc,char *argv[])
[*]{
[*] int i=0;
[*] com_type type;
[*] if( 0 == strcmp(argv,"-p") && 0 == strcmp(argv,"-f") && 0 == strcmp(argv,"-l") )
[*] type = create;
[*] else if(0 == strcmp(argv,"-f")&& 0 == strcmp(argv,"-d") )
[*] type = delete;
[*] else if(0 == strcmp(argv,"-f") && 0 == strcmp(argv,"-m"))
[*] type = modify;
[*] else
[*] {
[*] printf("command in bad format.\n");
[*] exit(BAD_FORMAT);
[*] }
[*] if(false == check_num_list(type,argc,argv))
[*] {
[*] printf("number list in bad format\n");
[*] exit(BAD_FORMAT);
[*] }
[*] return type;
[*]}
[*]void get_range(char *nums,int * num_min,int * num_max)
[*]{
[*] int minus_pos = find_minus(nums);
[*] if( -1 == minus_pos )
[*] *num_min = *num_max = atoi(nums);
[*] else
[*] {
[*] *num_min = atoi(nums);
[*] *num_max = atoi(nums+minus_pos+1);
[*] }
[*]}
[*]status create_list(int argc,char *argv[])
[*]{
[*] int i,num_min,num_max,j;
[*] FILE * fp;
[*] //printf("filename = %s\n",argv);
[*] fp= fopen(argv,"w");
[*] if(NULL == fp)
[*] {
[*] printf("creating file %s error.\n",argv);
[*] return false;
[*] }
[*] for(i = 6;i<argc;i++)
[*] {
[*] get_range(argv,&num_min,&num_max);
[*] //printf("num_min=%d num_max=%d\n",num_min,num_max);
[*] for(j=num_min;j<=num_max;j++)
[*] sprintf(machine_names,"%s%03d",argv,j);
[*] }
[*] for(i=0;i<names_number;i++)
[*] {
[*] fprintf(fp,"%s\n",machine_names);
[*] }
[*] fclose(fp);
[*] return true;
[*]}
[*]status delete_list(int argc,char *argv[])
[*]{
[*] status delete_flag_array;
[*] int i,num_min,num_max,j;
[*] FILE * fp;
[*] int minus_pos,id;
[*] char prefix;
[*] for(i = 0;i<MAX_NODE_NUMBER;i++)
[*] delete_flag_array = false;
[*] for(i = 4;i<argc;i++)
[*] {
[*] get_range(argv,&num_min,&num_max);
[*] for(j=num_min;j<=num_max;j++)
[*] delete_flag_array = true;
[*] }
[*] fp = fopen(argv,"r");
[*] if(NULL == fp)
[*] {
[*] printf("opening file %s error.\n",argv);
[*] return false;
[*] }
[*] while(!feof(fp))
[*] {
[*] fscanf(fp,"%s",machine_names);
[*] if(strcmp("",machine_names)!=0)
[*] names_number++;
[*] }
[*] fclose(fp);
[*] fp = fopen(argv,"w");
[*] if(NULL == fp)
[*] {
[*] printf("recreating file %s error.\n",argv);
[*] return false;
[*] }
[*] if(0 == names_number)
[*] return false;
[*] minus_pos = find_minus(machine_names);
[*] if(-1 == minus_pos)
[*] return false;
[*] for(i=0;i<names_number;i++)
[*] {
[*] id = atoi(machine_names+minus_pos+1);
[*] if(0 == id)
[*] continue;
[*] if(true != delete_flag_array)
[*] {
[*] fprintf(fp,"%s\n",machine_names);
[*] }
[*] }
[*] fclose(fp);
[*] return true;
[*]}
[*]status modify_list(int argc,char *argv[])
[*]{
[*] int id_map,i=0;
[*] FILE *fp;
[*] int minus_pos,id;
[*] char prefix;
[*] for(i=0;i<MAX_NODE_NUMBER;i++)
[*] {
[*] id_map = -1;
[*] }
[*] i=4;
[*] while(i<argc)
[*] {
[*] id_map)] = atoi(argv);
[*] i+=2;
[*] }
[*] fp = fopen(argv,"r");
[*] if(NULL == fp)
[*] {
[*] printf("opening file %s error.\n",argv);
[*] return false;
[*] }
[*] while(!feof(fp))
[*] {
[*] fscanf(fp,"%s",machine_names);
[*] if(strcmp("",machine_names)!=0)
[*] names_number++;
[*] }
[*] fclose(fp);
[*] fp = fopen(argv,"w");
[*] if(NULL == fp)
[*] {
[*] printf("recreating file %s error.\n",argv);
[*] return false;
[*] }
[*] if(0 == names_number)
[*] return false;
[*] minus_pos = find_minus(machine_names);
[*] if(-1 == minus_pos)
[*] return false;
[*] strncpy(prefix,machine_names,minus_pos+1);
[*] for(i=0;i<names_number;i++)
[*] {
[*] id = atoi(machine_names+minus_pos+1);
[*] if(0 == id)
[*] continue;
[*] if(-1 != id_map)
[*] {
[*] fprintf(fp,"%s%03d\n",prefix,id_map);
[*] }
[*] else
[*] {
[*] fprintf(fp,"%s\n",machine_names);
[*] }
[*] }
[*] fclose(fp);
[*] return true;
[*]}
[*]int main(int argc,char *argv[])
[*]{
[*] com_type type;
[*] status res;
[*] type = analyze_command(argc,argv);
[*] switch(type)
[*] {
[*] case create:
[*] res = create_list(argc,argv);
[*] if(false == res)
[*] {
[*] printf("creating list error.\n");
[*] exit(EXIT_FAILURE);
[*] }
[*]
[*] break;
[*] case delete:
[*] res = delete_list(argc,argv);
[*] if(false == res)
[*] {
[*] printf("deleting list error.\n");
[*] exit(EXIT_FAILURE);
[*] }
[*] break;
[*] case modify:
[*] res = modify_list(argc,argv);
[*] if(false == res)
[*] {
[*] printf("modifying list error.\n");
[*] exit(EXIT_FAILURE);
[*] }
[*] break;
[*] default:
[*] printf("Panic:type error.\n");
[*] exit(BAD_FORMAT);
[*] }
[*] return EXIT_SUCCESS;
[*]}
[*]
1000人关注你=8元。@【QQ】:214871860\n\n 来自 闫幸培我爱你大美女 的新浪微博 这是什么东东啊 女,喜甜食,甚胖!该女有一癖好:痛恨蚂蚁,见必杀之。问其故曰:这小东西,那么爱吃甜食,腰还那么细! 我喜欢孩子,更喜欢造孩子的过程! 恋爱就是无数个饭局,结婚就是一个饭局。 穿别人的鞋,走自己的路,让他们找去吧。
页:
[1]