int main (int argc, char** argv) { assert (NUM_PRODUCERS == NUM_CONSUMERS); spinlock_create(&item_lock); spinlock_create(&producer_wait_count_lock); spinlock_create(&consumer_wait_count_lock); uthread_init(NUM_PRODUCERS + NUM_CONSUMERS); uthread_t producers[NUM_PRODUCERS]; uthread_t consumers[NUM_CONSUMERS]; for (int i = 0; i < NUM_PRODUCERS; i++) { consumers[i] = uthread_create(consumer, NULL); producers[i] = uthread_create(producer, NULL); } for (int i = 0; i < NUM_PRODUCERS; i++) { uthread_join(consumers[i], NULL); uthread_join(producers[i], NULL); } printf("Producer Wait Time: %d\t\tConsumer Wait Time: %d\n", producer_wait_count, consumer_wait_count); printf("Histogram: [ "); int sum = 0; for (int i = 0; i < MAX_ITEMS + 1; i++) { sum += histogram[i]; printf("%d ", histogram[i]); } printf("]\tSum: %d\n", sum); }
int main (int argc, char** argv) { uthread_t t[4]; uthread_init (4); // TODO: Create Threads and Join struct Pool* p = createPool(); uthread_t prodthread[NUM_PRODUCERS]; uthread_t consthread[NUM_CONSUMERS]; for (int i = 0; i < NUM_PRODUCERS; i++){ prodthread[i] = uthread_create(producer, p); } for (int i = 0; i < NUM_CONSUMERS; i++){ consthread[i] = uthread_create(consumer, p); } uthread_join(prodthread[0], 0); uthread_join(consthread[0], 0); uthread_join(prodthread[1], 0); uthread_join(consthread[1], 0); printf ("producer_wait_count=%d, consumer_wait_count=%d\n", producer_wait_count, consumer_wait_count); printf ("items value histogram:\n"); int sum=0; for (int i = 0; i <= MAX_ITEMS; i++) { printf (" items=%d, %d times\n", i, histogram [i]); sum += histogram [i]; } assert (sum == sizeof (t) / sizeof (uthread_t) * NUM_ITERATIONS); }
int main() { // initialized to four processors uthread_init(4); // semaphores mutex = uthread_semaphore_create (1); empty = uthread_semaphore_create (0); full = uthread_semaphore_create (MAX_ITEMS); // create one thread for each producer and one thread for each consumer void* arg = (void*) NUM_ITERATIONS; uthread_t* producer_thread1 = uthread_create(producer, arg); uthread_t* producer_thread2 = uthread_create(producer, arg); uthread_t* consumer_thread1 = uthread_create(consumer, arg); uthread_t* consumer_thread2 = uthread_create(consumer, arg); // the main thread must wait for the producer and consumer threads to finish before it exits uthread_join(producer_thread1); uthread_join(producer_thread2); uthread_join(consumer_thread1); uthread_join(consumer_thread2); // TESTING AREA // the shared resources pool must be 0 when the program terminates, indicating that all produced items were consumed printf ("The Pool: %d\n", items); int total = 0; for (int i = 0; i < MAX_ITEMS+1; i++) { total += histogram[i]; printf ("Histogram: %d\n", histogram[i]); } printf ("Total changs to items: %d\n", total); }
int main (int argc, char** argv) { // TODO create threads to run the producers and consumers spinlock_create(&lock); uthread_init(4); uthread_t producerThread1; uthread_t producerThread2; uthread_t consumerThread1; uthread_t consumerThread2; producerThread1 = uthread_create(producer, 0); producerThread2 = uthread_create(producer, 0); consumerThread1 = uthread_create(consumer, 0); consumerThread2 = uthread_create(consumer, 0); uthread_join(producerThread1, 0); uthread_join(producerThread2, 0); uthread_join(consumerThread1, 0); uthread_join(consumerThread2, 0); printf("Producer wait: %d\nConsumer wait: %d\n", producer_wait_count, consumer_wait_count); for(int i=0;i<MAX_ITEMS+1;i++) printf("items %d count %d\n", i, histogram[i]); /* uthread_join(producerThread1, 0); uthread_join(producerThread2, 0); uthread_join(consumerThread1, 0); uthread_join(consumerThread2, 0); */ }
int main (int* argc, char** argv) { uthread_t *ping_thread, *pong_thread; int i; uthread_init (2); ping_thread = uthread_create (ping, 0); pong_thread = uthread_create (pong, 0); uthread_join (ping_thread); uthread_join (pong_thread); printf ("\n"); }
int main(void) { int tid[3]; int val[3]; libuthread_init(); tid[0] = uthread_create(func1,&val[0]); tid[1] = uthread_create(func1,&val[1]); tid[2] = uthread_create(func1,&val[2]); //val contem o tid do thread com o qual deve ser sincronizado val[0]=tid[1]; val[1]=tid[2]; val[2]=tid[0]; //tidglobal contem o inverso do 'val', o tid do proc. que fez a sincronização tidglobal[tid[0]]=tid[2]; tidglobal[tid[1]]=tid[0]; tidglobal[tid[2]]=tid[1]; if (uthread_join(tid[0])==-1) handle_error("Erro de sincronização"); printf("main: exiting\n"); return 0; }
int main( int argc, char* argv[] ) { int thread_id; int status; test_name = "multi-exit"; msg ("begin"); thread_id = uthread_create( child_thread, NULL ); if( -1 == thread_id ) { fail( "Test creation failed. Test fail" ); } // we will now wait for our child if( -1 == uthread_join( thread_id, &status ) ) { fail( "Thread join failed. Test fail" ); } if( CHILD_EXIT_CODE != status ) { fail( "Exit code wasn't sent properly" ); } msg ("end"); return 0; }
int main(void) { int arg=0, tid1, tid2; libuthread_init(); tid1 = uthread_create(func1,&arg); tid2 = uthread_create(func2,&arg); if (tid1==-1 ) handle_error("Erro ao criar thread"); uthread_join(tid1); uthread_join(tid2); printf("main: exiting\n"); return 0; }
int main(int argc, char* argv[]) { if(argc < 2) { printf("Please enter a positive integer as argument.\n"); return 0; } int n = atoi(argv[1]); if(n < 0) { printf("Negative argument. Please enter a positive integer as argument.\n"); return 0; } uthread_init(); id1 = uthread_create(proc1, &n); if(id1 != -1) { printf("--Created thread 1--\n"); } else { printf("--Creation of thread 1 failed--\n"); } id2 = uthread_create(proc2, &n); if(id2 != -1) { printf("--Created thread 2--\n"); } else { printf("--Creation of thread 2 failed--\n"); } uthread_join(id1); uthread_join(id2); printf("--End of main--\n"); return 0; }
void agent2() { for (int i = 0; i < 10; ++i) { printf("hello, world! --from agent2, i = %d\n", i); for (int j = 0; j < 100000000; ++j) ; if (i == 3) uthread_join(utid1); } }
int main(int ac, char **av) { int i; uthread_init(); uthread_mtx_init(&mtx1); uthread_mtx_init(&mtx2); uthread_mtx_init(&mtx3); uthread_cond_init(&cond); void (*tester[3]) (long a0, void* a1); tester[0] = tester1; tester[1] = tester2; tester[2] = tester3; for (i = 0; i < NUM_THREADS; i++) { uthread_create(&thr[i], tester[i], i, NULL, UTH_MAXPRIO - i % UTH_MAXPRIO //2 ); } //uthread_setprio(thr[0], 6); for (i = 0; i < NUM_THREADS; i++) { char pbuffer[SBUFSZ]; int tmp, ret; uthread_join(thr[i], &tmp); sprintf(pbuffer, "joined with thread %i, exited %i.\n", thr[i], tmp); ret = write(STDOUT_FILENO, pbuffer, strlen(pbuffer)); if (ret < 0) { perror("uthreads_test"); return EXIT_FAILURE; } /* uthread_mtx_lock(&mtx); uthread_cond_signal(&cond); uthread_mtx_unlock(&mtx); */ } printf("Main ends\n"); uthread_exit(0); return 0; }
/* Main: Compute ((1 + 2) - (3 + 4)) + 7 with threads. */ int main ( int* argc, char** argv ) { uthread_init( 2 ); /* Create structs for (1 + 2) and (3 + 4). */ struct Triple *t1 = (struct Triple*) malloc( sizeof( struct Triple ) ); t1->arg0 = 1; t1->arg1 = 2; struct Triple *t2 = (struct Triple*) malloc( sizeof( struct Triple ) ); t2->arg0 = 3; t2->arg1 = 4; /* Create threads for (1 + 2) and (3 + 4). */ uthread_t *t1_thread, *t2_thread; t1_thread = uthread_create( add, (void*) t1 ); t2_thread = uthread_create( add, (void*) t2 ); /* Execute (1 + 2) and (3 + 4) and store results. */ int r1 = *( (int *) uthread_join( t1_thread ) ); int r2 = *( (int *) uthread_join( t2_thread ) ); /* Create struct for (r1 - r2). */ struct Triple *t3 = (struct Triple*) malloc( sizeof( struct Triple ) ); t3->arg0 = r1; t3->arg1 = r2; /* Create thread for (r1 - r2). */ uthread_t *t3_thread = uthread_create ( sub, (void *) t3 ); int r3 = *( (int *) uthread_join( t3_thread ) ); /* Create struct for (r3 + 7). */ struct Triple *t4 = (struct Triple*) malloc( sizeof( struct Triple ) ); t4->arg0 = r3; t4->arg1 = 7; /* Create thread for (r3 + 7). */ uthread_t *t4_thread = uthread_create ( add, (void *) t4 ); int r4 = *( (int *) uthread_join( t4_thread ) ); printf( "%d\n", r4 ); }
int main (int argc, char** argv) { uthread_init (7); struct Agent* a = createAgent(); // TODO uthread_join (uthread_create (agent, a), 0); assert (signal_count [MATCH] == smoke_count [MATCH]); assert (signal_count [PAPER] == smoke_count [PAPER]); assert (signal_count [TOBACCO] == smoke_count [TOBACCO]); assert (smoke_count [MATCH] + smoke_count [PAPER] + smoke_count [TOBACCO] == NUM_ITERATIONS); printf ("Smoke counts: %d matches, %d paper, %d tobacco\n", smoke_count [MATCH], smoke_count [PAPER], smoke_count [TOBACCO]); }
int main(int argc, char *argv[]) { uthread_t a[2]; uthread_t b[2]; uthread_mutex_init(&mut); uthread_cond_init(&c1); uthread_cond_init(&c2); uthread_create(&a[0], thread1, NULL); uthread_create(&a[1], thread1, NULL); uthread_create(&b[0], thread2, (void *) 0); uthread_create(&b[1], thread2, (void *) 1000); uthread_join(a[0], NULL); uthread_join(a[1], NULL); uthread_join(b[0], NULL); uthread_join(b[1], NULL); printf("%ld\n", cnt); return 0; }
int main (int argc, char** argv) { assert (NUM_PRODUCERS == NUM_CONSUMERS); mx = uthread_mutex_create(); not_empty = uthread_cond_create(mx); not_full = uthread_cond_create(mx); uthread_init(NUM_PRODUCERS + NUM_CONSUMERS); uthread_t producers[NUM_PRODUCERS]; uthread_t consumers[NUM_CONSUMERS]; for (int i = 0; i < NUM_PRODUCERS; i++) { consumers[i] = uthread_create(consumer, NULL); producers[i] = uthread_create(producer, NULL); } for (int i = 0; i < NUM_PRODUCERS; i++) { uthread_join(consumers[i], NULL); uthread_join(producers[i], NULL); } uthread_cond_destroy(not_full); uthread_cond_destroy(not_empty); uthread_mutex_destroy(mx); printf("Producer Wait Time: %d\tConsumer Wait Time: %d\n", producer_wait_count, consumer_wait_count); printf("Histogram: [ "); int sum = 0; for (int i = 0; i < MAX_ITEMS + 1; i++) { sum += histogram[i]; printf("%d ", histogram[i]); } printf("]\tSum: %d\n", sum); }
int main(int ac, char **av) { int i; /* char buff[50]; memset(buff,0,50); sprintf(buff, "helloworld\n"); write(STDOUT_FILENO, buff, strlen(buff)); */ printf("hello\n"); uthread_init(); uthread_mtx_init(&mtx); uthread_cond_init(&cond); for (i = 0; i < NUM_THREADS; i++) { uthread_create(&thr[i], tester, i, NULL, 0); } uthread_setprio(thr[0], 2); for (i = 0; i < NUM_THREADS; i++) { char pbuffer[SBUFSZ]; int tmp, ret; uthread_join(thr[i], &tmp); sprintf(pbuffer, "joined with thread %i, exited %i.\n", thr[i], tmp); ret = write(STDOUT_FILENO, pbuffer, strlen(pbuffer)); if (ret < 0) { perror("uthreads_test"); return EXIT_FAILURE; } uthread_mtx_lock(&mtx); uthread_cond_signal(&cond); uthread_mtx_unlock(&mtx); } uthread_exit(0); return 0; }
int main() { uthread_initial(); int i ; printf("hello!\n"); tid_t tid ; for ( i = 0 ; i < 3 ; i ++ ) { uthread_create( &tid , thread , NULL ) ; if ( i != 2 ) uthread_detach( tid ) ; } printf("hello!!!\n"); uthread_join(tid,NULL); printf("hello!\n"); return 0 ; }
int main (int argc, char** argv) { // setup mutex and condition variables mx = uthread_mutex_create(); smoker_waiting = uthread_cond_create(mx); for (int i = 0; i < 3; i++) { resource[i] = uthread_cond_create(mx); } done_smoking = uthread_cond_create(mx); // setup threads and start agent uthread_init(1); uthread_mutex_lock(mx); for (int i = 0; i < 3; i++) { threads[i] = uthread_create(smoker, (void*)(intptr_t) i); uthread_cond_wait(smoker_waiting); } uthread_mutex_unlock(mx); threads[3] = uthread_create(agent, NULL); // wait for threads to finish for (int i = 3; i >= 0; i--) { uthread_join(threads[i], NULL); } // verify that the histograms of actual/expected match for (int i = 0; i < 3; i++) { assert(histo_expected[i] == histo_actual[i]); } // print histograms of expected and actual smoke counts int sum; printf("Expected: [ "); sum = 0; for (int i = 0; i < 3; i++) { sum += histo_expected[i]; printf("%d ", histo_expected[i]); } printf("]\tSum: %d\n", sum); sum = 0; printf("Actual: [ "); for (int i = 0; i < 3; i++) { sum += histo_actual[i]; printf("%d ", histo_actual[i]); } printf("]\tSum: %d\n", sum); }
void do_test (int count) { int i; uthread_t* t [NUM_READER_THREADS+NUM_WRITER_THREADS]; volatile struct drive_test_arg dta [NUM_READER_THREADS+NUM_WRITER_THREADS]; mon = uthread_monitor_create(); for (i=0; i<NUM_READER_THREADS+NUM_WRITER_THREADS; i++) { dta[i].count = count; dta[i].write = (i>=NUM_READER_THREADS); t[i] = uthread_create (drive_test, (void*) &dta[i]); } for (i=0; i<NUM_READER_THREADS+NUM_WRITER_THREADS; i++) uthread_join (t[i]); if (c0!=c1 || c0!=NUM_WRITER_THREADS*count) printf ("end error %d %d %d\n", c0, c1, NUM_WRITER_THREADS*count); }
void run (int numBlocks) { uthread_t thread [numBlocks]; for (int blockno = 0; blockno < numBlocks; blockno++) { // TODO // call readAndHandleBlock in a way that allows this // operation to be synchronous without stalling the CPU struct readAndHandleBlockArgs* temp = malloc(sizeof(readAndHandleBlock)); //create a struct temp->buf=malloc(8); temp->nbytes=8; temp->blockno=numBlocks; thread [blockno] = uthread_create(readAndHandleBlock, temp);//create a thread (the thread is made up of the struct) } for (int i=0; i<numBlocks; i++) uthread_join (thread [i], 0); }
void *func1(void *ptid) { int tid = *(int*)ptid; printf("thread %d esperando thread: %d \n", tidglobal[tid],tid); if (uthread_join(tid)==-1) { printf("Deadlock\n"); erro[tidglobal[tid]]=1; return NULL; } if (erro[tid]) { erro[tidglobal[tid]]=1; printf("Erro no thread: %d , thread %d abortado! \n", tid, tidglobal[tid]); } else printf("Fim Normal do thread: %d \n", tidglobal[tid]); return NULL; }
int main(int argc, char *argv[]) { uthread_t tids[1023]; long i, sum; for (i = 0; i < 1023; ++ i) { uthread_create(&tids[i], thread, (void *) i); } sum = 0; for (i = 0; i < 1023; ++ i) { long tmp; uthread_join(tids[i], (void **) &tmp); sum += tmp; } printf("%ld\n", sum); return 0; }
int main(int argc, char* argv[]) { if(argc < 2) { printf("Please enter a positive integer as argument.\n"); return 0; } int n = atoi(argv[1]); if(n < 0) { printf("Negative argument. Please enter a positive integer as argument.\n"); return 0; } int id; uthread_init(); id = uthread_create(parent, &n); if(id != -1) { printf("--Created parent thread--\n"); } else { printf("--Creation of parent thread failed--\n"); } uthread_join(id); printf("--End of parent thread--\n"); uthread_wait(); return 0; }
int __attribute__((noreturn)) dcc_tcp_read_task(armice_ctrl_t * ctrl, int id) { struct dcc_tcp_parm tcp_parm; jtag_arm_dcc_t * arm_dcc = &ctrl->arm_dcc; uint32_t buf[DCC_TCP_BUF_SIZE / sizeof(uint32_t)]; struct tcp_pcb * tp; struct tcp_pcb * mux; int th; int n; int port = 1001; for (;;) { mux = tcp_alloc(); DCC_LOG1(LOG_TRACE, "mux=%p", mux); tcp_bind(mux, INADDR_ANY, htons(port)); if (tcp_listen(mux, 1) != 0) { DCC_LOG1(LOG_WARNING, "<%d> tcp_listen()", id); break; } if ((tp = tcp_accept(mux)) == NULL) { DCC_LOG(LOG_ERROR, "tcp_accept()."); break; } tcp_close(mux); DCC_LOG2(LOG_TRACE, "%I:%d accepted.", tp->t_faddr, ntohs(tp->t_fport)); jtag_arm_dcc_open(arm_dcc); tcp_parm.tp = tp; tcp_parm.arm_dcc = arm_dcc; th = uthread_create(dcc_write_stack, sizeof(dcc_write_stack), (uthread_task_t)dcc_tcp_write_task, (void *)&tcp_parm, 0, NULL); if (th < 0) { DCC_LOG(LOG_ERROR, "uthread_create() fail!"); jtag_arm_dcc_close(arm_dcc); tcp_close(tp); DCC_LOG2(LOG_TRACE, "%I:%d closed.", tp->t_faddr, ntohs(tp->t_fport)); break; } for (;;) { if ((n = jtag_arm_dcc_read(arm_dcc, buf, sizeof(buf), 500)) < 0) { DCC_LOG1(LOG_WARNING, "jtag_arm_dcc_read(): %d", n); break; } DCC_LOG1(LOG_MSG, "jtag_arm_dcc_read(): %d", n); if (n == 0) { continue; } if ((n = tcp_send(tp, buf, n, 0)) < 0) { DCC_LOG2(LOG_TRACE, "%I:%d error.", tp->t_faddr, ntohs(tp->t_fport)); break; } } jtag_arm_dcc_close(arm_dcc); DCC_LOG2(LOG_TRACE, "%I:%d closed.", tp->t_faddr, ntohs(tp->t_fport)); tcp_close(tp); uthread_join(th); } for(;;); }