int main(int argc, char* argv[]) { gerarArrayDeSenhas(); int pid =mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0); int xsys = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0); for (xsys = 0; xsys < 500; xsys++ ) { pid = fork(); if (pid == 0) { /* child */ char output[33]; array_de_hashes[xsys] = md5FromString(array_de_senhas[xsys],output); printf("Child process prints %s\n", array_de_hashes[xsys]); exit(0); } if (pid < 0) { perror("fork"); exit(-1); } } waitall(); printf("Parent process prints %d\n", xsys); munmap(xsys, sizeof(int)); munmap(pid, sizeof(int)); }
/* * Run the whole workload. */ static void runit(unsigned numthinkers, unsigned numgrinders, unsigned numponggroups, unsigned ponggroupsize) { pid_t pids[numponggroups + 2]; time_t startsecs; unsigned long startnsecs; char buf[32]; unsigned i; tprintf("Running with %u thinkers, %u grinders, and %u pong groups " "of size %u each.\n", numthinkers, numgrinders, numponggroups, ponggroupsize); usem_init(&startsem, STARTSEM); createresultsfile(); forkem(numthinkers, nop, think, nop, 0, &pids[0]); forkem(numgrinders, nop, grind, nop, 1, &pids[1]); for (i=0; i<numponggroups; i++) { forkem(ponggroupsize, pong_prep, pong, pong_cleanup, i+2, &pids[i+2]); } usem_open(&startsem); tprintf("Forking done; starting the workload.\n"); __time(&startsecs, &startnsecs); Vn(&startsem, numthinkers + numgrinders + numponggroups * ponggroupsize); waitall(pids, numponggroups + 2); usem_close(&startsem); usem_cleanup(&startsem); openresultsfile(O_RDONLY); tprintf("--- Timings ---\n"); if (numthinkers > 0) { calcresult(0, startsecs, startnsecs, buf, sizeof(buf)); tprintf("Thinkers: %s\n", buf); } if (numgrinders > 0) { calcresult(1, startsecs, startnsecs, buf, sizeof(buf)); tprintf("Grinders: %s\n", buf); } for (i=0; i<numponggroups; i++) { calcresult(i+2, startsecs, startnsecs, buf, sizeof(buf)); tprintf("Pong group %u: %s\n", i, buf); } closeresultsfile(); destroyresultsfile(); }
int main() { hog(); hog(); hog(); cat(); waitall(); return 0; }
int main(void) { sink(); sink(); sink(); sink(); waitall(); return 0; }
int main(int argc, char *argv[]) { int i; int nforks; int pid; int stat_val; if (argc != 2) { fprintf(stderr, "Usage: %s nforks\n", argv[0]); exit(EXIT_FAILURE); } nforks = atoi(argv[1]); printf("%d fork()s\n", nforks); printf("Starting at "); fflush(stdout); stat_val = system("date"); if (!WIFEXITED(stat_val) || WEXITSTATUS(stat_val)) { fprintf(stderr, "system(\"date\") failed: %x\n", stat_val); } for (i = 0; i < nforks; i++) { pid = fork(); if (pid == 0) { /* child */ sleep(3); exit(0); } if (pid < 0) { /* parent, upon error */ perror("fork"); exit(EXIT_FAILURE); } } /* parent */ waitall(); printf("Finished at "); fflush(stdout); stat_val = system("date"); if (!WIFEXITED(stat_val) || WEXITSTATUS(stat_val)) { fprintf(stderr, "system(\"date\") failed: %x\n", stat_val); } return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { int pid; pid = fork(); if (pid == 0) { /* child */ x = 1; printf("Child process set x=1\n"); exit(0); } if (pid < 0) { /* parent, upon error */ perror("fork"); exit(-1); } /* parent */ waitall(); printf("Parent process sees x=%d\n", x); return 0; }
int main(int argc, char *argv[]) { char *bname_server = NULL, *bname_client = NULL; int i, j, ch; int error; int num = 1; int Servers = 1; int use_kerberos = 0; int refcnt = 0; pthread_t thread; pthread_attr_t attr[1]; char hostbuf[MAXHOSTNAME]; char *host = hostbuf; char *realm = NULL; char *ServicePrincipal = NULL; struct passwd *pent = NULL; kern_return_t kr; uid = getuid(); if (seteuid(uid)) { Log("Could not drop privilege"); exit(EXIT_FAILURE); } setprogname(argv[0]); /* Set up mech table */ mechtab[GSSD_KRB5_MECH] = GSS_KRB5_MECHANISM; mechtab[GSSD_SPNEGO_MECH] = GSS_SPNEGO_MECHANISM; mechtab[GSSD_NTLM_MECH] = GSS_NTLM_MECHANISM; while ((ch = getopt(argc, argv, "b:B:CdDef:hHikN:n:M:m:p:r:Rs:S:t:u:v:V")) != -1) { switch (ch) { case 'b': bname_client = optarg; break; case 'B': bname_server = optarg; break; case 'C': gssd_flags |= GSSD_NO_CANON; break; case 'd': debug++; break; case 'D': gssd_flags |= GSSD_NO_DEFAULT; break; case 'e': exitonerror = 1; break; case 'f': flags |= atoi(optarg); break; case 'H': gssd_flags &= ~GSSD_HOME_ACCESS_OK; break; case 'i': interactive = 1; break; case 'k': use_kerberos = 1; break; case 'M': max_retries = atoi(optarg); break; case 'm': if (strcmp(optarg, "krb5") == 0) mech = GSSD_KRB5_MECH; else if (strcmp(optarg, "spnego") == 0) mech = GSSD_SPNEGO_MECH; else if (strcmp(optarg, "ntlm") == 0) mech = GSSD_NTLM_MECH; else { Log("Unavailable gss mechanism %s\n", optarg); exit(EXIT_FAILURE); } break; case 'n': num = atoi(optarg); break; case 'N': if (strcmp(optarg, "uid") == 0) name_type = GSSD_MACHINE_UID; else if (strcmp(optarg, "suid") == 0) name_type = GSSD_STRING_UID; else if (strcmp(optarg, "user") == 0) name_type = GSSD_USER; else if (strcmp(optarg, "krb5") == 0) name_type = GSSD_KRB5_PRINCIPAL; else if (strcmp(optarg, "ntlm") == 0) name_type = GSSD_NTLM_PRINCIPAL; else { Log("Unsupported name type %s\n", optarg); exit(EXIT_FAILURE); } break; case 'p': principal = optarg; break; case 'r': realm = optarg; break; case 'R': refcnt = 1; break; case 's': Servers = atoi(optarg); break; case 'S': ServicePrincipal = optarg; break; case 't': timeout = atoi(optarg); break; case 'u': pent = getpwnam(optarg); if (pent) uid = pent->pw_uid; else Log("Could no find user %s\n", optarg); break; case 'V': verbose++; break; case 'v': version = atoi(optarg); break; default: Usage(); break; } } argc -= optind; argv += optind; if (argc == 0) { gethostname(hostbuf, MAXHOSTNAME); } else if (argc == 1) { host = argv[0]; } else { Usage(); } if (principal == NULL || *principal == '\0') { if (pent == NULL) pent = getpwuid(uid); principal = pent->pw_name; name_type = GSSD_USER; } clientp.nt = name_type; switch (name_type) { case GSSD_USER: case GSSD_STRING_UID: case GSSD_KRB5_PRINCIPAL: case GSSD_NTLM_PRINCIPAL: clientp.name = (gssd_byte_buffer) principal; clientp.len = (uint32_t) strlen(principal); break; default: Log("Unsupported name type for principal %s\n", principal); exit(EXIT_FAILURE); break; } printf("Using creds for %s host=%s\n", principal, host); if (bname_client) { kr = bootstrap_look_up(bootstrap_port, bname_client, &client_mp); if (kr != KERN_SUCCESS) { Log("bootstrap_look_up(): %s\n", bootstrap_strerror(kr)); exit(EXIT_FAILURE); } } else { client_mp = get_gssd_port(); } if (!MACH_PORT_VALID(client_mp)) { Log("Could not get a valid client port (%d)\n", client_mp); exit(EXIT_FAILURE); } if (refcnt) return do_refcount(mech, name_type, principal); if (ServicePrincipal) strlcpy(svcname, ServicePrincipal, sizeof(svcname)); else if (use_kerberos) { strlcpy(svcname, "nfs/", sizeof(svcname)); strlcat(svcname, host, sizeof(svcname)); if (realm) { strlcat(svcname, "@", sizeof(svcname)); strlcat(svcname, realm, sizeof(svcname)); } } else { strlcpy(svcname, "nfs@", sizeof(svcname)); strlcat(svcname, host, sizeof(svcname)); } if (!use_kerberos) { targetp.nt = GSSD_HOSTBASED; targetp.name = (gssd_byte_buffer)svcname; targetp.len = (uint32_t) strlen(svcname); } printf("Service name = %s\n", svcname); if (bname_server) { kr = bootstrap_look_up(bootstrap_port, bname_server, &server_mp); if (kr != KERN_SUCCESS) { Log("bootstrap_look_up(): %s\n", bootstrap_strerror(kr)); exit(EXIT_FAILURE); } } else { server_mp = get_gssd_port(); } if (!MACH_PORT_VALID(server_mp)) { Log("Could not get a valid server port (%d)\n", server_mp); exit(EXIT_FAILURE); } if (interactive) { printf("Hit enter to start "); (void) getchar(); } pthread_attr_init(attr); pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED); pthread_mutex_init(num_servers_lock, NULL); pthread_cond_init(num_servers_cv, NULL); for (i = 0; i < num; i++) { num_servers = Servers; for (j = 0; j < num_servers; j++) { error = pthread_create(&thread, attr, server, NULL); if (error) Log("Could not start server: %s\n", strerror(error)); } waitall(); } report_errors(); pthread_attr_destroy(attr); kr = mach_port_deallocate(mach_task_self(), client_mp); if (kr != KERN_SUCCESS) { Log("Could not delete send right!\n"); } kr = mach_port_deallocate(mach_task_self(), server_mp); if (kr != KERN_SUCCESS) { Log("Could not delete send right!\n"); } if (interactive) { printf("Hit enter to stop\n"); (void) getchar(); } return (0); }
/** * @brief benchmark * cbenchmark的入口 * 通过读取参数中的信息 * 开始执行 * * @param param * cbenchmark全局信息 */ void benchmark(void* param) { struct bench * bp = bm_init(param); struct timeval tvStart,tvEnd; float dif = 0; float tps = 0; if(bp->mode & BM_MODE_SHARE && (bp->libs != NULL)){ init_mw(bp); } //参数文件初始化 if(bp->infile != NULL){ init_proc_param(bp); } //日志初始化 openlog(bp->log.name, LOG_PID|LOG_CONS, LOG_USER); setlogmask(LOG_UPTO(bp->log.level)); /* * 为cbenchmark主进程注册信号处理函数 * 之后会对每个子进程也注册类似函数 * 这些信号处理统一由cb_trap管理 */ /* * SIGINT信号处理当用户通过Ctrl+C发送中断信息给主进程后 * 主进程将让子进程停止工作 * 同时自己也逐渐结束 */ if(signal(SIGINT,cb_trap) == SIG_ERR){ fprintf(stderr,"Register signal trap faile."); }; //起始时间 gettimeofday(&tvStart,NULL); dispatch(bp); //如果以定时器模式执行 //则启动定时器 //并设置回掉函数 //让其对子进程发送SIGUSR1信号 if(bp->mode & BM_MODE_TIMER){ timer(&bp->time,cb_kill_children_break,NULL); } //等待所有进程终止 waitall((void *)bp); //终止时间 gettimeofday(&tvEnd,NULL); dif = (tvEnd.tv_sec-tvStart.tv_sec) + (tvEnd.tv_usec-tvStart.tv_usec)/1000000.0; analyse((void *)bp,dif); //关闭日志 closelog(); //关闭动态库 if(bp->mode & BM_MODE_SHARE && (bp->libs != NULL)){ destroy_mw(bp); } }
int main(int argc, char *argv[]) { int n = 3; // number of rows int m = 3; // number of columns if (argc < 3) { fprintf(stderr, "Too few arguments\n"); return 1; } int i, j; char *file_a = argv[1]; char *file_b = argv[2]; FILE *fda = fopen(file_a, "r"); fscanf(fda, "%d %d", &n, &m); int m_b, p; FILE *fdb = fopen(file_b, "r"); fscanf(fdb, "%d %d", &m_b, &p); if (m != m_b) { fprintf(stderr, "Invalid matrices\n"); return 1; } int **matrix_a = malloc(sizeof(int*) * n); for (i = 0; i < n; i++) { matrix_a[i] = malloc(sizeof(int) * m); for (j = 0; j < m; j++) { int x; fscanf(fda, "%d", &x); matrix_a[i][j] = x; } } int **matrix_b = malloc(sizeof(int*) * m); for (i = 0; i < m; i++) { matrix_b[i] = malloc(sizeof(int) * p); for (j = 0; j < p; j++) { int x; fscanf(fdb, "%d", &x); matrix_b[i][j] = x; } } // Used to store the pid of each row pid_t *pids = malloc(sizeof(pid_t) * n); pid_t pid; int root_pid = getpid(); for (i = 0; i < n; i++) { pid = fork(); if (pid <= 0) break; pids[i] = pid; } if (pid == 0) { char filepath[15]; sprintf(filepath, "%d.row", getpid()); FILE *fd = fopen(filepath, "w"); for (j = 0; j < p; ++j) { int c = 0; for (i = 0; i < m; ++i) { c += (matrix_a[getpid() - root_pid - 1][i] * matrix_b[i][j]); } fprintf(fd, "%d ", c); } fclose(fd); } else if (pid < 0) { perror("fork\n"); exit(-1); } else { waitall(); char filepath[15]; size_t bufsize = 255; char content[bufsize]; for (i = 0; i < n; i++) { // For each row, read the entire row file and print to stdout sprintf(filepath, "%d.row", pids[i]); FILE *fd = fopen(filepath, "r"); for (;;) { size_t n = fread(content, 1, bufsize-1, fd); content[n] = 0; printf("%s", content); if (n < bufsize-1) { printf("\n"); break; } } fclose(fd); unlink(filepath); } } return 0; }