예제 #1
0
int main(int argc, const char** argv)
{
    Master M;
    
    (void)argc;
    (void)argv;
    
    memset(&M, 0, sizeof(Master));
    
    basic_preinit(B(&M));
    return master_main(&M);
}
예제 #2
0
파일: main.c 프로젝트: WinLinKer/alilua
int main ( int argc, char *argv[] )
{
    attach_on_exit ( on_master_exit_handler );

    /// 初始化进程命令行信息
    char *cwd = initProcTitle ( argc, argv );

    char *msg = NULL;
    
    if ( getarg ( "cache-size" ) ) {
        msg = getarg ( "cache-size" );
        YAC_CACHE_SIZE = atoi ( msg );
        if(msg[strlen(msg)-1] == 'm')YAC_CACHE_SIZE = YAC_CACHE_SIZE*1024*1024;
        else if(msg[strlen(msg)-1] == 'k')YAC_CACHE_SIZE = YAC_CACHE_SIZE*1024;

    }
    if ( YAC_CACHE_SIZE < 1024*1024*2 ) {
        YAC_CACHE_SIZE = 1024*1024*2;
    }

    if ( !yac_storage_startup ( YAC_CACHE_SIZE/16, YAC_CACHE_SIZE-(YAC_CACHE_SIZE/16), &msg ) ) {
        printf ( "Shared memory allocator startup failed at '%s': %s", msg,
                 strerror ( errno ) );
        exit ( 1 );
    }

    if ( getarg ( "log" ) && strlen ( getarg ( "log" ) ) > 0 ) {
        LOG_FD = fopen ( getarg ( "log" ), "a+" );

        if ( !LOG_FD ) {
            printf ( "fopen %s error\n" , getarg ( "log" ) );
            return -1;
        }

        setvbuf ( LOG_FD , LOG_BUF, _IOFBF , 40960 );
    }

    if ( getarg ( "errorlog" ) && strlen ( getarg ( "errorlog" ) ) > 0 ) {
        ERR_FD = fopen ( getarg ( "errorlog" ), "a+" );

        if ( !ERR_FD ) {
            printf ( "fopen %s error\n" , getarg ( "errorlog" ) );
            return -1;
        }

        setvbuf ( ERR_FD , ERR_BUF, _IOFBF , 4096 );
    }

    hostname[1023] = '\0';
    gethostname ( hostname, 1000 );

    lua_State *L = luaL_newstate();
    luaL_openlibs ( L );
    lua_getglobal ( L, "_VERSION" );
    const char *lua_ver = lua_tostring ( L, -1 );
    lua_getglobal ( L, "jit" );

    if ( lua_istable ( L, -1 ) ) {
        lua_getfield ( L, -1, "version" );

        if ( lua_isstring ( L, -1 ) ) {
            lua_ver = lua_tostring ( L, -1 );
        }
    }

    sprintf ( hostname, "%s/%s", hostname, lua_ver );
    lua_close ( L );

    if ( getarg ( "help" ) ) {
        printf ( "This is the aLiLua/%s Web Server.  Usage:\n"
                 "\n"
                 "    alilua [options]\n"
                 "\n"
                 "Options:\n"
                 "\n"
                 "  --bind=127.0.0.1:80  server bind. or --bind=80 for bind at 0.0.0.0:80\n"
                 "  --daemon             process mode\n"
                 "  --process=number     workers\n"
                 "  --log=file-path      access log\n"
                 "  --errorlog=file-path error log\n"
                 "  --host-route         Special route file path\n"
                 "  --code-cache-ttl     number of code cache time(sec)\n"
                 "  --cache-size         size of YAC shared memory cache (1m or 4096000k)\n"
                 "  \n"
                 "\n",
                 version
               );
        exit ( 0 );
    }

    /// 把进程放入后台
    if ( getarg ( "daemon" ) ) {
        is_daemon = 1;
        daemonize();
    }

    /// 创建4个子进程
    if ( getarg ( "process" ) ) {
        process_count = atoi ( getarg ( "process" ) );

    } else if ( process_count > 32 ) {
        process_count = 32;
    }

    if ( !getarg ( "daemon" ) ) {
        process_count = 1;
    }

    char *bind_addr = "0.0.0.0";

    if ( getarg ( "bind" ) ) {
        bind_addr = getarg ( "bind" );
    }

    char *_port;

    if ( !strstr ( bind_addr, "." ) ) {
        bind_addr = "0.0.0.0";
        _port = getarg ( "bind" );

    } else {
        _port = strstr ( bind_addr, ":" );

        if ( _port ) {
            bind_addr[strlen ( bind_addr ) - strlen ( _port )] = '\0';
            _port = _port + 1;
        }
    }

    int port = default_port;

    if ( _port ) {
        port = atoi ( _port );
    }

    if ( port < 1 ) {
        port = default_port;
    }

    int i = 0, status = 0;
    {
        /// init lua state
        _L = luaL_newstate();
        lua_gc ( _L, LUA_GCSTOP, 0 );
        luaL_openlibs ( _L ); /* Load Lua libraries */
        lua_gc ( _L, LUA_GCRESTART, 0 );

        if ( getarg ( "code-cache-ttl" ) ) { /// default = 60s
            lua_pushnumber ( _L, atoi ( getarg ( "code-cache-ttl" ) ) );
            lua_setglobal ( _L, "CODE_CACHE_TTL" );
        }

        if ( getarg ( "host-route" ) ) {
            lua_pushstring ( _L, getarg ( "host-route" ) );
            lua_setglobal ( _L, "HOST_ROUTE" );
        }

        lua_register ( _L, "errorlog", lua_errorlog );
        lua_register ( _L, "echo", lua_echo );
        lua_register ( _L, "sendfile", lua_sendfile );
        lua_register ( _L, "header", lua_header );
        lua_register ( _L, "clear_header", lua_clear_header );
        lua_register ( _L, "die", lua_die );
        lua_register ( _L, "get_post_body", lua_get_post_body );
        lua_register ( _L, "check_timeout", lua_check_timeout );
        lua_register ( _L, "is_websocket", lua_f_is_websocket );
        lua_register ( _L, "upgrade_to_websocket", lua_f_upgrade_to_websocket );
        lua_register ( _L, "websocket_send", lua_f_websocket_send );
        lua_register ( _L, "sleep", lua_f_sleep );

        lua_register ( _L, "random_string", lua_f_random_string );
        lua_register ( _L, "file_exists", lua_f_file_exists );

        lua_register ( _L, "cache_set", lua_f_cache_set );
        lua_register ( _L, "cache_get", lua_f_cache_get );
        lua_register ( _L, "cache_del", lua_f_cache_del );

        luaopen_fastlz ( _L );
        luaopen_coevent ( _L );
        luaopen_libfs ( _L );
        luaopen_string_utils ( _L );
        luaopen_crypto ( _L );

        lua_pop ( _L, 1 );

        sprintf ( tbuf_4096,
                  "package.path = '%s/lua-libs/?.lua;' .. package.path package.cpath = '%s/lua-libs/?.so;' .. package.cpath",
                  cwd, cwd );
        luaL_dostring ( _L, tbuf_4096 );

        /* Load the file containing the script we are going to run */
        status = luaL_loadfile ( _L, "script.lua" );

        if ( status || lua_resume ( _L, 0 ) ) {
            /* If something went wrong, error message is at the top of */
            /* the stack */
            fprintf ( stderr, "Couldn't load file: %s\n", lua_tostring ( _L, -1 ) );
            exit ( 1 );
        }

        lua_getglobal ( _L, "main" );
        main_handle_ref = luaL_ref ( _L, LUA_REGISTRYINDEX );
    }

    server_fd = network_bind ( bind_addr, port );

    for ( i = 0; i < process_count; i++ ) {
        if ( getarg ( "daemon" ) ) {
            forkProcess ( worker_main );

        } else {
            active_cpu ( 0 );
            new_thread_i ( worker_main, 0 );
        }
    }

    /// 设置进程归属用户
    setProcessUser ( /*user*/ NULL, /*group*/ NULL );

    /// 进入主进程处理
    master_main();

    return 0;
}
예제 #3
0
파일: hspwrap.c 프로젝트: hsptools/hsp-wrap
int
main (int argc, char **argv)
{
  char *ch;

  if (argc != 2) {
    fputs("Invalid number of arguments\n", stderr);
    fputs("usage: hspwrap EXEFILE\n", stderr);
    exit(EXIT_FAILURE);
  }

  ch = getenv("HSP_BCAST_CHUNK_SIZE");
  if (ch) {
    sscanf(ch, "%zu", &bcast_chunk_size);
  } else {
    bcast_chunk_size = 4L << 20;
  }

  ch = getenv("HSP_INPUT_FORMAT");
  if (!ch || ch[0] == '\0' || ch[0] == 'l') {
    info("Input format: Lines\n");
    input_fmt = 'l';
  } else if (ch[0] == 'f') {
    info("Input format: FASTA\n");
    input_fmt = 'f';
  } else {
    fputs("Invalid input format specified\n", stderr);
    exit(EXIT_FAILURE);
  }

  // Pre-fork process pool (even on master)
#ifndef TIMING_MODE
  sleep(1);
  pool_ctl = process_pool_fork();
  trace("Process pool created.\n");
  sleep(1);
#endif

  // Initialize MPI
  int rank, ranks;
  if (MPI_Init(NULL, NULL) != MPI_SUCCESS) {
    fprintf(stderr, "Error initialize MPI.\n");
    return EXIT_FAILURE;
  }

  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &ranks);
  trace("MPI Initialized.\n");

  // Initialize our state
  timing_init(&timing);
  if (rank) {
    slave_init(rank, ranks-1, NUM_PROCS);
  } else {
    print_banner_slant(stderr);
    master_init();
  }

  // Broadcast binary files first
  if (rank) {
    slave_broadcast_work_file("exefile");
  } else {
    master_broadcast_file(getenv("HSP_EXEFILE"));
  }

  // Distribute DB files
  MPI_Barrier(MPI_COMM_WORLD);
  timing_record(&timing.db_start);

  char *dbdir   = getenv("HSP_DBDIR");
  char *dbfiles = strdup(getenv("HSP_DBFILES"));
  char *fn, path[PATH_MAX];

  for (fn = strtok(dbfiles, ":"); fn; fn = strtok(NULL, ":")) {
    snprintf(path, sizeof(path), "%s/%s", dbdir, fn);    

    if (rank) {
      timing.db_kbytes += slave_broadcast_shared_file(path)/1024;
    } else {
      timing.db_kbytes += master_broadcast_file(path)/1024;
    }
  }
  free(dbfiles);

  MPI_Barrier(MPI_COMM_WORLD);
  timing_record(&timing.db_end);

