/** main thread -- parent **/ int main() { tcb_t *tid1, *tid2; startThreadSystem(); /* Create user threads */ tid1= newThread(sub1); tid2= newThread(sub2); printf("Waiting on tid1\n"); fflush(stdout); twait(tid1); printf("Waiting on tid2\n"); fflush(stdout); twait(tid2); printf("Main done\n"); exit(0); }
/** Main thread **/ int main() { tcb_t *tid1, *tid2, *tid3, *tid4; /* Start the child threads running, and wait for them to complete */ startThreadSystem(); tid1 = newThread(sub1); tid2 = newThread(sub2); tid3 = newThread(sub3); tid4 = newThread(sub4); twait(tid1); twait(tid2); twait(tid3); twait(tid4); /* All threads have finished */ printf("Main done\n"); exit(0); }
/** main **/ int main() { int ndx; mystack_t *stack1; mystack_t *stack2; startThreadSystem(); /* Create stack1 */ stack1 = newStack(100); /* And exercise it by spawning 4 threads */ for (ndx=0; ndx<4; ndx++) { parm[ndx].myID = ndx; parm[ndx].numWords = 30000; parm[ndx].stack = stack1; if ((ndx%2) == 0) { id[ndx] = newThread(pusher); } else { id[ndx] = newThread(popper); } } /* Create stack2 */ stack2 = newStack(5000); /* And exercise it by spawning 6 threads */ for (; ndx<10; ndx++) { parm[ndx].myID = ndx; parm[ndx].numWords = 9000; parm[ndx].stack = stack2; if ((ndx%2) == 0) { id[ndx] = newThread(pusher); } else { id[ndx] = newThread(popper); } } /* Wait for everyone to end */ for (ndx=0; ndx<10; ndx++) { twait(id[ndx]); if ((ndx % 2) == 1) { printf("Popper %d count = %d\n", ndx, parm[ndx].popcount); } } printf("Main thread done\n"); exit(0); }
int main() { struct rwlock m; int i; int children[N_THREADS]; char *stacks[N_THREADS]; int start = uptime(); for (i = 0; i < N_THREADS; i++) stacks[i] = malloc(STACK_SIZE); rwlock(&m, OP_INIT); for (i = 0; i < N_THREADS; i++) { children[i] = tfork(child_main, &m, stacks[i] + STACK_SIZE); if (children[i] < 0) handle_error("error on tfork()"); } for (i = 0; i < N_THREADS; i++) { if (twait(children[i]) < 0) handle_error("error on twait()"); } rwlock(&m, OP_DESTROY); printf(stderr, "Program finished in %d tick(s).\n", uptime() - start); if (!time_equal(uptime() - start, ONE_SEC * N_THREADS)) handle_error("Did not finish in a correct time."); for (i = 0; i < N_THREADS; i++) free(stacks[i]); printf(stdout, "hw3-test-writelock succeeded\n"); exit(); }
tmain() { int k, code = 0; Vmalloc_t *shm, *map; char *arg[5]; pid_t ppid, cpid; if(k = tchild()) { cpid = getpid(); shmfile = argv[k]; mapfile = argv[k+1]; tinfo("Child[pid=%d]: allocating heap memory before opening shm region", cpid); for(k = 0; k < 1024; ++k) if(!malloc(32*1024) ) terror("Child[pid=%d]: Can't allocate segment %d", cpid, k); if(*shmfile) { tinfo("Child[pid=%d]: opening shm region", cpid); if(!(shm = vmmopen(shmfile, 1, MAPSIZE)) ) terror("Child[pid=%d]: Can't open shm region in child process", cpid); tinfo("Child[pid=%d]: shm region opened", cpid); } tinfo("Child[pid=%d]: allocating heap memory before opening map region", cpid); for(k = 0; k < 1024; ++k) if(!malloc(32*1024) ) terror("Child[pid=%d]: Can't allocate segment %d", cpid, k); if(*mapfile) { tinfo("Child[pid=%d]: opening map region", cpid); if(!(map = vmmopen(mapfile, -1, MAPSIZE)) ) terror("Child[pid=%d]: Can't open map region in child process", cpid); tinfo("Child[pid=%d]: map region opened", cpid); } } else { ppid = getpid(); shmfile = tstfile("shm", -1); mapfile = tstfile("map", -1); (void)unlink(shmfile); (void)unlink(mapfile); tinfo("Parent[pid=%d]: %s: opening shm region", ppid, shmfile); if(shm = vmmopen(shmfile, 1, MAPSIZE) ) tinfo("Parent[pid=%d]: %s: shm region opened", ppid, shmfile); else { tnote("shm not supported"); shmfile = ""; } tinfo("Parent[pid=%d]: %s: opening map region", ppid, mapfile); if(map = vmmopen(mapfile, -1, MAPSIZE) ) tinfo("Parent[pid=%d]: %s: map region opened", ppid, mapfile); else { tnote("map not supported"); mapfile = ""; } switch((cpid = fork()) ) /* make a child process */ { default : code = twait(&cpid, 1); break; case 0 : arg[0] = argv[0]; arg[1] = "--child"; arg[2] = shmfile; arg[3] = mapfile; arg[4] = 0; if(execv(argv[0], arg) < 0 ) terror("Could not exec child process"); case -1: terror("Could not fork a child process"); } } vmmrelease(shm, 1); vmclose(shm); vmmrelease(map, 1); vmclose(map); texit(code); }
void m_keyb(int ch,int bank,int prg,int velo) { int bnk_m,bnk_l,pm,ch2; int a,i; unsigned char onkey[32],onnote[32]; unsigned char s[16],ke[128]; char obuf[64]; msg("Music KeyBoard Emulater"); if(rcd->act==0){all_note_off(0);} bnk_l=bank>>8;bnk_m=bank&0xff;ch=(ch-1)&31;ch2=ch&15; pm=rcd->put_mode;rcd->put_mode=rcd->ch_port[ch]; twait(5); if(bank>=0 && tim_asin[ch+1]>=7 && tim_asin[ch+1]<=14){ obuf[0]=0xb0+ch2;obuf[1]=0;obuf[2]=bnk_m; obuf[3]=32;obuf[4]=bnk_l;obuf[5]=255; (*rcd->mix_out)(obuf); } if(prg>=0){ obuf[0]=0xc0+ch2;obuf[1]=prg;obuf[2]=255; (*rcd->mix_out)(obuf); } twait(8); strcpy(onkey," "); while (1){ int ln,sh; if(INPOUT(0xff)==27){break;} sh=B_SFTSNS(); ln=0; for(i=0;i<=6;i++){ a=BITSNS(i); if( a & 1 ){ke[ln++]=bitkey[i][0];} if( a & 2 ){ke[ln++]=bitkey[i][1];} if( a & 4 ){ke[ln++]=bitkey[i][2];} if( a & 8 ){ke[ln++]=bitkey[i][3];} if( a & 16 ){ke[ln++]=bitkey[i][4];} if( a & 32 ){ke[ln++]=bitkey[i][5];} if( a & 64 ){ke[ln++]=bitkey[i][6];} if( a & 128 ){ke[ln++]=bitkey[i][7];} } ke[ln]=0; for (i=0;i<16;i++){ if ( onkey[i]>32 ) { int a; s[0]=onkey[i];s[1]=0; a=str_search(ke,s); if( a==0 ){ int note; onkey[i]=32;note=onnote[i]; /* note off */ obuf[0]=0x90+ch2;obuf[1]=note;obuf[2]=0; obuf[3]=255;(*rcd->mix_out)(obuf); } } } ln=0; for (i=0;i<16;i++){ if(ke[ln]==0){break;} if(onkey[i]==32){ s[0]=ke[ln++];s[1]=0; if(str_search(onkey,s)==0){ int a; a=str_search(keytable,s); if(a!=0){ int note; onkey[i]=s[0];note=keycode[a]; if(sh&1){note+=12;}/*shift*/ if(sh&2){note-=12;}/*ctrl*/ if(sh&128){note+=12;}/*caps*/ if(sh&256){note-=12;}/*かな*/ onnote[i]=note; /* note on */ obuf[0]=0x90+ch2;obuf[1]=note;obuf[2]=velo; obuf[3]=255;(*rcd->mix_out)(obuf); } } } } } /* 97-10-24 key.emu.で発音したままになるかもしれないのを修正した for (i=0;i<16;i++){ if ( onkey[i]>32 ) { int note; note=onnote[i]; *//* note off *//* obuf[0]=0x90+ch2;obuf[1]=note;obuf[2]=0; obuf[3]=255;(*rcd->mix_out)(obuf); } } */ snsclr();rcd->put_mode=pm;msg_clr(); }