1 / 4

Pthread 를 이용한 구구단 .

Pthread 를 이용한 구구단. 2011. 12. 5 최 윤 정. 누구나 알 수 있는 쉬운 구구단 코드에 쓰레드를 적용해 봅니다 . 오늘의 실습코드는 단 1 개입니다 . 약 30 분 이상의 시간을 들여 충분히 코드를 분석해 본 후 , 최소한으로 참고하여 코딩하고 결과를 확인한 후 실습을 마치도록 합니다 . 따라서 , 충분히 이해하고 분석해 보도록 하세요. Thread 를 이용한 구구단의 계산 : 1 ~9 개. for (i = 0; i<MAX_THREAD; i++){

zeph-marsh
Télécharger la présentation

Pthread 를 이용한 구구단 .

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pthread를 이용한 구구단. 2011. 12. 5 최윤 정

  2. 누구나 알 수 있는 쉬운 구구단 코드에 쓰레드를 적용해 봅니다. 오늘의 실습코드는 단 1개입니다. 약 30분 이상의 시간을 들여 충분히 코드를 분석해 본 후, 최소한으로 참고하여 코딩하고 결과를 확인한 후 실습을 마치도록 합니다. 따라서, 충분히 이해하고 분석해 보도록 하세요.

  3. Thread를 이용한 구구단의 계산 : 1 ~9개 for (i = 0; i<MAX_THREAD; i++){ rc = pthread_join(threads[i], (void **)&status); if (rc == 0) { printf("Completed join with thread %d status= %d\n",i, status); } else { printf("ERROR; return code from pthread_join() is %d, thread %d\n", rc, i); return -1; } } for(i=0; i<MAX_NUM; i++) { //쓰레드들이 구구단의 계산을 모두 마친 후, 구구단출력 for(j=0; j<MAX_NUM; j++) { printf("%d X %d = %d\n",i+1,j+1,arr[i][j]); } printf("\n"); } return 0; } #include<stdio.h> #include<string.h> #include<pthread.h> // 최대로사용할쓰레드수, 조정하여 테스트해보세요 #defineMAX_THREAD 9 #define MAX_NUM 9 // 구구단에 사용할 숫자 최대치 pthread_t threads[MAX_THREAD]; intarr[MAX_NUM][MAX_NUM]; //구구단 넣을 배열 void *thread_main(void *); int main(void) { inti,j, rc, status; for (i = 0; i < MAX_THREAD; i++){ // 쓰레드를생성한다. //i인자를넘겨주어시작할처음위치를알려줌. pthread_create(&threads[i], NULL, &thread_main, (void *)i); }

  4. void *thread_main(void *arg) void *thread_main(void *arg) { intj,k; intmy_start; // 쓰레드아규먼트를 가져온다. // (void *)형태이므로, 쓰레드로 전달했던 형태로 형변환을하여사용한다. my_start = (int)(void *)arg; // 쓰레드갯수만큼건너뛰어계산한다. for( ; my_start < MAX_NUM; my_start += MAX_THREAD) { for(j=0; j < MAX_NUM; j++) { // pthread_self()로 쓰레드ID를확인한다. printf("%d X %d work in thread %ul\n",my_start+1,j,pthread_self()); arr[my_start][j]=(my_start+1)*(j+1); usleep(1); // 동시성을 확인하기 위하여 약간의 sleep 추가1 (ms) = 1/1000000 (sec) //sleep(1); 1초. } } }

More Related