#ifdef TIMING_MODE
  if (!rank) {
    timing_print(&timing);
  }
  MPI_Finalize();
  return 0;
#endif

  // FIXME: The order of things is generally wrong. Should be:
  // Fork Forker, MPI_Init, PS Ctl, EXE/DB distro, forking, main loop

#if 0
  // Now print some stats
  if (rank) {
    MPI_Barrier(MPI_COMM_WORLD);
    printf("Rank %d Processes: %d", rank, ps_ctl->nprocesses);
    printf("  Process ID: %d", getpid());
    printf("  Files: %d (", ps_ctl->ft.nfiles);
    for (i=0; i<ps_ctl->ft.nfiles; ++i) {
      printf("%s, ", ps_ctl->ft.file[i].name);
    }
    puts(")");
  } else {
    printf("Ranks: %d\n\n", ranks);
    MPI_Barrier(MPI_COMM_WORLD);
  }
#endif

  if (rank) {
    slave_main(argv[1]);
  } else {
    master_main(ranks-1);
    timing_print(&timing);
  }

  return 0;
}
예제 #4
0
파일: mpmt_os2.c 프로젝트: kheradmand/Break
int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s )
{
    char *listener_shm_name;
    parent_info_t *parent_info;
    ULONG rc;
    pconf = _pconf;
    ap_server_conf = s;
    restart_pending = 0;

    DosSetMaxFH(ap_thread_limit * 2);
    listener_shm_name = apr_psprintf(pconf, "/sharemem/httpd/parent_info.%d", getppid());
    rc = DosGetNamedSharedMem((PPVOID)&parent_info, listener_shm_name, PAG_READ);
    is_parent_process = rc != 0;
    ap_scoreboard_fname = apr_psprintf(pconf, "/sharemem/httpd/scoreboard.%d", is_parent_process ? getpid() : getppid());

    if (rc == 0) {
        /* Child process */
        ap_listen_rec *lr;
        int num_listeners = 0;

        ap_mpm_accept_mutex = parent_info->accept_mutex;

        /* Set up a default listener if necessary */
        if (ap_listeners == NULL) {
            ap_listen_rec *lr = apr_pcalloc(s->process->pool, sizeof(ap_listen_rec));
            ap_listeners = lr;
            apr_sockaddr_info_get(&lr->bind_addr, "0.0.0.0", APR_UNSPEC,
                                  DEFAULT_HTTP_PORT, 0, s->process->pool);
            apr_socket_create(&lr->sd, lr->bind_addr->family,
                              SOCK_STREAM, s->process->pool);
        }

        for (lr = ap_listeners; lr; lr = lr->next) {
            apr_sockaddr_t *sa;
            apr_os_sock_put(&lr->sd, &parent_info->listeners[num_listeners].listen_fd, pconf);
            apr_socket_addr_get(&sa, APR_LOCAL, lr->sd);
            num_listeners++;
        }

        DosFreeMem(parent_info);

        /* Do the work */
        ap_mpm_child_main(pconf);

        /* Outta here */
        return 1;
    }
    else {
        /* Parent process */
        char restart;
        is_parent_process = TRUE;

        if (ap_setup_listeners(ap_server_conf) < 1) {
            ap_log_error(APLOG_MARK, APLOG_ALERT, 0, s,
                         "no listening sockets available, shutting down");
            return 1;
        }

        ap_log_pid(pconf, ap_pid_fname);

        restart = master_main();
        ++ap_my_generation;
        ap_scoreboard_image->global->running_generation = ap_my_generation;

        if (!restart) {
            const char *pidfile = ap_server_root_relative(pconf, ap_pid_fname);

            if (pidfile != NULL && remove(pidfile) == 0) {
                ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS,
                             ap_server_conf, "removed PID file %s (pid=%d)",
                             pidfile, getpid());
            }

            ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
                         "caught SIGTERM, shutting down");
            return 1;
        }
    }  /* Parent process */

    return 0; /* Restart */
}
예제 #5
0
파일: main.c 프로젝트: rdebath/dtach
int
main(int argc, char **argv)
{
	int mode = 0;

	/* Save the program name */
	progname = argv[0];
	++argv; --argc;

	/* Parse the arguments */
	if (argc >= 1 && **argv == '-')
	{
		if (strncmp(*argv, "--help", strlen(*argv)) == 0)
			usage();
		else if (strncmp(*argv, "--version", strlen(*argv)) == 0)
		{
			printf("dtach - version %s, compiled on %s at %s.\n",
				PACKAGE_VERSION, __DATE__, __TIME__);
			return 0;
		}

		mode = argv[0][1];
		if (mode == '?')
			usage();
		else if (mode != 'a' && mode != 'c' && mode != 'n' &&
			 mode != 'A' && mode != 'N' && mode != 'p')
		{
			printf("%s: Invalid mode '-%c'\n", progname, mode);
			printf("Try '%s --help' for more information.\n",
				progname);
			return 1;
		}
	}
	if (!mode)
	{
		printf("%s: No mode was specified.\n", progname);
		printf("Try '%s --help' for more information.\n",
			progname);
		return 1;
	}
	++argv; --argc;

	if (argc < 1)
	{
		printf("%s: No socket was specified.\n", progname);
		printf("Try '%s --help' for more information.\n",
			progname);
		return 1;
	}
	sockname = *argv;
	++argv; --argc;

	if (mode == 'p')
	{
		if (argc > 0)
		{
			printf("%s: Invalid number of arguments.\n",
				progname);
			printf("Try '%s --help' for more information.\n",
				progname);
			return 1;
		}
		return push_main();
	}

	while (argc >= 1 && **argv == '-')
	{
		char *p;

		for (p = argv[0] + 1; *p; ++p)
		{
			if (*p == 'E')
				detach_char = -1;
			else if (*p == 'z')
				no_suspend = 1;
			else if (*p == 't')
				no_ansiterm = 1;
			else if (*p == 'e')
			{
				++argv; --argc;
				if (argc < 1)
				{
					printf("%s: No escape character "
						"specified.\n", progname);	
					printf("Try '%s --help' for more "
						"information.\n", progname);
					return 1;
				}
				if (argv[0][0] == '^' && argv[0][1])
				{
					if (argv[0][1] == '?')
						detach_char = '\177';
					else
						detach_char = argv[0][1] & 037;
				}
				else
					detach_char = argv[0][0];
				break;
			}
			else if (*p == 'r')
			{
				++argv; --argc;
				if (argc < 1)
				{
					printf("%s: No redraw method "
						"specified.\n", progname);	
					printf("Try '%s --help' for more "
						"information.\n", progname);
					return 1;
				}
				if (strcmp(argv[0], "none") == 0)
					redraw_method = REDRAW_NONE;
				else if (strcmp(argv[0], "ctrl_l") == 0)
					redraw_method = REDRAW_CTRL_L;
				else if (strcmp(argv[0], "winch") == 0)
					redraw_method = REDRAW_WINCH;
				else
				{
					printf("%s: Invalid redraw method "
						"specified.\n", progname);	
					printf("Try '%s --help' for more "
						"information.\n", progname);
					return 1;
				}
				break;
			}
			else
			{
				printf("%s: Invalid option '-%c'\n",
					progname, *p);
				printf("Try '%s --help' for more information.\n",
					progname);
				return 1;
			}
		}
		++argv; --argc;
	}

	if (mode != 'a' && argc < 1)
	{
		printf("%s: No command was specified.\n", progname);
		printf("Try '%s --help' for more information.\n",
			progname);
		return 1;
	}

	/* Save the original terminal settings. */
	if (tcgetattr(0, &orig_term) < 0)
	{
		memset(&orig_term, 0, sizeof(struct termios));
		dont_have_tty = 1;
	}

	if (dont_have_tty && mode != 'n' && mode != 'N')
	{
		printf("%s: Attaching to a session requires a terminal.\n",
			progname);
		return 1;
	}

	if (mode == 'a')
	{
		if (argc > 0)
		{
			printf("%s: Invalid number of arguments.\n",
				progname);
			printf("Try '%s --help' for more information.\n",
				progname);
			return 1;
		}
		return attach_main(0);
	}
	else if (mode == 'n')
		return master_main(argv, 0, 0);
	else if (mode == 'N')
		return master_main(argv, 0, 1);
	else if (mode == 'c')
	{
		if (master_main(argv, 1, 0) != 0)
			return 1;
		return attach_main(0);
	}
	else if (mode == 'A')
	{
		/* Try to attach first. If that doesn't work, create a new
		** socket. */
		if (attach_main(1) != 0)
		{
			if (errno == ECONNREFUSED || errno == ENOENT)
			{
				if (errno == ECONNREFUSED)
					unlink(sockname);
				if (master_main(argv, 1, 0) != 0)
					return 1;
			}
			return attach_main(0);
		}
	}
	return 0;
}
예제 #6
0
int main(int argc, char *argv[])
{
	const char *host = NULL;
	int port = MPI_QUEUE_DEFAULT_PORT;
	char addr[LINK_ADDRESS_MAX];
	char c;
	int w, rank;

	signal(SIGTERM, handle_abort);
	signal(SIGQUIT, handle_abort);
	signal(SIGINT, handle_abort);

	MPI_Init(&argc, &argv);

	debug_config(argv[0]);

	while((c = getopt(argc, argv, "d:ho:t:w:v")) != (char) -1) {
		switch (c) {
		case 'd':
			debug_flags_set(optarg);
			break;
		case 't':
			idle_timeout = string_time_parse(optarg);
			break;
		case 'o':
			debug_config_file(optarg);
			break;
		case 'v':
			show_version(argv[0]);
			return 0;
		case 'w':
			w = string_metric_parse(optarg);
			link_window_set(w, w);
			break;
		case 'h':
		default:
			show_help(argv[0]);
			return 1;
		}
	}

	if ((argc - optind) != 2) {
	    show_help(argv[0]);
	    return 1;
	}

	host = argv[optind];
	port = atoi(argv[optind + 1]);

	if(!domain_name_cache_lookup(host, addr)) {
		fprintf(stderr, "couldn't lookup address of host %s\n", host);
		exit(1);
	}

	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	if(rank) {
		return worker_main();
	} else {
		return master_main(host, port, addr);
	}
	
}
예제 #7
0
파일: main.cpp 프로젝트: parry2403/CodeRepo
int main(int argc, char *argv[])
{
    std::cout << "Started";
    // set up MPI
    MPI_Init(&argc, &argv);

    // get communicator size and my rank
    MPI_Comm comm = MPI_COMM_WORLD;
    int p, rank;
    MPI_Comm_size(comm, &p);
    MPI_Comm_rank(comm, &rank);

    /* code */
    //testrandom(10, 32, 9);
    bits_t* input;// = getrandom(10, 32, 9);
    if (rank == 0)
    {
        // read input file
        if (argc < 2)
        {
            printUsage();
            exit(EXIT_FAILURE);
        }

        // get the master depth, (i.e. the number of bits till which the
        // master process solves the problem)
        std::string depthstr = argv[1];
        std::istringstream ssargv(depthstr);
        unsigned int master_depth;
        ssargv >> master_depth;

        // open input file (if given, else default to stdin)
        std::istream* instream_ptr = &std::cin;
        std::ifstream infile;
        if (argc >= 3)
        {
            //std::string infilename(argv[2]);
            infile.open(argv[2]);
            if (!(infile.good() && infile.is_open()))
            {
                printUsage();
                exit(EXIT_FAILURE);
            }
            instream_ptr = &infile;
        }
        std::istream& instream = *instream_ptr;

        // open output file (if given, else use stdout)
        std::ostream* outstream_ptr = &std::cout;
        std::ofstream outfile;
        if (argc == 4)
        {
            //std::string outfilename(argv[3]);
            outfile.open(argv[3]);
            if (!(outfile.good() && outfile.is_open()))
            {
                printUsage();
                exit(EXIT_FAILURE);
            }
            outstream_ptr = &outfile;
        }
        std::ostream& outstream = *outstream_ptr;


        // read input from file
        unsigned int n, l, d;
        instream >> n >> l >> d;
        std::vector<bits_t> inputdata(n);
        for (unsigned int i = 0; i < n; ++i)
        {
            instream >> inputdata[i];
        }
        // get data as pointer (for more C-like interface)
        input = &inputdata[0];

        // prepare results
        std::vector<bits_t> results;

        // start timer
        //   we omit the file loading and argument parsing from the runtime
        //   timings, we measure the time needed by the master process
        struct timespec t_start, t_end;
    //    clock_gettime(CLOCK_MONOTONIC,  &t_start);
        if (p == 1)
        {
            std::cerr << "[WARNING]: Running the sequential solver. Start with mpirun to execute the parallel version." << std::endl;
            // call the sequential solver
            results = findmotifs(n, l, d, input);
        }
        else
        {
            // call the parallel solver function
            results = master_main(n, l, d, input, master_depth);
        }
        // end timer
      //  clock_gettime(CLOCK_MONOTONIC,  &t_end);
        // time in seconds
        double time_secs = (t_end.tv_sec - t_start.tv_sec)
                         + (double) (t_end.tv_nsec - t_start.tv_nsec) * 1e-9;

        // output time
        std::cerr << time_secs << std::endl;

        // write output file in ascending order
        std::sort(results.begin(), results.end());
        for (std::size_t i = 0; i < results.size(); ++i)
        {
            outstream << results[i] << std::endl;
        }
    }
    else
    {