You are on page 1of 3

File: /home/user/Desktop/C/thread/my_redemption/sts_kb.

c Page 1 of 3

1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <pthread.h>
5 #include <termios.h>
6  
7 int ch;
8 struct param {
9 int status1;
10 };
11  
12  
13 static int peek_character = -1;
14 static struct termios init_setting, new_setting;
15  
16 void
17 init_kibor();
18 void
19 close_kibor();
20 int
21 init_khbit(); 
22 int 
23 read__();
24  
25 /* prototype fungsi */
26 void 
27 error(int res, char* msg);
28 void*
29 thread_func(void* par);
30 void*
31 thread_func2(void* par);
32 void 
33 cancel_thread(int ret, pthread_t thread);
34 void
35 k_stroke(int ch__);
36  
37  
38 /* sampai disini */
39 /* fungsi utama */
40  
41 int
42 main()
43 {
44 /* s1 sebagai status u/ menghidupkan thread */
45 struct param s1;
46 pthread_t thread_id; 
47 pthread_t thread_id2;
48 /* return value */
49 int res;
50 /* buat thread passing dengan argumen s1 
51 sebagai status u mnghdpkan thread*/
52 pthread_create(&thread_id, NULL, &thread_func, &s1);
53 pthread_create(&thread_id2, NULL, &thread_func2, &s1);
54 system("clear"); /* bersihkan layar */
55
56 /* hanya menu simple */
57 printf("\n...PROGRAM THREAD....\n\n");
58 printf("ketik \"1\" u/ run thread 1\n");
59 printf("ketik \"2\" u/ run thread 2\n");
60 printf("ketik \"3\" u/ menghentikan thread dan keluar dari program\n");
61 /* loop forever sampai kibor 3 ditekan */
62 while(1) {
63 //scanf("%d",&s1.status1); /* baca kibor forever */
64 printf("main\n");
65 sleep(1);
66 k_stroke(s1.status1);
67 };
File: /home/user/Desktop/C/thread/my_redemption/sts_kb.c Page 2 of 3

68 return 0;
69 }
70  
71 /* fungsi untuk menghandle error */
72 void
73 error(int set, char* msg)
74 {
75 if (set != 0) {
76 perror(msg);
77 exit(1);
78 }
79 }
80  
81 /* fungsi thread 1*/
82 void*
83 thread_func(void* par )
84 {
85 int count =0;
86 struct param *sts = (struct param*) par;
87 int res;
88 res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
89 error(res, "Error setcancelstate!");
90 res = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
91 error(res, "Error setcanceltype!");
92 while(1)
93 while(sts->status1 == 1 ) {
94 count ++;
95 printf("TR 1 (%d)\n", count+1);
96 usleep(300000); /* jeda 0,5 detik */
97 };
98 pthread_exit(0);
99 }
100  
101 /* fungsi thread 2*/
102 void*
103 thread_func2(void* par )
104 {
105 int count =0;
106 struct param *sts = (struct param*) par;
107 int res;
108 res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
109 error(res, "Error setcancelstate!");
110 res = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
111 error(res, "Error setcanceltype!");
112 while(1)
113 while(sts->status1 == 2 ) {
114 count ++;
115 printf("TR 2 (%d)\n", count+1);
116 usleep(100000); /* jeda 0,7 detik */
117 };
118 pthread_exit(0);
119 }
120  
121 /* fungsi untuk menhentikan thread */
122 void 
123 cancel_thread(int ret, pthread_t thread)
124 {
125 ret = pthread_cancel(thread);
126 }
127  
128 void 
129 init_kibor()
130 {
131 tcgetattr(0, &init_setting);
132 new_setting = init_setting;
133 new_setting.c_lflag &= ~ICANON;
134 new_setting.c_lflag &= ~ECHO;
File: /home/user/Desktop/C/thread/my_redemption/sts_kb.c Page 3 of 3

135 new_setting.c_lflag &= ~ISIG;
136 new_setting.c_cc[VMIN] = 1;
137 new_setting.c_cc[VTIME] = 0;
138 tcsetattr(0, TCSANOW, &new_setting);
139 }
140  
141 void
142 close_kibor()
143 {
144 tcsetattr (0, TCSANOW, &init_setting);
145 }
146  
147 int 
148 khbit()
149 {
150 char ch;
151 int nread;
152 if (peek_character != -1)
153 return 1;
154 new_setting.c_cc[VMIN] = 0;
155 tcsetattr(0,TCSANOW, &new_setting);
156 nread = read(0, &ch, 1);
157 new_setting.c_cc[VMIN] = 1;
158 tcsetattr(0, TCSANOW, &new_setting);
159 if (nread == 1) {
160 peek_character = ch;
161 return 1;
162 }
163 return 0;
164 }
165  
166 int
167 read__()
168 {
169 char ch;
170 if (peek_character != 1) {
171 ch = peek_character;
172 peek_character = -1;
173 return ch;
174 }
175 read(0, &ch, 1);
176 return ch;
177 }
178  
179 /* fungsi  key stroke */
180 void
181 k_stroke(int ch__)
182 {
183 init_kibor();
184 if (ch != ch__) {
185 if (khbit()) {
186 ch = read__();
187 printf("you hit %c\n", ch);
188
189 if (ch == ch__) {
190 close_kibor();
191 exit (EXIT_SUCCESS);
192 }
193 }
194 }
195 close_kibor();
196 }
197  
198

You might also like