Print Fibonacci sequence using fork() – fork() 함수와 동작하는 부모 프로세스와 자식 프로세스의 피보나치 수열 리눅스 c 코드입니다.

다음 코드는 여기서 참고를 하였습니다.

https://gist.github.com/devkoriel/b6e5f2c9a59286c90da5c42e4d5d4729

코드는 다음과 같습니다.

 

#include <stdio.h>
#include <sys/types.h>
#include <wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char* argv[])
{
  int arg = atoi(argv[1]); // 시스템 인자를 정수형으로 변환

  long long fib_0 = 0, fib_1 = 1, fib_n;
  pid_t pid, pid_child;
  int status;

  if (argc == 1) { // 시스템 인자가 없을 경우
    printf("%s\n", "Please provide the number of sequence.");
  } else if (arg < 0) { // 시스템 인자가 음수일 경우
    printf("%s\n", "Please provide the positive number of sequence.");
  } else {
    pid = fork(); // fork system call 호출. 프로세스를 생성한다.

    if (pid == -1) { // 프로세스 생성에 실패한 경우
      perror("Fork failed");
      exit(EXIT_FAILURE); // 프로세스 종료
    } else if (pid == 0) { // 프로세스가 성공적으로 생성된 경우
      // 자식 프로세스
      printf("%s\n", "Child forked.");
      printf("%lld, %lld", fib_0, fib_1);

      // 피보나치 수열 출력
      for(register int i = 1; i < arg - 1; i++) {
        fib_n = fib_0 + fib_1;
        fib_0 = fib_1;
        fib_1 = fib_n;
        printf(", %lld", fib_n);
      }

      printf("\n%s\n", "Child ended.");
      _exit(EXIT_SUCCESS); // 자식 프로세스 종료
    } else {
      // 부모 프로세스
      printf("%s\n", "Parent is waiting for the child to complete.");
      pid_child = wait(&status); // 자식 프로세스가 종료될 때까지 대기

      printf("Ended child process id is %d\n", pid_child); // 종료된 자식 프로세스의 ID 출력

      if ((status & 0xFF) == 0) { // 자식 프로세스가 성공적으로 종료된 경우
        printf("Child process ended successfully. Status: %d\n", status >> 8);
      } else { // 자식 프로세스가 성공적으로 종료되지 않은 경우
        printf("Child process ended unsuccessfully. Status: %d\n", status);
      }
    }
  }
  return EXIT_SUCCESS;
}

gcc에서 이 파일을
fibonacci.c  로 저장 하였다면 다음과 같이 컴파일합니다.

 

gcc -o fibonacci fibonacci.c

그러면 인자를 주어 무자비하게 실험을 해보기로 합니다.

^^

 

