-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample.html
980 lines (560 loc) · 40.8 KB
/
example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
<!DOCTYPE HTML>
<html lang="ko" >
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-161001174-3"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-161001174-3');
</script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>예제 | Introduction</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.7">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="next" href="./tcp_ip.html" />
<link rel="prev" href="./extensions.html" />
</head>
<body>
<div class="book"
data-level="13.1"
data-chapter-title="예제"
data-filepath="example.md"
data-basepath="."
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" data-path="bug_reports.html">
<a href="./bug_reports.html">
<i class="fa fa-check"></i>
<b>1.</b>
Bug reports
</a>
</li>
<li class="chapter " data-level="2" data-path="basics.html">
<a href="./basics.html">
<i class="fa fa-check"></i>
<b>2.</b>
Basics
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="data_types.html">
<a href="./data_types.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
Data Types
</a>
</li>
<li class="chapter " data-level="2.2" data-path="variables.html">
<a href="./variables.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Variables
</a>
</li>
<li class="chapter " data-level="2.3" data-path="regex.html">
<a href="./regex.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
Regex
</a>
</li>
<li class="chapter " data-level="2.4" data-path="functions.html">
<a href="./functions.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
Functions
</a>
</li>
<li class="chapter " data-level="2.5" data-path="escape_sequences.html">
<a href="./escape_sequences.html">
<i class="fa fa-check"></i>
<b>2.5.</b>
Escape sequences
</a>
</li>
<li class="chapter " data-level="2.6" data-path="begin_end.html">
<a href="./begin_end.html">
<i class="fa fa-check"></i>
<b>2.6.</b>
BEGIN, END
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="builtin_variables.html">
<a href="./builtin_variables.html">
<i class="fa fa-check"></i>
<b>3.</b>
Built-in Variables
</a>
</li>
<li class="chapter " data-level="4" data-path="record_sep.html">
<a href="./record_sep.html">
<i class="fa fa-check"></i>
<b>4.</b>
Record 분리
</a>
</li>
<li class="chapter " data-level="5" data-path="field_sep.html">
<a href="./field_sep.html">
<i class="fa fa-check"></i>
<b>5.</b>
Field 분리
</a>
</li>
<li class="chapter " data-level="6" data-path="zero_one.html">
<a href="./zero_one.html">
<i class="fa fa-check"></i>
<b>6.</b>
$0=$0, $1=$1
</a>
</li>
<li class="chapter " data-level="7" data-path="redirection.html">
<a href="./redirection.html">
<i class="fa fa-check"></i>
<b>7.</b>
Redirection
</a>
</li>
<li class="chapter " data-level="8" data-path="getline.html">
<a href="./getline.html">
<i class="fa fa-check"></i>
<b>8.</b>
getline
</a>
</li>
<li class="chapter " data-level="9" data-path="arrays.html">
<a href="./arrays.html">
<i class="fa fa-check"></i>
<b>9.</b>
Arrays
</a>
<ul class="articles">
<li class="chapter " data-level="9.1" data-path="sort.html">
<a href="./sort.html">
<i class="fa fa-check"></i>
<b>9.1.</b>
Sort
</a>
</li>
<li class="chapter " data-level="9.2" data-path="GROUP_BY.html">
<a href="./GROUP_BY.html">
<i class="fa fa-check"></i>
<b>9.2.</b>
GROUP BY
</a>
</li>
<li class="chapter " data-level="9.3" data-path="JOIN.html">
<a href="./JOIN.html">
<i class="fa fa-check"></i>
<b>9.3.</b>
JOIN
</a>
</li>
<li class="chapter " data-level="9.4" data-path="subquery.html">
<a href="./subquery.html">
<i class="fa fa-check"></i>
<b>9.4.</b>
Subquery
</a>
</li>
<li class="chapter " data-level="9.5" data-path="index2.html">
<a href="./index2.html">
<i class="fa fa-check"></i>
<b>9.5.</b>
Index
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="10" data-path="control_statements.html">
<a href="./control_statements.html">
<i class="fa fa-check"></i>
<b>10.</b>
Control Statements
</a>
</li>
<li class="chapter " data-level="11" data-path="operators.html">
<a href="./operators.html">
<i class="fa fa-check"></i>
<b>11.</b>
Operators
</a>
</li>
<li class="chapter " data-level="12" data-path="builtin_functions.html">
<a href="./builtin_functions.html">
<i class="fa fa-check"></i>
<b>12.</b>
Built-in Functions
</a>
<ul class="articles">
<li class="chapter " data-level="12.1" data-path="builtin/numeric.html">
<a href="./builtin/numeric.html">
<i class="fa fa-check"></i>
<b>12.1.</b>
Numeric Functions
</a>
</li>
<li class="chapter " data-level="12.2" data-path="builtin/string.html">
<a href="./builtin/string.html">
<i class="fa fa-check"></i>
<b>12.2.</b>
String Functions
</a>
</li>
<li class="chapter " data-level="12.3" data-path="builtin/io.html">
<a href="./builtin/io.html">
<i class="fa fa-check"></i>
<b>12.3.</b>
I/O Functions
</a>
</li>
<li class="chapter " data-level="12.4" data-path="builtin/time.html">
<a href="./builtin/time.html">
<i class="fa fa-check"></i>
<b>12.4.</b>
Time Functions
</a>
</li>
<li class="chapter " data-level="12.5" data-path="builtin/bitwise.html">
<a href="./builtin/bitwise.html">
<i class="fa fa-check"></i>
<b>12.5.</b>
Bitwise Functions
</a>
</li>
<li class="chapter " data-level="12.6" data-path="builtin/type.html">
<a href="./builtin/type.html">
<i class="fa fa-check"></i>
<b>12.6.</b>
Type Functions
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="13" data-path="extensions.html">
<a href="./extensions.html">
<i class="fa fa-check"></i>
<b>13.</b>
Dynamic Extensions
</a>
<ul class="articles">
<li class="chapter active" data-level="13.1" data-path="example.html">
<a href="./example.html">
<i class="fa fa-check"></i>
<b>13.1.</b>
예제
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="14" data-path="tcp_ip.html">
<a href="./tcp_ip.html">
<i class="fa fa-check"></i>
<b>14.</b>
TCP/IP Internetworking
</a>
</li>
<li class="chapter " data-level="15" data-path="command_line_options.html">
<a href="./command_line_options.html">
<i class="fa fa-check"></i>
<b>15.</b>
Command-line options
</a>
</li>
<li class="chapter " data-level="16" data-path="debugging.html">
<a href="./debugging.html">
<i class="fa fa-check"></i>
<b>16.</b>
Debugging
</a>
</li>
<li class="chapter " data-level="17" data-path="tips.html">
<a href="./tips.html">
<i class="fa fa-check"></i>
<b>17.</b>
Tips
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >Introduction</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<h2 id="예-제-">예 제 )</h2>
<p>예제에서는 특정 상황에 대해서 여러 가지로 처리할 수 있는 방법에 대해 알아보겠습니다.
tail 명령을 이용하는 방법, fork() 을 이용해 멀티코어 cpu 를 활용하는 방법에 대해서도 알아보겠습니다.</p>
<p>아래와 같은 형식의 데이터가 계속 추가되어 하루에 200,000 라인 정도가 발생합니다.
keyfile 은 약 10,000 개의 키값을 가지고 있습니다.
일정 시간 간격으로 datafile 에 있는 key 값을 keyfile 에 있는 값과 비교하여
존재하지 않을 경우 not_exist.txt 파일에 저장하는 것입니다.
keyfile 의 경우 내용이 고정되어 있는 경우와 매번 바뀌는 경우를 알아보겠습니다.</p>
<pre><code class="lang-bash">$ cat datafile.txt $ cat keyfile.txt
<span class="hljs-number">1358726575123</span> <span class="hljs-comment"># key 1358726575123</span>
Joseph Muller <span class="hljs-comment"># name 9973834728345</span>
carpenter <span class="hljs-comment"># job 7836472098652</span>
<span class="hljs-number">5573834728345</span> .....
Andres Smith .....
student
<span class="hljs-number">7836472098652</span>
Mariah Anthony
dentist
.....
.....
</code></pre>
<h3 id="첫-번째">첫 번째</h3>
<p>keyfile 값이 고정되어 있을 경우는 datafile 에 추가되는 내용만 처리하면 되므로
다음과 같이 <code>tail -f</code> 명령과 함께 처리하는 것이 효율적입니다.</p>
<pre><code class="lang-bash">$ cat program.awk
<span class="hljs-comment">#!/usr/bin/awk -f</span>
BEGIN {
keyfile = <span class="hljs-string">"keyfile.txt"</span>
datafile = <span class="hljs-string">"datafile.txt"</span>
not_exist = <span class="hljs-string">"not_exist.txt"</span>
<span class="hljs-comment"># 먼저 keyfile 을 읽어들입니다.</span>
<span class="hljs-keyword">while</span> (( getline < keyfile) > <span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> ( <span class="hljs-variable">$1</span> != <span class="hljs-string">""</span> ) keys[<span class="hljs-variable">$1</span>]
close(keyfile)
RS=<span class="hljs-string">"[0-9]{12,}"</span>
}
{
<span class="hljs-keyword">if</span> ( RT != <span class="hljs-string">""</span> && ! ( RT <span class="hljs-keyword">in</span> keys )) {
<span class="hljs-built_in">print</span> RT <span class="hljs-string">" not exist."</span> >> not_exist
<span class="hljs-comment"># awk 가 종료되지 않고 지속적으로 실행 중에 있는 상태이므로</span>
<span class="hljs-comment"># 실제 파일에 쓰기가 되려면 fflush() 해야 됩니다.</span>
fflush(not_exist)
}
}
</code></pre>
<p>실행은 다음과 같이 합니다.</p>
<pre><code class="lang-bash">$ tail <span class="hljs-operator">-f</span> datafile.txt | ./program.awk
$ ls
datafile.txt keyfile.txt not_exist.txt
$ cat not_exist.txt
<span class="hljs-number">9330232478366</span> not exist.
<span class="hljs-number">3709306406653</span> not exist.
<span class="hljs-number">2607832268163</span> not exist.
.....
</code></pre>
<h3 id="두-번째">두 번째</h3>
<p>두 번째는 keyfile 값이 고정되지 않고 변경되는 경우입니다.
따라서 keyfile 도 매번 다시 읽어들여야 합니다.
변경된 keyfile 값이 전체 datafile 에 다시 적용되야 하므로 첫 번째 경우와 같이 tail 명령을
사용하여 새로 입력되는 데이터만 처리할 수 없습니다.</p>
<p>interval 을 60 초로 하여 매분마다 실행하고
매 실행 시마다 새로 keyfile 과 datafile 을 읽어들여야 하므로 작업 완료 후에는 close() 합니다.
not_exist 파일도 쓰기가 완료되기 위해 close() 합니다.</p>
<pre><code class="lang-bash">$ cat program.awk
<span class="hljs-comment">#!/usr/bin/awk -f</span>
@load <span class="hljs-string">"time"</span>
BEGIN {
keyfile = <span class="hljs-string">"keyfile.txt"</span>
datafile = <span class="hljs-string">"datafile.txt"</span>
not_exist = <span class="hljs-string">"not_exist.txt"</span>
interval = <span class="hljs-number">60</span>
<span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>) {
RS=<span class="hljs-string">"\n"</span>
<span class="hljs-keyword">while</span> (( getline < keyfile) > <span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> ( <span class="hljs-variable">$1</span> != <span class="hljs-string">""</span> ) keys[<span class="hljs-variable">$1</span>]
close(keyfile) <span class="hljs-comment"># 필수</span>
RS=<span class="hljs-string">"[0-9]{12,}"</span>
<span class="hljs-keyword">while</span> (( getline var < datafile) > <span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> ( RT != <span class="hljs-string">""</span> && ! ( RT <span class="hljs-keyword">in</span> keys ))
<span class="hljs-built_in">print</span> RT <span class="hljs-string">" not exist."</span> >> not_exist
close(datafile); close(not_exist) <span class="hljs-comment"># 필수</span>
sleep(interval)
}
}
</code></pre>
<p>하루에 200,000 라인이 생성되므로 x 30 을 해서 6,000,000 라인을 만들어서
while(1) 문을 주석 처리한 후 1 회 실행에 걸린 시간을 측정한 것입니다.</p>
<pre><code class="lang-bash">$ time ./program.awk
real <span class="hljs-number">0</span>m4.<span class="hljs-number">663</span>s
user <span class="hljs-number">0</span>m4.<span class="hljs-number">560</span>s
sys <span class="hljs-number">0</span>m0.<span class="hljs-number">100</span>s
</code></pre>
<h3 id="세-번째">세 번째</h3>
<p>이번에는 fork 을 이용해 cpu core 수만큼 child 프로세스를 생성하여 실행합니다.
먼저 datafile 을 split 한 후에 각각의 파일을 child 프로세스로 처리합니다.
이때는 여러 개의 child 프로세스가 하나의 not_exist 파일에 동시에 쓰기를 하는 상태입니다.
awk 는 기본적으로 4096 bytes 블록 단위로 버퍼링을 하여 쓰기를 하는데 이렇게 되면
끝부분에 위치한 라인이 잘릴 수가 있습니다.
왜냐하면 다음에 다른 프로세스의 블록이 쓰여질수 있기 때문입니다.
따라서 정상적으로 데이터가 쓰여지기 위해서는 fflush() 를 이용해 라인 단위로
쓰기를 해야 합니다.</p>
<pre><code class="lang-bash">$ cat program.awk
<span class="hljs-comment">#!/usr/bin/awk -f</span>
@load <span class="hljs-string">"time"</span>
@load <span class="hljs-string">"fork"</span>
BEGIN {
keyfile = <span class="hljs-string">"keyfile.txt"</span>
datafile = <span class="hljs-string">"datafile.txt"</span>
not_exist = <span class="hljs-string">"not_exist.txt"</span>
interval = <span class="hljs-number">60</span>
core = <span class="hljs-number">4</span> <span class="hljs-comment"># 4 개의 cpu core</span>
<span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>) {
RS=<span class="hljs-string">"\n"</span>
<span class="hljs-keyword">while</span> (( getline < keyfile) > <span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> ( <span class="hljs-variable">$1</span> != <span class="hljs-string">""</span> ) keys[<span class="hljs-variable">$1</span>]
close(keyfile)
RS=<span class="hljs-string">"[0-9]{12,}"</span>
system(<span class="hljs-string">"split -d --additional-suffix=.split -n l/"</span> core <span class="hljs-string">" "</span> datafile)
<span class="hljs-keyword">for</span> ( i=<span class="hljs-number">0</span>; i < core; i++ ) { <span class="hljs-comment"># core 수 만큼 child process 를 생성</span>
<span class="hljs-keyword">if</span> (( pid = fork()) == <span class="hljs-number">0</span>) { <span class="hljs-comment"># child process</span>
datafile = sprintf(<span class="hljs-string">"x%02d.split"</span>,i)
<span class="hljs-keyword">while</span> (( getline var < datafile) > <span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> ( RT != <span class="hljs-string">""</span> && ! ( RT <span class="hljs-keyword">in</span> keys )) {
<span class="hljs-built_in">print</span> RT <span class="hljs-string">" not exist."</span> >> not_exist
fflush(not_exist) <span class="hljs-comment"># 필수</span>
}
<span class="hljs-built_in">exit</span> <span class="hljs-comment"># child process exit</span>
}
}
<span class="hljs-comment"># parent 는 child process 가 모두 종료될 때까지 기다림</span>
<span class="hljs-keyword">for</span> ( i=<span class="hljs-number">0</span>; i < core; i++ ) <span class="hljs-built_in">wait</span>()
system( <span class="hljs-string">"rm -f *.split"</span> )
sleep(interval)
}
}
</code></pre>
<p>두 번째 single process 에 비해 시간이 줄기는 했지만
print 명령으로 write 한 후에 매번 fflush() 를 호출하여
sys time 이 많이 증가된 것을 볼 수 있습니다.
( fflush() 함수는 내부적으로 write() system call 을 사용합니다.)
다음 측정값은 fflush() 를 제거해본 것인데요.
확실히 write 할 때 버퍼를 사용하는 것이 얼마나 효율적이고 시간이 적게 걸리는지 알 수 있습니다.</p>
<pre><code class="lang-bash">$ time ./program.awk <span class="hljs-comment"># fflush() 사용</span>
real <span class="hljs-number">0</span>m3.<span class="hljs-number">427</span>s
user <span class="hljs-number">0</span>m6.<span class="hljs-number">624</span>s
sys <span class="hljs-number">0</span>m4.<span class="hljs-number">324</span>s
$ time ./program.awk <span class="hljs-comment"># fflush() 미사용</span>
real <span class="hljs-number">0</span>m1.<span class="hljs-number">977</span>s
user <span class="hljs-number">0</span>m4.<span class="hljs-number">972</span>s
sys <span class="hljs-number">0</span>m0.<span class="hljs-number">172</span>s
</code></pre>
<blockquote>
<p>여러 개의 코어에서 동시에 하나의 파일에 쓰기를 하면 문제가 되지 않을까 생각할 수 있는데요.
이것은 각 코어별로 직접 디스크에 쓰는 것이 아니라 A 라는 파일에 대해서
시스템 전체에서 공유되는 버퍼가 있다고 생각하면 됩니다.
이것을 화장실에 비유하면 core1 에서 쓰기를 하기위해 화장실에 들어갈 때 팻말을 "사용중" 으로 돌려놓고
들어가면 core2 에서 팻말을 보고 대기하게 됩니다.</p>
</blockquote>
<h4 id="fflush-를-사용하지-않은-경우">fflush() 를 사용하지 않은 경우</h4>
<p>버퍼를 사용하게 되어 효율적이고 속도가 빠르지만
동시에 여러 프로세스가 같은 파일에 쓰기를 할 경우 블록 끝부분에 있는 라인이
잘릴 수가 있습니다.</p>
<p><img src="images/fflush_n.png" alt=""></p>
<p>동시에 여러 프로세스가 같은 파일에 쓰기를 하여 라인이 잘린 경우</p>
<p><img src="images/fflush_n2.png" alt=""></p>
<h4 id="fflush-를-사용한-경우">fflush() 를 사용한 경우</h4>
<p>버퍼를 사용하지 않아 비효율적이고 속도가 느리지만
라인 단위로 쓰여지므로 정상적으로 쓰기가 됩니다.</p>
<p><img src="images/fflush_y.png" alt=""></p>
<h3 id="네-번째">네 번째</h3>
<p>이번에는 여러 프로세스가 동시에 하나의 파일에 쓰기를 하여 fflush() 사용으로 인한
속도 저하를 방지하기 위해 각각의 child 프로세스가 각각의 tmpfile 에 쓰기를 하고 작업이 완료되면
parent 프로세스가 하나로 합칩니다.</p>
<pre><code class="lang-bash">$ cat program.awk
<span class="hljs-comment">#!/usr/bin/awk -f</span>
@load <span class="hljs-string">"time"</span>
@load <span class="hljs-string">"fork"</span>
BEGIN {
keyfile = <span class="hljs-string">"keyfile.txt"</span>
datafile = <span class="hljs-string">"datafile.txt"</span>
not_exist = <span class="hljs-string">"not_exist.txt"</span>
interval = <span class="hljs-number">60</span>
core = <span class="hljs-number">4</span>
<span class="hljs-string">"mktemp -p ."</span> | getline tmpfile
<span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>) {
RS=<span class="hljs-string">"\n"</span>
<span class="hljs-keyword">while</span> (( getline < keyfile) > <span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> ( <span class="hljs-variable">$1</span> != <span class="hljs-string">""</span> ) keys[<span class="hljs-variable">$1</span>]
close(keyfile)
RS=<span class="hljs-string">"[0-9]{12,}"</span>
system(<span class="hljs-string">"split -d --additional-suffix=.split -n l/"</span> core <span class="hljs-string">" "</span> datafile)
<span class="hljs-keyword">for</span> ( i=<span class="hljs-number">0</span>; i < core; i++ ) {
<span class="hljs-keyword">if</span> (( pid = fork()) == <span class="hljs-number">0</span>) { <span class="hljs-comment"># child process</span>
datafile = sprintf(<span class="hljs-string">"x%02d.split"</span>,i)
<span class="hljs-keyword">while</span> (( getline var < datafile) > <span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> ( RT != <span class="hljs-string">""</span> && ! ( RT <span class="hljs-keyword">in</span> keys ))
<span class="hljs-built_in">print</span> RT <span class="hljs-string">" not exist."</span> >> tmpfile i
<span class="hljs-built_in">exit</span>
}
}
cmd = <span class="hljs-string">"cat "</span>
<span class="hljs-keyword">for</span> ( i=<span class="hljs-number">0</span>; i < core; i++ ) {
<span class="hljs-built_in">wait</span>()
cmd = cmd <span class="hljs-string">" "</span> tmpfile i
}
cmd = cmd <span class="hljs-string">" >> "</span> not_exist
cmd = cmd <span class="hljs-string">"; rm -f "</span> tmpfile <span class="hljs-string">"*"</span>
cmd = cmd <span class="hljs-string">"; rm -f *.split"</span>
system( cmd )
sleep(interval)
}
}
</code></pre>
<p>확실히 fflush() 를 사용하지 않으니 시간이 많이 단축되었습니다.</p>
<pre><code class="lang-bash">$ time ./program.awk
real <span class="hljs-number">0</span>m2.<span class="hljs-number">007</span>s
user <span class="hljs-number">0</span>m5.<span class="hljs-number">012</span>s
sys <span class="hljs-number">0</span>m0.<span class="hljs-number">264</span>s
</code></pre>
</section>
</div>
</div>
</div>
<a href="./extensions.html" class="navigation navigation-prev " aria-label="Previous page: Dynamic Extensions"><i class="fa fa-angle-left"></i></a>
<a href="./tcp_ip.html" class="navigation navigation-next " aria-label="Next page: TCP/IP Internetworking"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
<script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>