//主上收到仲裁备节点信息变动 int mb_binfo_update(int voteid, char *data, int datalen) { WThread *wt = g_runtime->wthread; BackupInfo *binfo = wt->backup_info; BackupItem *bitem; //释放以前所有备节点得信息 while (binfo->item) { bitem = binfo->item; binfo->item = bitem->next; if (bitem->mbconn && bitem->mbconn->sock > 0) //bitem->mbconn->destroy((Conn *)bitem->mbconn); DNOTE("destroy mbconn\n"); mb_conn_destroy_delay((Conn *)bitem->mbconn); bitem->mbconn = NULL; bitem->state = STATE_NOWRITE; zz_free(bitem); } close(wt->backup_info->hbsock); event_del(&wt->backup_info->hbevt); event_del(&wt->backup_info->timer_check_evt); zz_free(wt->backup_info); master_init(voteid, data, datalen); return 0; }
int master_main(Master* M) { ExceptionScope exScope; int volatile ret = 0; int ex; exception_begin_try(B(M), &exScope); switch ((ex = exception_try(B(M)))) { case Try: master_init(M); master_start_log_writer(M); master_start_login(M); master_start_char_select(M); // Start the IPC thread now to avoid needing to hold the lock while the above processes start master_start_ipc_thread(M); master_main_loop(M); break; case Finally: master_deinit(M); break; default: printf(TERM_RED "[master_main] Unhandled exception (%i): %s\n" TERM_DEFAULT, ex, string_data(exception_get_message(B(M)))); exception_handled(B(M)); ret = ex; break; } exception_end_try_with_finally(B(M)); return ret; }
// called by main on one or all procs void BackEnd::init(int argc, char **argv) { gNAMDBinaryName = argv[0]+strlen(argv[0])-1; while(gNAMDBinaryName != argv[0]){ if(*gNAMDBinaryName=='/' || *gNAMDBinaryName=='\\'){ gNAMDBinaryName++; break; } gNAMDBinaryName--; } #ifdef NAMD_CUDA // look for but don't remove +idlepoll on command line int idlepoll = 0; for ( int i = 0; i < argc; ++i ) { if ( 0==strcmp(argv[i],"+idlepoll") ) { idlepoll = 1; break; } } #endif ConverseInit(argc, argv, slave_init, 1, 1); // calls slave_init on others #if defined(NAMD_CUDA) && CMK_NET_VERSION if ( ! idlepoll ) { NAMD_die("Please add +idlepoll to command line for proper performance."); } #endif master_init(argc, argv); }
//备切换成主 int switch_master(int voteid, char *data, int datalen) { WThread *wt = g_runtime->wthread; MasterInfo *minfo = wt->master_info; if (g_cf->role == ROLE_MASTER) { DERROR("master can not switch to master\n"); return -1; } else if (g_cf->role == ROLE_BACKUP) { DINFO("backup switch to master\n"); if (minfo != NULL) { //销毁备上面以前保存得主节点得信息 event_del(&minfo->hb_timer_evt); event_del(&minfo->hbevt); if (minfo->hbsock > 0) close(minfo->hbsock); minfo->hbsock = -1; wt->master_info = NULL; zz_free(minfo); //构建主节点上得备节点信息 master_init(voteid, data, datalen); if (wt->clog->state == INCOMPLETE) { int ret; int cmdlen; char *cmd = commitlog_get_cmd(wt->clog, &cmdlen); pthread_mutex_lock(&g_runtime->mutex); ret = wdata_apply(cmd, cmdlen, MEMLINK_NO_LOG, NULL); DINFO("===wdata_apply ret: %d\n", ret); if ( ret == 0) { int sret; sret = synclog_write(g_runtime->synclog, wt->clog->data, wt->clog->len); if (sret < 0) { DERROR("synclog_write error: %d\n", sret); MEMLINK_EXIT; } char *cmd; int cmdlen; cmd = commitlog_get_cmd(wt->clog, &cmdlen); sret = syncmem_write(g_runtime->syncmem, cmd, cmdlen, g_runtime->synclog->version, g_runtime->synclog->index_pos - 1); if (sret < 0) { DERROR("syncmem_write error: %d\n", sret); MEMLINK_EXIT; } } pthread_mutex_unlock(&g_runtime->mutex); } } else { DERROR("master info is null, can not switch\n"); return -3; } } return 0; }
int main(int argc, char **argv) { for(int i=1; i<argc; ++i) { if(std::string(argv[i])=="-s" || std::string(argv[i])=="--skin") { if(i+1<argc) Game::spritemod=argv[++i]; else error("no skin specified"); } else if(std::string(argv[i])=="-h" || std::string(argv[i])=="--help") { std::cout<< "Free Pokémon client version 1.0.0\n" "An open source game engine licensed under GPLv3.\n" "Basis by Robin Stjerndorff,\nwork in progress by Jerry Liu.\n\n" "short\tlong\t\taction\n" "-f\t--fullscreen\tenable fullscreen\n" "-h\t--help\t\toutput this information\n" "-s\t--skin\t\tselect a skin folder\n" "-z\t--zoom\t\tzoom the window (2 to 8)"<<std::endl; exit(0); } else if(std::string(argv[i])=="-z" || std::string(argv[i])=="--zoom") { if(i+1<argc) { int value=convert<int>(argv[++i]); if(value<2) error("too small zoom value"); if(value>8) error("too big zoom value"); Video::scale=value; } else error("no zoom value given"); } else if(std::string(argv[i])=="-f" || std::string(argv[i])=="--fullscreen") { Video::fullscreen=true; } else { warning("unknown argv \""+std::string(argv[i])+"\" has no effect"); } } atexit(master_kill); master_init(); while(Game::run) { master_update(); } return 0; }
// called on slave procs void slave_init(int argc, char **argv) { #if CMK_SMP //the original main thread could now be a comm thread //and a slave thread could now be the main thread, //so we have to do the master initialization here if(CmiMyRank()==0){ master_init(argc, argv); if(CmiMyPe()==0) after_backend_init(argc, argv); return; } #endif all_init(argc, argv); if (CkMyRank() < CkMyNodeSize()) // skip the communication thread CsdScheduler(-1); }
int vote_ready(Conn *conn, char *buf, int datalen) { short retcode; uint8_t cmd; int count = sizeof(int); char ip[INET_ADDRSTRLEN]; uint16_t port; uint64_t voteid; memcpy(&cmd, buf+count, sizeof(cmd)); count += sizeof(cmd); if (g_cf->sync_mode == MODE_MASTER_SLAVE) { conn_send_buffer_reply(conn, MEMLINK_ERR_SLAVE, NULL, 0); return 0; } switch (cmd) { case CMD_VOTE: memcpy(&retcode, buf+count, sizeof(retcode)); count += sizeof(retcode); switch (retcode) { case CMD_VOTE_WAIT: DINFO("*** VOTE WAITING\n"); int timeout = 10*g_cf->timeout; if (timeout < 60) { timeout = 60; } else if (timeout > 300) { timeout = 300; } //*((int *)conn->thread) = VOTE_WAITING; conn->vote_status = VOTE_WAITING; event_del(&conn->evt); change_event(conn, EV_READ|EV_PERSIST, timeout, 1); break; case CMD_VOTE_MASTER: DINFO("*** VOTE MASTER\n"); /* if (conn->thread != g_runtime->wthread) *((int *)conn->thread) = VOTE_OK; */ conn->vote_status = VOTE_OK; count += unpack_voteid(buf+count, &voteid); if (g_cf->role == ROLE_NONE) { DINFO("*** master init\n"); master_init(voteid, buf+count, datalen-count); g_cf->role = ROLE_MASTER; g_runtime->voteid = voteid; } else { DINFO("*** switch to master\n"); if (switch_master(voteid, buf+count, datalen-count) < 0) { DERROR("*** FATAL ERROR : switch to master error\n"); g_cf->role = ROLE_NONE; g_runtime->voteid = 0; } else { g_cf->role = ROLE_MASTER; g_runtime->voteid = voteid; } } DINFO("*** voteid: %llu\n", (unsigned long long)g_runtime->voteid); DINFO("*** VOTE: I am master\n"); while (count < datalen) { count += unpack_votehost(buf+count, ip, &port); DINFO("*** VOTE backup: %s(%d)\n", ip, port); } /* Host **hostp = &g_runtime->hosts; while (count < datalen) { count += unpack_votehost(buf+count, ip, &port); DINFO("*** VOTE backup: %s(%d)\n", ip, port); if (*hostp == NULL) { *hostp = (Host *)zz_malloc(sizeof(Host)); strcpy((*hostp)->ip, ip); (*hostp)->port = port; (*hostp)->next = NULL; hostp = &(*hostp)->next; } else { strcpy((*hostp)->ip, ip); (*hostp)->port = port; hostp = &(*hostp)->next; } } if (*hostp != NULL) { release_hosts(*hostp); *hostp = NULL; } */ conn_send_buffer_reply(conn, MEMLINK_OK, NULL, 0); break; case CMD_VOTE_BACKUP: DINFO("*** VOTE BACKUP\n"); /* if (conn->thread != g_runtime->wthread) *((int *)conn->thread) = VOTE_OK; */ conn->vote_status = VOTE_OK; count += unpack_voteid(buf+count, &voteid); unpack_votehost(buf+count, ip, &port); if (g_cf->role == ROLE_NONE) { DINFO("*** backup init\n"); backup_init(voteid, buf+count, datalen-count); g_cf->role = ROLE_BACKUP; g_runtime->voteid = voteid; } else { DINFO("*** switch to backup\n"); if (switch_backup(voteid, buf+count, datalen-count) < 0) { DERROR("*** FATAL ERROR : switch to backup error\n"); g_cf->role = ROLE_NONE; g_runtime->voteid = 0; } else { g_cf->role = ROLE_BACKUP; g_runtime->voteid = voteid; } } DINFO("*** VOTE: I am backup, master is %s(%d)\n", ip, port); DINFO("*** voteid: %llu\n", (unsigned long long)g_runtime->voteid); /* Host *host; if (g_runtime->hosts == NULL) { host = (Host *)zz_malloc(sizeof(Host)); if (host == NULL) { DERROR("*** VOTE: zz_malloc error\n"); MEMLINK_EXIT; } host->next = NULL; strcpy(host->ip, ip); host->port = port; g_runtime->hosts = host; } else { host = g_runtime->hosts; strcpy(host->ip, ip); host->port = port; release_hosts(host->next); host->next = NULL; } */ conn_send_buffer_reply(conn, MEMLINK_OK, NULL, 0); break; case CMD_VOTE_NONEED: DINFO("*** VOTE NONEED\n"); if (g_cf->role == ROLE_NONE) { //*((int *)conn->thread) = VOTE_CONTINUE; conn->vote_status = VOTE_CONTINUE; break; } //*((int *)conn->thread) = VOTE_OK; conn->vote_status = VOTE_OK; backup_create_heartbeat(g_runtime->wthread); break; case MEMLINK_ERR_VOTE_PARAM: DERROR("*** VOTE PARAM ERROR\n"); //*((int *)conn->thread) = VOTE_ERR_PARAM; conn->vote_status = VOTE_ERR_PARAM; break; default: DERROR("*** VOTE UNKNOWN ERROR\n"); //if (conn->thread != g_runtime->wthread) // *((int *)conn->thread) = VOTE_OK; conn->vote_status = VOTE_OK; break; } break; case CMD_VOTE_UPDATE: DINFO("*** VOTE UPDATING\n"); if (g_cf->role != ROLE_MASTER) { conn_send_buffer_reply(conn, MEMLINK_ERR_NOT_MASTER, NULL, 0); break; } //DINFO("*** VOTE MASTER\n"); /* if (conn->thread != g_runtime->wthread) *((int *)conn->thread) = VOTE_OK; */ //conn->vote_status = VOTE_OK; count += unpack_voteid(buf+count, &voteid); DINFO("*** voteid: %llu\n", (unsigned long long)voteid); DINFO("*** VOTE: I am master\n"); g_cf->role = ROLE_MASTER; g_runtime->voteid = voteid; mb_binfo_update(voteid, buf+count, datalen-count); while (count < datalen) { count += unpack_votehost(buf+count, ip, &port); DINFO("*** VOTE backup: %s(%d)\n", ip, port); } /* Host **hostp = &g_runtime->hosts; while (count < datalen) { count += unpack_votehost(buf+count, ip, &port); DINFO("*** backup: %s(%d)\n", ip, port); if (*hostp == NULL) { *hostp = (Host *)zz_malloc(sizeof(Host)); strcpy((*hostp)->ip, ip); (*hostp)->port = port; (*hostp)->next = NULL; hostp = &(*hostp)->next; } else { strcpy((*hostp)->ip, ip); (*hostp)->port = port; hostp = &(*hostp)->next; } } if (*hostp != NULL) { release_hosts(*hostp); *hostp = NULL; } */ conn_send_buffer_reply(conn, MEMLINK_OK, NULL, 0); break; case CMD_VOTE_DETECT: DINFO("*** VOTE DETECT\n"); if (g_cf->role == ROLE_MASTER) { retcode = CMD_VOTE_MASTER; } else if (g_cf->role == ROLE_BACKUP) { retcode = CMD_VOTE_BACKUP; } else { retcode = MEMLINK_ERR_NO_ROLE; } conn_send_buffer_reply(conn, retcode, (char *)&g_runtime->voteid, sizeof(g_runtime->voteid)); break; default: break; } return 0; }
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; }
/* * main() - startup the program. * * In: argc - number of command-line arguments. * argv - string array containing command-line arguments. * * Returns: 0 on exit, -1 on error. * * Abstract: We set up the signal handler, parse arguments, * turn into a daemon, write our pid to /var/run/dnrd.pid, * setup our sockets, and then parse packets until we die. */ int main(int argc, char *argv[]) { int i; FILE *filep; struct servent *servent; /* Let's be good and find the port numbers the right way */ struct passwd *pwent; DIR *dirp; struct dirent *direntry; struct stat st; int rslt; /* * Setup signal handlers. */ signal(SIGINT, sig_handler); signal(SIGQUIT, sig_handler); signal(SIGTERM, sig_handler); signal(SIGUSR1, sig_handler); #if defined(_WanAutoDetect_) || defined(_IQSETUP_) signal(SIGUSR2, sig_handler); #endif /* * Handling TCP requests is done by forking a child. When they terminate * they send SIGCHLDs to the parent. This will eventually interrupt * some system calls. Because I don't know if this is handled it's better * to ignore them -- 14OCT99wzk */ signal(SIGCHLD, SIG_IGN); /* * Parse the command line. */ parse_args(argc, argv); openlog(progname, LOG_PID, LOG_DAEMON); /* * Kill any currently running copies of the program. */ kill_current(); /* * Setup the thread synchronization semaphore */ #if PTHREAD_LIB if (sem_init(&dnrd_sem, 0, 1) == -1) { log_msg(LOG_ERR, "Couldn't initialize semaphore"); cleanexit(-1); } #endif /* * Write our pid to the appropriate file. * Just open the file here. We'll write to it after we fork. */ filep = fopen(pid_file, "w"); if (!filep) { log_msg(LOG_ERR, "can't write to %s. " "Check that dnrd was started by root.", pid_file); exit(-1); } /* * Pretend we don't know that we want port 53 */ servent = getservbyname("domain", "udp"); if (servent != getservbyname("domain", "tcp")) { log_msg(LOG_ERR, "domain ports for udp & tcp differ. " "Check /etc/services"); exit(-1); } recv_addr.sin_port = servent ? servent->s_port : htons(53); /* * Setup our DNS query reception socket. */ if ((isock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { log_msg(LOG_ERR, "isock: Couldn't open socket"); cleanexit(-1); } else { int opt = 1; setsockopt(isock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); } if (bind(isock, (struct sockaddr *)&recv_addr, sizeof(recv_addr)) < 0) { log_msg(LOG_ERR, "isock: Couldn't bind local address"); cleanexit(-1); } /* * Setup our DNS tcp proxy socket. */ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { log_msg(LOG_ERR, "tcpsock: Couldn't open socket"); cleanexit(-1); } else { int opt = 1; setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); } if (bind(tcpsock, (struct sockaddr *)&recv_addr, sizeof(recv_addr)) < 0) { log_msg(LOG_ERR, "tcpsock: Couldn't bind local address"); cleanexit(-1); } if (listen(tcpsock, 5) != 0) { log_msg(LOG_ERR, "tcpsock: Can't listen"); cleanexit(-1); } /* Initialise our cache */ #ifdef ENABLE_CACHE cache_init(); /* Initialise out master DNS */ master_init(); #endif pwent = getpwnam("nobody"); /* * Change our root and current working directories to /etc/dnrd. * Also, so some sanity checking on that directory first. */ dirp = opendir("/etc/dnrd"); if (!dirp) { log_msg(LOG_ERR, "The directory /etc/dnrd must be created before " "dnrd will run"); } rslt = stat("/etc/dnrd", &st); if (st.st_uid != 0) { log_msg(LOG_ERR, "The /etc/dnrd directory must be owned by root"); cleanexit(-1); } if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0) { log_msg(LOG_ERR, "The /etc/dnrd directory should only be user writable"); cleanexit(-1); } while ((direntry = readdir(dirp)) != NULL) { if (!strcmp(direntry->d_name, ".") || !strcmp(direntry->d_name, "..")) { continue; } rslt = stat(direntry->d_name, &st); if (rslt) continue; if (S_ISDIR(st.st_mode)) { log_msg(LOG_ERR, "The /etc/dnrd directory must not contain " "subdirectories"); cleanexit(-1); } if ((st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH|S_IWGRP|S_IWOTH)) != 0) { log_msg(LOG_ERR, "A file in /etc/dnrd has either execute " "permissions or non-user write permission. Please do a " "\"chmod a-x,go-w\" on all files in this directory"); cleanexit(-1); } if (st.st_uid != 0) { log_msg(LOG_ERR, "All files in /etc/dnrd must be owned by root"); cleanexit(-1); } } closedir(dirp); if (chdir("/etc/dnrd")) { log_msg(LOG_ERR, "couldn't chdir to %s, %s", "/etc/dnrd", strerror(errno)); cleanexit(-1); } if (chroot("/etc/dnrd")) { log_msg(LOG_ERR, "couldn't chroot to %s, %s", "/etc/dnrd", strerror(errno)); cleanexit(-1); } /* * Change uid/gid to something other than root. */ /* drop supplementary groups */ if (setgroups(0, NULL) < 0) { log_msg(LOG_ERR, "can't drop supplementary groups"); cleanexit(-1); } /* * Switch uid/gid to something safer than root if requested. * By default, attempt to switch to user & group id 65534. */ if (daemongid != 0) { if (setgid(daemongid) < 0) { log_msg(LOG_ERR, "couldn't switch to gid %i", daemongid); cleanexit(-1); } } else if (!pwent) { log_msg(LOG_ERR, "Couldn't become the \"nobody\" user. Please use " "the \"-uid\" option.\n" " dnrd must become a non-root process."); cleanexit(-1); } else if (setgid(pwent->pw_gid) < 0){ log_msg(LOG_ERR, "couldn't switch to gid %i", pwent->pw_gid); cleanexit(-1); } if (daemonuid != 0) { if (setuid(daemonuid) < 0) { log_msg(LOG_ERR, "couldn't switch to uid %i", daemonuid); cleanexit(-1); } } else if (!pwent) { log_msg(LOG_ERR, "Couldn't become the \"nobody\" user. Please use " "the \"-uid\" option.\n" " dnrd must become a non-root process."); cleanexit(-1); } else if (setuid(pwent->pw_uid) < 0){ log_msg(LOG_ERR, "couldn't switch to uid %i", pwent->pw_uid); cleanexit(-1); } /* * Setup our DNS query forwarding socket. */ for (i = 0; i < serv_cnt; i++) { if ((dns_srv[i].sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { log_msg(LOG_ERR, "osock: Couldn't open socket"); cleanexit(-1); } dns_srv[i].addr.sin_family = AF_INET; dns_srv[i].addr.sin_port = htons(53); } /* * Now it's time to become a daemon. */ if (!opt_debug) { pid_t pid = fork(); if (pid < 0) { log_msg(LOG_ERR, "%s: Couldn't fork\n", progname); exit(-1); } if (pid != 0) exit(0); gotterminal = 0; setsid(); chdir("/"); umask(077); fclose(stdin); fclose(stdout); fclose(stderr); } /* * Write our pid to the appropriate file. * Now we actually write to it and close it. */ fprintf(filep, "%i\n", (int)getpid()); fclose(filep); /* * Run forever. */ run(); exit(0); /* to make compiler happy */ }
int master(workorder_t *w) { int rc; int i; int thr_count; int nthr, nproc; int id; int goodbye_timeout = UPERF_GOODBYE_TIMEOUT; hrtime_t start, stop; uperf_shm_t *shm; goodbye_stat_t gtotal; int error; if ((shm = master_init(w)) == NULL) exit(1); /* * We need to get the total number of threads to arm the * start, and end barriers. */ nproc = workorder_num_strands_bytype(w, STRAND_TYPE_PROCESS); nthr = workorder_num_strands_bytype(w, STRAND_TYPE_THREAD); thr_count = nproc + nthr; if (handshake(shm, w) != UPERF_SUCCESS) { uperf_info("Error in handshake\n"); shm_fini(shm); exit(1); } if (nthr == 0 && nproc != 0) { (void) printf("Starting %d processes running profile:%s ... ", thr_count, w->name); } else if (nproc == 0 && nthr != 0) { (void) printf("Starting %d threads running profile:%s ... ", thr_count, w->name); } else { (void) printf( "Starting %d threads, %d processes running profile:%s ... ", nthr, nproc, w->name); } (void) fflush(stdout); start = GETHRTIME(); /* Traverse through the worklist and create threads */ id = 0; for (i = 0; i < w->ngrp; i++) { w->grp[i].groupid = i; if (shm->global_error > 0) break; if (spawn_strands_group(shm, &w->grp[i], id) != 0) shm->global_error++; id += w->grp[i].nthreads; } if (shm->global_error > 0) { master_prepare_to_exit(shm); shm_fini(shm); return (UPERF_FAILURE); } stop = GETHRTIME(); (void) printf(" %5.2f seconds\n", (stop-start)/1.0e+9); (void) fflush(stdout); /* Handshake end */ if (handshake_end_master(shm, w) != UPERF_SUCCESS) { return (UPERF_FAILURE); } #ifdef ENABLE_NETSTAT if (ENABLED_PACKET_STATS(options)) netstat_snap(SNAP_BEGIN); #endif /* ENABLE_NETSTAT */ /* * The main Loop. * if either global_error is not 0 or master_poll returns 1 * let wait_for_strands know that. */ newstat_begin(0, AGG_STAT(shm), 0, 0); error = master_poll(shm); if (error == 0 && shm->global_error != 0) error = shm->global_error; (void) wait_for_strands(shm, error); newstat_end(0, AGG_STAT(shm), 0, 0); shm->current_time = GETHRTIME(); #ifdef ENABLE_NETSTAT if (ENABLED_PACKET_STATS(options)) netstat_snap(SNAP_END); #endif /* ENABLE_NETSTAT */ if (ENABLED_STATS(options)) { print_summary(AGG_STAT(shm), 0); } if (shm->global_error > 0) { /* decrease timeout coz no point in waiting */ goodbye_timeout = 1000; } if (ENABLED_GROUP_STATS(options)) print_group_details(shm); if (ENABLED_THREAD_STATS(options)) print_strand_details(shm); if (ENABLED_TXN_STATS(options)) print_txn_averages(shm); if (ENABLED_FLOWOP_STATS(options)) print_flowop_averages(shm); #ifdef ENABLE_NETSTAT if (ENABLED_PACKET_STATS(options)) print_netstat(); #endif /* ENABLE_NETSTAT */ if (ENABLED_ERROR_STATS(options)) { goodbye_stat_t local; (void) memset(>otal, 0, sizeof (goodbye_stat_t)); if ((rc = say_goodbyes_and_close(>otal, goodbye_timeout)) == 0) { update_aggr_stat(shm); local.elapsed_time = (AGG_STAT(shm))->end_time - (AGG_STAT(shm))->start_time; local.error = 0; local.bytes_xfer = (AGG_STAT(shm))->size; local.count = (AGG_STAT(shm))->count; print_goodbye_stat("master", &local); print_difference(local, gtotal); } } uperf_log_flush(); if (ENABLED_HISTORY_STATS(options)) { (void) fclose(options.history_fd); } /* Cleanup */ if (shm->global_error != 0) { (void) printf("\nWARNING: Errors detected during run\n"); shm_fini(shm); exit(1); } shm_fini(shm); return (rc); }
int main(int argc, char **argv) { int port = 0, obs_port = 0, c = 0, verbose = 0; char exec[64], list[64], comp_host[64]; signal(SIGINT, finish); /* Initialize strings */ list[0] = 0; exec[0] = 0; comp_host[0] = 0; port = TN_MASTER_PORT; obs_port = ADRT_PORT; /* Parse command line options */ while ((c = #ifdef HAVE_GETOPT_LONG getopt_long(argc, argv, shortopts, longopts, NULL) #else getopt(argc, argv, shortopts) #endif )!= -1) { switch (c) { case 'c': strncpy(comp_host, optarg, 64-1); comp_host[64-1] = '\0'; /* sanity */ break; case 'h': help(); return EXIT_SUCCESS; case 'o': obs_port = atoi(optarg); break; case 'p': port = atoi(optarg); break; case 'l': strncpy(list, optarg, 64-1); list[64-1] = '\0'; /* sanity */ break; case 'e': strncpy(exec, optarg, 64-1); exec[64-1] = '\0'; /* sanity */ break; case 'b': printf("adrt_master build: %s %s\n", __DATE__, __TIME__); return EXIT_SUCCESS; break; case 'v': verbose = 1; break; default: help(); return EXIT_FAILURE; } } argc -= optind; argv += optind; master_init (port, obs_port, list, exec, comp_host, verbose); return EXIT_SUCCESS; }
int main(){ printf("NEW GAME!\n\n"); volatile int old_game_state = 0; int doge = 0; //animation counter //buffer init stuff pixel_buffer = alt_up_pixel_buffer_dma_open_dev("/dev/pixel_buffer_dma"); char_buffer = alt_up_char_buffer_open_dev("/dev/char_drawer"); //phil's stuff master* m = master_init(); printf("NEW GAME!\n\n"); // Set the 1st buffer address alt_up_pixel_buffer_dma_change_back_buffer_address(pixel_buffer,pixel_buffer_addr1); // Swap buffers – we have to swap because there is only an API function // to set the address of the background buffer. alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while (alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)); // Set the 2nd buffer address alt_up_pixel_buffer_dma_change_back_buffer_address(pixel_buffer,pixel_buffer_addr2); printf("NEW GAME!\n\n"); // Clear the screen alt_up_pixel_buffer_dma_clear_screen(pixel_buffer, 1); alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while (alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)); alt_up_pixel_buffer_dma_clear_screen(pixel_buffer, 1); alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while (alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)); alt_up_char_buffer_clear(char_buffer); //Write some text alt_up_char_buffer_string(char_buffer, "LOADING...", 0, 0); //load bitmap files title = read_bmp("title.bmp"); alt_up_char_buffer_string(char_buffer, "title.bmp", 0, 2); menu = read_bmp("menu.bmp"); alt_up_char_buffer_string(char_buffer, "MENU.BMP", 0, 3); selA = read_bmp("selA.bmp"); alt_up_char_buffer_string(char_buffer, "selA.bmp", 0, 4); selB = read_bmp("selB.bmp"); alt_up_char_buffer_string(char_buffer, "selB.bmp", 0, 5); selC = read_bmp("selC.bmp"); alt_up_char_buffer_string(char_buffer, "selC.bmp", 0, 6); dead = read_bmp("dead.bmp"); alt_up_char_buffer_string(char_buffer, "dead.bmp", 0, 7); bmp * b = read_bmp("para1.bmp"); alt_up_char_buffer_string(char_buffer, "para.bmp", 0, 8); bmp * doge0 = read_bmp("doge0.bmp"); alt_up_char_buffer_string(char_buffer, "doge0.bmp", 0, 9); bmp * doge1 = read_bmp("doge1.bmp"); alt_up_char_buffer_string(char_buffer, "doge1.bmp", 0, 10); bmp * doge2 = read_bmp("doge2.bmp"); alt_up_char_buffer_string(char_buffer, "doge2.bmp", 0, 11); bmp * doge3 = read_bmp("doge3.bmp"); alt_up_char_buffer_string(char_buffer, "doge3.bmp", 0, 12); bmp * flat = read_bmp("flat.bmp"); alt_up_char_buffer_string(char_buffer, "flat.bmp", 0, 13); bmp * coin = read_bmp("coin.bmp"); alt_up_char_buffer_string(char_buffer, "coin.bmp", 0, 14); bmp * spike = read_bmp("spike.bmp"); alt_up_char_buffer_string(char_buffer, "spike.bmp", 0, 15); bmp * box1 = read_bmp("box1.bmp"); alt_up_char_buffer_string(char_buffer, "box1.bmp", 0, 16); bmp * box3 = read_bmp("box3.bmp"); alt_up_char_buffer_string(char_buffer, "box3.bmp", 0, 17); bmp * low = read_bmp("low.bmp"); alt_up_char_buffer_string(char_buffer, "low.bmp", 0, 18); bmp * flatb = read_bmp("flatb.bmp"); alt_up_char_buffer_string(char_buffer, "flatb.bmp", 0, 19); bmp * flatr = read_bmp("flatr.bmp"); alt_up_char_buffer_string(char_buffer, "flatr.bmp", 0, 20); bmp * blue = read_bmp("bstar.bmp"); alt_up_char_buffer_string(char_buffer, "blue.bmp", 0, 21); bmp * red = read_bmp("rstar.bmp"); alt_up_char_buffer_string(char_buffer, "red.bmp", 0, 22); bmp * flag_img = read_bmp("flag.bmp"); alt_up_char_buffer_string(char_buffer, "flag.bmp", 0, 23); name = read_bmp("name.bmp"); alt_up_char_buffer_string(char_buffer, "name.bmp", 0, 24); instr = read_bmp("instr.bmp"); alt_up_char_buffer_string(char_buffer, "instr.bmp", 0, 25); dcol = read_bmp("dcol.bmp"); alt_up_char_buffer_string(char_buffer, "dcol.bmp", 0, 26); win = read_bmp("win.bmp"); alt_up_char_buffer_string(char_buffer, "win.bmp", 0,27); alt_up_char_buffer_clear(char_buffer); printf("NEW GAME!\n\n"); //interrupt init stuff (for object writing) //TIMERPERIOD int timer_period = 1 * 500000; IOWR_16DIRECT(TIMER_0_BASE, 8, timer_period & 0xFFFF); //writes the period to the hardware timer IOWR_16DIRECT(TIMER_0_BASE, 12, timer_period >> 16); IOWR_16DIRECT(TIMER_0_BASE, 4, 1 << 3); //stop timer alt_irq_register(TIMER_0_IRQ,NULL,(void*)handle_timer_interrupts);//registers function to a specific IRQ //IOWR_16DIRECT(TIMER_0_BASE, 4, 0x5); //start timer //SET UP KEYBOARD INTERRUPT// ps2 = alt_up_ps2_open_dev(KEYBOARD_NAME); alt_up_ps2_init(ps2); alt_up_ps2_clear_fifo(ps2); //void* keyboard_control_register_ptr = (void*) (PS2_0_BASE + 4); alt_irq_register(PS2_0_IRQ, m, keyboard_ISR); alt_up_ps2_enable_read_interrupt(ps2); char sw = 0; char p_sw = 0; ///////////////////////////////////////////////////////////////////////// printf("NEW GAME!\n\n"); //SUPERDUPERLOOP while (1){ printf("old state:%i\nnew state: %i\n\n",old_game_state, game_state); draw_menu(game_state); //update screen while (old_game_state == game_state); printf("old state:%i\nnew state: %i\n\n",old_game_state, game_state); //only when entering a new menu alt_up_char_buffer_clear(char_buffer); //ENTER GAME LOOP if (game_state == 5){ printf("START GAME! LEVEL: %i\n\n", highlighted_level); alt_up_char_buffer_string(char_buffer, playername, 10, 4); if (highlighted_level == 1) { free_bmp(b); b = read_bmp("para1.bmp"); game_start(m,b,"lvl/1.txt","song1.wav"); } else if (highlighted_level == 2) { free_bmp(b); b = read_bmp("bg2.bmp"); game_start(m,b,"lvl/2.txt","a/abcd.wav"); } else{ free_bmp(b); b = read_bmp("bg3.bmp"); game_start(m,b,"lvl/2.txt","a/nyan1.wav"); } //collision loop while(!m->c->collide && !m->c->win){ alt_up_char_buffer_string(char_buffer, "POINTS: ", 50, 4); char str[15]; sprintf(str, "%d", m->c->points); alt_up_char_buffer_string(char_buffer, str, 58, 4); sw = IORD_8DIRECT(SWITCHES_BASE,0); IOWR_8DIRECT(LEDS_BASE,0,sw); if(sw == 1 && p_sw == 0){ //m->ab->sfx_flag = 1; m->c->jump_pressed = 1; } p_sw = sw; //boxes int i; for( i= 0 ; i < OBJECT_SIZE ; i++) { if(m->o->color[i] == -1) draw_object(pixel_buffer, box, flat, i); else if(m->o->color[i] == 0) draw_object(pixel_buffer, box, flatb, i); else if(m->o->color[i] == 1) draw_object(pixel_buffer, box, flatr, i); draw_object(pixel_buffer, co, coin, i ); draw_object(pixel_buffer, spikes, spike, i); draw_object(pixel_buffer, box_3, box3, i); draw_object(pixel_buffer, box_1, box1, i); // if(m->color_gates->color[i] == 1) // draw_object(pixel_buffer,cgates, rgate,i); // else if (m->color_gates->color[i] == 0) // draw_object(pixel_buffer,cgates, bgate,i); } //draws the win flag draw_object(pixel_buffer, flag, flag_img, 0); //Draw Doge if (m->c->ducking) draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,low); else{ doge++; if(doge == 37) doge = 0; if( doge <9) draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,doge0); else if (doge <18) draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,doge1); else if (doge <27) draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,doge2); else draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,doge3); } //Draw Color Indicator if(m->c->color == 0) draw_bmp(pixel_buffer, m->c->x- m->c->width + 5, m->c->y - m->c->height - 10, blue); else draw_bmp(pixel_buffer,m->c->x- m->c->width + 5, m->c->y - m->c->height - 10, red); p_counter++; if(p_counter == 3){ p_shift++; alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while(alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)) refill_buffer(m->ab, "a/abcd.wav");//refills the audio buffer unrolled_parallax_draw(pixel_buffer, b); }else if(p_counter == 4){ //if(p_counter == 1){ alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while(alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)) refill_buffer(m->ab, "a/abcd.wav");//refills the audio buffer unrolled_parallax_draw(pixel_buffer, b); p_counter = 0; }else{ // alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); // while(alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)) // refill_buffer(m->ab, "a/abcd.wav");//refills the audio buffer // unrolled_parallax_draw(pixel_buffer, b); alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while(alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)) refill_buffer(m->ab, "a/abcd.wav");//refills the audio buffer int j; for( j = 0 ; j < OBJECT_SIZE ; j++) { clear_object(pixel_buffer, box, b, j); clear_object(pixel_buffer, co, b, j); clear_object(pixel_buffer, spikes, b, j); clear_object(pixel_buffer, box_3, b, j); clear_object(pixel_buffer, box_1, b, j); } clear_object(pixel_buffer,flag,b,0); //clear doge clear_doge(pixel_buffer, m->c->x - m->c->width, m->c->y - m->c->height , b); //clear_loc(pixel_buffer,m->c->x- m->c->width + 5, m->c->y - m->c->height - 10,m->c->x- m->c->width + 5 - 10, m->c->y - m->c->height - 20,b); } } alt_up_char_buffer_string(char_buffer, "POINTS: ", 50, 4); char str[15]; sprintf(str, "%d", m->c->points); alt_up_char_buffer_string(char_buffer, str, 58, 4); printf("game exited\n"); if(m->c->win) game_state = 7; else game_state = 6; highlighted_item = 3; game_reset(m); } //exit game mode, restart superduperloop in main menu old_game_state = game_state; } return 0; }
rstatus_t init_server(struct instance *nci) { rstatus_t status; uint32_t i; redisDb *db; vr_conf *conf; conf = conf_create(nci->conf_filename); server.pid = getpid(); server.configfile = getAbsolutePath(nci->conf_filename); server.hz = 10; server.dbnum = 16; array_init(&server.dbs, server.dbnum, sizeof(redisDb)); server.pidfile = nci->pid_filename; server.executable = NULL; server.activerehashing = CONFIG_DEFAULT_ACTIVE_REHASHING; get_random_hex_chars(server.runid, CONFIG_RUN_ID_SIZE); server.arch_bits = (sizeof(long) == 8) ? 64 : 32; server.requirepass = NULL; server.starttime = time(NULL); server.maxclients = conf->max_clients; server.maxmemory = conf->maxmemory == CONF_UNSET_NUM ? 0 : conf->maxmemory; server.maxmemory_policy = CONFIG_DEFAULT_MAXMEMORY_POLICY; server.client_max_querybuf_len = PROTO_MAX_QUERYBUF_LEN; server.commands = dictCreate(&commandTableDictType,NULL); populateCommandTable(); server.delCommand = lookupCommandByCString("del"); server.multiCommand = lookupCommandByCString("multi"); server.lpushCommand = lookupCommandByCString("lpush"); server.lpopCommand = lookupCommandByCString("lpop"); server.rpopCommand = lookupCommandByCString("rpop"); server.sremCommand = lookupCommandByCString("srem"); server.execCommand = lookupCommandByCString("exec"); for (i = 0; i < server.dbnum; i ++) { db = array_push(&server.dbs); redisDbInit(db); } server.monitors = listCreate(); server.loading = 0; server.lua_timedout = 0; server.aof_state = AOF_OFF; server.stop_writes_on_bgsave_err = 0; server.ready_keys = listCreate(); server.slowlog = listCreate(); server.slowlog_entry_id = 0; server.slowlog_log_slower_than = -1; server.slowlog_max_len = CONFIG_DEFAULT_SLOWLOG_MAX_LEN; server.stat_peak_memory = 0; server.system_memory_size = zmalloc_get_memory_size(); server.rdb_child_pid = -1; server.aof_child_pid = -1; server.hash_max_ziplist_entries = OBJ_HASH_MAX_ZIPLIST_ENTRIES; server.hash_max_ziplist_value = OBJ_HASH_MAX_ZIPLIST_VALUE; server.list_max_ziplist_size = OBJ_LIST_MAX_ZIPLIST_SIZE; server.list_compress_depth = OBJ_LIST_COMPRESS_DEPTH; server.set_max_intset_entries = OBJ_SET_MAX_INTSET_ENTRIES; server.zset_max_ziplist_entries = OBJ_ZSET_MAX_ZIPLIST_ENTRIES; server.zset_max_ziplist_value = OBJ_ZSET_MAX_ZIPLIST_VALUE; server.hll_sparse_max_bytes = CONFIG_DEFAULT_HLL_SPARSE_MAX_BYTES; vr_replication_init(); createSharedObjects(); status = master_init(conf); if (status != VR_OK) { log_error("init master thread failed"); return VR_ERROR; } server.port = master.listen->port; status = workers_init(nci->thread_num); if (status != VR_OK) { log_error("init worker threads failed"); return VR_ERROR; } log_debug(LOG_NOTICE, "mem_alloc_lock_type: %s", malloc_lock_type()); log_debug(LOG_NOTICE, "malloc lib: %s", VR_MALLOC_LIB); return VR_OK; }
void connection_init(){ pthread_mutex_init(&mutex,NULL); int sock,sock_r; struct sockaddr_in sa; int bytes_sent; int s = sizeof(sa); char buffer[1024]; char buffer2[1024]; char buffer3[1024]; bzero(buffer3,1024); strcpy(buffer,"Noen gangstas her?"); strcpy(buffer2,"Halla"); clients = 0; int fd; struct ifreq ifr; fd = socket(AF_INET, SOCK_DGRAM, 0); ifr.ifr_addr.sa_family = AF_INET; strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1); ioctl(fd, SIOCGIFBRDADDR, &ifr); close(fd); char* BROADCASTIP = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr); printf("%s\n",BROADCASTIP); //Set up socket sock = socket(AF_INET, SOCK_DGRAM, 0); int broadcastEnable=1; int ret=setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)); if (-1 == sock) { printf("Error Creating Socket"); exit(EXIT_FAILURE); } memset(&sa, 0, sizeof sa); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(&BROADCASTIP); sa.sin_port = htons(PORT); for(int i = 0;i<10;i++){ bytes_sent = sendto(sock, buffer, strlen(buffer), 0,(struct sockaddr*)&sa, s); if (bytes_sent < 0) network = 0; else{ network = 1; printf("Packet %d sent \n",(i+1)); } } close(sock); //UDP packets sent, awaiting answers if(network){ sock_r = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); ssize_t recsize; socklen_t fromlen; memset(&sa, 0, sizeof sa); sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_ANY); sa.sin_port = htons(PORT); fromlen = sizeof(sa); setsockopt(sock_r, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); if (-1 == bind(sock_r, (struct sockaddr *)&sa, sizeof sa)) { perror("error bind failed2"); close(sock); exit(EXIT_FAILURE); } sleep(1); //Set up timed socket struct timeval tv; tv.tv_sec = 2; /* 2 Secs Timeout */ tv.tv_usec = 0; setsockopt(sock_r, SOL_SOCKET, SO_RCVTIMEO,(char *)&tv,sizeof(tv)); recsize = recvfrom(sock_r, (void*)buffer3, sizeof buffer3, 0, (struct sockaddr*)&sa, &fromlen); if(!strcmp(buffer3,buffer2)) { serv_ip = inet_ntoa(sa.sin_addr); printf("Master discovered, i'm slave :(\n"); connection = 1; client_init(); } else { printf("Master not discovered\n"); printf("Recieved: %s\n", buffer3); master_init(); } close(sock_r); } else{ puts("No network"); pthread_t elev_thread,button_thread, check_network_thread; connection = 1; pthread_create(&elev_thread, NULL, elevator_control, NULL); pthread_create(&button_thread, NULL, button_check, NULL); pthread_create(&check_network_thread, NULL, check_network, NULL); pthread_join(elev_thread,NULL); pthread_join(check_network_thread, NULL); puts("Elevator Thread Killed"); pthread_join(button_thread, NULL); puts("Button Check Thread Killed"); connection_init(); } }