gildang@gildang-P5Q ~/ccode/clang/780470147627_download $ ./fibonacci 1000
Parent is waiting for the child to complete.
Child forked.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, -6246583658587674878, 1293530146158671551, -4953053512429003327, -3659523366270331776, -8612576878699335103, 6174643828739884737, -2437933049959450366, 3736710778780434371, 1298777728820984005, 5035488507601418376, 6334266236422402381, -7076989329685730859, -742723093263328478, -7819712422949059337, -8562435516212387815, 2064596134548104464, -6497839381664283351, -4433243247116178887, 7515661444929089378, 3082418197812910491, -7848664430967551747, -4766246233154641256, 5831833409587358613, 1065587176432717357, 6897420586020075970, 7963007762452793327, -3586315725236682319, 4376692037216111008, 790376311979428689, 5167068349195539697, 5957444661174968386, -7322231063339043533, -1364786402164075147, -8687017465503118680, 8394940206042357789, -292077259460760891, 8102862946581596898, 7810785687120836007, -2533095440007118711, 5277690247113717296, 2744594807106598585, 8022285054220315881, -7679864212382637150, 342420841837678731, -7337443370544958419, -6995022528707279688, 4114278174457313509, -2880744354249966179, 1233533820207347330, -1647210534042618849, -413676713835271519, -2060887247877890368, -2474563961713161887, -4535451209591052255, -7010015171304214142, 6901277692814285219, -108737478489928923, 6792540214324356296, 6683802735834427373, -4970401123550767947, 1713401612283659426, -3256999511267108521, -1543597898983449095, -4800597410250557616, -6344195309234006711, 7301951354224987289, 957756044990980578, 8259707399215967867, 9217463444206948445, -969573230286635304, 8247890213920313141, 7278316983633677837, -2920536876155560638, 4357780107478117199, 1437243231322556561, 5795023338800673760, 7232266570123230321, -5419454164785647535, 1812812405337582786, -3606641759448064749, -1793829354110481963, -5400471113558546712, -7194300467669028675, 5851972492481976229, -1342327975187052446, 4509644517294923783, 3167316542107871337, 7676961059402795120, -7602466472198885159, 74494587203909961, -7527971884994975198, -7453477297791065237, 3465294890923511181, -3988182406867554056, -522887515944042875, -4511069922811596931, -5033957438755639806, 8901716712142314879, 3867759273386675073, -5677268088180561664, -1809508814793886591, -7486776902974448255, 9150458355941216770, 1663681452966768515, -7632604264801566331, -5968922811834797816, 4845216997073187469, -1123705814761610347, 3721511182311577122, 2597805367549966775, 6319316549861543897, 8917121917411510672, -3210305606436497047, 5706816310975013625, 2496510704538516578, 8203327015513530203, -7746906353657504835, 456420661856025368, -7290485691801479467, -6834065029945454099, 4322193351962618050, -2511871677982836049, 1810321673979782001, -701550004003054048, 1108771669976727953, 407221665973673905, 1515993335950401858, 1923215001924075763, 3439208337874477621, 5362423339798553384, 8801631677673031005, -4282689056237967227, 4518942621435063778, 236253565197096551, 4755196186632160329, 4991449751829256880, -8700098135248134407, -3708648383418877527, 6037997555042539682, 2329349171623662155, 8367346726666201837, -7750048175419687624, 617298551246514213, -7132749624173173411, -6515451072926659198, 4798543376609719007, -1716907696316940191, 3081635680292778816, 1364727983975838625, 4446363664268617441, 5811091648244456066, -8189288761196478109, -2378197112952022043, 7879258199561051464, 5501061086609029421, -5066424787539470731, 434636299069558690, -4631788488469912041, -4197152189400353351, -8828940677870265392, 5420651206438932873, -3408289471431332519, 2012361735007600354, -1395927736423732165, 616433998583868189, -779493737839863976, -163059739255995787, -942553477095859763, -1105613216351855550, -2048166693447715313, -3153779909799570863, -5201946603247286176, -8355726513046857039, 4889070957415408401, -3466655555631448638, 1422415401783959763, -2044240153847488875, -621824752063529112, -2666064905911017987, -3287889657974547099, -5953954563885565086, 9204899851849439431, 3250945287963874345, -5990898933896237840, -2739953645932363495, -8730852579828601335, 6975937847948586786, -1754914731880014549, 5221023116068572237, 3466108384188557688, 8687131500257129925, -6293504189263864003, 2393627310993265922, -3899876878270598081, -1506249567277332159, -5406126445547930240, -6912376012825262399, 6128241615336358977, -784134397488903422, 5344107217847455555, 4559972820358552133, -8542664035503543928, -3982691215144991795, 5921388823061015893, 1938697607916024098, 7860086430977039991, -8647960034816487527, -787873603839447536, 9010910435053616553, 8223036831214169017, -1212796807441766046, 7010240023772402971, 5797443216330636925, -5639060833606511720, 158382382724125205, -5480678450882386515, -5322296068158261310, 7643769554668903791, 2321473486510642481, -8481501032530005344, -6160027546019362863, 3805215495160183409, -2354812050859179454, 1450403444301003955, -904408606558175499, 545994837742828456, -358413768815347043, 187581068927481413, -170832699887865630, 16748369039615783, -154084330848249847, -137335961808634064, -291420292656883911, -428756254465517975, -720176547122401886, -1148932801587919861, -1869109348710321747, -3018042150298241608, -4887151499008563355, -7905193649306804963, 5654398925394183298, -2250794723912621665, 3403604201481561633, 1152809477568939968, 4556413679050501601, 5709223156619441569, -8181107238039608446, -2471884081420166877, 7793752754249776293, 5321868672829609416, -5331122646630165907, -9253973800556491, -5340376620430722398, -5349630594231278889, 7756736859047550329, 2407106264816271440, -8282900949845729847, -5875794685029458407, 4288048438834363362, -1587746246195095045, 2700302192639268317, 1112555946444173272, 3812858139083441589, 4925414085527614861, 8738272224611056450, -4783057763570880305, 3955214461040176145, -827843302530704160, 3127371158509471985, 2299527855978767825, 5426899014488239810, 7726426870467007635, -5293418188754304171, 2433008681712703464, -2860409507041600707, -427400825328897243, -3287810332370497950, -3715211157699395193, -7003021490069893143, 7728511425940263280, 725489935870370137, 8454001361810633417, 9179491297681003554, -813251414217914645, 8366239883463088909, 7552988469245174264, -2527515721001288443, 5025472748243885821, 2497957027242597378, 7523429775486483199, -8425357270980471039, -901927495493987840, 9119459307235092737, 8217531811741104897, -1109752954733353982, 7107778857007750915, 5998025902274396933, -5340939314427403768, 657086587846993165, -4683852726580410603, -4026766138733417438, -8710618865313828041, 5709359069662306137, -3001259795651521904, 2708099274010784233, -293160521640737671, 2414938752370046562, 2121778230729308891, 4536716983099355453, 6658495213828664344, -7251531876781531819, -593036662952867475, -7844568539734399294, -8437605202687266769, 2164570331287885553, -6273034871399381216, -4108464540111495663, 8065244662198674737, 3956780122087179074, -6424719289423697805, -2467939167336518731, -8892658456760216536, 7086146449612816349, -1806512007147400187, 5279634442465416162, 3473122435318015975, 8752756877783432137, -6220864760608103504, 2531892117175328633, -3688972643432774871, -1157080526257446238, -4846053169690221109, -6003133695947667347, 7597557208071663160, 1594423512123995813, 9191980720195658973, -7660339841389896830, 1531640878805762143, -6128698962584134687, -4597058083778372544, 7720987027347044385, 3123928943568671841, -7601828102793835390, -4477899159225163549, 6367016811690552677, 1889117652465389128, 8256134464155941805, -8301491957088220683, -45357492932278878, -8346849450020499561, -8392206942952778439, 1707687680736273616, -6684519262216504823, -4976831581480231207, 6785393230012815586, 1808561648532584379, 8593954878545399965, -8044227546631567272, 549727331913832693, -7494500214717734579, -6944772882803901886, 4007470976187915151, -2937301906615986735, 1070169069571928416, -1867132837044058319, -796963767472129903, -2664096604516188222, -3461060371988318125, -6125156976504506347, 8860526725216727144, 2735369748712220797, -6850847599780603675, -4115477851068382878, 7480418622860565063, 3364940771792182185, -7601384679056804368, -4236443907264622183, 6608915487388125065, 2372471580123502882, 8981387067511627947, -7092885426074420787, 1888501641437207160, -5204383784637213627, -3315882143200006467, -8520265927837220094, 6610596002672325055, -1909669925164895039, 4700926077507430016, 2791256152342534977, 7492182229849964993, -8163305691517051646, -671123461667086653, -8834429153184138299, 8941191458858326664, 106762305674188365, 9047953764532515029, 9154716070206703394, -244074238970333193, 8910641831236370201, 8666567592266037008, -869534650207144407, 7797032942058892601, 6927498291851748194, -3722212839798910821, 3205285452052837373, -516927387746073448, 2688358064306763925, 2171430676560690477, 4859788740867454402, 7031219417428144879, -6555735915413952335, 475483502014192544, -6080252413399759791, -5604768911385567247, 6761722748924224578, 1156953837538657331, 7918676586462881909, 9075630424001539240, -1452437063245130467, 7623193360756408773, 6170756297511278306, -4652794415441864537, 1517961882069413769, -3134832533372450768, -1616870651303036999, -4751703184675487767, -6368573835978524766, 7326467053055539083, 957893217077014317, 8284360270132553400, -9204490586499983899, -920130316367430499, 8322123170842137218, 7401992854474706719, -2722628048392707679, 4679364806081999040, 1956736757689291361, 6636101563771290401, 8592838321460581762, -3217804188477679453, 5375034132982902309, 2157229944505222856, 7532264077488125165, -8757250051716203595, -1224985974228078430, 8464508047765269591, 7239522073537191161, -2742713952407090864, 4496808121130100297, 1754094168723009433, 6250902289853109730, 8004996458576119163, -4190845325280322723, 3814151133295796440, -376694191984526283, 3437456941311270157, 3060762749326743874, 6498219690638014031, -8887761633744793711, -2389541943106779680, 7169440496857978225, 4779898553751198545, -6497405023100374846, -1717506469349176301, -8214911492449551147, 8514326111910824168, 299414619461273021, 8813740731372097189, 9113155350833370210, -519847991504084217, 8593307359329285993, 8073459367825201776, -1779977346555063847, 6293482021270137929, 4513504674715074082, -7639757377724339605, -3126252703009265523, 7680733992975946488, 4554481289966680965, -6211528790766924163, -1657047500800243198, -7868576291567167361, 8921120281342141057, 1052543989774973696, -8473079802592436863, -7420535812817463167, 2553128458299651586, -4867407354517811581, -2314278896218159995, -7181686250735971576, 8950778926755420045, 1769092676019448469, -7726872470934683102, -5957779794915234633, 4762091807859633881, -1195687987055600752, 3566403820804033129, 2370715833748432377, 5937119654552465506, 8307835488300897883, -4201788930856188227, 4106046557444709656, -95742373411478571, 4010304184033231085, 3914561810621752514, 7924865994654983599, -6607316268432815503, 1317549726222168096, -5289766542210647407, -3972216815988479311, 9184760715510424898, 5212543899521945587, -4049439458677181131, 1163104440844764456, -2886335017832416675, -1723230576987652219, -4609565594820068894, -6332796171807721113, 7504382307081761609, 1171586135274040496, 8675968442355802105, -8599189496079709015, 76778946276093090, -8522410549803615925, -8445631603527522835, 1478701920378412856, -6966929683149109979, -5488227762770697123, 5991586627789744514, 503358865019047391, 6494945492808791905, 6998304357827839296, -4953494223072920415, 2044810134754918881, -2908684088318001534, -863873953563082653, -3772558041881084187, -4636431995444166840, -8408990037325251027, 5401322040940133749, -3007667996385117278, 2393654044555016471, -614013951830100807, 1779640092724915664, 1165626140894814857, 2945266233619730521, 4110892374514545378, 7056158608134275899, -7279693091060730339, -223534482926454440, -7503227573987184779, -7726762056913639219, 3216754442808727618, -4510007614104911601, -1293253171296183983, -5803260785401095584, -7096513956697279567, 5546969331611176465, -1549544625086103102, 3997424706525073363, 2447880081438970261, 6445304787964043624, 8893184869403013885, -3108254416342494107, 5784930453060519778, 2676676036718025671, 8461606489778545449, -7308461547212980496, 1153144942565564953, -6155316604647415543, -5002171662081850590, 7289255806980285483, 2287084144898434893, -8870404121830831240, -6583319976932396347, 2993019974946324029, -3590300001986072318, -597280027039748289, -4187580029025820607, -4784860056065568896, -8972440085091389503, 4689443932552593217, -4282996152538796286, 406447780013796931, -3876548372524999355, -3470100592511202424, -7346648965036201779, 7629994516162147413, 283345551125945634, 7913340067288093047, 8196685618414038681, -2336718388007419888, 5859967230406618793, 3523248842399198905, -9063528000903733918, -5540279158504535013, 3842936914301282685, -1697342244203252328, 2145594670098030357, 448252425894778029, 2593847095992808386, 3042099521887586415, 5635946617880394801, 8678046139767981216, -4132751316061175599, 4545294823706805617, 412543507645630018, 4957838331352435635, 5370381838998065653, -8118523903359050328, -2748142064360984675, 7580078105989516613, 4831936041628531938, -6034729926091503065, -1202793884462971127, -7237523810554474192, -8440317695017445319, 2768902568137632105, -5671415126879813214, -2902512558742181109, -8573927685621994323, 6970303829345376184, -1603623856276618139, 5366679973068758045, 3763056116792139906, 9129736089860897951, -5553951867056513759, 3575784222804384192, -1978167644252129567, 1597616578552254625, -380551065699874942, 1217065512852379683, 836514447152504741, 2053579960004884424, 2890094407157389165, 4943674367162273589, 7833768774319662754, -5669300932227615273, 2164467842092047481, -3504833090135567792, -1340365248043520311, -4845198338179088103, -6185563586222608414, 7415982149307855099, 1230418563085246685, 8646400712393101784, -8569924798231203147, 76475914161898637, -8493448884069304510, -8416972969907405873, 1536322219732841233, -6880650750174564640, -5344328530441723407, 6221764793093263569, 877436262651540162, 7099201055744803731, 7976637318396343893, -3370905699568403992, 4605731618827939901, 1234825919259535909, 5840557538087475810, 7075383457347011719, -5530803078275064087, 1544580379071947632, -3986222699203116455, -2441642320131168823, -6427865019334285278, -8869507339465454101, 3149371714909812237, -5720135624555641864, -2570763909645829627, -8290899534201471491, 7585080629862250498, -705818904339220993, 6879261725523029505, 6173442821183808512, -5394039527002713599, 779403294181094913, -4614636232821618686, -3835232938640523773, -8449869171462142459, 6161641963606885384, -2288227207855257075, 3873414755751628309, 1585187547896371234, 5458602303647999543, 7043789851544370777, -5944351918517181296, 1099437933027189481, -4844913985489991815, -3745476052462802334, -8590390037952794149, 6110877983293955133, -2479512054658839016, 3631365928635116117, 1151853873976277101, 4783219802611393218, 5935073676587670319, -7728450594510488079, -1793376917922817760, 8924916561276245777, 7131539643353428017, -2390287869079877822, 4741251774273550195, 2350963905193672373, 7092215679467222568, -9003564489048656675, -1911348809581434107, 7531830775079460834, 5620481965498026727, -5294431333132064055, 326050632365962672, -4968380700766101383, -4642330068400138711, 8836033304543311522, 4193703236143172811, -5417007533023067283, -1223304296879894472, -6640311829902961755, -7863616126782856227, 3942816117023733634, -3920800009759122593, 22016107264611041, -3898783902494511552, -3876767795229900511, -7775551697724412063, 6794424580755239042, -981127116969173021, 5813297463786066021, 4832170346816893000, -7801276263106592595, -2969105916289699595, 7676361894313259426, 4707255978023559831, -6063126201372732359, -1355870223349172528, -7418996424721904887, -8774866648071077415, 2252881000916569314, -6521985647154508101, -4269104646237938787, 7655653780317104728, 3386549134079165941, -7404541159313280947, -4017992025234115006, 7024210889162155663, 3006218863928040657, -8416314320619355296, -5410095456691314639, 4620334296398881681, -789761160292432958, 3830573136106448723, 3040811975814015765, 6871385111920464488, -8534546985975071363, -1663161874054606875, 8249035213679873378, 6585873339625266503, -3611835520404411735, 2974037819220854768, -637797701183556967, 2336240118037297801, 1698442416853740834, 4034682534891038635, 5733124951744779469, -8678936587073733512, -2945811635328954043, 6821995851306864061, 3876184215977910018, -7748564006424777537, -3872379790446867519, 6825800276837906560, 2953420486391039041, -8667523310480606015, -5714102824089566974, 4065117939139378627, -1648984884950188347, 2416133054189190280, 767148169239001933, 3183281223428192213, 3950429392667194146, 7133710616095386359, -7362604064946971111, -228893448851584752, -7591497513798555863, -7820390962650140615, 3034855597260855138, -4785535365389285477, -1750679768128430339, -6536215133517715816, -8286894901646146155, 3623634038545689645, -4663260863100456510, -1039626824554766865, -5702887687655223375, -6742514512209990240, 6001341873844338001, -741172638365652239, 5260169235478685762, 4518996597113033523, -8667578241117832331, -4148581644004798808, 5630584188586920477, 1482002544582121669, 7112586733169042146, 8594589277751163815, -2739568062789345655, 5855021214961818160, 3115453152172472505, 8970474367134290665, -6360816554402788446, 2609657812731502219, -3751158741671286227, -1141500928939784008, -4892659670611070235, -6034160599550854243, 7519923803547627138, 1485763203996772895, 9005687007544400033, -7955293862168378688, 1050393145376021345, -6904900716792357343, -5854507571416335998, 5687335785500858275, -167171785915477723, 5520163999585380552, 5352992213669902829, -7573587860454268235, -2220595646784365406, 8652560566470917975, 6431964919686552569, -3362218587552081072, 3069746332134471497, -292472255417609575, 2777274076716861922, 2484801821299252347, 5262075898016114269, 7746877719315366616, -5437790456378070731, 2309087262937295885, -3128703193440774846, -819615930503478961, -3948319123944253807, -4767935054447732768, -8716254178391986575, 4962554840869832273, -3753699337522154302, 1208855503347677971, -2544843834174476331, -1335988330826798360, -3880832165001274691, -5216820495828073051, -9097652660829347742, 4132270917052130823, -4965381743777216919, -833110826725086096, -5798492570502303015, -6631603397227389111, 6016648105979859490, -614955291247529621, 5401692814732329869, 4786737523484800248, -8258313735492421499, -3471576212007621251, 6716854126209508866, 3245277914201887615, -8484612033298155135, -5239334119096267520, 4722797921315128961, -516536197781138559, 4206261723533990402, 3689725525752851843, 7895987249286842245, -6861031298669857528, 1034955950616984717, -5826075348052872811, -4791119397435888094, 7829549328220790711, 3038429930784902617, -7578764814703858288, -4540334883918955671, 6327644375086737657, 1787309491167781986, 8114953866254519643, -8544480716287249987, -429526850032730344, -8974007566319980331, 9043209657356840941, 69202091036860610, 9112411748393701551, 9181613839430562161, -152718485885287904, 9028895353545274257, 8876176867659986353, -541671852504291006, 8334505015155695347, 7792833162651404341, -2319405895902451928, 5473427266748952413, 3154021370846500485, 8627448637595452898, -6665274065267598233, 1962174572327854665, -4703099492939743568, -2740924920611888903, -7444024413551632471, 8261794739546030242
Child ended.
Ended child process id is 29317
Child process ended successfully. Status: 0
gildang@gildang-P5Q ~/ccode/clang/780470147627_download $ ./fibonacci 10
Parent is waiting for the child to complete.
Child forked.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Child ended.
Ended child process id is 29501
Child process ended successfully. Status: 0

 

감사합니다. ^^

 

 

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

This site uses Akismet to reduce spam. Learn how your comment data is processed.