1 / 28

Thread Example Program

Thread Example Program. fork.c : fork hello.c : thread he1.c : thread with sleep() hello_p.c : pthread in UNIX hello_p.c with Make.rr : pthread with time-sharing. fork.c. - 새로운 프로세스를 생성. << fork.c >> 1 /*********************************************************** 2 *

ira-levy
Download Presentation

Thread Example Program

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. Thread Example Program fork.c : fork hello.c : thread he1.c : thread with sleep() hello_p.c : pthread in UNIX hello_p.c with Make.rr : pthread with time-sharing Thread Example Program

  2. fork.c - 새로운 프로세스를 생성 Thread Example Program

  3. << fork.c >> 1 /*********************************************************** 2 * 3 * Test Program for fork() system call 4 * 1998.8.19. by yypark 5 * 6 ***********************************************************/ 7 #include <stdio.h> 8 9 main( argc, argv ) 10 int argc; 11 char *argv[]; 12 13 int pid; 14 char ch, first, last; 15 long i,j; 16 17 if( pid = fork() > 0 ) 18 /* parent */ 19 printf("---------- Start Parent Process : PID(%d) : PPID(%d) ----------" 20 , getpid(), getppid()); 21 first = 'A'; 22 last = 'Z'; 23 Thread Example Program

  4. 24 else if( pid == 0 ) 25 /* child */ 26 printf("---------- Start Child Process : PID(%d) : PPID(%d) ----------" 27 , getpid(), getppid()); 28 first = 'a'; 29 last = 'z'; 30 31 else 32 /* not fork(2) : Error Handling Routine */ 33 perror(argv[0]); 34 exit(1); 35 36 37 for( j=0; j<2; j++ ) 38 39 for( ch = first; ch <= last; ch++ ) 40 41 /* delay routine */ 42 for( i=0; i<= 1000000; i++ ) ; 43 write(1, &ch, 1); 44 45 46 Thread Example Program

  5. << fork.hwp >> 1 aAbcBdCDefEFgGhHiIJjkKlLmMNnoOpPqQrRSsTtUuVWvwXYxyZAzBCabDcEdFefGgHhIiJKjLklMmNnoOPpQqRrSTsUtVuWvwXxYyZ ---------- Start Parent Process : PID(302) : PPID(27918) ---------- 2 z---------- Start Child Process : PID(303) : PPID(302) ---------- Thread Example Program

  6. hellow.c thread 테스트 순차실행 Thread Example Program

  7. << hello.c >> 1 /****************************************************** 2 * 3 * Test Program for thr_create() system call 4 * 1997.9.18. by yypark 5 * Compiled by gcc -o hello -lthreadhello.c 6 * 7 ******************************************************/ 8 #include <stdio.h> 9 /* 10 #include "/usr/include/thread.h" 11 #include <unistd.h> 12 */ 13 static void * printThread(void *); 14 static void * showThread(void *); 15 static void * GranChildThread(void *); 16 17 void * 18 main(void) 19 20 int i = 0; 21 int ret; 22 char ch; 23 24 ret = thr_create( NULL, 0, printThread, NULL, 0, NULL ); 25 ret = thr_create( NULL, 0, showThread, NULL, 0, NULL ); 26 Thread Example Program

  8. 27 printf("- Hello, Main Thread id ---> Tid(%d) : Pid(%d)", thr_self(), getpid()); 28 29 for( ch = 'a'; ch <= 'z'; ch++ ) 30 31 write(1, &ch, 1); 32 for(i=1;i<1000000;i++); 33 34 35 printf("- End Main Thread --"); 36 thr_exit(( void *)0 ); 37 Thread Example Program

  9. 39 static void * 40 printThread(void *arg) 41 42 int i; 43 char ch; 44 45 printf("- Hello, Print Thread id ---> Tid(%d) : Pid(%d)", thr_self(), getpid()); 46 47 for( ch = 'A'; ch <= 'Z'; ch++ ) 48 49 write(1, &ch, 1); 50 for(i=1;i<1000000;i++); 51 52 53 printf("- End Print Thread --"); 54 thr_exit((void *)0); 55 return; 56 57 Thread Example Program

  10. 58 static void * 59 GrandChildThread(void *arg) 60 61 int i; 62 char ch; 63 64 printf("- Hello, GrandChild Thread id ---> Tid(%d) : Pid(%d)", thr_self(), getpid()); 65 66 printf("- End GrandChild Thread --"); 67 thr_exit((void *)0); 68 return; 69 70 Thread Example Program

  11. 71 static void * 72 showThread(void *arg) 73 74 int i,ret; 75 char ch; 76 77 printf("- Hello, Show Thread id ---> Tid(%d) : Pid(%d)", thr_self(), getpid()); 78 79 ret = thr_create( NULL, 0, GrandChildThread, NULL, 0, NULL ); 80 for( ch = '0'; ch <= '9'; ch++ ) 81 82 write(1, &ch, 1); 83 for(i=1;i<1000000;i++); 84 85 86 printf("- End show Thread --"); 87 thr_exit((void *)0); 88 return; 89 90 Thread Example Program

  12. << hello.hwp >> 1 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 2 -- Hello, Main Thread id ---> Tid(1) : Pid(469) 3 4 -- End Main Thread -- 5 6 -- Hello, Print Thread id ---> Tid(4) : Pid(469) 7 8 -- End Print Thread -- 9 10 -- Hello, Show Thread id ---> Tid(5) : Pid(469) 11 12 -- End show Thread -- 13 14 -- Hello, GrandChild Thread id ---> Tid(6) : Pid(469) 15 16 -- End GrandChild Thread -- Thread Example Program

  13. he1.c thread 컨텍스 스위치 테스트 sleep() 이용 Thread Example Program

  14. << he1.c >> 1 /****************************************************** 2 * 3 * Test Program for thr_create() system call with sleep() 4 * 1997.9.18. by yypark 5 * Compiled by gcc -o hello -lthreadhello.c 6 * 7 ******************************************************/ 8 #include <stdio.h> 9 /* 10 #include <thread.h> 11 #include <unistd.h> 12 */ 13 static void * printThread(void *); 14 static void * showThread(void *); 15 16 void * 17 main(void) 18 19 int i = 0; 20 char ch; 21 22 thr_create( NULL, 0, printThread, NULL, 0, NULL ); 23 thr_create( NULL, 0, showThread, NULL, 0, NULL ); 24 25 printf("- Hello, Main Thread id ---> Tid(%d) : Pid(%d)", thr_self(), getpid()); 26 Thread Example Program

  15. 27 for( ch = 'a'; ch <= 'z'; ch++ ) 28 29 write(1, &ch, 1); 30 if( ch == 'k' || ch == 'q' ) 31 32 sleep(1); 33 printf(""); 34 35 for(i=1;i<1000000;i++); 36 37 38 printf("- End Main Thread --"); 39 thr_exit(( void *)0 ); 40 41 Thread Example Program

  16. 42 static void * 43 printThread(void *arg) 44 45 int i; 46 char ch; 47 48 printf("- Hello, Print Thread id ---> Tid(%d) : Pid(%d)", thr_self(), getpid()); 49 50 for( ch = 'A'; ch <= 'Z'; ch++ ) 51 52 write(1, &ch, 1); 53 if( ch == 'K' || ch == 'Q' ) 54 55 sleep(1); 56 printf(""); 57 58 for(i=1;i<1000000;i++); 59 60 61 printf("- End Print Thread --"); 62 thr_exit((void *)0); 63 return; 64 65 Thread Example Program

  17. 66 static void * 67 showThread(void *arg) 68 69 int i; 70 char ch; 71 72 printf("- Hello, Show Thread id ---> Tid(%d) : Pid(%d)", thr_self(), getpid()); 73 74 for( ch = '0'; ch <= '9'; ch++ ) 75 76 write(1, &ch, 1); 77 if( ch == '4' || ch == '8' ) 78 79 sleep(1); 80 printf(""); 81 82 for(i=1;i<1000000;i++); 83 84 85 printf("- End show Thread --"); 86 thr_exit((void *)0); 87 return; 88 Thread Example Program

  18. << he1.hwp >> 1 abcdefghijkABCDEFGHIJK01234lmnopqLMNOPQ5678rstuvwxyzRSTUVWXYZ9 2 -- Hello, Main Thread id ---> Tid(1) : Pid(701) 3 4 -- Hello, Print Thread id ---> Tid(4) : Pid(701) 5 6 -- Hello, Show Thread id ---> Tid(5) : Pid(701) 7 8 9 10 11 12 -- End Main Thread -- 13 14 15 -- End Print Thread -- 16 17 18 -- End show Thread -- Thread Example Program

  19. hellow_p.c pthread 테스트 순차실행 UNIX pthread 이용 Thread Example Program

  20. << hello_p.c >> 1 /****************************************************** 2 * 3 * Test Program for thr_create() system call 4 * 2001.4.2. by yypark 5 * Compiled by gcc -o hello_p -lpthread hello_p.c 6 * 7 ******************************************************/ 8 #include <stdio.h> 9 /* 10 #include <pthread.h> 11 #include "/usr/include/thread.h" 12 #include <unistd.h> 13 */ 14 static void * printPThread(void *); 15 static void * showPThread(void *); 16 static void * GranChildPThread(void *); 17 Thread Example Program

  21. 18 void * 19 main(void) 20 21 int i = 0; 22 int th1,th2; 23 int ret; 24 char ch; 25 26 pthread_create(&th1, NULL, printPThread, &ret ); 27 pthread_create(&th2, NULL, showPThread, &ret ); 28 29 printf("- Hello, Main PThread id ---> Tid(%d) : Pid(%d) ", pthread_self(), getpid()); 30 31 for( ch = 'a'; ch <= 'z'; ch++ ) 32 33 write(1, &ch, 1); 34 for(i=1;i<1000000;i++); 35 36 37 printf("- End Main PThread --"); 38 pthread_exit(( void *)0 ); 39 40 Thread Example Program

  22. 41 static void * 42 printPThread(void *arg) 43 44 int i; 45 char ch; 46 47 printf("- Hello, Print PThread id ---> Tid(%d) : Pid(%d)", pthread_self(), getpid()); 48 49 for( ch = 'A'; ch <= 'Z'; ch++ ) 50 51 write(1, &ch, 1); 52 for(i=1;i<1000000;i++); 53 54 55 printf("- End Print PThread --"); 56 pthread_exit((void *)0); 57 return; 58 59 Thread Example Program

  23. 60 static void * 61 GrandChildPThread(void *arg) 62 63 int i; 64 char ch; 65 66 printf("- Hello, GrandChildPThread id ---> Tid(%d) : Pid(%d)", pthread_self(), getpid()); 67 68 printf("- End GrandChildPThread --"); 69 pthread_exit((void *)0); 70 return; 71 72 Thread Example Program

  24. 73 static void * 74 showPThread(void *arg) 75 76 int i,ret; 77 char ch; 78 79 printf("- Hello, Show PThread id ---> Tid(%d) : Pid(%d)", pthread_self(), getpid()); 80 81 ret = thr_create( NULL, 0, GrandChildPThread, NULL, 0, NULL ); 82 for( ch = '0'; ch <= '9'; ch++ ) 83 84 write(1, &ch, 1); 85 for(i=1;i<1000000;i++); 86 87 88 printf("- End show PThread --"); 89 pthread_exit((void *)0); 90 return; 91 92 Thread Example Program

  25. << hello_p.hwp >> 1 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 2 -- Hello, Main PThread id ---> Tid(1) : Pid(976) 3 4 -- End Main PThread -- 5 6 -- Hello, Print PThread id ---> Tid(4) : Pid(976) 7 8 -- End Print PThread -- 9 10 -- Hello, Show PThread id ---> Tid(5) : Pid(976) 11 12 -- End show PThread -- 13 14 -- Hello, GrandChildPThread id ---> Tid(6) : Pid(976) 15 16 -- End GrandChildPThread-- Thread Example Program

  26. hellow_p.c pthread 테스트 time_sharing between pthreads 수정된 pthread 이용 Thread Example Program

  27. << Make.rr >> 1 # 2 #echo "" 3 CFILE = hello_p.c 4 OBJS = hello_p.o 5 HEADER = 6 7 TARGET = hello_pthread 8 9 INC = /home1/etri99/edom/thread/include 10 RRLIB = /home1/etri99/edom/thread/lib/libgthreads.a 11 RRMEM = /home1/etri99/edom/thread/malloc/gmalloc.o 12 LIB1 = nsl 13 LIB2 = socket 14 15 CC_OPT = -I$(INC) 16 CC_OPT1 = -l$(LIB1) -l$(LIB2) $(RRLIB) $(RRMEM) 17 CC = gcc 18 19 $(TARGET) : $(OBJS) 20 $(CC) $(CC_OPT) $(CC_OPT1) -o $(TARGET) $(OBJS) 21 22 hello_p.o : $(HEADER) 23 $(CC) $(CC_OPT) -c hello_p.c 24 25 clean : 26 rm -f *.o Thread Example Program

  28. << pthread.hwp >> 1 aA0bB1c2C3dD4eEF5fgG6H7h8iIJ9jkKlLMmnNOoPpQqRSrTsUtuVWvXwYxZyz 2 -- Hello, Main PThread id ---> Tid(208896) : Pid(1127) 3 4 -- Hello, Print PThread id ---> Tid(210944) : Pid(1127) 5 6 -- Hello, Show PThread id ---> Tid(270336) : Pid(1127) 7 8 -- End show PThread -- 9 10 -- End Print PThread -- 11 12 -- End Main PThread -- Thread Example Program

More Related