├── .gitignore ├── LICENSE ├── README.md ├── cw01 ├── report.txt ├── results │ ├── results_dynamic_O.txt │ ├── results_dynamic_O0.txt │ ├── results_dynamic_O1.txt │ ├── results_dynamic_O2.txt │ ├── results_dynamic_O3.txt │ ├── results_dynamic_Os.txt │ ├── results_shared_O.txt │ ├── results_shared_O0.txt │ ├── results_shared_O1.txt │ ├── results_shared_O2.txt │ ├── results_shared_O3.txt │ ├── results_shared_Os.txt │ ├── results_static_O.txt │ ├── results_static_O0.txt │ ├── results_static_O1.txt │ ├── results_static_O2.txt │ ├── results_static_O3.txt │ └── results_static_Os.txt ├── zad1 │ ├── Makefile │ ├── libwc.c │ ├── libwc.h │ └── libwc_so.h ├── zad2 │ ├── Makefile │ ├── dllmanager.h │ ├── libwc.c │ ├── libwc.h │ ├── libwc_so.h │ └── wc_repl.c ├── zad3a │ ├── Makefile │ ├── dllmanager.h │ ├── libwc.c │ ├── libwc.h │ ├── libwc_so.h │ └── wc_repl.c └── zad3b │ ├── Makefile │ ├── dllmanager.h │ ├── libwc.c │ ├── libwc.h │ ├── libwc_so.h │ └── wc_repl.c ├── cw02 ├── report.txt ├── zad1 │ ├── Makefile │ ├── pomiar_zad_1_std.txt │ ├── pomiar_zad_1_sys.txt │ └── replace.c ├── zad2 │ ├── Makefile │ ├── pomiar_zad_2_1.txt │ ├── pomiar_zad_2_1024.txt │ └── reverse.c ├── zad3 │ ├── Makefile │ └── dir_size.c └── zad4 │ ├── Makefile │ └── tree_size.c ├── cw03 ├── report.txt ├── zad1 │ ├── Makefile │ └── spawn.c ├── zad2 │ ├── Makefile │ └── lspp.c └── zad3 │ ├── Makefile │ └── rec.c ├── cw04 ├── report.txt ├── zad1 │ ├── Makefile │ ├── exec.txt │ ├── fork.txt │ ├── report.txt │ └── signals.c ├── zad2 │ ├── Makefile │ ├── report.txt │ └── sigtest.c └── zad3 │ ├── Makefile │ ├── catcher.c │ └── sender.c ├── cw05 ├── report.txt ├── zad1 │ ├── Makefile │ └── fortune_cowsay.c ├── zad2 │ ├── Makefile │ ├── integral.c │ └── report.txt └── zad3 │ ├── Makefile │ ├── integral.c │ ├── report.txt │ └── worker.c ├── cw06 ├── report.txt ├── zad1 │ ├── Makefile │ ├── client.c │ ├── common.h │ └── server.c └── zad2 │ ├── Makefile │ ├── client.c │ ├── common.h │ └── server.c ├── cw07 ├── report.txt ├── zad1 │ ├── Makefile │ ├── barber.c │ ├── client.c │ ├── common.h │ ├── queue.c │ ├── queue.h │ ├── sem.c │ ├── sem.h │ ├── shared_memory.c │ ├── shared_memory.h │ └── simulation.c └── zad2 │ ├── Makefile │ ├── barber.c │ ├── client.c │ ├── common.h │ ├── queue.c │ ├── queue.h │ ├── sem.c │ ├── sem.h │ ├── shared_memory.c │ ├── shared_memory.h │ └── simulation.c ├── cw08 ├── report.txt ├── zad1 │ ├── Makefile │ ├── grid.c │ ├── grid.h │ └── life.c └── zad2 │ ├── Makefile │ ├── grid.c │ ├── grid.h │ └── life.c ├── cw09 ├── report.txt └── zad1 │ ├── Makefile │ └── santa.c └── cw10 ├── report.txt ├── zad1 ├── Makefile ├── client.c ├── common.h ├── message.h └── server.c └── zad2 ├── Makefile ├── client.c ├── common.h ├── message.h └── server.c /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | *.exe 3 | *.o 4 | *.a 5 | *.so 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | There never was much hope 2 | 3 | Just a fool's hope 4 | -------------------------------------------------------------------------------- /cw01/report.txt: -------------------------------------------------------------------------------- 1 | Zad 2, 3a i 3b zawiera ten sam kod (finalna wersje rozwiazujaca wszystkie na raz) 2 | Zrobione 100% 3 | Rezultaty są w "results/" 4 | Statyczna biblioteka jest najszybsza, dlopen i linkowana na początku porównywalne 5 | Optymalizacja dużo zmienia (zwlaszcza przeskok 2->3) 6 | "make report" tworzy rezultaty 7 | 8 | 🤠 9 | -------------------------------------------------------------------------------- /cw01/results/results_dynamic_O.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 8660ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 5098778ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2581622ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2706712ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2887675ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2594613ns 23 | USER: 0ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2845421ns 27 | USER: 1ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2448710ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 2375165ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 3140369ns 39 | USER: 1ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 6083570ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 6055214ns 47 | USER: 0ticks 48 | SYST: 1ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 5676383ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>>  45 105 1246 dllmanager.h 54 | 55 | EXECUTION TIME: 56 | REAL: 4750ns 57 | USER: 0ticks 58 | SYST: 0ticks 59 | [WC REPL] >>>  34 207 18720 wc_repl_dlopen.exe 60 | 61 | EXECUTION TIME: 62 | REAL: 2374ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>>  30 184 18408 wc_repl_shared.exe 66 | 67 | EXECUTION TIME: 68 | REAL: 2375ns 69 | USER: 0ticks 70 | SYST: 0ticks 71 | [WC REPL] >>> EXECUTION TIME: 72 | REAL: 3841ns 73 | USER: 0ticks 74 | SYST: 0ticks 75 | [WC REPL] >>> EXECUTION TIME: 76 | REAL: 4331ns 77 | USER: 0ticks 78 | SYST: 0ticks 79 | [WC REPL] >>> EXECUTION TIME: 80 | REAL: 1885ns 81 | USER: 0ticks 82 | SYST: 0ticks 83 | -------------------------------------------------------------------------------- /cw01/results/results_dynamic_O0.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 9010ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4771771ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2559063ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2725569ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2830194ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2709924ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2950465ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2552008ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 5177491ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 2617452ns 39 | USER: 0ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 2704127ns 43 | USER: 1ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2471689ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2537690ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2459885ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2711391ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2461072ns 63 | USER: 1ticks 64 | SYST: 0ticks 65 | [WC REPL] >>>  45 105 1246 dllmanager.h 66 | 67 | EXECUTION TIME: 68 | REAL: 5238ns 69 | USER: 0ticks 70 | SYST: 0ticks 71 | [WC REPL] >>>  82 224 1699 results_dynamic_O.txt 72 | 73 | EXECUTION TIME: 74 | REAL: 1467ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>>  78 | EXECUTION TIME: 79 | REAL: 4749ns 80 | USER: 0ticks 81 | SYST: 0ticks 82 | [WC REPL] >>> EXECUTION TIME: 83 | REAL: 3841ns 84 | USER: 0ticks 85 | SYST: 0ticks 86 | [WC REPL] >>> EXECUTION TIME: 87 | REAL: 4680ns 88 | USER: 0ticks 89 | SYST: 0ticks 90 | [WC REPL] >>> EXECUTION TIME: 91 | REAL: 1956ns 92 | USER: 0ticks 93 | SYST: 0ticks 94 | -------------------------------------------------------------------------------- /cw01/results/results_dynamic_O1.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 10616ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4885756ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2421540ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2581412ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2599641ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2281574ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2376981ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2169197ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4898747ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 2539297ns 39 | USER: 1ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 2229751ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 3233889ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2967227ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2370206ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2515620ns 59 | USER: 1ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2193432ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2384105ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2540204ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2311258ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>>  45 105 1246 dllmanager.h 78 | 79 | EXECUTION TIME: 80 | REAL: 4749ns 81 | USER: 0ticks 82 | SYST: 0ticks 83 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 84 | 85 | EXECUTION TIME: 86 | REAL: 2375ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>>  90 | EXECUTION TIME: 91 | REAL: 4400ns 92 | USER: 0ticks 93 | SYST: 0ticks 94 | [WC REPL] >>> EXECUTION TIME: 95 | REAL: 3423ns 96 | USER: 0ticks 97 | SYST: 0ticks 98 | [WC REPL] >>> EXECUTION TIME: 99 | REAL: 3841ns 100 | USER: 0ticks 101 | SYST: 0ticks 102 | [WC REPL] >>> EXECUTION TIME: 103 | REAL: 2375ns 104 | USER: 0ticks 105 | SYST: 0ticks 106 | -------------------------------------------------------------------------------- /cw01/results/results_dynamic_O2.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 10965ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4557771ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2544885ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2877060ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2151177ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 3087497ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2214735ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2094953ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4292226ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 5239164ns 39 | USER: 0ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 2474482ns 43 | USER: 1ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2104800ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2082451ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2210684ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2351906ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2613960ns 63 | USER: 1ticks 64 | SYST: 0ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2746243ns 67 | USER: 0ticks 68 | SYST: 1ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2236736ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2087829ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>> EXECUTION TIME: 78 | REAL: 2648462ns 79 | USER: 0ticks 80 | SYST: 0ticks 81 | [WC REPL] >>> EXECUTION TIME: 82 | REAL: 2499975ns 83 | USER: 1ticks 84 | SYST: 0ticks 85 | [WC REPL] >>> EXECUTION TIME: 86 | REAL: 2498438ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>>  45 105 1246 dllmanager.h 90 | 91 | EXECUTION TIME: 92 | REAL: 4191ns 93 | USER: 0ticks 94 | SYST: 0ticks 95 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 96 | 97 | EXECUTION TIME: 98 | REAL: 1886ns 99 | USER: 0ticks 100 | SYST: 0ticks 101 | [WC REPL] >>>  105 286 2174 results_dynamic_O1.txt 102 | 103 | EXECUTION TIME: 104 | REAL: 2304ns 105 | USER: 0ticks 106 | SYST: 0ticks 107 | [WC REPL] >>> EXECUTION TIME: 108 | REAL: 3771ns 109 | USER: 0ticks 110 | SYST: 0ticks 111 | [WC REPL] >>> EXECUTION TIME: 112 | REAL: 4750ns 113 | USER: 0ticks 114 | SYST: 0ticks 115 | [WC REPL] >>> EXECUTION TIME: 116 | REAL: 1956ns 117 | USER: 0ticks 118 | SYST: 0ticks 119 | -------------------------------------------------------------------------------- /cw01/results/results_dynamic_O3.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 10895ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 5116448ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2190778ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2751621ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2725499ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2612632ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2784936ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2299105ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4210650ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 5643487ns 39 | USER: 1ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 4438339ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2155228ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2061079ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2070997ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2113532ns 59 | USER: 1ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2129805ns 63 | USER: 0ticks 64 | SYST: 1ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 1825078ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2493759ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2559971ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>> EXECUTION TIME: 78 | REAL: 2302598ns 79 | USER: 0ticks 80 | SYST: 0ticks 81 | [WC REPL] >>> EXECUTION TIME: 82 | REAL: 2044387ns 83 | USER: 0ticks 84 | SYST: 0ticks 85 | [WC REPL] >>> EXECUTION TIME: 86 | REAL: 2461422ns 87 | USER: 1ticks 88 | SYST: 0ticks 89 | [WC REPL] >>> EXECUTION TIME: 90 | REAL: 2293867ns 91 | USER: 0ticks 92 | SYST: 0ticks 93 | [WC REPL] >>> EXECUTION TIME: 94 | REAL: 1969304ns 95 | USER: 0ticks 96 | SYST: 0ticks 97 | [WC REPL] >>> EXECUTION TIME: 98 | REAL: 2410645ns 99 | USER: 0ticks 100 | SYST: 0ticks 101 | [WC REPL] >>>  45 105 1246 dllmanager.h 102 | 103 | EXECUTION TIME: 104 | REAL: 5169ns 105 | USER: 0ticks 106 | SYST: 0ticks 107 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 108 | 109 | EXECUTION TIME: 110 | REAL: 1886ns 111 | USER: 0ticks 112 | SYST: 0ticks 113 | [WC REPL] >>>  105 286 2174 results_dynamic_O1.txt 114 | 115 | EXECUTION TIME: 116 | REAL: 1886ns 117 | USER: 0ticks 118 | SYST: 0ticks 119 | [WC REPL] >>> EXECUTION TIME: 120 | REAL: 3842ns 121 | USER: 0ticks 122 | SYST: 0ticks 123 | [WC REPL] >>> EXECUTION TIME: 124 | REAL: 4330ns 125 | USER: 0ticks 126 | SYST: 0ticks 127 | [WC REPL] >>> EXECUTION TIME: 128 | REAL: 1885ns 129 | USER: 0ticks 130 | SYST: 0ticks 131 | -------------------------------------------------------------------------------- /cw01/results/results_dynamic_Os.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 10965ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 5652776ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2198810ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2772224ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2447453ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2682476ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2242322ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2806937ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 5397358ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 5191041ns 39 | USER: 1ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 4765206ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 5693705ns 47 | USER: 1ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2425941ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2287651ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2391439ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2219694ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2338916ns 67 | USER: 0ticks 68 | SYST: 1ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 1990397ns 71 | USER: 1ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2293378ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>> EXECUTION TIME: 78 | REAL: 2121703ns 79 | USER: 0ticks 80 | SYST: 0ticks 81 | [WC REPL] >>> EXECUTION TIME: 82 | REAL: 2112554ns 83 | USER: 0ticks 84 | SYST: 0ticks 85 | [WC REPL] >>> EXECUTION TIME: 86 | REAL: 2091391ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>> EXECUTION TIME: 90 | REAL: 2425941ns 91 | USER: 1ticks 92 | SYST: 0ticks 93 | [WC REPL] >>> EXECUTION TIME: 94 | REAL: 2620665ns 95 | USER: 0ticks 96 | SYST: 0ticks 97 | [WC REPL] >>> EXECUTION TIME: 98 | REAL: 1728414ns 99 | USER: 0ticks 100 | SYST: 0ticks 101 | [WC REPL] >>> EXECUTION TIME: 102 | REAL: 2498508ns 103 | USER: 0ticks 104 | SYST: 0ticks 105 | [WC REPL] >>> EXECUTION TIME: 106 | REAL: 2184562ns 107 | USER: 0ticks 108 | SYST: 0ticks 109 | [WC REPL] >>> EXECUTION TIME: 110 | REAL: 2437814ns 111 | USER: 0ticks 112 | SYST: 0ticks 113 | [WC REPL] >>>  45 105 1246 dllmanager.h 114 | 115 | EXECUTION TIME: 116 | REAL: 4749ns 117 | USER: 0ticks 118 | SYST: 0ticks 119 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 120 | 121 | EXECUTION TIME: 122 | REAL: 2375ns 123 | USER: 0ticks 124 | SYST: 0ticks 125 | [WC REPL] >>>  105 286 2174 results_dynamic_O1.txt 126 | 127 | EXECUTION TIME: 128 | REAL: 1886ns 129 | USER: 0ticks 130 | SYST: 0ticks 131 | [WC REPL] >>> EXECUTION TIME: 132 | REAL: 3282ns 133 | USER: 0ticks 134 | SYST: 0ticks 135 | [WC REPL] >>> EXECUTION TIME: 136 | REAL: 3911ns 137 | USER: 0ticks 138 | SYST: 0ticks 139 | [WC REPL] >>> EXECUTION TIME: 140 | REAL: 978ns 141 | USER: 0ticks 142 | SYST: 0ticks 143 | -------------------------------------------------------------------------------- /cw01/results/results_shared_O.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 8590ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4410750ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2855059ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2448291ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2760561ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2777812ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2826283ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2918616ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 2483422ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 2847795ns 39 | USER: 0ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 5249640ns 43 | USER: 1ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 5713191ns 47 | USER: 0ticks 48 | SYST: 1ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 5862795ns 51 | USER: 1ticks 52 | SYST: 0ticks 53 | [WC REPL] >>>  45 105 1246 dllmanager.h 54 | 55 | EXECUTION TIME: 56 | REAL: 4820ns 57 | USER: 0ticks 58 | SYST: 0ticks 59 | [WC REPL] >>>  34 207 18720 wc_repl_dlopen.exe 60 | 61 | EXECUTION TIME: 62 | REAL: 2375ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>>  30 184 18408 wc_repl_shared.exe 66 | 67 | EXECUTION TIME: 68 | REAL: 2375ns 69 | USER: 0ticks 70 | SYST: 0ticks 71 | [WC REPL] >>> EXECUTION TIME: 72 | REAL: 3912ns 73 | USER: 0ticks 74 | SYST: 0ticks 75 | [WC REPL] >>> EXECUTION TIME: 76 | REAL: 4260ns 77 | USER: 0ticks 78 | SYST: 0ticks 79 | [WC REPL] >>> EXECUTION TIME: 80 | REAL: 1886ns 81 | USER: 0ticks 82 | SYST: 0ticks 83 | -------------------------------------------------------------------------------- /cw01/results/results_shared_O0.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 12013ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4863336ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 3127657ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2867771ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2143844ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2257967ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 1603954ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 1446526ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4212745ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 1581045ns 39 | USER: 0ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 2174924ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2158021ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2126941ns 51 | USER: 1ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2052977ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2193991ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2151596ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>>  45 105 1246 dllmanager.h 66 | 67 | EXECUTION TIME: 68 | REAL: 4820ns 69 | USER: 0ticks 70 | SYST: 0ticks 71 | [WC REPL] >>>  82 224 1699 results_dynamic_O.txt 72 | 73 | EXECUTION TIME: 74 | REAL: 2305ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>>  78 | EXECUTION TIME: 79 | REAL: 4400ns 80 | USER: 0ticks 81 | SYST: 0ticks 82 | [WC REPL] >>> EXECUTION TIME: 83 | REAL: 2934ns 84 | USER: 0ticks 85 | SYST: 0ticks 86 | [WC REPL] >>> EXECUTION TIME: 87 | REAL: 4330ns 88 | USER: 0ticks 89 | SYST: 0ticks 90 | [WC REPL] >>> EXECUTION TIME: 91 | REAL: 2375ns 92 | USER: 0ticks 93 | SYST: 0ticks 94 | -------------------------------------------------------------------------------- /cw01/results/results_shared_O1.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 7613ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4248854ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2277035ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2710413ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2243789ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2294147ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2833338ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2284159ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4566222ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 2122681ns 39 | USER: 0ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 2003458ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2133995ns 47 | USER: 1ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2908140ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2267676ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2186448ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2879085ns 63 | USER: 0ticks 64 | SYST: 1ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2145869ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2570587ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2821464ns 75 | USER: 1ticks 76 | SYST: 0ticks 77 | [WC REPL] >>>  45 105 1246 dllmanager.h 78 | 79 | EXECUTION TIME: 80 | REAL: 5658ns 81 | USER: 0ticks 82 | SYST: 0ticks 83 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 84 | 85 | EXECUTION TIME: 86 | REAL: 2305ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>>  90 | EXECUTION TIME: 91 | REAL: 5308ns 92 | USER: 0ticks 93 | SYST: 0ticks 94 | [WC REPL] >>> EXECUTION TIME: 95 | REAL: 4260ns 96 | USER: 0ticks 97 | SYST: 0ticks 98 | [WC REPL] >>> EXECUTION TIME: 99 | REAL: 5169ns 100 | USER: 0ticks 101 | SYST: 0ticks 102 | [WC REPL] >>> EXECUTION TIME: 103 | REAL: 2374ns 104 | USER: 0ticks 105 | SYST: 0ticks 106 | -------------------------------------------------------------------------------- /cw01/results/results_shared_O2.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 8102ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4724557ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2929303ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2819998ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2653700ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2542928ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2594613ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2279549ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4686353ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 4828135ns 39 | USER: 0ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 2324738ns 43 | USER: 1ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2663338ns 47 | USER: 0ticks 48 | SYST: 1ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2138536ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2565558ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2378796ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2620245ns 63 | USER: 1ticks 64 | SYST: 0ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2725848ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2647903ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2353234ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>> EXECUTION TIME: 78 | REAL: 2792968ns 79 | USER: 0ticks 80 | SYST: 0ticks 81 | [WC REPL] >>> EXECUTION TIME: 82 | REAL: 2524909ns 83 | USER: 1ticks 84 | SYST: 0ticks 85 | [WC REPL] >>> EXECUTION TIME: 86 | REAL: 2341779ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>>  45 105 1246 dllmanager.h 90 | 91 | EXECUTION TIME: 92 | REAL: 4679ns 93 | USER: 0ticks 94 | SYST: 0ticks 95 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 96 | 97 | EXECUTION TIME: 98 | REAL: 1886ns 99 | USER: 0ticks 100 | SYST: 0ticks 101 | [WC REPL] >>>  105 286 2174 results_dynamic_O1.txt 102 | 103 | EXECUTION TIME: 104 | REAL: 1815ns 105 | USER: 0ticks 106 | SYST: 0ticks 107 | [WC REPL] >>> EXECUTION TIME: 108 | REAL: 3841ns 109 | USER: 0ticks 110 | SYST: 0ticks 111 | [WC REPL] >>> EXECUTION TIME: 112 | REAL: 3772ns 113 | USER: 0ticks 114 | SYST: 0ticks 115 | [WC REPL] >>> EXECUTION TIME: 116 | REAL: 2374ns 117 | USER: 0ticks 118 | SYST: 0ticks 119 | -------------------------------------------------------------------------------- /cw01/results/results_shared_O3.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 8032ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4434009ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2157812ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2257478ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2724521ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2393533ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2269911ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2296661ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4938697ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 5068746ns 39 | USER: 1ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 4908874ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2291911ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2365945ns 51 | USER: 1ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2208309ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2617870ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2231846ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2167660ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2917638ns 71 | USER: 1ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2609071ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>> EXECUTION TIME: 78 | REAL: 2449757ns 79 | USER: 0ticks 80 | SYST: 0ticks 81 | [WC REPL] >>> EXECUTION TIME: 82 | REAL: 2307207ns 83 | USER: 0ticks 84 | SYST: 0ticks 85 | [WC REPL] >>> EXECUTION TIME: 86 | REAL: 2591749ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>> EXECUTION TIME: 90 | REAL: 2446824ns 91 | USER: 0ticks 92 | SYST: 0ticks 93 | [WC REPL] >>> EXECUTION TIME: 94 | REAL: 2718165ns 95 | USER: 0ticks 96 | SYST: 1ticks 97 | [WC REPL] >>> EXECUTION TIME: 98 | REAL: 2496832ns 99 | USER: 1ticks 100 | SYST: 0ticks 101 | [WC REPL] >>>  45 105 1246 dllmanager.h 102 | 103 | EXECUTION TIME: 104 | REAL: 4680ns 105 | USER: 0ticks 106 | SYST: 0ticks 107 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 108 | 109 | EXECUTION TIME: 110 | REAL: 2444ns 111 | USER: 0ticks 112 | SYST: 0ticks 113 | [WC REPL] >>>  105 286 2174 results_dynamic_O1.txt 114 | 115 | EXECUTION TIME: 116 | REAL: 2375ns 117 | USER: 0ticks 118 | SYST: 0ticks 119 | [WC REPL] >>> EXECUTION TIME: 120 | REAL: 3772ns 121 | USER: 0ticks 122 | SYST: 0ticks 123 | [WC REPL] >>> EXECUTION TIME: 124 | REAL: 3772ns 125 | USER: 0ticks 126 | SYST: 0ticks 127 | [WC REPL] >>> EXECUTION TIME: 128 | REAL: 2374ns 129 | USER: 0ticks 130 | SYST: 0ticks 131 | -------------------------------------------------------------------------------- /cw01/results/results_shared_Os.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 9429ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 5137052ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2419446ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2829426ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2945086ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2698539ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2694838ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2284787ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4744183ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 4735382ns 39 | USER: 1ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 5699292ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 4433240ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2048647ns 51 | USER: 1ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2194480ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2522046ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2726687ns 63 | USER: 0ticks 64 | SYST: 1ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2782421ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2288489ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2470780ns 75 | USER: 1ticks 76 | SYST: 0ticks 77 | [WC REPL] >>> EXECUTION TIME: 78 | REAL: 2367761ns 79 | USER: 0ticks 80 | SYST: 0ticks 81 | [WC REPL] >>> EXECUTION TIME: 82 | REAL: 2513664ns 83 | USER: 0ticks 84 | SYST: 0ticks 85 | [WC REPL] >>> EXECUTION TIME: 86 | REAL: 2548516ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>> EXECUTION TIME: 90 | REAL: 2414068ns 91 | USER: 1ticks 92 | SYST: 0ticks 93 | [WC REPL] >>> EXECUTION TIME: 94 | REAL: 2473574ns 95 | USER: 0ticks 96 | SYST: 0ticks 97 | [WC REPL] >>> EXECUTION TIME: 98 | REAL: 2597826ns 99 | USER: 0ticks 100 | SYST: 0ticks 101 | [WC REPL] >>> EXECUTION TIME: 102 | REAL: 2123938ns 103 | USER: 0ticks 104 | SYST: 0ticks 105 | [WC REPL] >>> EXECUTION TIME: 106 | REAL: 2598104ns 107 | USER: 0ticks 108 | SYST: 0ticks 109 | [WC REPL] >>> EXECUTION TIME: 110 | REAL: 2470920ns 111 | USER: 0ticks 112 | SYST: 0ticks 113 | [WC REPL] >>>  45 105 1246 dllmanager.h 114 | 115 | EXECUTION TIME: 116 | REAL: 6286ns 117 | USER: 0ticks 118 | SYST: 0ticks 119 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 120 | 121 | EXECUTION TIME: 122 | REAL: 2793ns 123 | USER: 0ticks 124 | SYST: 0ticks 125 | [WC REPL] >>>  105 286 2174 results_dynamic_O1.txt 126 | 127 | EXECUTION TIME: 128 | REAL: 2305ns 129 | USER: 0ticks 130 | SYST: 0ticks 131 | [WC REPL] >>> EXECUTION TIME: 132 | REAL: 4680ns 133 | USER: 0ticks 134 | SYST: 0ticks 135 | [WC REPL] >>> EXECUTION TIME: 136 | REAL: 4679ns 137 | USER: 0ticks 138 | SYST: 0ticks 139 | [WC REPL] >>> EXECUTION TIME: 140 | REAL: 2375ns 141 | USER: 0ticks 142 | SYST: 0ticks 143 | -------------------------------------------------------------------------------- /cw01/results/results_static_O.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 6774ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4885266ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2605369ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2536223ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2545582ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2443262ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2378238ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2369158ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 2239180ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 2400728ns 39 | USER: 0ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 5277787ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 5369491ns 47 | USER: 1ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 5566449ns 51 | USER: 0ticks 52 | SYST: 1ticks 53 | [WC REPL] >>>  45 105 1246 dllmanager.h 54 | 55 | EXECUTION TIME: 56 | REAL: 2445ns 57 | USER: 0ticks 58 | SYST: 0ticks 59 | [WC REPL] >>>  34 207 18720 wc_repl_dlopen.exe 60 | 61 | EXECUTION TIME: 62 | REAL: 2375ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>>  30 184 18408 wc_repl_shared.exe 66 | 67 | EXECUTION TIME: 68 | REAL: 1955ns 69 | USER: 0ticks 70 | SYST: 0ticks 71 | [WC REPL] >>> EXECUTION TIME: 72 | REAL: 1955ns 73 | USER: 0ticks 74 | SYST: 0ticks 75 | [WC REPL] >>> EXECUTION TIME: 76 | REAL: 2794ns 77 | USER: 0ticks 78 | SYST: 0ticks 79 | [WC REPL] >>> EXECUTION TIME: 80 | REAL: 1885ns 81 | USER: 0ticks 82 | SYST: 0ticks 83 | -------------------------------------------------------------------------------- /cw01/results/results_static_O0.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 9359ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 5938296ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2583437ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2550821ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2185820ns 19 | USER: 1ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2262578ns 23 | USER: 0ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2207122ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2586721ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4057344ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 2357425ns 39 | USER: 0ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 2456253ns 43 | USER: 1ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2616962ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 1903652ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2032374ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2227376ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2005344ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>>  45 105 1246 dllmanager.h 66 | 67 | EXECUTION TIME: 68 | REAL: 3352ns 69 | USER: 0ticks 70 | SYST: 0ticks 71 | [WC REPL] >>>  82 224 1699 results_dynamic_O.txt 72 | 73 | EXECUTION TIME: 74 | REAL: 2444ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>>  78 | EXECUTION TIME: 79 | REAL: 2934ns 80 | USER: 0ticks 81 | SYST: 0ticks 82 | [WC REPL] >>> EXECUTION TIME: 83 | REAL: 2445ns 84 | USER: 0ticks 85 | SYST: 0ticks 86 | [WC REPL] >>> EXECUTION TIME: 87 | REAL: 2934ns 88 | USER: 0ticks 89 | SYST: 0ticks 90 | [WC REPL] >>> EXECUTION TIME: 91 | REAL: 2304ns 92 | USER: 0ticks 93 | SYST: 0ticks 94 | -------------------------------------------------------------------------------- /cw01/results/results_static_O1.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 9080ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 3359398ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 1579020ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 1855600ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2419026ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2399261ns 23 | USER: 0ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2119328ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2055212ns 31 | USER: 1ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4248923ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 2461700ns 39 | USER: 0ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 2163539ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2014773ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 1974404ns 51 | USER: 1ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2038030ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2129525ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2035656ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 1983832ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2324179ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2247211ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>>  45 105 1246 dllmanager.h 78 | 79 | EXECUTION TIME: 80 | REAL: 2793ns 81 | USER: 0ticks 82 | SYST: 0ticks 83 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 84 | 85 | EXECUTION TIME: 86 | REAL: 1956ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>>  90 | EXECUTION TIME: 91 | REAL: 2794ns 92 | USER: 0ticks 93 | SYST: 0ticks 94 | [WC REPL] >>> EXECUTION TIME: 95 | REAL: 2305ns 96 | USER: 0ticks 97 | SYST: 0ticks 98 | [WC REPL] >>> EXECUTION TIME: 99 | REAL: 2864ns 100 | USER: 0ticks 101 | SYST: 0ticks 102 | [WC REPL] >>> EXECUTION TIME: 103 | REAL: 1397ns 104 | USER: 0ticks 105 | SYST: 0ticks 106 | -------------------------------------------------------------------------------- /cw01/results/results_static_O2.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 7892ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 5416006ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2269911ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2461282ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2531195ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2696374ns 23 | USER: 0ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2673675ns 27 | USER: 1ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2524700ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4787765ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 4723161ns 39 | USER: 1ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 2592657ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2209845ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2447662ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2319639ns 55 | USER: 0ticks 56 | SYST: 1ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2918686ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2443960ns 63 | USER: 1ticks 64 | SYST: 0ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2866094ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2583996ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2346250ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>> EXECUTION TIME: 78 | REAL: 2542858ns 79 | USER: 0ticks 80 | SYST: 0ticks 81 | [WC REPL] >>> EXECUTION TIME: 82 | REAL: 2498369ns 83 | USER: 1ticks 84 | SYST: 0ticks 85 | [WC REPL] >>> EXECUTION TIME: 86 | REAL: 2397374ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>>  45 105 1246 dllmanager.h 90 | 91 | EXECUTION TIME: 92 | REAL: 2933ns 93 | USER: 0ticks 94 | SYST: 0ticks 95 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 96 | 97 | EXECUTION TIME: 98 | REAL: 2444ns 99 | USER: 0ticks 100 | SYST: 0ticks 101 | [WC REPL] >>>  105 286 2174 results_dynamic_O1.txt 102 | 103 | EXECUTION TIME: 104 | REAL: 2793ns 105 | USER: 0ticks 106 | SYST: 0ticks 107 | [WC REPL] >>> EXECUTION TIME: 108 | REAL: 2445ns 109 | USER: 0ticks 110 | SYST: 0ticks 111 | [WC REPL] >>> EXECUTION TIME: 112 | REAL: 3353ns 113 | USER: 0ticks 114 | SYST: 0ticks 115 | [WC REPL] >>> EXECUTION TIME: 116 | REAL: 2794ns 117 | USER: 0ticks 118 | SYST: 0ticks 119 | -------------------------------------------------------------------------------- /cw01/results/results_static_O3.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 9499ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 5373682ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2539436ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2427827ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2941734ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2719353ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2613121ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2652093ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4627196ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 4397760ns 39 | USER: 1ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 4643399ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 2205445ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 2601527ns 51 | USER: 0ticks 52 | SYST: 0ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2296801ns 55 | USER: 1ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2583159ns 59 | USER: 0ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 2076724ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2687226ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2410017ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 3001800ns 75 | USER: 1ticks 76 | SYST: 1ticks 77 | [WC REPL] >>> EXECUTION TIME: 78 | REAL: 3023032ns 79 | USER: 0ticks 80 | SYST: 0ticks 81 | [WC REPL] >>> EXECUTION TIME: 82 | REAL: 2439560ns 83 | USER: 0ticks 84 | SYST: 0ticks 85 | [WC REPL] >>> EXECUTION TIME: 86 | REAL: 2376352ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>> EXECUTION TIME: 90 | REAL: 2199159ns 91 | USER: 0ticks 92 | SYST: 0ticks 93 | [WC REPL] >>> EXECUTION TIME: 94 | REAL: 2558783ns 95 | USER: 0ticks 96 | SYST: 0ticks 97 | [WC REPL] >>> EXECUTION TIME: 98 | REAL: 2607533ns 99 | USER: 0ticks 100 | SYST: 0ticks 101 | [WC REPL] >>>  45 105 1246 dllmanager.h 102 | 103 | EXECUTION TIME: 104 | REAL: 2794ns 105 | USER: 0ticks 106 | SYST: 0ticks 107 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 108 | 109 | EXECUTION TIME: 110 | REAL: 2445ns 111 | USER: 0ticks 112 | SYST: 0ticks 113 | [WC REPL] >>>  105 286 2174 results_dynamic_O1.txt 114 | 115 | EXECUTION TIME: 116 | REAL: 1886ns 117 | USER: 0ticks 118 | SYST: 0ticks 119 | [WC REPL] >>> EXECUTION TIME: 120 | REAL: 2445ns 121 | USER: 0ticks 122 | SYST: 0ticks 123 | [WC REPL] >>> EXECUTION TIME: 124 | REAL: 2863ns 125 | USER: 0ticks 126 | SYST: 0ticks 127 | [WC REPL] >>> EXECUTION TIME: 128 | REAL: 1886ns 129 | USER: 0ticks 130 | SYST: 0ticks 131 | -------------------------------------------------------------------------------- /cw01/results/results_static_Os.txt: -------------------------------------------------------------------------------- 1 | [WC REPL] >>> EXECUTION TIME: 2 | REAL: 8451ns 3 | USER: 0ticks 4 | SYST: 0ticks 5 | [WC REPL] >>> EXECUTION TIME: 6 | REAL: 4862219ns 7 | USER: 0ticks 8 | SYST: 0ticks 9 | [WC REPL] >>> EXECUTION TIME: 10 | REAL: 2644132ns 11 | USER: 0ticks 12 | SYST: 0ticks 13 | [WC REPL] >>> EXECUTION TIME: 14 | REAL: 2230798ns 15 | USER: 0ticks 16 | SYST: 0ticks 17 | [WC REPL] >>> EXECUTION TIME: 18 | REAL: 2630931ns 19 | USER: 0ticks 20 | SYST: 0ticks 21 | [WC REPL] >>> EXECUTION TIME: 22 | REAL: 2739887ns 23 | USER: 1ticks 24 | SYST: 0ticks 25 | [WC REPL] >>> EXECUTION TIME: 26 | REAL: 2372790ns 27 | USER: 0ticks 28 | SYST: 0ticks 29 | [WC REPL] >>> EXECUTION TIME: 30 | REAL: 2865395ns 31 | USER: 0ticks 32 | SYST: 0ticks 33 | [WC REPL] >>> EXECUTION TIME: 34 | REAL: 4677902ns 35 | USER: 0ticks 36 | SYST: 0ticks 37 | [WC REPL] >>> EXECUTION TIME: 38 | REAL: 4908036ns 39 | USER: 1ticks 40 | SYST: 0ticks 41 | [WC REPL] >>> EXECUTION TIME: 42 | REAL: 4386376ns 43 | USER: 0ticks 44 | SYST: 0ticks 45 | [WC REPL] >>> EXECUTION TIME: 46 | REAL: 4420739ns 47 | USER: 0ticks 48 | SYST: 0ticks 49 | [WC REPL] >>> EXECUTION TIME: 50 | REAL: 1775978ns 51 | USER: 0ticks 52 | SYST: 1ticks 53 | [WC REPL] >>> EXECUTION TIME: 54 | REAL: 2164378ns 55 | USER: 0ticks 56 | SYST: 0ticks 57 | [WC REPL] >>> EXECUTION TIME: 58 | REAL: 2269632ns 59 | USER: 1ticks 60 | SYST: 0ticks 61 | [WC REPL] >>> EXECUTION TIME: 62 | REAL: 1937945ns 63 | USER: 0ticks 64 | SYST: 0ticks 65 | [WC REPL] >>> EXECUTION TIME: 66 | REAL: 2162771ns 67 | USER: 0ticks 68 | SYST: 0ticks 69 | [WC REPL] >>> EXECUTION TIME: 70 | REAL: 2473435ns 71 | USER: 0ticks 72 | SYST: 0ticks 73 | [WC REPL] >>> EXECUTION TIME: 74 | REAL: 2445427ns 75 | USER: 0ticks 76 | SYST: 0ticks 77 | [WC REPL] >>> EXECUTION TIME: 78 | REAL: 2894032ns 79 | USER: 1ticks 80 | SYST: 0ticks 81 | [WC REPL] >>> EXECUTION TIME: 82 | REAL: 2234570ns 83 | USER: 0ticks 84 | SYST: 0ticks 85 | [WC REPL] >>> EXECUTION TIME: 86 | REAL: 2840043ns 87 | USER: 0ticks 88 | SYST: 0ticks 89 | [WC REPL] >>> EXECUTION TIME: 90 | REAL: 2501511ns 91 | USER: 0ticks 92 | SYST: 0ticks 93 | [WC REPL] >>> EXECUTION TIME: 94 | REAL: 2204677ns 95 | USER: 0ticks 96 | SYST: 0ticks 97 | [WC REPL] >>> EXECUTION TIME: 98 | REAL: 2105849ns 99 | USER: 1ticks 100 | SYST: 0ticks 101 | [WC REPL] >>> EXECUTION TIME: 102 | REAL: 2435020ns 103 | USER: 0ticks 104 | SYST: 0ticks 105 | [WC REPL] >>> EXECUTION TIME: 106 | REAL: 2070997ns 107 | USER: 0ticks 108 | SYST: 0ticks 109 | [WC REPL] >>> EXECUTION TIME: 110 | REAL: 2640989ns 111 | USER: 0ticks 112 | SYST: 0ticks 113 | [WC REPL] >>>  45 105 1246 dllmanager.h 114 | 115 | EXECUTION TIME: 116 | REAL: 2374ns 117 | USER: 0ticks 118 | SYST: 0ticks 119 | [WC REPL] >>>  93 253 1917 results_dynamic_O0.txt 120 | 121 | EXECUTION TIME: 122 | REAL: 1885ns 123 | USER: 0ticks 124 | SYST: 0ticks 125 | [WC REPL] >>>  105 286 2174 results_dynamic_O1.txt 126 | 127 | EXECUTION TIME: 128 | REAL: 2375ns 129 | USER: 0ticks 130 | SYST: 0ticks 131 | [WC REPL] >>> EXECUTION TIME: 132 | REAL: 2864ns 133 | USER: 0ticks 134 | SYST: 0ticks 135 | [WC REPL] >>> EXECUTION TIME: 136 | REAL: 2864ns 137 | USER: 0ticks 138 | SYST: 0ticks 139 | [WC REPL] >>> EXECUTION TIME: 140 | REAL: 2445ns 141 | USER: 0ticks 142 | SYST: 0ticks 143 | -------------------------------------------------------------------------------- /cw01/zad1/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall 3 | LIB=libwc 4 | 5 | all: 6 | make lib_static 7 | make lib_shared 8 | 9 | lib_static: $(LIB).c 10 | $(CC) $(CFLAGS) -c $(LIB).c 11 | ar cr $(LIB).a $(LIB).o 12 | 13 | lib_shared: $(LIB).c 14 | $(CC) $(CFLAGS) -c -fPIC $(LIB).c 15 | $(CC) $(CFLAGS) -shared -o $(LIB).so $(LIB).o 16 | 17 | clean: 18 | rm -f *.o *.gch *.a *.so -------------------------------------------------------------------------------- /cw01/zad1/libwc.c: -------------------------------------------------------------------------------- 1 | #include "libwc.h" 2 | 3 | char command[LIBWC_COMMAND_BUFF_SIZE] = ""; 4 | 5 | LibWCMemory LibWCMemory_create(size_t size) { 6 | return (LibWCMemory) { 7 | .arr = calloc(size, sizeof(char*)), 8 | .active = calloc(size, sizeof(bool)), 9 | .top = 0, 10 | .size = size, 11 | }; 12 | } 13 | 14 | void LibWCMemory_init(LibWCMemory* LibWCMemory, size_t size) { 15 | LibWCMemory->arr = calloc(size, sizeof(char*)); 16 | LibWCMemory->active = calloc(size, sizeof(bool)); 17 | LibWCMemory->top = 0; 18 | LibWCMemory->size = size; 19 | } 20 | 21 | void LibWCMemory_clear(LibWCMemory* LibWCMemory) { 22 | for (size_t i = 0; i < LibWCMemory->top; i++) 23 | if (LibWCMemory->active[i]) 24 | free(LibWCMemory->arr[i]); 25 | LibWCMemory->top = 0; 26 | memset(LibWCMemory->active, false, sizeof(bool)*(LibWCMemory->size)); 27 | } 28 | 29 | void LibWCMemory_destruct(LibWCMemory* LibWCMemory) { 30 | LibWCMemory_clear(LibWCMemory); 31 | free(LibWCMemory->arr); 32 | free(LibWCMemory->active); 33 | } 34 | 35 | bool LibWCMemory_range_check(LibWCMemory* LibWCMemory, size_t index) { 36 | if (LibWCMemory->top <= index) { 37 | fprintf(stderr, "[LIB WC] INDEX OUT OF RANGE\n"); 38 | return false; 39 | } 40 | if (!LibWCMemory->active[index]) { 41 | fprintf(stderr, "[LIB WC] INDEX ALREADY REMOVED\n"); 42 | return false; 43 | } 44 | return true; 45 | } 46 | 47 | char* LibWCMemory_get(LibWCMemory* LibWCMemory, size_t index) { 48 | if (LibWCMemory_range_check(LibWCMemory, index)) 49 | return LibWCMemory->arr[index]; 50 | return ""; 51 | } 52 | 53 | void LibWCMemory_pop(LibWCMemory* LibWCMemory, size_t index) { 54 | if (LibWCMemory_range_check(LibWCMemory, index)) { 55 | free(LibWCMemory->arr[index]); 56 | LibWCMemory->active[index] = false; 57 | } 58 | } 59 | 60 | long get_file_size(FILE* file) { 61 | fseek(file, 0, SEEK_END); 62 | long size = ftell(file); 63 | fseek(file, 0, SEEK_SET); 64 | return size; 65 | } 66 | 67 | char* get_file_content(char* filename) { 68 | FILE* file = fopen(filename, "r"); 69 | size_t size = get_file_size(file); 70 | char* buff = calloc(size, sizeof(char)); 71 | fread(buff, sizeof(char), size, file); 72 | fclose(file); 73 | return buff; 74 | } 75 | 76 | void LibWCMemory_push(LibWCMemory* LibWCMemory, char* input_filename) { 77 | char tmp_filename[] = "/tmp/wclib_XXXXXX"; 78 | int tmp_file = mkstemp(tmp_filename); 79 | 80 | if (tmp_file == 0) { 81 | fprintf(stderr, "[LIB WC] FAILED TO CREATE A TEMPORARY FILE\n"); 82 | return; 83 | } 84 | 85 | snprintf(command, LIBWC_COMMAND_BUFF_SIZE, "wc '%s' 1> '%s' 2>/dev/null", input_filename, tmp_filename); 86 | system(command); 87 | 88 | char* wc_output = get_file_content(tmp_filename); 89 | if (strlen(wc_output) == 0) { 90 | fprintf(stderr, "[LIB WC] FAILED TO READ THE INPUT FILE (%s)\n", input_filename); 91 | return; 92 | } 93 | 94 | snprintf(command, LIBWC_COMMAND_BUFF_SIZE, "rm -f '%s' 2>/dev/null", tmp_filename); 95 | system(command); 96 | 97 | if (LibWCMemory->top < LibWCMemory->size) { 98 | LibWCMemory->arr[LibWCMemory->top] = wc_output; 99 | LibWCMemory->active[LibWCMemory->top] = true; 100 | (LibWCMemory->top)++; 101 | } else { 102 | fprintf(stderr, "[LIB WC] NOT ENOUGH MEMORY\n"); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /cw01/zad1/libwc.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBWC_H__ 2 | #define __LIBWC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define LIBWC_COMMAND_BUFF_SIZE 2048 10 | 11 | typedef struct { 12 | char** arr; 13 | bool* active; 14 | size_t top; 15 | size_t size; 16 | } LibWCMemory; 17 | 18 | LibWCMemory LibWCMemory_create(size_t size); 19 | 20 | void LibWCMemory_init(LibWCMemory* LibWCMemory, size_t size); 21 | 22 | void LibWCMemory_clear(LibWCMemory* LibWCMemory); 23 | 24 | void LibWCMemory_destruct(LibWCMemory* LibWCMemory); 25 | 26 | char* LibWCMemory_get(LibWCMemory* LibWCMemory, size_t index); 27 | 28 | void LibWCMemory_pop(LibWCMemory* LibWCMemory, size_t index); 29 | 30 | void LibWCMemory_push(LibWCMemory* LibWCMemory, char* input_filename); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /cw01/zad1/libwc_so.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBWC_H__ 2 | #define __LIBWC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define LIBWC_COMMAND_BUFF_SIZE 2048 10 | 11 | typedef struct { 12 | char** arr; 13 | bool* active; 14 | size_t top; 15 | size_t size; 16 | } LibWCMemory; 17 | 18 | LibWCMemory (*LibWCMemory_create)(size_t); 19 | void (*LibWCMemory_init)(LibWCMemory*, size_t); 20 | void (*LibWCMemory_clear)(LibWCMemory*); 21 | void (*LibWCMemory_destruct)(LibWCMemory*); 22 | char* (*LibWCMemory_get)(LibWCMemory*, size_t); 23 | void (*LibWCMemory_pop)(LibWCMemory*, size_t); 24 | void (*LibWCMemory_push)(LibWCMemory*, char* ); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /cw01/zad2/Makefile: -------------------------------------------------------------------------------- 1 | SHELL=/bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall 4 | LIB=libwc 5 | REPL=wc_repl.c 6 | BINS=wc_repl.c dllmanager.h 7 | 8 | all: 9 | make lib_static 10 | make lib_shared 11 | make exe_static 12 | make exe_shared 13 | make exe_dlopen 14 | 15 | o: 16 | make all OPT_FLAGS="-O" 17 | 18 | o0: 19 | make all OPT_FLAGS="-O0" 20 | 21 | o1: 22 | make all OPT_FLAGS="-O1" 23 | 24 | o2: 25 | make all OPT_FLAGS="-O2" 26 | 27 | o3: 28 | make all OPT_FLAGS="-O3" 29 | 30 | os: 31 | make all OPT_FLAGS="-Os" 32 | 33 | test: 34 | sudo mv libwc.so /usr/lib 35 | make test_create 36 | ./wc_repl_static.exe < test.in > results_static_$(O_SUFFIX).txt 2>/dev/null 37 | ./wc_repl_shared.exe < test.in > results_shared_$(O_SUFFIX).txt 2>/dev/null 38 | ./wc_repl_dlopen.exe < test.in > results_dynamic_$(O_SUFFIX).txt 2>/dev/null 39 | 40 | test_create: 41 | echo "init 1000" > test.in 42 | 43 | (ls -p | grep -v / | sed 's/^/count /') >> test.in 44 | 45 | echo "show 0" >> test.in 46 | echo "show 1" >> test.in 47 | echo "show 2" >> test.in 48 | 49 | echo "delete 1" >> test.in 50 | echo "destroy" >> test.in 51 | echo "exit" >> test.in 52 | 53 | report: 54 | sudo ls > /dev/null 55 | make o 56 | make test O_SUFFIX="O" 57 | make o0 58 | make test O_SUFFIX="O0" 59 | make o1 60 | make test O_SUFFIX="O1" 61 | make o2 62 | make test O_SUFFIX="O2" 63 | make o3 64 | make test O_SUFFIX="O3" 65 | make os 66 | make test O_SUFFIX="Os" 67 | make o3 68 | rm -f test.in 69 | 70 | lib_static: $(LIB).c 71 | $(CC) $(CFLAGS) $(OPT_FLAGS) -c $(LIB).c 72 | ar cr $(LIB)$(SUFFIX).a $(LIB).o 73 | 74 | lib_shared: $(LIB).c 75 | $(CC) $(CFLAGS) $(OPT_FLAGS) -c -fPIC $(LIB).c 76 | $(CC) $(CFLAGS) $(OPT_FLAGS) -shared -o $(LIB).so $(LIB).o 77 | 78 | exe_static: $(LIB).c $(BINS) 79 | $(CC) $(CFLAGS) $(OPT_FLAGS) $(REPL) -o wc_repl_static.exe -L . $(LIB).a 80 | 81 | exe_shared: $(LIB).c $(BINS) 82 | $(CC) $(CFLAGS) $(OPT_FLAGS) $(REPL) -o wc_repl_shared.exe -L . $(LIB).so 83 | 84 | exe_dlopen: $(LIB).c $(BINS) 85 | $(CC) $(CFLAGS) $(OPT_FLAGS) $(REPL) -o wc_repl_dlopen.exe -ldl -D USE_DLL 86 | 87 | clean: 88 | rm -f *.o *.gch *.a *.so *.exe test.in *.txt -------------------------------------------------------------------------------- /cw01/zad2/dllmanager.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLLMANAGER_H__ 2 | #define __DLLMANAGER_H__ 3 | 4 | #ifdef USE_DLL 5 | #include 6 | #include 7 | #include 8 | #define WCLIB_SYMBOL_TABLE_SIZE 7 9 | void* _dll_ptrs[WCLIB_SYMBOL_TABLE_SIZE]; 10 | 11 | char* _dll_symbol_names[] = { 12 | "LibWCMemory", 13 | "LibWCMemory_init", 14 | "LibWCMemory_clear", 15 | "LibWCMemory_destruct", 16 | "LibWCMemory_get", 17 | "LibWCMemory_pop", 18 | "LibWCMemory_push", 19 | }; 20 | 21 | void load_dll_symbols(const char* filepath) { 22 | void *handle = dlopen(filepath, RTLD_LAZY); 23 | 24 | if (handle == NULL) { 25 | fprintf(stderr, "[DLL MANAGER] DLL NOT FOUND (%s)\n", filepath); 26 | return; 27 | } 28 | 29 | // rapidly dwindling sanity of C developers 30 | *(void **) (&LibWCMemory_create) = dlsym(handle,"LibWCMemory_create"); 31 | *(void **) (&LibWCMemory_init) = dlsym(handle,"LibWCMemory_init"); 32 | *(void **) (&LibWCMemory_clear) = dlsym(handle,"LibWCMemory_clear"); 33 | *(void **) (&LibWCMemory_destruct) = dlsym(handle,"LibWCMemory_destruct"); 34 | *(void **) (&LibWCMemory_get) = dlsym(handle,"LibWCMemory_get"); 35 | *(void **) (&LibWCMemory_pop) = dlsym(handle,"LibWCMemory_pop"); 36 | *(void **) (&LibWCMemory_push) = dlsym(handle,"LibWCMemory_push"); 37 | } 38 | 39 | #else 40 | void load_dll_symbols(const char* filepath) {} 41 | #endif 42 | 43 | #endif 44 | 45 | 46 | -------------------------------------------------------------------------------- /cw01/zad2/libwc.c: -------------------------------------------------------------------------------- 1 | #include "libwc.h" 2 | 3 | char command[LIBWC_COMMAND_BUFF_SIZE] = ""; 4 | int exit_code; 5 | 6 | LibWCMemory LibWCMemory_create(size_t size) { 7 | return (LibWCMemory) { 8 | .arr = calloc(size, sizeof(char*)), 9 | .active = calloc(size, sizeof(bool)), 10 | .top = 0, 11 | .size = size, 12 | }; 13 | } 14 | 15 | void LibWCMemory_init(LibWCMemory* LibWCMemory, size_t size) { 16 | LibWCMemory->arr = calloc(size, sizeof(char*)); 17 | LibWCMemory->active = calloc(size, sizeof(bool)); 18 | LibWCMemory->top = 0; 19 | LibWCMemory->size = size; 20 | } 21 | 22 | void LibWCMemory_clear(LibWCMemory* LibWCMemory) { 23 | for (size_t i = 0; i < LibWCMemory->top; i++) 24 | if (LibWCMemory->active[i]) 25 | free(LibWCMemory->arr[i]); 26 | LibWCMemory->top = 0; 27 | memset(LibWCMemory->active, false, sizeof(bool)*(LibWCMemory->size)); 28 | } 29 | 30 | void LibWCMemory_destruct(LibWCMemory* LibWCMemory) { 31 | LibWCMemory_clear(LibWCMemory); 32 | free(LibWCMemory->arr); 33 | free(LibWCMemory->active); 34 | } 35 | 36 | bool LibWCMemory_range_check(LibWCMemory* LibWCMemory, size_t index) { 37 | if (LibWCMemory->top <= index) { 38 | fprintf(stderr, "[LIB WC] INDEX OUT OF RANGE\n"); 39 | return false; 40 | } 41 | if (!LibWCMemory->active[index]) { 42 | fprintf(stderr, "[LIB WC] INDEX ALREADY REMOVED\n"); 43 | return false; 44 | } 45 | return true; 46 | } 47 | 48 | char* LibWCMemory_get(LibWCMemory* LibWCMemory, size_t index) { 49 | if (LibWCMemory_range_check(LibWCMemory, index)) 50 | return LibWCMemory->arr[index]; 51 | return ""; 52 | } 53 | 54 | void LibWCMemory_pop(LibWCMemory* LibWCMemory, size_t index) { 55 | if (LibWCMemory_range_check(LibWCMemory, index)) { 56 | free(LibWCMemory->arr[index]); 57 | LibWCMemory->active[index] = false; 58 | } 59 | } 60 | 61 | long get_file_size(FILE* file) { 62 | fseek(file, 0, SEEK_END); 63 | long size = ftell(file); 64 | fseek(file, 0, SEEK_SET); 65 | return size; 66 | } 67 | 68 | char* get_file_content(char* filename) { 69 | FILE* file = fopen(filename, "r"); 70 | size_t size = get_file_size(file); 71 | char* buff = calloc(size, sizeof(char)); 72 | exit_code = fread(buff, sizeof(char), size, file); 73 | exit_code = fclose(file); 74 | return buff; 75 | } 76 | 77 | void LibWCMemory_push(LibWCMemory* LibWCMemory, char* input_filename) { 78 | char tmp_filename[] = "/tmp/wclib_XXXXXX"; 79 | int tmp_file = mkstemp(tmp_filename); 80 | 81 | if (tmp_file == 0) { 82 | fprintf(stderr, "[LIB WC] FAILED TO CREATE A TEMPORARY FILE\n"); 83 | return; 84 | } 85 | 86 | snprintf(command, LIBWC_COMMAND_BUFF_SIZE, "wc '%s' 1> '%s' 2>/dev/null", input_filename, tmp_filename); 87 | exit_code = system(command); 88 | 89 | char* wc_output = get_file_content(tmp_filename); 90 | if (strlen(wc_output) == 0) { 91 | fprintf(stderr, "[LIB WC] FAILED TO READ THE INPUT FILE (%s)\n", input_filename); 92 | return; 93 | } 94 | 95 | snprintf(command, LIBWC_COMMAND_BUFF_SIZE, "rm -f '%s' 2>/dev/null", tmp_filename); 96 | exit_code = system(command); 97 | 98 | if (LibWCMemory->top < LibWCMemory->size) { 99 | LibWCMemory->arr[LibWCMemory->top] = wc_output; 100 | LibWCMemory->active[LibWCMemory->top] = true; 101 | (LibWCMemory->top)++; 102 | } else { 103 | fprintf(stderr, "[LIB WC] NOT ENOUGH MEMORY\n"); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /cw01/zad2/libwc.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBWC_H__ 2 | #define __LIBWC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define LIBWC_COMMAND_BUFF_SIZE 2048 10 | 11 | typedef struct { 12 | char** arr; 13 | bool* active; 14 | size_t top; 15 | size_t size; 16 | } LibWCMemory; 17 | 18 | LibWCMemory LibWCMemory_create(size_t size); 19 | 20 | void LibWCMemory_init(LibWCMemory* LibWCMemory, size_t size); 21 | 22 | void LibWCMemory_clear(LibWCMemory* LibWCMemory); 23 | 24 | void LibWCMemory_destruct(LibWCMemory* LibWCMemory); 25 | 26 | char* LibWCMemory_get(LibWCMemory* LibWCMemory, size_t index); 27 | 28 | void LibWCMemory_pop(LibWCMemory* LibWCMemory, size_t index); 29 | 30 | void LibWCMemory_push(LibWCMemory* LibWCMemory, char* input_filename); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /cw01/zad2/libwc_so.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBWC_H__ 2 | #define __LIBWC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define LIBWC_COMMAND_BUFF_SIZE 2048 10 | 11 | typedef struct { 12 | char** arr; 13 | bool* active; 14 | size_t top; 15 | size_t size; 16 | } LibWCMemory; 17 | 18 | LibWCMemory (*LibWCMemory_create)(size_t); 19 | void (*LibWCMemory_init)(LibWCMemory*, size_t); 20 | void (*LibWCMemory_clear)(LibWCMemory*); 21 | void (*LibWCMemory_destruct)(LibWCMemory*); 22 | char* (*LibWCMemory_get)(LibWCMemory*, size_t); 23 | void (*LibWCMemory_pop)(LibWCMemory*, size_t); 24 | void (*LibWCMemory_push)(LibWCMemory*, char* ); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /cw01/zad3a/Makefile: -------------------------------------------------------------------------------- 1 | SHELL=/bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall 4 | LIB=libwc 5 | REPL=wc_repl.c 6 | BINS=wc_repl.c dllmanager.h 7 | 8 | all: 9 | make lib_static 10 | make lib_shared 11 | make exe_static 12 | make exe_shared 13 | make exe_dlopen 14 | 15 | o: 16 | make all OPT_FLAGS="-O" 17 | 18 | o0: 19 | make all OPT_FLAGS="-O0" 20 | 21 | o1: 22 | make all OPT_FLAGS="-O1" 23 | 24 | o2: 25 | make all OPT_FLAGS="-O2" 26 | 27 | o3: 28 | make all OPT_FLAGS="-O3" 29 | 30 | os: 31 | make all OPT_FLAGS="-Os" 32 | 33 | test: 34 | sudo mv libwc.so /usr/lib 35 | make test_create 36 | ./wc_repl_static.exe < test.in > results_static_$(O_SUFFIX).txt 2>/dev/null 37 | ./wc_repl_shared.exe < test.in > results_shared_$(O_SUFFIX).txt 2>/dev/null 38 | ./wc_repl_dlopen.exe < test.in > results_dynamic_$(O_SUFFIX).txt 2>/dev/null 39 | 40 | test_create: 41 | echo "init 1000" > test.in 42 | 43 | (ls -p | grep -v / | sed 's/^/count /') >> test.in 44 | 45 | echo "show 0" >> test.in 46 | echo "show 1" >> test.in 47 | echo "show 2" >> test.in 48 | 49 | echo "delete 1" >> test.in 50 | echo "destroy" >> test.in 51 | echo "exit" >> test.in 52 | 53 | report: 54 | sudo ls > /dev/null 55 | make o 56 | make test O_SUFFIX="O" 57 | make o0 58 | make test O_SUFFIX="O0" 59 | make o1 60 | make test O_SUFFIX="O1" 61 | make o2 62 | make test O_SUFFIX="O2" 63 | make o3 64 | make test O_SUFFIX="O3" 65 | make os 66 | make test O_SUFFIX="Os" 67 | make o3 68 | rm -f test.in 69 | 70 | lib_static: $(LIB).c 71 | $(CC) $(CFLAGS) $(OPT_FLAGS) -c $(LIB).c 72 | ar cr $(LIB)$(SUFFIX).a $(LIB).o 73 | 74 | lib_shared: $(LIB).c 75 | $(CC) $(CFLAGS) $(OPT_FLAGS) -c -fPIC $(LIB).c 76 | $(CC) $(CFLAGS) $(OPT_FLAGS) -shared -o $(LIB).so $(LIB).o 77 | 78 | exe_static: $(LIB).c $(BINS) 79 | $(CC) $(CFLAGS) $(OPT_FLAGS) $(REPL) -o wc_repl_static.exe -L . $(LIB).a 80 | 81 | exe_shared: $(LIB).c $(BINS) 82 | $(CC) $(CFLAGS) $(OPT_FLAGS) $(REPL) -o wc_repl_shared.exe -L . $(LIB).so 83 | 84 | exe_dlopen: $(LIB).c $(BINS) 85 | $(CC) $(CFLAGS) $(OPT_FLAGS) $(REPL) -o wc_repl_dlopen.exe -ldl -D USE_DLL 86 | 87 | clean: 88 | rm -f *.o *.gch *.a *.so *.exe test.in *.txt -------------------------------------------------------------------------------- /cw01/zad3a/dllmanager.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLLMANAGER_H__ 2 | #define __DLLMANAGER_H__ 3 | 4 | #ifdef USE_DLL 5 | #include 6 | #include 7 | #include 8 | #define WCLIB_SYMBOL_TABLE_SIZE 7 9 | void* _dll_ptrs[WCLIB_SYMBOL_TABLE_SIZE]; 10 | 11 | char* _dll_symbol_names[] = { 12 | "LibWCMemory", 13 | "LibWCMemory_init", 14 | "LibWCMemory_clear", 15 | "LibWCMemory_destruct", 16 | "LibWCMemory_get", 17 | "LibWCMemory_pop", 18 | "LibWCMemory_push", 19 | }; 20 | 21 | void load_dll_symbols(const char* filepath) { 22 | void *handle = dlopen(filepath, RTLD_LAZY); 23 | 24 | if (handle == NULL) { 25 | fprintf(stderr, "[DLL MANAGER] DLL NOT FOUND (%s)\n", filepath); 26 | return; 27 | } 28 | 29 | // rapidly dwindling sanity of C developers 30 | *(void **) (&LibWCMemory_create) = dlsym(handle,"LibWCMemory_create"); 31 | *(void **) (&LibWCMemory_init) = dlsym(handle,"LibWCMemory_init"); 32 | *(void **) (&LibWCMemory_clear) = dlsym(handle,"LibWCMemory_clear"); 33 | *(void **) (&LibWCMemory_destruct) = dlsym(handle,"LibWCMemory_destruct"); 34 | *(void **) (&LibWCMemory_get) = dlsym(handle,"LibWCMemory_get"); 35 | *(void **) (&LibWCMemory_pop) = dlsym(handle,"LibWCMemory_pop"); 36 | *(void **) (&LibWCMemory_push) = dlsym(handle,"LibWCMemory_push"); 37 | } 38 | 39 | #else 40 | void load_dll_symbols(const char* filepath) {} 41 | #endif 42 | 43 | #endif 44 | 45 | 46 | -------------------------------------------------------------------------------- /cw01/zad3a/libwc.c: -------------------------------------------------------------------------------- 1 | #include "libwc.h" 2 | 3 | char command[LIBWC_COMMAND_BUFF_SIZE] = ""; 4 | int exit_code; 5 | 6 | LibWCMemory LibWCMemory_create(size_t size) { 7 | return (LibWCMemory) { 8 | .arr = calloc(size, sizeof(char*)), 9 | .active = calloc(size, sizeof(bool)), 10 | .top = 0, 11 | .size = size, 12 | }; 13 | } 14 | 15 | void LibWCMemory_init(LibWCMemory* LibWCMemory, size_t size) { 16 | LibWCMemory->arr = calloc(size, sizeof(char*)); 17 | LibWCMemory->active = calloc(size, sizeof(bool)); 18 | LibWCMemory->top = 0; 19 | LibWCMemory->size = size; 20 | } 21 | 22 | void LibWCMemory_clear(LibWCMemory* LibWCMemory) { 23 | for (size_t i = 0; i < LibWCMemory->top; i++) 24 | if (LibWCMemory->active[i]) 25 | free(LibWCMemory->arr[i]); 26 | LibWCMemory->top = 0; 27 | memset(LibWCMemory->active, false, sizeof(bool)*(LibWCMemory->size)); 28 | } 29 | 30 | void LibWCMemory_destruct(LibWCMemory* LibWCMemory) { 31 | LibWCMemory_clear(LibWCMemory); 32 | free(LibWCMemory->arr); 33 | free(LibWCMemory->active); 34 | } 35 | 36 | bool LibWCMemory_range_check(LibWCMemory* LibWCMemory, size_t index) { 37 | if (LibWCMemory->top <= index) { 38 | fprintf(stderr, "[LIB WC] INDEX OUT OF RANGE\n"); 39 | return false; 40 | } 41 | if (!LibWCMemory->active[index]) { 42 | fprintf(stderr, "[LIB WC] INDEX ALREADY REMOVED\n"); 43 | return false; 44 | } 45 | return true; 46 | } 47 | 48 | char* LibWCMemory_get(LibWCMemory* LibWCMemory, size_t index) { 49 | if (LibWCMemory_range_check(LibWCMemory, index)) 50 | return LibWCMemory->arr[index]; 51 | return ""; 52 | } 53 | 54 | void LibWCMemory_pop(LibWCMemory* LibWCMemory, size_t index) { 55 | if (LibWCMemory_range_check(LibWCMemory, index)) { 56 | free(LibWCMemory->arr[index]); 57 | LibWCMemory->active[index] = false; 58 | } 59 | } 60 | 61 | long get_file_size(FILE* file) { 62 | fseek(file, 0, SEEK_END); 63 | long size = ftell(file); 64 | fseek(file, 0, SEEK_SET); 65 | return size; 66 | } 67 | 68 | char* get_file_content(char* filename) { 69 | FILE* file = fopen(filename, "r"); 70 | size_t size = get_file_size(file); 71 | char* buff = calloc(size, sizeof(char)); 72 | exit_code = fread(buff, sizeof(char), size, file); 73 | exit_code = fclose(file); 74 | return buff; 75 | } 76 | 77 | void LibWCMemory_push(LibWCMemory* LibWCMemory, char* input_filename) { 78 | char tmp_filename[] = "/tmp/wclib_XXXXXX"; 79 | int tmp_file = mkstemp(tmp_filename); 80 | 81 | if (tmp_file == 0) { 82 | fprintf(stderr, "[LIB WC] FAILED TO CREATE A TEMPORARY FILE\n"); 83 | return; 84 | } 85 | 86 | snprintf(command, LIBWC_COMMAND_BUFF_SIZE, "wc '%s' 1> '%s' 2>/dev/null", input_filename, tmp_filename); 87 | exit_code = system(command); 88 | 89 | char* wc_output = get_file_content(tmp_filename); 90 | if (strlen(wc_output) == 0) { 91 | fprintf(stderr, "[LIB WC] FAILED TO READ THE INPUT FILE (%s)\n", input_filename); 92 | return; 93 | } 94 | 95 | snprintf(command, LIBWC_COMMAND_BUFF_SIZE, "rm -f '%s' 2>/dev/null", tmp_filename); 96 | exit_code = system(command); 97 | 98 | if (LibWCMemory->top < LibWCMemory->size) { 99 | LibWCMemory->arr[LibWCMemory->top] = wc_output; 100 | LibWCMemory->active[LibWCMemory->top] = true; 101 | (LibWCMemory->top)++; 102 | } else { 103 | fprintf(stderr, "[LIB WC] NOT ENOUGH MEMORY\n"); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /cw01/zad3a/libwc.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBWC_H__ 2 | #define __LIBWC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define LIBWC_COMMAND_BUFF_SIZE 2048 10 | 11 | typedef struct { 12 | char** arr; 13 | bool* active; 14 | size_t top; 15 | size_t size; 16 | } LibWCMemory; 17 | 18 | LibWCMemory LibWCMemory_create(size_t size); 19 | 20 | void LibWCMemory_init(LibWCMemory* LibWCMemory, size_t size); 21 | 22 | void LibWCMemory_clear(LibWCMemory* LibWCMemory); 23 | 24 | void LibWCMemory_destruct(LibWCMemory* LibWCMemory); 25 | 26 | char* LibWCMemory_get(LibWCMemory* LibWCMemory, size_t index); 27 | 28 | void LibWCMemory_pop(LibWCMemory* LibWCMemory, size_t index); 29 | 30 | void LibWCMemory_push(LibWCMemory* LibWCMemory, char* input_filename); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /cw01/zad3a/libwc_so.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBWC_H__ 2 | #define __LIBWC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define LIBWC_COMMAND_BUFF_SIZE 2048 10 | 11 | typedef struct { 12 | char** arr; 13 | bool* active; 14 | size_t top; 15 | size_t size; 16 | } LibWCMemory; 17 | 18 | LibWCMemory (*LibWCMemory_create)(size_t); 19 | void (*LibWCMemory_init)(LibWCMemory*, size_t); 20 | void (*LibWCMemory_clear)(LibWCMemory*); 21 | void (*LibWCMemory_destruct)(LibWCMemory*); 22 | char* (*LibWCMemory_get)(LibWCMemory*, size_t); 23 | void (*LibWCMemory_pop)(LibWCMemory*, size_t); 24 | void (*LibWCMemory_push)(LibWCMemory*, char* ); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /cw01/zad3b/Makefile: -------------------------------------------------------------------------------- 1 | SHELL=/bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall 4 | LIB=libwc 5 | REPL=wc_repl.c 6 | BINS=wc_repl.c dllmanager.h 7 | 8 | all: 9 | make lib_static 10 | make lib_shared 11 | make exe_static 12 | make exe_shared 13 | make exe_dlopen 14 | 15 | o: 16 | make all OPT_FLAGS="-O" 17 | 18 | o0: 19 | make all OPT_FLAGS="-O0" 20 | 21 | o1: 22 | make all OPT_FLAGS="-O1" 23 | 24 | o2: 25 | make all OPT_FLAGS="-O2" 26 | 27 | o3: 28 | make all OPT_FLAGS="-O3" 29 | 30 | os: 31 | make all OPT_FLAGS="-Os" 32 | 33 | test: 34 | sudo mv libwc.so /usr/lib 35 | make test_create 36 | ./wc_repl_static.exe < test.in > results_static_$(O_SUFFIX).txt 2>/dev/null 37 | ./wc_repl_shared.exe < test.in > results_shared_$(O_SUFFIX).txt 2>/dev/null 38 | ./wc_repl_dlopen.exe < test.in > results_dynamic_$(O_SUFFIX).txt 2>/dev/null 39 | 40 | test_create: 41 | echo "init 1000" > test.in 42 | 43 | (ls -p | grep -v / | sed 's/^/count /') >> test.in 44 | 45 | echo "show 0" >> test.in 46 | echo "show 1" >> test.in 47 | echo "show 2" >> test.in 48 | 49 | echo "delete 1" >> test.in 50 | echo "destroy" >> test.in 51 | echo "exit" >> test.in 52 | 53 | report: 54 | sudo ls > /dev/null 55 | make o 56 | make test O_SUFFIX="O" 57 | make o0 58 | make test O_SUFFIX="O0" 59 | make o1 60 | make test O_SUFFIX="O1" 61 | make o2 62 | make test O_SUFFIX="O2" 63 | make o3 64 | make test O_SUFFIX="O3" 65 | make os 66 | make test O_SUFFIX="Os" 67 | make o3 68 | rm -f test.in 69 | 70 | lib_static: $(LIB).c 71 | $(CC) $(CFLAGS) $(OPT_FLAGS) -c $(LIB).c 72 | ar cr $(LIB)$(SUFFIX).a $(LIB).o 73 | 74 | lib_shared: $(LIB).c 75 | $(CC) $(CFLAGS) $(OPT_FLAGS) -c -fPIC $(LIB).c 76 | $(CC) $(CFLAGS) $(OPT_FLAGS) -shared -o $(LIB).so $(LIB).o 77 | 78 | exe_static: $(LIB).c $(BINS) 79 | $(CC) $(CFLAGS) $(OPT_FLAGS) $(REPL) -o wc_repl_static.exe -L . $(LIB).a 80 | 81 | exe_shared: $(LIB).c $(BINS) 82 | $(CC) $(CFLAGS) $(OPT_FLAGS) $(REPL) -o wc_repl_shared.exe -L . $(LIB).so 83 | 84 | exe_dlopen: $(LIB).c $(BINS) 85 | $(CC) $(CFLAGS) $(OPT_FLAGS) $(REPL) -o wc_repl_dlopen.exe -ldl -D USE_DLL 86 | 87 | clean: 88 | rm -f *.o *.gch *.a *.so *.exe test.in *.txt -------------------------------------------------------------------------------- /cw01/zad3b/dllmanager.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLLMANAGER_H__ 2 | #define __DLLMANAGER_H__ 3 | 4 | #ifdef USE_DLL 5 | #include 6 | #include 7 | #include 8 | #define WCLIB_SYMBOL_TABLE_SIZE 7 9 | void* _dll_ptrs[WCLIB_SYMBOL_TABLE_SIZE]; 10 | 11 | char* _dll_symbol_names[] = { 12 | "LibWCMemory", 13 | "LibWCMemory_init", 14 | "LibWCMemory_clear", 15 | "LibWCMemory_destruct", 16 | "LibWCMemory_get", 17 | "LibWCMemory_pop", 18 | "LibWCMemory_push", 19 | }; 20 | 21 | void load_dll_symbols(const char* filepath) { 22 | void *handle = dlopen(filepath, RTLD_LAZY); 23 | 24 | if (handle == NULL) { 25 | fprintf(stderr, "[DLL MANAGER] DLL NOT FOUND (%s)\n", filepath); 26 | return; 27 | } 28 | 29 | // rapidly dwindling sanity of C developers 30 | *(void **) (&LibWCMemory_create) = dlsym(handle,"LibWCMemory_create"); 31 | *(void **) (&LibWCMemory_init) = dlsym(handle,"LibWCMemory_init"); 32 | *(void **) (&LibWCMemory_clear) = dlsym(handle,"LibWCMemory_clear"); 33 | *(void **) (&LibWCMemory_destruct) = dlsym(handle,"LibWCMemory_destruct"); 34 | *(void **) (&LibWCMemory_get) = dlsym(handle,"LibWCMemory_get"); 35 | *(void **) (&LibWCMemory_pop) = dlsym(handle,"LibWCMemory_pop"); 36 | *(void **) (&LibWCMemory_push) = dlsym(handle,"LibWCMemory_push"); 37 | } 38 | 39 | #else 40 | void load_dll_symbols(const char* filepath) {} 41 | #endif 42 | 43 | #endif 44 | 45 | 46 | -------------------------------------------------------------------------------- /cw01/zad3b/libwc.c: -------------------------------------------------------------------------------- 1 | #include "libwc.h" 2 | 3 | char command[LIBWC_COMMAND_BUFF_SIZE] = ""; 4 | int exit_code; 5 | 6 | LibWCMemory LibWCMemory_create(size_t size) { 7 | return (LibWCMemory) { 8 | .arr = calloc(size, sizeof(char*)), 9 | .active = calloc(size, sizeof(bool)), 10 | .top = 0, 11 | .size = size, 12 | }; 13 | } 14 | 15 | void LibWCMemory_init(LibWCMemory* LibWCMemory, size_t size) { 16 | LibWCMemory->arr = calloc(size, sizeof(char*)); 17 | LibWCMemory->active = calloc(size, sizeof(bool)); 18 | LibWCMemory->top = 0; 19 | LibWCMemory->size = size; 20 | } 21 | 22 | void LibWCMemory_clear(LibWCMemory* LibWCMemory) { 23 | for (size_t i = 0; i < LibWCMemory->top; i++) 24 | if (LibWCMemory->active[i]) 25 | free(LibWCMemory->arr[i]); 26 | LibWCMemory->top = 0; 27 | memset(LibWCMemory->active, false, sizeof(bool)*(LibWCMemory->size)); 28 | } 29 | 30 | void LibWCMemory_destruct(LibWCMemory* LibWCMemory) { 31 | LibWCMemory_clear(LibWCMemory); 32 | free(LibWCMemory->arr); 33 | free(LibWCMemory->active); 34 | } 35 | 36 | bool LibWCMemory_range_check(LibWCMemory* LibWCMemory, size_t index) { 37 | if (LibWCMemory->top <= index) { 38 | fprintf(stderr, "[LIB WC] INDEX OUT OF RANGE\n"); 39 | return false; 40 | } 41 | if (!LibWCMemory->active[index]) { 42 | fprintf(stderr, "[LIB WC] INDEX ALREADY REMOVED\n"); 43 | return false; 44 | } 45 | return true; 46 | } 47 | 48 | char* LibWCMemory_get(LibWCMemory* LibWCMemory, size_t index) { 49 | if (LibWCMemory_range_check(LibWCMemory, index)) 50 | return LibWCMemory->arr[index]; 51 | return ""; 52 | } 53 | 54 | void LibWCMemory_pop(LibWCMemory* LibWCMemory, size_t index) { 55 | if (LibWCMemory_range_check(LibWCMemory, index)) { 56 | free(LibWCMemory->arr[index]); 57 | LibWCMemory->active[index] = false; 58 | } 59 | } 60 | 61 | long get_file_size(FILE* file) { 62 | fseek(file, 0, SEEK_END); 63 | long size = ftell(file); 64 | fseek(file, 0, SEEK_SET); 65 | return size; 66 | } 67 | 68 | char* get_file_content(char* filename) { 69 | FILE* file = fopen(filename, "r"); 70 | size_t size = get_file_size(file); 71 | char* buff = calloc(size, sizeof(char)); 72 | exit_code = fread(buff, sizeof(char), size, file); 73 | exit_code = fclose(file); 74 | return buff; 75 | } 76 | 77 | void LibWCMemory_push(LibWCMemory* LibWCMemory, char* input_filename) { 78 | char tmp_filename[] = "/tmp/wclib_XXXXXX"; 79 | int tmp_file = mkstemp(tmp_filename); 80 | 81 | if (tmp_file == 0) { 82 | fprintf(stderr, "[LIB WC] FAILED TO CREATE A TEMPORARY FILE\n"); 83 | return; 84 | } 85 | 86 | snprintf(command, LIBWC_COMMAND_BUFF_SIZE, "wc '%s' 1> '%s' 2>/dev/null", input_filename, tmp_filename); 87 | exit_code = system(command); 88 | 89 | char* wc_output = get_file_content(tmp_filename); 90 | if (strlen(wc_output) == 0) { 91 | fprintf(stderr, "[LIB WC] FAILED TO READ THE INPUT FILE (%s)\n", input_filename); 92 | return; 93 | } 94 | 95 | snprintf(command, LIBWC_COMMAND_BUFF_SIZE, "rm -f '%s' 2>/dev/null", tmp_filename); 96 | exit_code = system(command); 97 | 98 | if (LibWCMemory->top < LibWCMemory->size) { 99 | LibWCMemory->arr[LibWCMemory->top] = wc_output; 100 | LibWCMemory->active[LibWCMemory->top] = true; 101 | (LibWCMemory->top)++; 102 | } else { 103 | fprintf(stderr, "[LIB WC] NOT ENOUGH MEMORY\n"); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /cw01/zad3b/libwc.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBWC_H__ 2 | #define __LIBWC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define LIBWC_COMMAND_BUFF_SIZE 2048 10 | 11 | typedef struct { 12 | char** arr; 13 | bool* active; 14 | size_t top; 15 | size_t size; 16 | } LibWCMemory; 17 | 18 | LibWCMemory LibWCMemory_create(size_t size); 19 | 20 | void LibWCMemory_init(LibWCMemory* LibWCMemory, size_t size); 21 | 22 | void LibWCMemory_clear(LibWCMemory* LibWCMemory); 23 | 24 | void LibWCMemory_destruct(LibWCMemory* LibWCMemory); 25 | 26 | char* LibWCMemory_get(LibWCMemory* LibWCMemory, size_t index); 27 | 28 | void LibWCMemory_pop(LibWCMemory* LibWCMemory, size_t index); 29 | 30 | void LibWCMemory_push(LibWCMemory* LibWCMemory, char* input_filename); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /cw01/zad3b/libwc_so.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBWC_H__ 2 | #define __LIBWC_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define LIBWC_COMMAND_BUFF_SIZE 2048 10 | 11 | typedef struct { 12 | char** arr; 13 | bool* active; 14 | size_t top; 15 | size_t size; 16 | } LibWCMemory; 17 | 18 | LibWCMemory (*LibWCMemory_create)(size_t); 19 | void (*LibWCMemory_init)(LibWCMemory*, size_t); 20 | void (*LibWCMemory_clear)(LibWCMemory*); 21 | void (*LibWCMemory_destruct)(LibWCMemory*); 22 | char* (*LibWCMemory_get)(LibWCMemory*, size_t); 23 | void (*LibWCMemory_pop)(LibWCMemory*, size_t); 24 | void (*LibWCMemory_push)(LibWCMemory*, char* ); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /cw02/report.txt: -------------------------------------------------------------------------------- 1 | Ukonczone 100% 2 | 3 | Zad 1. 4 | Program dziala podobnie jak tr 5 | Pomiary robi `make report` 6 | Pomiary znajduja sie w plikach "pomiar_zad_1_{PREFIX}.txt" 7 | Wersja sys jest zazwyczaj szybsza 8 | 9 | Zad 2. 10 | Pomiary robi `make report` 11 | Pomiary znajduja sie w plikach "pomiar_zad_2_{PREFIX}.txt" 12 | Dodane wersja z rozmiarem bloku 8 do testow 13 | 14 | Zad 3. 15 | Program dziala podobnie do "wc --bytes *" 16 | 17 | Zad 4. 18 | Występują pewne różnice od 19 | "find nazwa_katalogu | xargs wc --bytes" 20 | tree_size wydaje sie przeszukiwac wiecej sciezek, 21 | wyniki przecietnie są wieksze lub rowne 22 | 23 | 👽 24 | -------------------------------------------------------------------------------- /cw02/zad1/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: 6 | make std 7 | make sys 8 | 9 | std: replace.c 10 | $(CC) $(CFLAGS) replace.c -o replace_std 11 | 12 | sys: replace.c 13 | $(CC) $(CFLAGS) replace.c -o replace_sys -D SYS 14 | 15 | report: 16 | make all 17 | 18 | rm -f pomiar_zad_2_1.txt pomiar_zad_2_1024.txt 19 | 20 | i=1 ; while [[ $$i -le 10 ]] ; do \ 21 | head -c 1048576 test.in ; \ 22 | ./replace_sys a b test.in tmp_sys >> pomiar_zad_1_sys.txt ; \ 23 | ./replace_std a b test.in tmp_std >> pomiar_zad_1_std.txt ; \ 24 | ((i = i + 1)) ; \ 25 | done 26 | 27 | rm -f test.in tmp_std tmp_sys 28 | 29 | 30 | clean: 31 | rm -f replace_std replace_sys 32 | -------------------------------------------------------------------------------- /cw02/zad1/pomiar_zad_1_std.txt: -------------------------------------------------------------------------------- 1 | EXECUTION TIME: 0s 468089ns 2 | EXECUTION TIME: 0s 1150040ns 3 | EXECUTION TIME: 0s 585007ns 4 | EXECUTION TIME: 0s 440642ns 5 | EXECUTION TIME: 0s 998341ns 6 | EXECUTION TIME: 0s 850273ns 7 | EXECUTION TIME: 0s 570410ns 8 | EXECUTION TIME: 0s 856558ns 9 | EXECUTION TIME: 0s 861098ns 10 | EXECUTION TIME: 0s 852857ns 11 | -------------------------------------------------------------------------------- /cw02/zad1/pomiar_zad_1_sys.txt: -------------------------------------------------------------------------------- 1 | EXECUTION TIME: 0s 1096540ns 2 | EXECUTION TIME: 0s 411168ns 3 | EXECUTION TIME: 0s 409282ns 4 | EXECUTION TIME: 0s 391542ns 5 | EXECUTION TIME: 0s 742085ns 6 | EXECUTION TIME: 0s 961952ns 7 | EXECUTION TIME: 0s 435962ns 8 | EXECUTION TIME: 0s 746555ns 9 | EXECUTION TIME: 0s 421994ns 10 | EXECUTION TIME: 0s 744041ns 11 | -------------------------------------------------------------------------------- /cw02/zad1/replace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | size_t _; 11 | 12 | typedef union { 13 | int posix; 14 | FILE* standard; 15 | } UniversalFile; 16 | 17 | typedef enum { 18 | READ, 19 | WRITE, 20 | } FILE_MODE; 21 | 22 | UniversalFile universal_open(const char* filepath, FILE_MODE mode) { 23 | UniversalFile file; 24 | #ifndef SYS 25 | file.standard = fopen(filepath, mode == READ ? "r" : "w"); 26 | #else 27 | file.posix = open(filepath, mode == READ ? O_RDONLY : O_WRONLY); 28 | #endif 29 | return file; 30 | } 31 | 32 | size_t universal_size(UniversalFile file) { 33 | #ifndef SYS 34 | fseek(file.standard, 0, SEEK_END); 35 | long size = ftell(file.standard); 36 | fseek(file.standard, 0, SEEK_SET); 37 | return size; 38 | #else 39 | struct stat st; 40 | fstat(file.posix, &st); 41 | return st.st_size; 42 | #endif 43 | }; 44 | 45 | char* universal_read(UniversalFile file) { 46 | size_t size = universal_size(file); 47 | char* buff = (char*)calloc(size, sizeof(char)); 48 | 49 | #ifndef SYS 50 | _ = fread(buff, sizeof(char), size, file.standard); 51 | #else 52 | _ = read(file.posix, buff, sizeof(char)*size); 53 | #endif 54 | 55 | return buff; 56 | } 57 | 58 | void universal_write(UniversalFile file, size_t size, size_t count, void* src) { 59 | #ifndef SYS 60 | _ = fwrite(src, size, count, file.standard); 61 | #else 62 | _ = write(file.posix, src, size*count); 63 | #endif 64 | } 65 | 66 | void universal_close(UniversalFile file) { 67 | #ifndef SYS 68 | fclose(file.standard); 69 | #else 70 | close(file.posix); 71 | #endif 72 | } 73 | 74 | char* universal_full_read(const char* filepath) { 75 | UniversalFile file = universal_open(filepath, READ); 76 | char* buff = universal_read(file); 77 | universal_close(file); 78 | return buff; 79 | } 80 | 81 | void universal_full_write(const char* filepath, const char* buff) { 82 | UniversalFile file = universal_open(filepath, WRITE); 83 | universal_write(file, sizeof(char), strlen(buff), (void*)buff); 84 | universal_close(file); 85 | } 86 | 87 | struct timespec timespec_diff(struct timespec start, struct timespec end) { 88 | struct timespec out; 89 | 90 | if ((end.tv_nsec-start.tv_nsec)<0) { 91 | out.tv_sec = end.tv_sec-start.tv_sec-1; 92 | out.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; 93 | } 94 | else { 95 | out.tv_sec = end.tv_sec-start.tv_sec; 96 | out.tv_nsec = end.tv_nsec-start.tv_nsec; 97 | } 98 | return out; 99 | } 100 | 101 | int main(int argc, char** argv) { 102 | if (argc != 5) { 103 | fprintf(stderr, "[REPLACE] Invalid arguments\n"); 104 | return 1; 105 | } 106 | 107 | struct timespec timespec_buff_start, timespec_buff_end; 108 | clock_gettime(CLOCK_REALTIME, ×pec_buff_start); 109 | 110 | char from = argv[1][0]; 111 | char into = argv[2][0]; 112 | char* input_filename = argv[3]; 113 | char* output_filename = argv[4]; 114 | 115 | char* content = universal_full_read(input_filename); 116 | char* tmp = content; 117 | 118 | while (*tmp) { 119 | if (*tmp == from) 120 | *tmp = into; 121 | tmp++; 122 | } 123 | 124 | universal_full_write(output_filename, content); 125 | 126 | clock_gettime(CLOCK_REALTIME, ×pec_buff_end); 127 | struct timespec diff = timespec_diff(timespec_buff_start, timespec_buff_end); 128 | printf("EXECUTION TIME: %lds %ldns\n", diff.tv_sec, diff.tv_nsec); 129 | 130 | return 0; 131 | } 132 | -------------------------------------------------------------------------------- /cw02/zad2/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: 6 | make single BLOCK_SIZE=1 7 | make single BLOCK_SIZE=8 8 | make single BLOCK_SIZE=1024 9 | 10 | single: 11 | $(CC) $(CFLAGS) reverse.c -o reverse_$(BLOCK_SIZE) -D BLOCK_SIZE=$(BLOCK_SIZE) 12 | 13 | report: 14 | make all 15 | 16 | rm -f pomiar_zad_2_1.txt pomiar_zad_2_1024.txt 17 | 18 | i=1 ; while [[ $$i -le 10 ]] ; do \ 19 | head -c 1048576 test.in ; \ 20 | ./reverse_1 test.in tmp1 >> pomiar_zad_2_1.txt ; \ 21 | ./reverse_1024 test.in tmp1024 >> pomiar_zad_2_1024.txt ; \ 22 | ((i = i + 1)) ; \ 23 | done 24 | 25 | rm -f test.in tmp1 tmp1024 26 | 27 | 28 | clean: 29 | rm -f reverse_* test.in 30 | -------------------------------------------------------------------------------- /cw02/zad2/pomiar_zad_2_1.txt: -------------------------------------------------------------------------------- 1 | EXECUTION TIME: 0s 360496298ns 2 | EXECUTION TIME: 0s 373109047ns 3 | EXECUTION TIME: 0s 356942293ns 4 | EXECUTION TIME: 0s 360045313ns 5 | EXECUTION TIME: 0s 375455238ns 6 | EXECUTION TIME: 0s 360498108ns 7 | EXECUTION TIME: 0s 352721207ns 8 | EXECUTION TIME: 0s 363405636ns 9 | EXECUTION TIME: 0s 377410789ns 10 | EXECUTION TIME: 0s 362206345ns 11 | -------------------------------------------------------------------------------- /cw02/zad2/pomiar_zad_2_1024.txt: -------------------------------------------------------------------------------- 1 | EXECUTION TIME: 0s 2498524ns 2 | EXECUTION TIME: 0s 1084674ns 3 | EXECUTION TIME: 0s 2473519ns 4 | EXECUTION TIME: 0s 1069379ns 5 | EXECUTION TIME: 0s 2175496ns 6 | EXECUTION TIME: 0s 2608528ns 7 | EXECUTION TIME: 0s 1088237ns 8 | EXECUTION TIME: 0s 2637653ns 9 | EXECUTION TIME: 0s 1069239ns 10 | EXECUTION TIME: 0s 1057925ns 11 | -------------------------------------------------------------------------------- /cw02/zad2/reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char read_buffer[BLOCK_SIZE+1] = ""; 8 | int _; 9 | 10 | char* reverse(char* str) { 11 | size_t n = strlen(str); 12 | char tmp; 13 | 14 | for (int i = 0; i < n / 2; i++) { 15 | tmp = str[i]; 16 | str[i] = str[n-i-1]; 17 | str[n-i-1] = tmp; 18 | } 19 | 20 | return str; 21 | } 22 | 23 | long get_file_size(FILE* file) { 24 | fseek(file, 0, SEEK_END); 25 | long size = ftell(file); 26 | fseek(file, 0, SEEK_SET); 27 | return size; 28 | } 29 | 30 | struct timespec timespec_diff(struct timespec start, struct timespec end) { 31 | struct timespec out; 32 | 33 | if ((end.tv_nsec-start.tv_nsec)<0) { 34 | out.tv_sec = end.tv_sec-start.tv_sec-1; 35 | out.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; 36 | } 37 | else { 38 | out.tv_sec = end.tv_sec-start.tv_sec; 39 | out.tv_nsec = end.tv_nsec-start.tv_nsec; 40 | } 41 | return out; 42 | } 43 | 44 | 45 | int main(int argc, char** argv) { 46 | if (argc != 3) { 47 | fprintf(stderr, "[REVERSE] Invalid arguments\n"); 48 | return 1; 49 | } 50 | 51 | struct timespec timespec_buff_start, timespec_buff_end; 52 | clock_gettime(CLOCK_REALTIME, ×pec_buff_start); 53 | 54 | char* input_filename = argv[1]; 55 | char* output_filename = argv[2]; 56 | size_t end; 57 | 58 | FILE* input_file = fopen(input_filename, "r"); 59 | FILE* output_file = fopen(output_filename, "w"); 60 | 61 | long input_file_size = get_file_size(input_file); 62 | size_t block_count = input_file_size / ((size_t) BLOCK_SIZE ); 63 | size_t remainder = input_file_size % ((size_t) BLOCK_SIZE ); 64 | 65 | for (int i = 0; i <= block_count; i++) { 66 | fseek(input_file, -BLOCK_SIZE*i, SEEK_END); 67 | end = fread(read_buffer, sizeof(char), BLOCK_SIZE, input_file); 68 | read_buffer[end] = 0; 69 | fprintf(output_file, "%s", reverse(read_buffer)); 70 | } 71 | 72 | fseek(input_file, 0, SEEK_SET); 73 | end = fread(read_buffer, sizeof(char), remainder, input_file); 74 | read_buffer[end] = 0; 75 | fprintf(output_file, "%s", reverse(read_buffer)); 76 | 77 | fclose(input_file); 78 | fclose(output_file); 79 | 80 | clock_gettime(CLOCK_REALTIME, ×pec_buff_end); 81 | 82 | struct timespec diff = timespec_diff(timespec_buff_start, timespec_buff_end); 83 | 84 | printf("EXECUTION TIME: %lds %ldns\n", diff.tv_sec, diff.tv_nsec); 85 | 86 | return 0; 87 | } -------------------------------------------------------------------------------- /cw02/zad3/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -O3 3 | 4 | all: dir_size.c 5 | $(CC) $(CFLAGS) dir_size.c -o dir_size 6 | 7 | clean: 8 | rm -f dir_size 9 | -------------------------------------------------------------------------------- /cw02/zad3/dir_size.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | int main(int argc, char** argv) { 10 | if (argc != 2) { 11 | fprintf(stderr, "[DIR SIZE] Invalid arguments\n"); 12 | return 1; 13 | } 14 | 15 | char* input_dirname = argv[1]; 16 | 17 | DIR* dir = opendir(input_dirname); 18 | 19 | if (dir == NULL) { 20 | fprintf(stderr, "[DIR SIZE] Couldn't read dir (%s)\n", input_dirname); 21 | return 2; 22 | } 23 | 24 | int error = chdir(input_dirname); 25 | 26 | if (error == -1) { 27 | fprintf(stderr, "[DIR SIZE] Failed to enter dir (%s)\n", input_dirname); 28 | return 3; 29 | } 30 | 31 | struct dirent* current_file; 32 | long long total_size = 0; 33 | struct stat stats; 34 | 35 | while ((current_file = readdir(dir))) { 36 | stat(current_file->d_name, &stats); 37 | 38 | if (S_ISDIR(stats.st_mode)) 39 | continue; 40 | 41 | printf("%s %ld\n", current_file->d_name, stats.st_size); 42 | total_size += stats.st_size; 43 | } 44 | 45 | printf("TOTAL %lld\n", total_size); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /cw02/zad4/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -O3 3 | 4 | all: tree_size.c 5 | $(CC) $(CFLAGS) tree_size.c -o tree_size 6 | 7 | clean: 8 | rm -f tree_size 9 | -------------------------------------------------------------------------------- /cw02/zad4/tree_size.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | long long total_size = 0; 11 | 12 | int file_callback(const char *fpath, const struct stat *sb, int typeflag) { 13 | if (typeflag != FTW_D) { 14 | struct stat stats; 15 | stat(fpath, &stats); 16 | printf("%s %ld\n", fpath, stats.st_size); 17 | total_size += stats.st_size; 18 | } 19 | return 0; 20 | } 21 | 22 | 23 | int main(int argc, char** argv) { 24 | if (argc != 2) { 25 | fprintf(stderr, "[TREE SIZE] Invalid arguments\n"); 26 | return 1; 27 | } 28 | 29 | char* input_dirname = argv[1]; 30 | int error = ftw(input_dirname, file_callback, 1); 31 | 32 | if (error == -1) { 33 | fprintf(stderr, "[TREE SIZE] FTW error has occured (%s)\n", strerror(errno)); 34 | return 2; 35 | } 36 | 37 | printf("TOTAL %lld\n", total_size); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /cw03/report.txt: -------------------------------------------------------------------------------- 1 | Ukonczone 100% 2 | 3 | Zad 1. 4 | * brak komentarza * 5 | 6 | Zad 2. 7 | Program zostal przetestowany (i dziala) na wymienionych przypadkach brzegowych. 8 | 9 | Zad 3. 10 | Zastosowalem fprintf zamiast perror w momencie gdy muszę formatować otrzymane bledy. 11 | Czekam na zakonczenie procesow potomnych. 12 | Dodalem dodatkowy parametr 'argv[3] = depth', aby moc w kontrolowanych warunkach wykonywac ten program np na '/'. 13 | Sprowadzam wszystkie sciezki do formy globalnej aby uniknac problemow. 14 | W przypadku wykonania programu na pliku (nie katalogu) zachowuje sie tak, 15 | jakby napotkal ten plik podczas rekursywnego przeszukiwania. 16 | 17 | 😎 18 | -------------------------------------------------------------------------------- /cw03/zad1/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: spawn.c 6 | $(CC) $(CFLAGS) spawn.c -o spawn 7 | 8 | clean: 9 | rm -f spawn 10 | -------------------------------------------------------------------------------- /cw03/zad1/spawn.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char** argv) { 7 | if (argc != 2) { 8 | fprintf(stderr, "[SPAWN] Invalid arguments\n"); 9 | return 1; 10 | } 11 | 12 | int n = atoi(argv[1]); 13 | 14 | for (int i = 0; i < n; i++) { 15 | if (fork() == 0) { 16 | printf("PARENT: %d | OWN: %d\n", getppid(), getpid()); 17 | return 0; 18 | } 19 | } 20 | 21 | while (wait(NULL) > 0); 22 | printf("ARGV[1] = %d\n", n); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /cw03/zad2/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: lspp.c 6 | $(CC) $(CFLAGS) lspp.c -o lspp 7 | 8 | clean: 9 | rm -f lspp 10 | -------------------------------------------------------------------------------- /cw03/zad2/lspp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char** argv) { 6 | if (argc != 2) { 7 | fprintf(stderr, "[LS++] Invalid arguments\n"); 8 | return 1; 9 | } 10 | 11 | printf("%s ", argv[0]); 12 | fflush(stdout); 13 | execl("/bin/ls", "ls", argv[1], NULL); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /cw03/zad3/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: rec.c 6 | $(CC) $(CFLAGS) rec.c -o rec 7 | 8 | clean: 9 | rm -f rec 10 | -------------------------------------------------------------------------------- /cw03/zad3/rec.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define MAX_PATTERN_SIZE 255 12 | #define RESOLVED_PATH_BUFFER_SIZE 4096 13 | char resolved_path[RESOLVED_PATH_BUFFER_SIZE] = ""; 14 | char exe_path[RESOLVED_PATH_BUFFER_SIZE] = ""; 15 | char entry_path[RESOLVED_PATH_BUFFER_SIZE] = ""; 16 | char fread_buffer[MAX_PATTERN_SIZE + 1]; 17 | char next_depth_buffer[12] = ""; 18 | 19 | int main(int argc, char** argv) { 20 | int depth = -1; 21 | 22 | // CUSTOM DEPTH 23 | if (argc == 4) { 24 | depth = atoi(argv[3]); 25 | sprintf(next_depth_buffer, "%d", depth-1); 26 | if (depth < 1) 27 | return 0; 28 | } else if (argc != 3) { 29 | fprintf(stderr, "[REC] Invalid arguments\n"); 30 | return 1; 31 | } 32 | 33 | if (strlen(argv[2]) > MAX_PATTERN_SIZE) { 34 | fprintf(stderr, "[REC] argv[1] too long\n"); 35 | return 2; 36 | } 37 | 38 | size_t pattern_size = strlen(argv[2]); 39 | 40 | struct stat st; 41 | 42 | char* err; 43 | 44 | err = realpath(argv[0], exe_path); 45 | if (err == NULL && exe_path[0] != '/') { 46 | fprintf(stderr, "[REC] Error resolving path (%s)\n", argv[0]); 47 | return 3; 48 | } 49 | 50 | 51 | err = realpath(argv[1], resolved_path); 52 | if (err == NULL && resolved_path[0] != '/') { 53 | fprintf(stderr, "[REC] Error resolving path (%s)\n", argv[0]); 54 | return 3; 55 | } 56 | 57 | if (stat(resolved_path, &st) == -1) { 58 | fprintf(stderr, "[REC] Stat error (%s)\n", strerror(errno)); 59 | return 4; 60 | } 61 | 62 | if (S_ISDIR(st.st_mode)) { 63 | DIR* dir = opendir(resolved_path); 64 | 65 | if (dir == NULL) { 66 | fprintf(stderr, "[REC] Couldn't read dir (%s)\n", resolved_path); 67 | return 5; 68 | } 69 | 70 | struct dirent* entry; 71 | while ((entry = readdir(dir))) { 72 | if (strcmp(entry->d_name, ".") == 0) continue; 73 | if (strcmp(entry->d_name, "..") == 0) continue; 74 | 75 | strcpy(entry_path, resolved_path); 76 | strcat(entry_path, "/"); 77 | strcat(entry_path, entry->d_name); 78 | 79 | if (fork() == 0) { 80 | if (argc == 4) 81 | execl(exe_path, exe_path, entry_path, argv[2], next_depth_buffer, NULL); 82 | else 83 | execl(exe_path, exe_path, entry_path, argv[2], NULL); 84 | } 85 | 86 | } 87 | } else { 88 | FILE* file = fopen(resolved_path, "r"); 89 | size_t size = fread(fread_buffer, sizeof(char), pattern_size, file); 90 | fread_buffer[size] = 0; 91 | 92 | if (strcmp(fread_buffer, argv[2]) == 0) { 93 | printf("%s %d\n", resolved_path, getpid()); 94 | fflush(NULL); 95 | } 96 | } 97 | 98 | // Wait for all the children, leave no orphans 99 | // Although not specified in the specification waiting is preferable 100 | while (wait(NULL) > 0); 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /cw04/report.txt: -------------------------------------------------------------------------------- 1 | Ukonczone 100% 2 | 3 | Zad 1. 4 | `make report` tworzy raport 5 | 6 | Zad 2. 7 | `make report` wyswietla opis jak utworzyc raport, poniewaz make nie lubi sie z sygnalami 8 | 9 | Zad 3. 10 | Nie uzylem sigsuspend poniewaz dzialalo mniej stabilnie niz rozwiazanie z licznikiem potwierdzen. 11 | Potwierdzane jest odebranie sygnalu, co nie oznacza pewnosci wykonania polecenia przez catcher, 12 | poniewaz nie ma kolejkowania i niektore operacje (zwlaszcza wypisywanie 100 liczb) zajmuja sporo czasu. 13 | Jesli sender nie dostanie potwierdzenia to po 5 sekundach ponownie wysyla sygnal. 14 | 15 | 💀 16 | -------------------------------------------------------------------------------- /cw04/zad1/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: 6 | make fork 7 | make exec 8 | 9 | fork: signals.c 10 | $(CC) $(CFLAGS) signals.c -o signals_fork 11 | 12 | exec: signals.c 13 | $(CC) $(CFLAGS) signals.c -o signals_exec -D EXEC 14 | 15 | report: 16 | make all 17 | 18 | echo "" > fork.txt 19 | echo "" > exec.txt 20 | 21 | echo ">>> Ignore:" >> fork.txt 22 | ./signals_fork ignore >> fork.txt 23 | echo "" >> fork.txt 24 | 25 | echo ">>> Handler:" >> fork.txt 26 | ./signals_fork handler >> fork.txt 27 | echo "" >> fork.txt 28 | 29 | echo ">>> Mask:" >> fork.txt 30 | ./signals_fork mask >> fork.txt 31 | echo "" >> fork.txt 32 | 33 | echo ">>> Pending:" >> fork.txt 34 | ./signals_fork pending >> fork.txt 35 | echo "" >> fork.txt 36 | 37 | echo ">>> Ignore:" >> exec.txt 38 | ./signals_exec ignore >> exec.txt 39 | echo "" >> exec.txt 40 | 41 | echo ">>> Mask:" >> exec.txt 42 | ./signals_exec mask >> exec.txt 43 | echo "" >> exec.txt 44 | 45 | echo ">>> Pending:" >> exec.txt 46 | ./signals_exec pending >> exec.txt 47 | echo "" >> exec.txt 48 | 49 | 50 | clean: 51 | rm -f signals_fork signals_exec 52 | -------------------------------------------------------------------------------- /cw04/zad1/exec.txt: -------------------------------------------------------------------------------- 1 | 2 | >>> Ignore: 3 | [SIGNALS] (34429) Raising signal. 4 | [SIGNALS] (34429) Raising signal. 5 | 6 | >>> Mask: 7 | [SIGNALS] (34433) Raising signal. 8 | 9 | >>> Pending: 10 | [SIGNALS] (34437) Raising signal. 11 | [SIGNALS] (34437) Signal pending. 12 | [SIGNALS] (34437) Signal pending. 13 | 14 | -------------------------------------------------------------------------------- /cw04/zad1/fork.txt: -------------------------------------------------------------------------------- 1 | 2 | >>> Ignore: 3 | [SIGNALS] (34409) Raising signal. 4 | [SIGNALS] (34410) Raising signal. 5 | 6 | >>> Handler: 7 | [SIGNALS] (34414) Raising signal. 8 | [SIGNALS] (34414) Received signal. 9 | [SIGNALS] (34415) Raising signal. 10 | [SIGNALS] (34415) Received signal. 11 | 12 | >>> Mask: 13 | [SIGNALS] (34419) Raising signal. 14 | 15 | >>> Pending: 16 | [SIGNALS] (34424) Raising signal. 17 | [SIGNALS] (34424) Signal pending. 18 | [SIGNALS] (34425) Signal not pending. 19 | 20 | -------------------------------------------------------------------------------- /cw04/zad1/report.txt: -------------------------------------------------------------------------------- 1 | >>> fork: 2 | ignore: Sygnal jest ignorowany w obydwu 3 | handler: Handler wywoluje sie w obydwu 4 | mask: Maska dziala w obydwu 5 | pending: Oczekujacy sygnal jest widoczny w rodzicu, ale nie w dziecku 6 | 7 | >>> exec: 8 | ignore: Sygnal jest ignorowany w obydwu 9 | mask: Maska dziala w obydwu 10 | pending: Oczekujacy sygnal jest widoczny w obydwu 11 | -------------------------------------------------------------------------------- /cw04/zad1/signals.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define SIGNAL SIGUSR1 10 | 11 | int is_parent = 1; 12 | 13 | typedef enum { 14 | INVALID, 15 | IGNORE, 16 | HANDLER, 17 | MASK, 18 | PENDING, 19 | } TASK_TYPE; 20 | 21 | TASK_TYPE str_to_task_type(const char* str) { 22 | if (strcmp(str, "ignore") == 0) return IGNORE; 23 | if (strcmp(str, "handler") == 0) return HANDLER; 24 | if (strcmp(str, "mask") == 0) return MASK; 25 | if (strcmp(str, "pending") == 0) return PENDING; 26 | return INVALID; 27 | } 28 | 29 | void signal_handler(int signo) { 30 | printf("[SIGNALS] (%d) Received signal.\n", getpid()); 31 | } 32 | 33 | void print_is_pending() { 34 | sigset_t sigset; 35 | sigpending(&sigset); 36 | if (sigismember(&sigset, SIGNAL)) 37 | printf("[SIGNALS] (%d) Signal pending.\n", getpid()); 38 | else 39 | printf("[SIGNALS] (%d) Signal not pending.\n", getpid()); 40 | } 41 | 42 | void block_signal() { 43 | struct sigaction action; 44 | sigemptyset(&action.sa_mask); 45 | sigaddset(&action.sa_mask, SIGNAL); 46 | sigprocmask(SIG_BLOCK, &action.sa_mask, NULL); 47 | } 48 | 49 | void raise_signal() { 50 | printf("[SIGNALS] (%d) Raising signal.\n", getpid()); 51 | raise(SIGNAL); 52 | } 53 | 54 | TASK_TYPE handle_input(int argc, char** argv) { 55 | if (argc < 2 || argc > 3) { 56 | fprintf(stderr, "[SIGNALS] Invalid argument count.\n"); 57 | exit(1); 58 | } 59 | 60 | #ifdef EXEC 61 | if (argc == 3 && strcmp(argv[2], "child") != 0) { 62 | fprintf(stderr, "[SIGNALS] argv[3] should never be set manually.\n"); 63 | exit(2); 64 | } 65 | #else 66 | if (argc == 3) { 67 | fprintf(stderr, "[SIGNALS] Invalid argument count. Only EXEC version takes in 3 arguments.\n"); 68 | exit(2); 69 | } 70 | #endif 71 | 72 | TASK_TYPE task_type = INVALID; 73 | if ((task_type = str_to_task_type(argv[1])) == INVALID) { 74 | fprintf(stderr, "[SIGNALS] Invalid task_type (%s).\n", argv[2]); 75 | exit(3); 76 | } 77 | 78 | #ifdef EXEC 79 | if (task_type == HANDLER) { 80 | fprintf(stderr, "[SIGNALS] Handler is not a valid task_type for EXEC version.\n"); 81 | exit(4); 82 | } 83 | 84 | if (argc == 3) { 85 | is_parent = 0; 86 | } 87 | #endif 88 | 89 | return task_type; 90 | } 91 | 92 | void process(TASK_TYPE task_type) { 93 | switch (task_type) { 94 | case IGNORE: 95 | signal(SIGNAL, SIG_IGN); 96 | raise_signal(); 97 | break; 98 | 99 | case HANDLER: 100 | signal(SIGNAL, signal_handler); 101 | raise_signal(); 102 | break; 103 | 104 | case MASK: 105 | case PENDING: 106 | signal(SIGNAL, signal_handler); 107 | 108 | if (is_parent) { 109 | block_signal(); 110 | raise_signal(); 111 | } 112 | 113 | if (task_type == PENDING) 114 | print_is_pending(); 115 | break; 116 | 117 | case INVALID: 118 | break; 119 | } 120 | } 121 | 122 | int main(int argc, char** argv) { 123 | TASK_TYPE task_type = handle_input(argc, argv); 124 | 125 | process(task_type); 126 | fflush(NULL); 127 | 128 | #ifdef EXEC 129 | if (is_parent) 130 | execl(argv[0], argv[0], argv[1], "child", NULL); 131 | #else 132 | is_parent = fork(); 133 | if (!is_parent) 134 | process(task_type); 135 | else 136 | wait(NULL); 137 | #endif 138 | 139 | fflush(NULL); 140 | return 0; 141 | } -------------------------------------------------------------------------------- /cw04/zad2/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: sigtest.c 6 | $(CC) $(CFLAGS) sigtest.c -o sigtest 7 | 8 | report: 9 | echo "Report is just a copied output, signals can't be used well with make." 10 | 11 | clean: 12 | rm -f sigtest 13 | -------------------------------------------------------------------------------- /cw04/zad2/report.txt: -------------------------------------------------------------------------------- 1 | [SIGTEST] SIGINFO: 2 | [SIGTEST] SELF: 3 | [SIGTEST] Signal number: 10 4 | [SIGTEST] PID: 35050 5 | [SIGTEST] UID: 1000 6 | [SIGTEST] POSIX timer ID: 35050 7 | [SIGTEST] Exit value / signal: 0 8 | 9 | [SIGTEST] CHILD: 10 | [SIGTEST] Signal number: 10 11 | [SIGTEST] PID: 35051 12 | [SIGTEST] UID: 1000 13 | [SIGTEST] POSIX timer ID: 35051 14 | [SIGTEST] Exit value / signal: 0 15 | 16 | [SIGTEST] CUSTOM: 17 | [SIGTEST] Signal number: 10 18 | [SIGTEST] PID: 35050 19 | [SIGTEST] UID: 1000 20 | [SIGTEST] POSIX timer ID: 35050 21 | [SIGTEST] Exit value / signal: badc0de 22 | 23 | [SIGTEST] NODEFER: 24 | [SIGTEST] (+) call id: 0, call depth: 0 25 | [SIGTEST] (+) call id: 1, call depth: 1 26 | [SIGTEST] (+) call id: 2, call depth: 2 27 | [SIGTEST] (+) call id: 3, call depth: 3 28 | [SIGTEST] (-) call id: 3, call depth: 3 29 | [SIGTEST] (-) call id: 3, call depth: 2 30 | [SIGTEST] (-) call id: 3, call depth: 1 31 | [SIGTEST] (-) call id: 3, call depth: 0 32 | 33 | [SIGTEST] RESETHAND: 34 | [SIGTEST] (+) call id: 0, call depth: 0 35 | [SIGTEST] (-) call id: 0, call depth: 0 36 | User defined signal 1 37 | -------------------------------------------------------------------------------- /cw04/zad2/sigtest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define SIGNAL SIGUSR1 8 | #define MAX_CALLS 4 9 | 10 | #define ANSI_COLOR_GREEN "\x1b[32m" 11 | #define ANSI_COLOR_CYAN "\x1b[36m" 12 | #define ANSI_COLOR_RESET "\x1b[0m" 13 | #define ANSI_COLOR_BOLD "\x1b[1m" 14 | 15 | int call_id; 16 | int call_depth; 17 | 18 | typedef void (*handler_t)(int, siginfo_t*, void*); 19 | 20 | void set_action(struct sigaction action, handler_t handler, int flag) { 21 | sigemptyset(&action.sa_mask); 22 | action.sa_sigaction = handler; 23 | action.sa_flags = flag; 24 | sigaction(SIGNAL, &action, NULL); 25 | call_id = 0; 26 | call_depth = 0; 27 | } 28 | 29 | void depth_handler(int signo, siginfo_t* info, void* context) { 30 | printf("[SIGTEST] (+) call id: %d, call depth: %d\n", call_id, call_depth); 31 | 32 | call_id++; 33 | call_depth++; 34 | if (call_id < MAX_CALLS) 35 | kill(getpid(), SIGNAL); 36 | call_depth--; 37 | 38 | printf("[SIGTEST] (-) call id: %d, call depth: %d\n", call_id-1, call_depth); 39 | } 40 | 41 | void info_handler(int signo, siginfo_t* info, void* context) { 42 | printf(ANSI_COLOR_BOLD "[SIGTEST] Signal number:%s %d%s\n", ANSI_COLOR_RESET ANSI_COLOR_CYAN, info->si_signo, ANSI_COLOR_RESET); 43 | printf(ANSI_COLOR_BOLD "[SIGTEST] PID:%s %d%s\n", ANSI_COLOR_RESET ANSI_COLOR_CYAN, info->si_pid, ANSI_COLOR_RESET); 44 | printf(ANSI_COLOR_BOLD "[SIGTEST] UID:%s %d%s\n", ANSI_COLOR_RESET ANSI_COLOR_CYAN, info->si_uid, ANSI_COLOR_RESET); 45 | printf(ANSI_COLOR_BOLD "[SIGTEST] POSIX timer ID:%s %d%s\n", ANSI_COLOR_RESET ANSI_COLOR_CYAN, info->si_timerid, ANSI_COLOR_RESET); 46 | printf(ANSI_COLOR_BOLD "[SIGTEST] Exit value / signal:%s %x%s\n", ANSI_COLOR_RESET ANSI_COLOR_CYAN, info->si_status, ANSI_COLOR_RESET); 47 | } 48 | 49 | void testNODEFER(struct sigaction act) { 50 | set_action(act, depth_handler, SA_NODEFER); 51 | kill(getpid(), SIGNAL); 52 | } 53 | 54 | void testSIGINFO(struct sigaction act) { 55 | printf(ANSI_COLOR_BOLD "[SIGTEST] SELF:\n" ANSI_COLOR_RESET); 56 | set_action(act, info_handler, SA_SIGINFO); 57 | kill(getpid(), SIGNAL); 58 | printf("\n"); 59 | 60 | printf(ANSI_COLOR_BOLD "[SIGTEST] CHILD:\n" ANSI_COLOR_RESET); 61 | if (fork() == 0) { 62 | kill(getpid(), SIGNAL); 63 | exit(0); 64 | } 65 | else 66 | wait(NULL); 67 | printf("\n"); 68 | 69 | printf(ANSI_COLOR_BOLD "[SIGTEST] CUSTOM:\n" ANSI_COLOR_RESET); 70 | set_action(act, info_handler, SA_SIGINFO); 71 | sigval_t sig_val = {0xBADC0DE}; 72 | sigqueue(getpid(), SIGNAL, sig_val); 73 | } 74 | 75 | void testRESETHAND(struct sigaction act) { 76 | set_action(act, depth_handler, SA_RESETHAND); 77 | kill(getpid(), SIGNAL); 78 | kill(getpid(), SIGNAL); 79 | } 80 | 81 | int main(int argc, char** argv) { 82 | if (argc != 1) { 83 | fprintf(stderr, "[SIGTEST] Invalid arguments.\n"); 84 | exit(1); 85 | } 86 | 87 | struct sigaction action; 88 | 89 | printf(ANSI_COLOR_GREEN ANSI_COLOR_BOLD "[SIGTEST] SIGINFO:\n" ANSI_COLOR_RESET); 90 | testSIGINFO(action); 91 | printf("\n"); 92 | 93 | printf(ANSI_COLOR_GREEN ANSI_COLOR_BOLD "[SIGTEST] NODEFER:\n" ANSI_COLOR_RESET); 94 | testNODEFER(action); 95 | printf("\n"); 96 | 97 | printf(ANSI_COLOR_GREEN ANSI_COLOR_BOLD "[SIGTEST] RESETHAND:\n" ANSI_COLOR_RESET); 98 | testRESETHAND(action); 99 | 100 | return 0; 101 | } -------------------------------------------------------------------------------- /cw04/zad3/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: 6 | make catcher 7 | make sender 8 | 9 | catcher: catcher.c 10 | $(CC) $(CFLAGS) catcher.c -o catcher 11 | 12 | sender: sender.c 13 | $(CC) $(CFLAGS) sender.c -o sender 14 | 15 | clean: 16 | rm -f catcher sender 17 | -------------------------------------------------------------------------------- /cw04/zad3/catcher.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define SIGNAL SIGUSR1 9 | 10 | typedef enum { 11 | NUMBERS = 1, 12 | TIME_ONCE = 2, 13 | REQUESTS = 3, 14 | TIME_LOOP = 4, 15 | FINISH = 5, 16 | } CATCHER_STATE; 17 | 18 | CATCHER_STATE state = NUMBERS; 19 | bool done = true; 20 | int request_count = 0; 21 | 22 | void print_current_time() { 23 | static time_t raw_time; 24 | static struct tm* time_info; 25 | time(&raw_time); 26 | time_info = localtime(&raw_time); 27 | printf("[CATCHER] Current timestamp: %s", asctime(time_info)); 28 | } 29 | 30 | void numbers() { 31 | for (int i = 1; i <= 100; i++) 32 | printf("[CATCHER] %i\n", i); 33 | } 34 | 35 | void time_once() { 36 | print_current_time(); 37 | } 38 | 39 | void requests() { 40 | printf("[CATCHER] Number of valid requests: %d\n", request_count); 41 | } 42 | 43 | void time_loop(double dt) { 44 | static double acc = 1.0; 45 | 46 | acc += dt; 47 | if (acc >= 1.0) { 48 | print_current_time(); 49 | acc = 0.0; 50 | } 51 | } 52 | 53 | void finish() { 54 | printf("[CATCHER] Ending.\n"); 55 | fflush(NULL); 56 | exit(0); 57 | } 58 | 59 | void handler(int signo, siginfo_t* info, void* context) { 60 | int pid = info->si_pid; 61 | int request = info->si_status; 62 | 63 | if (request < 1 || request > 5) { 64 | fprintf(stderr, "[CATCHER] Invalid request (%d).\n", request); 65 | } else { 66 | request_count += 1; 67 | state = (CATCHER_STATE) request; 68 | done = false; 69 | } 70 | 71 | kill(pid, SIGNAL); 72 | } 73 | 74 | double maxd(double x, double y) { 75 | return x > y ? x : y; 76 | } 77 | 78 | int main() { 79 | 80 | struct sigaction action; 81 | sigemptyset(&action.sa_mask); 82 | action.sa_sigaction = handler; 83 | action.sa_flags = SA_SIGINFO; 84 | sigaction(SIGNAL, &action, NULL); 85 | 86 | clock_t begin = clock(); 87 | clock_t end = clock(); 88 | 89 | printf("[CATCHER] Starting, PID=(%d)\n", getpid()); 90 | while (true) { 91 | double dt = maxd(0.0, (double)(end-begin)/CLOCKS_PER_SEC); 92 | 93 | begin = clock(); 94 | 95 | if (done) 96 | continue; 97 | 98 | switch (state) { 99 | case NUMBERS: numbers(); break; 100 | case TIME_ONCE: time_once(); break; 101 | case REQUESTS: requests(); break; 102 | case TIME_LOOP: time_loop(dt); break; 103 | case FINISH: finish(); break; 104 | } 105 | 106 | if (state != TIME_LOOP) 107 | done = true; 108 | 109 | end = clock(); 110 | } 111 | 112 | return 0; 113 | } 114 | 115 | -------------------------------------------------------------------------------- /cw04/zad3/sender.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define SIGNAL SIGUSR1 9 | #define STATES_START 2 10 | 11 | volatile int ack = STATES_START; 12 | 13 | void handler(int signo) { 14 | ack++; 15 | } 16 | 17 | int main(int argc, char** argv) { 18 | if (argc < 3) { 19 | fprintf(stderr, "[SENDER] Invalid arguments.\n"); 20 | exit(1); 21 | } 22 | 23 | int state; 24 | bool failed = false; 25 | 26 | int catcher_pid = atoi(argv[1]); 27 | for (int i = STATES_START; i < argc; i++) { 28 | failed = false; 29 | state = atoi(argv[i]); 30 | 31 | struct sigaction action; 32 | sigemptyset(&action.sa_mask); 33 | action.sa_handler = handler; 34 | sigaction(SIGNAL, &action, NULL); 35 | 36 | sigval_t sig_val = {state}; 37 | sigqueue(catcher_pid, SIGNAL, sig_val); 38 | printf("[SENDER] Sent state (%d).\n", state); 39 | 40 | time_t start = clock(); 41 | while (ack <= i) { 42 | if (clock()-start > CLOCKS_PER_SEC*5) { 43 | printf("[SENDER] No confirmation, retrying.\n"); 44 | failed = true; 45 | i--; 46 | break; 47 | } 48 | } 49 | 50 | if (!failed) 51 | printf("[SENDER] Received comfirmation.\n"); 52 | } 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /cw05/report.txt: -------------------------------------------------------------------------------- 1 | Ukonczone 100% 2 | 3 | Zad 1. 4 | Zamiast `mail` program wykonuje `fortune | cowsay` zgodnie z umowa z zajec. 5 | 6 | Zad 2. 7 | `make report` tworzy raport 8 | 9 | Zad 3. 10 | Przy czytaniu z fifo wykorzystany zostal strtok poniewaz pare dzieci moze napisac zanim parent zdazy odczytac. 11 | `make report` tworzy raport 12 | 13 | 🐄 14 | -------------------------------------------------------------------------------- /cw05/zad1/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: fortune_cowsay.c 6 | $(CC) $(CFLAGS) fortune_cowsay.c -o fortune_cowsay 7 | 8 | clean: 9 | rm -f fortune_cowsay 10 | -------------------------------------------------------------------------------- /cw05/zad1/fortune_cowsay.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define BUFF_SIZE 2048 6 | char fortune_buff[BUFF_SIZE] = ""; 7 | 8 | int main(int argc, char** argv) { 9 | FILE* fortune_output = popen("fortune", "r"); 10 | size_t size = fread(fortune_buff, sizeof(char), BUFF_SIZE, fortune_output); 11 | fortune_buff[size] = 0; 12 | pclose(fortune_output); 13 | 14 | FILE* cowsay_input = popen("cowsay", "w"); 15 | fwrite(fortune_buff, sizeof(char), strlen(fortune_buff), cowsay_input); 16 | pclose(cowsay_input); 17 | } 18 | -------------------------------------------------------------------------------- /cw05/zad2/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: integral.c 6 | $(CC) $(CFLAGS) integral.c -o integral 7 | 8 | report: 9 | make all 10 | echo "" > report.txt 11 | ./integral 0.000000001 1 >> report.txt 12 | ./integral 0.000000001 2 >> report.txt 13 | ./integral 0.000000001 10 >> report.txt 14 | 15 | clean: 16 | rm -f integral 17 | -------------------------------------------------------------------------------- /cw05/zad2/integral.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define BUFF_SIZE 256 9 | char write_buff[BUFF_SIZE] = ""; 10 | char read_buff[BUFF_SIZE] = ""; 11 | int _; 12 | 13 | double f(double x) { 14 | return 4/(x*x+1); 15 | } 16 | 17 | double integral(double a, double b, double dx) { 18 | double acc = 0.0; 19 | for (double x = a; x < b; x += dx) 20 | acc += f(x)*dx; 21 | return acc; 22 | } 23 | 24 | double sum(double* arr, double n) { 25 | double acc = 0.0; 26 | for (int i = 0; i < n; i++) 27 | acc += arr[i]; 28 | return acc; 29 | } 30 | 31 | struct timespec timespec_diff(struct timespec start, struct timespec end) { 32 | struct timespec out; 33 | 34 | if ((end.tv_nsec-start.tv_nsec)<0) { 35 | out.tv_sec = end.tv_sec-start.tv_sec-1; 36 | out.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; 37 | } 38 | else { 39 | out.tv_sec = end.tv_sec-start.tv_sec; 40 | out.tv_nsec = end.tv_nsec-start.tv_nsec; 41 | } 42 | return out; 43 | } 44 | 45 | 46 | int main(int argc, char** argv) { 47 | double dx = strtod(argv[1], NULL); 48 | int n = atoi(argv[2]); 49 | double inv_n = 1.0/n; 50 | 51 | struct timespec timespec_buff_start, timespec_buff_end; 52 | clock_gettime(CLOCK_REALTIME, ×pec_buff_start); 53 | 54 | int* pipes = calloc(n, sizeof(int)); 55 | 56 | for (int i = 0; i < n; i++) { 57 | int fd[2]; 58 | _ = pipe(fd); 59 | if (fork()) { // parent 60 | close(fd[1]); 61 | pipes[i] = fd[0]; 62 | } else { // child 63 | close(fd[0]); 64 | double out = integral(i*inv_n, (i+1)*inv_n, dx); 65 | size_t size = snprintf(write_buff, BUFF_SIZE, "%lf", out); 66 | _ = write(fd[1], write_buff, size); 67 | exit(0); 68 | } 69 | } 70 | 71 | while (wait(NULL) > 0); 72 | 73 | double result = 0.0; 74 | for (int i = 0; i < n; i++) { 75 | size_t size = read(pipes[i], read_buff, BUFF_SIZE); 76 | read_buff[size] = 0; 77 | result += strtod(read_buff, NULL); 78 | } 79 | 80 | free(pipes); 81 | 82 | clock_gettime(CLOCK_REALTIME, ×pec_buff_end); 83 | struct timespec diff = timespec_diff(timespec_buff_start, timespec_buff_end); 84 | printf("RESULT: %lf\nN: %d\nDX: %.17f\nTIME: %lds %ldns\n\n", result, n, dx, diff.tv_sec, diff.tv_nsec); 85 | 86 | return 0; 87 | } 88 | 89 | -------------------------------------------------------------------------------- /cw05/zad2/report.txt: -------------------------------------------------------------------------------- 1 | 2 | RESULT: 3.141593 3 | N: 1 4 | DX: 0.00000000100000000 5 | TIME: 2s 91172985ns 6 | 7 | RESULT: 3.141592 8 | N: 2 9 | DX: 0.00000000100000000 10 | TIME: 1s 51517913ns 11 | 12 | RESULT: 3.141594 13 | N: 10 14 | DX: 0.00000000100000000 15 | TIME: 0s 420399072ns 16 | 17 | -------------------------------------------------------------------------------- /cw05/zad3/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: 6 | make integral 7 | make worker 8 | 9 | integral: integral.c 10 | $(CC) $(CFLAGS) integral.c -o integral 11 | 12 | worker: worker.c 13 | $(CC) $(CFLAGS) worker.c -o worker 14 | 15 | report: 16 | make all 17 | echo "" > report.txt 18 | ./integral 0.000000001 1 >> report.txt 19 | ./integral 0.000000001 2 >> report.txt 20 | ./integral 0.000000001 10 >> report.txt 21 | 22 | clean: 23 | rm -f integral worker 24 | -------------------------------------------------------------------------------- /cw05/zad3/integral.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #define FIFO_PATH "/tmp/integral_queue" 13 | #define BUFF_SIZE 2048 14 | char write_buff[BUFF_SIZE] = ""; 15 | char read_buff[BUFF_SIZE] = ""; 16 | int _; 17 | 18 | char arg2[BUFF_SIZE]; 19 | char arg3[BUFF_SIZE]; 20 | 21 | struct timespec timespec_diff(struct timespec start, struct timespec end) { 22 | struct timespec out; 23 | 24 | if ((end.tv_nsec-start.tv_nsec)<0) { 25 | out.tv_sec = end.tv_sec-start.tv_sec-1; 26 | out.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; 27 | } 28 | else { 29 | out.tv_sec = end.tv_sec-start.tv_sec; 30 | out.tv_nsec = end.tv_nsec-start.tv_nsec; 31 | } 32 | return out; 33 | } 34 | 35 | 36 | int main(int argc, char** argv) { 37 | double dx = strtod(argv[1], NULL); 38 | int n = atoi(argv[2]); 39 | double inv_n = 1.0/n; 40 | 41 | struct timespec timespec_buff_start, timespec_buff_end; 42 | clock_gettime(CLOCK_REALTIME, ×pec_buff_start); 43 | 44 | mkfifo(FIFO_PATH, 0666); 45 | 46 | for (int i = 0; i < n; i++) 47 | if (!fork()) { 48 | snprintf(arg2, BUFF_SIZE, "%lf", i*inv_n); 49 | snprintf(arg3, BUFF_SIZE, "%lf", (i+1)*inv_n); 50 | execl("./worker", "worker", argv[1], arg2, arg3, NULL); 51 | } 52 | 53 | double result = 0.0; 54 | 55 | int fifo = open(FIFO_PATH, O_RDONLY); 56 | int already_read = 0; 57 | while (already_read < n) { 58 | size_t size = read(fifo, read_buff, BUFF_SIZE); 59 | read_buff[size] = 0; 60 | 61 | char delim[] = "\n"; 62 | char* token; 63 | 64 | token = strtok(read_buff, delim); 65 | for (;token; token = strtok(NULL, delim)) { 66 | result += strtod(token, NULL); 67 | already_read++; 68 | } 69 | } 70 | close(fifo); 71 | 72 | remove(FIFO_PATH); 73 | 74 | clock_gettime(CLOCK_REALTIME, ×pec_buff_end); 75 | struct timespec diff = timespec_diff(timespec_buff_start, timespec_buff_end); 76 | printf("RESULT: %lf\nN: %d\nDX: %.17f\nTIME: %lds %ldns\n\n", result, n, dx, diff.tv_sec, diff.tv_nsec); 77 | 78 | return 0; 79 | } 80 | 81 | -------------------------------------------------------------------------------- /cw05/zad3/report.txt: -------------------------------------------------------------------------------- 1 | 2 | RESULT: 3.141593 3 | N: 1 4 | DX: 0.00000000100000000 5 | TIME: 2s 112599837ns 6 | 7 | RESULT: 3.141592 8 | N: 2 9 | DX: 0.00000000100000000 10 | TIME: 1s 80949331ns 11 | 12 | RESULT: 3.141594 13 | N: 10 14 | DX: 0.00000000100000000 15 | TIME: 0s 420206634ns 16 | 17 | -------------------------------------------------------------------------------- /cw05/zad3/worker.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define FIFO_PATH "/tmp/integral_queue" 7 | #define BUFF_SIZE 256 8 | char write_buff[BUFF_SIZE] = ""; 9 | int _; 10 | 11 | double f(double x) { 12 | return 4/(x*x+1); 13 | } 14 | 15 | double integral(double a, double b, double dx) { 16 | double acc = 0.0; 17 | for (double x = a; x < b; x += dx) 18 | acc += f(x)*dx; 19 | return acc; 20 | } 21 | 22 | int main(int argc, char** argv) { 23 | double dx = strtod(argv[1], NULL); 24 | double a = strtod(argv[2], NULL); 25 | double b = strtod(argv[3], NULL); 26 | 27 | double out = integral(a, b, dx); 28 | size_t size = snprintf(write_buff, BUFF_SIZE, "%lf\n", out); 29 | 30 | int fifo = open(FIFO_PATH, O_WRONLY); 31 | _ = write(fifo, write_buff, size); 32 | close(fifo); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /cw06/report.txt: -------------------------------------------------------------------------------- 1 | Ukonczone 100% 2 | 3 | Zad 1. 4 | *brak komentarza* 5 | 6 | Zad 2. 7 | *brak komentarza* 8 | 9 | 🤔 10 | -------------------------------------------------------------------------------- /cw06/zad1/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: 6 | $(CC) $(CFLAGS) server.c -o server 7 | $(CC) $(CFLAGS) client.c -o client 8 | 9 | clean: 10 | rm -f server client -------------------------------------------------------------------------------- /cw06/zad1/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "common.h" 9 | 10 | key_t qKey; 11 | int qID; 12 | int squeueID; 13 | int client_id; 14 | 15 | int handle_init(); 16 | void handle_stop(); 17 | void handle_server_message(); 18 | void handle_LIST(); 19 | void handle_2ALL(char *message); 20 | void handle_2ONE(int other_id, char *message); 21 | 22 | int main() { 23 | srand(time(NULL)); 24 | 25 | qKey = ftok(HOME_PATH, rand() % 255 + 1); 26 | qID = msgget(qKey, IPC_CREAT | 0666); 27 | key_t sKey = ftok(HOME_PATH, SERVER_ID); 28 | squeueID = msgget(sKey, 0); 29 | client_id = handle_init(); 30 | 31 | signal(SIGINT, handle_stop); 32 | 33 | size_t len = 0; 34 | ssize_t read; 35 | char *command = NULL; 36 | 37 | while (1) { 38 | printf("User command: "); 39 | read = getline(&command, &len, stdin); 40 | command[read - 1] = '\0'; 41 | 42 | handle_server_message(); 43 | 44 | if (strcmp(command, "") == 0) { 45 | continue; 46 | } 47 | 48 | char *curr_cmd = strtok(command, " "); 49 | if (strcmp(curr_cmd, "LIST") == 0) { 50 | handle_LIST(); 51 | } else if (strcmp(curr_cmd, "2ALL") == 0) { 52 | curr_cmd = strtok(NULL, " "); 53 | char *message = curr_cmd; 54 | handle_2ALL(message); 55 | } else if (strcmp(curr_cmd, "2ONE") == 0) { 56 | curr_cmd = strtok(NULL, " "); 57 | int destinationID = atoi(curr_cmd); 58 | curr_cmd = strtok(NULL, " "); 59 | char *message = curr_cmd; 60 | handle_2ONE(destinationID, message); 61 | } else if (strcmp(curr_cmd, "STOP") == 0) { 62 | handle_stop(); 63 | } else { 64 | printf("Command not recognized!\n"); 65 | } 66 | } 67 | } 68 | 69 | int handle_init() { 70 | time_t my_time = time(NULL); 71 | MessageBuffer *pMessageBuffer = malloc(sizeof(MessageBuffer)); 72 | 73 | pMessageBuffer->time_struct = *localtime(&my_time); 74 | pMessageBuffer->type = INIT; 75 | pMessageBuffer->queue_key = qKey; 76 | 77 | msgsnd(squeueID, pMessageBuffer, MSG_SIZE, 0); 78 | msgrcv(qID, pMessageBuffer, MSG_SIZE, 0, 0); 79 | 80 | int client_id = pMessageBuffer->client_id; 81 | if (client_id == -1) { 82 | printf("Client limit had been reached, leaving..\n"); 83 | exit(0); 84 | } 85 | 86 | return client_id; 87 | } 88 | 89 | void handle_LIST() { 90 | time_t my_time = time(NULL); 91 | MessageBuffer *pMessageBuffer = malloc(sizeof(MessageBuffer)); 92 | 93 | pMessageBuffer->time_struct = *localtime(&my_time); 94 | pMessageBuffer->type = LIST; 95 | pMessageBuffer->client_id = client_id; 96 | 97 | msgsnd(squeueID, pMessageBuffer, MSG_SIZE, 0); 98 | msgrcv(qID, pMessageBuffer, MSG_SIZE, 0, 0); 99 | printf("%s\n", pMessageBuffer->content); 100 | } 101 | 102 | void handle_2ALL(char *message) { 103 | time_t my_time = time(NULL); 104 | MessageBuffer *pMessageBuffer = malloc(sizeof(MessageBuffer)); 105 | 106 | pMessageBuffer->time_struct = *localtime(&my_time); 107 | pMessageBuffer->type = TALL; 108 | strcpy(pMessageBuffer->content, message); 109 | 110 | pMessageBuffer->client_id = client_id; 111 | msgsnd(squeueID, pMessageBuffer, MSG_SIZE, 0); 112 | } 113 | 114 | void handle_2ONE(int other_id, char *message) { 115 | time_t my_time = time(NULL); 116 | MessageBuffer *pMessageBuffer = malloc(sizeof(MessageBuffer)); 117 | 118 | pMessageBuffer->time_struct = *localtime(&my_time); 119 | pMessageBuffer->type = TONE; 120 | strcpy(pMessageBuffer->content, message); 121 | 122 | pMessageBuffer->client_id = client_id; 123 | pMessageBuffer->other_id = other_id; 124 | msgsnd(squeueID, pMessageBuffer, MSG_SIZE, 0); 125 | } 126 | 127 | void handle_stop() { 128 | time_t my_time = time(NULL); 129 | MessageBuffer *pMessageBuffer = malloc(sizeof(MessageBuffer)); 130 | 131 | pMessageBuffer->time_struct = *localtime(&my_time); 132 | pMessageBuffer->type = STOP; 133 | pMessageBuffer->client_id = client_id; 134 | 135 | msgsnd(squeueID, pMessageBuffer, MSG_SIZE, 0); 136 | msgctl(qID, IPC_RMID, NULL); 137 | exit(0); 138 | } 139 | 140 | void handle_server_message() { 141 | MessageBuffer *msg_rcv = malloc(sizeof(MessageBuffer)); 142 | while (msgrcv(qID, msg_rcv, MSG_SIZE, 0, IPC_NOWAIT) >= 0) { 143 | if (msg_rcv->type == STOP) { 144 | printf("Received stop message, leaving..\n"); 145 | handle_stop(); 146 | } else { 147 | struct tm my_time = msg_rcv->time_struct; 148 | printf("Msg from: %d has been sent at %02d:%02d:%02d:\n%s\n", 149 | msg_rcv->client_id, 150 | my_time.tm_hour, 151 | my_time.tm_min, 152 | my_time.tm_sec, 153 | msg_rcv->content); 154 | } 155 | } 156 | } -------------------------------------------------------------------------------- /cw06/zad1/common.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMMON_H__ 2 | #define __COMMON_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | #define HOME_PATH getenv("HOME") 13 | #define SERVER_ID 1 14 | #define MAX_NO_CLIENTS 10 15 | #define MAX_SIZE_MESSAGE 512 16 | 17 | 18 | typedef struct MessageBuffer { 19 | key_t queue_key; 20 | int client_id; 21 | int other_id; 22 | long type; 23 | char content[MAX_SIZE_MESSAGE]; 24 | struct tm time_struct; 25 | } MessageBuffer; 26 | 27 | typedef enum MessageType { 28 | INIT = 1, 29 | LIST = 2, 30 | TONE = 3, 31 | TALL = 4, 32 | STOP = 5 33 | } MessageType; 34 | 35 | 36 | const int MSG_SIZE = sizeof(MessageBuffer); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /cw06/zad1/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "common.h" 9 | 10 | int first_available_ID = 0; 11 | int squeue; 12 | key_t cQueues[MAX_NO_CLIENTS]; 13 | 14 | void service_INIT(MessageBuffer *messageBuffer); 15 | 16 | void service_STOP(int cID); 17 | 18 | void service_END(); 19 | 20 | void service_LIST(int cID); 21 | 22 | void service_2ALL(MessageBuffer *messageBuffer); 23 | 24 | void service_2ONE(MessageBuffer *messageBuffer); 25 | 26 | void store_message_logs(MessageBuffer *messageBuffer); 27 | 28 | int main() { 29 | 30 | for (int i = 0; i < MAX_NO_CLIENTS; i++) { 31 | cQueues[i] = -1; 32 | } 33 | 34 | key_t queue_key = ftok(HOME_PATH, SERVER_ID); 35 | squeue = msgget(queue_key, IPC_CREAT | 0666); 36 | 37 | signal(SIGINT, service_END); 38 | 39 | MessageBuffer *pMessageBuffer = malloc(sizeof(MessageBuffer)); 40 | while (1) { 41 | msgrcv(squeue, pMessageBuffer, MSG_SIZE, -6, 0); 42 | 43 | switch (pMessageBuffer->type) { 44 | case INIT: 45 | service_INIT(pMessageBuffer); 46 | break; 47 | case LIST: 48 | service_LIST(pMessageBuffer->client_id); 49 | store_message_logs(pMessageBuffer); 50 | break; 51 | case TALL: 52 | service_2ALL(pMessageBuffer); 53 | store_message_logs(pMessageBuffer); 54 | break; 55 | case TONE: 56 | service_2ONE(pMessageBuffer); 57 | store_message_logs(pMessageBuffer); 58 | break; 59 | case STOP: 60 | service_STOP(pMessageBuffer->client_id); 61 | store_message_logs(pMessageBuffer); 62 | break; 63 | default: 64 | printf("Unexpected message type!\n"); 65 | } 66 | } 67 | } 68 | 69 | void service_INIT(MessageBuffer *messageBuffer) { 70 | while (cQueues[first_available_ID] != -1 && first_available_ID < MAX_NO_CLIENTS - 1) { 71 | first_available_ID++; 72 | } 73 | 74 | 75 | if (cQueues[first_available_ID] != -1 && first_available_ID == MAX_NO_CLIENTS - 1) { 76 | messageBuffer->client_id = -1; 77 | } else { 78 | messageBuffer->client_id = first_available_ID; 79 | cQueues[first_available_ID] = messageBuffer->queue_key; 80 | 81 | if (first_available_ID < MAX_NO_CLIENTS - 1) { 82 | first_available_ID++; 83 | } 84 | } 85 | 86 | int cQueueID = msgget(messageBuffer->queue_key, 0); 87 | msgsnd(cQueueID, messageBuffer, MSG_SIZE, 0); 88 | store_message_logs(messageBuffer); 89 | } 90 | 91 | void service_STOP(int cID) { 92 | cQueues[cID] = -1; 93 | 94 | if (cID < first_available_ID) { 95 | first_available_ID = cID; 96 | } 97 | } 98 | 99 | void service_END() { 100 | MessageBuffer *pMessageBuffer = malloc(sizeof(MessageBuffer)); 101 | 102 | for (int i = 0; i < MAX_NO_CLIENTS; i++) { 103 | if (cQueues[i] != -1) { 104 | pMessageBuffer->type = STOP; 105 | int cQueueID = msgget(cQueues[i], 0); 106 | msgsnd(cQueueID, pMessageBuffer, MSG_SIZE, 0); 107 | msgrcv(squeue, pMessageBuffer, MSG_SIZE, STOP, 0); 108 | } 109 | } 110 | 111 | msgctl(squeue, IPC_RMID, NULL); 112 | exit(0); 113 | } 114 | 115 | void service_LIST(int cID) { 116 | MessageBuffer *pMessageBuffer = malloc(sizeof(MessageBuffer)); 117 | strcpy(pMessageBuffer->content, ""); 118 | 119 | for (int i = 0; i < MAX_NO_CLIENTS; i++) { 120 | if (cQueues[i] != -1) { 121 | sprintf(pMessageBuffer->content + strlen(pMessageBuffer->content), "ID %d is running..\n", i); 122 | } 123 | } 124 | 125 | pMessageBuffer->type = LIST; 126 | int cQueueID = msgget(cQueues[cID], 0); 127 | 128 | msgsnd(cQueueID, pMessageBuffer, MSG_SIZE, 0); 129 | } 130 | 131 | void service_2ALL(MessageBuffer *messageBuffer) { 132 | for (int i = 0; i < MAX_NO_CLIENTS; i++) { 133 | if (cQueues[i] != -1 && i != messageBuffer->client_id) { 134 | int otherQueueID = msgget(cQueues[i], 0); 135 | msgsnd(otherQueueID, messageBuffer, MSG_SIZE, 0); 136 | } 137 | } 138 | } 139 | 140 | void service_2ONE(MessageBuffer *messageBuffer) { 141 | int otherQueueID = msgget(cQueues[messageBuffer->other_id], 0); 142 | msgsnd(otherQueueID, messageBuffer, MSG_SIZE, 0); 143 | } 144 | 145 | void store_message_logs(MessageBuffer *messageBuffer) { 146 | struct tm my_time = messageBuffer->time_struct; 147 | 148 | FILE *result_file = fopen("logs.txt", "a"); 149 | 150 | switch (messageBuffer->type) { 151 | case INIT: 152 | if (messageBuffer->client_id == -1) { 153 | fprintf(result_file, "(INIT) Max number of clients is reached!\n"); 154 | } else { 155 | fprintf(result_file, "(INIT) Client ID: %d\n", messageBuffer->client_id); 156 | } 157 | break; 158 | case LIST: 159 | fprintf(result_file, "(LIST) Client ID: %d\n", messageBuffer->client_id); 160 | break; 161 | case TALL: 162 | fprintf(result_file, "Message: %s\n", messageBuffer->content); 163 | fprintf(result_file, "(2ALL) Client ID: %d\n", messageBuffer->client_id); 164 | break; 165 | case TONE: 166 | fprintf(result_file, "Message: %s\n", messageBuffer->content); 167 | fprintf(result_file, "(2ONE) Sender ID: %d, Receiver ID %d\n", messageBuffer->client_id, messageBuffer->other_id); 168 | break; 169 | case STOP: 170 | fprintf(result_file, "(STOP) Client ID: %d\n", messageBuffer->client_id); 171 | break; 172 | } 173 | 174 | fprintf(result_file, "sent at: %02d:%02d:%02d\n\n\n", 175 | my_time.tm_hour, 176 | my_time.tm_min, 177 | my_time.tm_sec); 178 | 179 | fclose(result_file); 180 | } 181 | -------------------------------------------------------------------------------- /cw06/zad2/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: 6 | $(CC) $(CFLAGS) server.c -o server -lrt 7 | $(CC) $(CFLAGS) client.c -o client -lrt 8 | 9 | clean: 10 | rm -f server client -------------------------------------------------------------------------------- /cw06/zad2/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "common.h" 9 | 10 | int cID; 11 | char qName[LENN]; 12 | mqd_t qDesc; 13 | mqd_t sQueueDesc; 14 | 15 | int manage_init(); 16 | void manage_stop(); 17 | void handle_server_message(); 18 | void get_new_name(); 19 | void manage_LIST(); 20 | void manage_2ALL(char *message); 21 | void manage_2ONE(int other_id, char *message); 22 | 23 | int main() { 24 | srand(time(NULL)); 25 | 26 | get_new_name(); 27 | qDesc = create_queue(qName); 28 | sQueueDesc = mq_open(SQUEUE, O_RDWR); 29 | cID = manage_init(); 30 | 31 | signal(SIGINT, manage_stop); 32 | 33 | size_t len = 0; 34 | ssize_t read; 35 | char *command = NULL; 36 | 37 | while (1) { 38 | printf("User command: "); 39 | read = getline(&command, &len, stdin); 40 | command[read - 1] = '\0'; 41 | 42 | handle_server_message(); 43 | 44 | if (strcmp(command, "") == 0) { 45 | continue; 46 | } 47 | 48 | char *curr_cmd = strtok(command, " "); 49 | if (strcmp(curr_cmd, "LIST") == 0) { 50 | manage_LIST(); 51 | } else if (strcmp(curr_cmd, "2ALL") == 0) { 52 | curr_cmd = strtok(NULL, " "); 53 | char *message = curr_cmd; 54 | manage_2ALL(message); 55 | } else if (strcmp(curr_cmd, "2ONE") == 0) { 56 | curr_cmd = strtok(NULL, " "); 57 | int destinationID = atoi(curr_cmd); 58 | curr_cmd = strtok(NULL, " "); 59 | char *message = curr_cmd; 60 | manage_2ONE(destinationID, message); 61 | } else if (strcmp(curr_cmd, "STOP") == 0) { 62 | manage_stop(); 63 | } else { 64 | printf("Command not recognized!\n"); 65 | } 66 | } 67 | } 68 | 69 | int manage_init() { 70 | time_t my_time = time(NULL); 71 | message_buffer messageBuffer; 72 | 73 | messageBuffer.time_struct = *localtime(&my_time); 74 | messageBuffer.type = INIT; 75 | 76 | strcpy(messageBuffer.content, qName); 77 | mq_send(sQueueDesc, (char *) &messageBuffer, MSG_SIZE, 0); 78 | mq_receive(qDesc, (char *) &messageBuffer, MSG_SIZE, NULL); 79 | 80 | cID = messageBuffer.client_id; 81 | if (cID == -1) { 82 | printf("No room for more clients, leaving..\n"); 83 | exit(0); 84 | } 85 | 86 | return cID; 87 | } 88 | 89 | void manage_stop() { 90 | time_t my_time = time(NULL); 91 | message_buffer messageBuffer; 92 | 93 | messageBuffer.time_struct = *localtime(&my_time); 94 | messageBuffer.type = STOP; 95 | messageBuffer.client_id = cID; 96 | 97 | mq_send(sQueueDesc, (char *) &messageBuffer, MSG_SIZE, 0); 98 | mq_close(sQueueDesc); 99 | exit(0); 100 | } 101 | 102 | void handle_server_message() { 103 | struct timespec my_time; 104 | message_buffer messageBuffer; 105 | 106 | clock_gettime(CLOCK_REALTIME, &my_time); 107 | my_time.tv_sec += 0.1; 108 | while (mq_timedreceive(qDesc, (char *) &messageBuffer, 109 | MSG_SIZE, NULL, &my_time) != -1) { 110 | 111 | if (messageBuffer.type == STOP) { 112 | manage_stop(); 113 | } else { 114 | printf("%s\n", messageBuffer.content); 115 | } 116 | } 117 | } 118 | 119 | void get_new_name() { 120 | qName[0] = '/'; 121 | 122 | for (int i = 1; i < LENN; i++) { 123 | qName[i] = randomC(); 124 | } 125 | } 126 | 127 | void manage_LIST() { 128 | time_t my_time = time(NULL); 129 | message_buffer messageBuffer; 130 | 131 | messageBuffer.time_struct = *localtime(&my_time); 132 | messageBuffer.type = LIST; 133 | messageBuffer.client_id = cID; 134 | 135 | mq_send(sQueueDesc, (char *) &messageBuffer, MSG_SIZE, 0); 136 | mq_receive(qDesc, (char *) &messageBuffer, MSG_SIZE, NULL); 137 | printf("%s\n", messageBuffer.content); 138 | } 139 | 140 | void manage_2ALL(char *message) { 141 | time_t my_time = time(NULL); 142 | message_buffer messageBuffer; 143 | 144 | messageBuffer.time_struct = *localtime(&my_time); 145 | messageBuffer.type = TALL; 146 | strcpy(messageBuffer.content, message); 147 | 148 | messageBuffer.client_id = cID; 149 | mq_send(sQueueDesc, (char *) &messageBuffer, MSG_SIZE, 0); 150 | } 151 | 152 | void manage_2ONE(int other_id, char *message) { 153 | time_t my_time = time(NULL); 154 | message_buffer messageBuffer; 155 | 156 | messageBuffer.time_struct = *localtime(&my_time); 157 | messageBuffer.type = TONE; 158 | strcpy(messageBuffer.content, message); 159 | 160 | messageBuffer.client_id = cID; 161 | messageBuffer.other_id = other_id; 162 | mq_send(sQueueDesc, (char *) &messageBuffer, MSG_SIZE, 0); 163 | } 164 | -------------------------------------------------------------------------------- /cw06/zad2/common.h: -------------------------------------------------------------------------------- 1 | #ifndef LAB6_COMMON_H 2 | #define LAB6_COMMON_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | #define LENN 3 13 | #define SQUEUE "/SERVER" 14 | #define MAX_NO_CLIENTS 10 15 | #define MAX_MESSAGE_SIZE 512 16 | 17 | 18 | typedef struct message_buffer{ 19 | int client_id; 20 | int other_id; 21 | long type; 22 | char content[MAX_MESSAGE_SIZE]; 23 | struct tm time_struct; 24 | } message_buffer; 25 | 26 | typedef enum MessageType { 27 | INIT = 1, 28 | LIST = 2, 29 | TONE = 3, 30 | TALL = 4, 31 | STOP = 5 32 | } MessageType; 33 | 34 | 35 | const int MSG_SIZE = sizeof(message_buffer); 36 | 37 | 38 | char randomC() { 39 | return rand() % ('Z' - 'A' + 1) + 'A'; 40 | } 41 | 42 | mqd_t create_queue(const char* name) { 43 | struct mq_attr attr; 44 | attr.mq_maxmsg = MAX_NO_CLIENTS; 45 | attr.mq_msgsize = MSG_SIZE; 46 | return mq_open(name, O_RDWR | O_CREAT, 0666, &attr); 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /cw06/zad2/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "common.h" 8 | 9 | mqd_t sQueue; 10 | char *cQueue[MAX_NO_CLIENTS]; 11 | int first_available_ID = 0; 12 | 13 | 14 | void man_s_INIT(message_buffer message); 15 | void man_s_STOP(int cID); 16 | void man_s_END(); 17 | void man_LIST(int cID); 18 | void man_2ALL(message_buffer message); 19 | void man_2ONE(message_buffer message); 20 | void store_logs(message_buffer message); 21 | 22 | int main() { 23 | 24 | for (int i = 0; i < MAX_NO_CLIENTS; i++) { 25 | cQueue[i] = NULL; 26 | } 27 | 28 | mq_unlink(SQUEUE); 29 | sQueue = create_queue(SQUEUE); 30 | 31 | signal(SIGINT, man_s_END); 32 | message_buffer messageBuffer; 33 | 34 | while (1) { 35 | mq_receive(sQueue, (char *) &messageBuffer, MSG_SIZE, NULL); 36 | switch (messageBuffer.type) { 37 | case INIT: 38 | man_s_INIT(messageBuffer); 39 | break; 40 | case LIST: 41 | man_LIST(messageBuffer.client_id); 42 | store_logs(messageBuffer); 43 | break; 44 | case TALL: 45 | man_2ALL(messageBuffer); 46 | store_logs(messageBuffer); 47 | break; 48 | case TONE: 49 | man_2ONE(messageBuffer); 50 | store_logs(messageBuffer); 51 | break; 52 | case STOP: 53 | man_s_STOP(messageBuffer.client_id); 54 | store_logs(messageBuffer); 55 | break; 56 | default: 57 | printf("Command not recognized!\n"); 58 | } 59 | } 60 | } 61 | 62 | void man_s_INIT(message_buffer message) { 63 | while (cQueue[first_available_ID] != NULL && first_available_ID < MAX_NO_CLIENTS - 1) { 64 | first_available_ID++; 65 | } 66 | 67 | if (cQueue[first_available_ID] != NULL && first_available_ID == MAX_NO_CLIENTS - 1) { 68 | message.client_id = -1; 69 | } else { 70 | message.client_id = first_available_ID; 71 | cQueue[first_available_ID] = (char *) calloc(LENN, sizeof(char)); 72 | strcpy(cQueue[first_available_ID], message.content); 73 | 74 | if (first_available_ID < MAX_NO_CLIENTS - 1) { 75 | first_available_ID++; 76 | } 77 | } 78 | 79 | mqd_t cQueueDesc = mq_open(message.content, O_RDWR); 80 | mq_send(cQueueDesc, (char *) &message, MSG_SIZE, 0); 81 | mq_close(cQueueDesc); 82 | store_logs(message); 83 | } 84 | 85 | void man_s_STOP(int cID) { 86 | cQueue[cID] = NULL; 87 | 88 | if (cID < first_available_ID) { 89 | first_available_ID = cID; 90 | } 91 | } 92 | 93 | void man_s_END() { 94 | message_buffer messageBuffer; 95 | for (int i = 0; i < MAX_NO_CLIENTS; i++) { 96 | if (cQueue[i] != NULL) { 97 | messageBuffer.type = STOP; 98 | mqd_t otherQueueDecs = mq_open(cQueue[i], O_RDWR); 99 | 100 | mq_send(otherQueueDecs, (char *) &messageBuffer, MSG_SIZE, 0); 101 | mq_receive(sQueue, (char *) &messageBuffer, MSG_SIZE, NULL); 102 | mq_close(otherQueueDecs); 103 | } 104 | } 105 | 106 | mq_close(sQueue); 107 | exit(0); 108 | } 109 | 110 | void man_LIST(int cID) { 111 | message_buffer messageBuffer; 112 | strcpy(messageBuffer.content, ""); 113 | 114 | for (int i = 0; i < MAX_NO_CLIENTS; i++) { 115 | if (cQueue[i] != NULL) { 116 | sprintf(messageBuffer.content + strlen(messageBuffer.content), "ID %d is running..\n", i); 117 | } 118 | } 119 | 120 | mqd_t cQueueDesc = mq_open(cQueue[cID], O_RDWR); 121 | mq_send(cQueueDesc, (char *) &messageBuffer, MSG_SIZE, 0); 122 | mq_close(cQueueDesc); 123 | } 124 | 125 | void man_2ALL(message_buffer message) { 126 | for (int i = 0; i < MAX_NO_CLIENTS; i++) { 127 | if (i != message.client_id && cQueue[i] != NULL) { 128 | mqd_t other_client_queue_desc = mq_open(cQueue[i], O_RDWR); 129 | 130 | mq_send(other_client_queue_desc, (char *) &message, MSG_SIZE, 0); 131 | mq_close(other_client_queue_desc); 132 | } 133 | } 134 | } 135 | 136 | void man_2ONE(message_buffer message) { 137 | mqd_t otherQueueDesc = mq_open(cQueue[message.other_id], O_RDWR); 138 | 139 | mq_send(otherQueueDesc, (char *) &message, MSG_SIZE, 0); 140 | mq_close(otherQueueDesc); 141 | } 142 | 143 | void store_logs(message_buffer message) { 144 | struct tm my_time = message.time_struct; 145 | 146 | FILE *result_file = fopen("logs.txt", "a"); 147 | 148 | switch (message.type) { 149 | case INIT: 150 | if (message.client_id == -1) { 151 | fprintf(result_file, "(INIT) Max number of clients is reached!\n"); 152 | } else { 153 | fprintf(result_file, "(INIT) Client ID: %d\n", message.client_id); 154 | } 155 | break; 156 | case LIST: 157 | fprintf(result_file, "(LIST) Client ID: %d\n", message.client_id); 158 | break; 159 | case TALL: 160 | fprintf(result_file, "Message: %s\n", message.content); 161 | fprintf(result_file, "(2ALL) Client ID: %d\n", message.client_id); 162 | break; 163 | case TONE: 164 | fprintf(result_file, "Message: %s\n", message.content); 165 | fprintf(result_file, "(2ONE) Sender ID: %d, Receiver ID %d\n", message.client_id, message.other_id); 166 | break; 167 | case STOP: 168 | fprintf(result_file, "(STOP) Client ID: %d\n", message.client_id); 169 | break; 170 | } 171 | 172 | fprintf(result_file, "sent at: %02d:%02d:%02d\n\n\n", 173 | my_time.tm_hour, 174 | my_time.tm_min, 175 | my_time.tm_sec); 176 | 177 | fclose(result_file); 178 | } 179 | -------------------------------------------------------------------------------- /cw07/report.txt: -------------------------------------------------------------------------------- 1 | Ukonczone 100% 2 | 3 | Zad 1. 4 | Budowana jest od razu wersja POSIX i SYSTEM V 5 | 6 | Zad 2. 7 | Zawiera ten sam kod co zadanie 1, poniewaz jest uniwersalny 8 | 9 | 👽🤝👽 10 | -------------------------------------------------------------------------------- /cw07/zad1/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | OTHER=sem.c shared_memory.c queue.c 5 | 6 | all: 7 | make simulation STANDARD=POSIX 8 | make simulation STANDARD=SYSTEM_V 9 | 10 | simulation: client barber 11 | $(CC) $(CFLAGS) $(SOURCES) simulation.c $(OTHER) -D$(STANDARD) -lrt -lpthread -o simulation_$(STANDARD) 12 | 13 | client: 14 | $(CC) $(CFLAGS) $(SOURCES) client.c $(OTHER) -D$(STANDARD) -lrt -lpthread -o client_$(STANDARD) 15 | 16 | barber: 17 | $(CC) $(CFLAGS) $(SOURCES) barber.c $(OTHER) -D$(STANDARD) -lrt -lpthread -o barber_$(STANDARD) 18 | 19 | clean: 20 | rm -rf barber_* client_* simulation_* -------------------------------------------------------------------------------- /cw07/zad1/barber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "common.h" 9 | #include "shared_memory.h" 10 | #include "queue.h" 11 | #include "sem.h" 12 | 13 | #define HAIRCUT_TIME 1000 14 | #define TIMEOUT 1000000 15 | 16 | static Semaphore sem_queue; 17 | static Semaphore sem_chairs; 18 | static Semaphore sem_barbers; 19 | static Semaphore buffer_mutex; 20 | 21 | void open_semaphores(); 22 | 23 | int main(void) 24 | { 25 | char *queue = attach_shared_memory(PROJECT_IDENTIFIER, BUFFER_SIZE); 26 | if (queue == NULL) 27 | { 28 | fprintf(stderr, "[ERROR] Can't open queue.\n"); 29 | exit(EXIT_FAILURE); 30 | } 31 | open_semaphores(); 32 | 33 | printf("\t[BARBER-%d] Spawned\n", getpid()); 34 | fflush(stdout); 35 | 36 | while (true) 37 | { 38 | 39 | aquire(sem_barbers); 40 | 41 | release(buffer_mutex); 42 | char haircut = queue_pop(queue); 43 | release(buffer_mutex); 44 | 45 | printf("\t[BARBER-%d] Processing hairuct no. %d\n", getpid(), haircut); 46 | fflush(stdout); 47 | 48 | usleep(HAIRCUT_TIME); 49 | 50 | printf("\t[BARBER-%d] Done with hairuct no. %d\n", getpid(), haircut); 51 | fflush(stdout); 52 | 53 | release(sem_chairs); 54 | release(sem_queue); 55 | 56 | if (queue_empty(queue)) 57 | { 58 | usleep(TIMEOUT); 59 | if (queue_empty(queue)) 60 | break; 61 | } 62 | } 63 | printf("\t[BARBER-%d] Queue empty. Closing. \n", getpid()); 64 | fflush(stdout); 65 | 66 | detach_shared_memory(queue); 67 | return EXIT_SUCCESS; 68 | } 69 | 70 | void open_semaphores() 71 | { 72 | sem_queue = open_semaphore(SEM_QUEUE_FNAME); 73 | sem_chairs = open_semaphore(SEM_CHAIRS_FNAME); 74 | sem_barbers = open_semaphore(SEM_BARBERS_FNAME); 75 | buffer_mutex = open_semaphore(SEM_BUFFER_MUTEX_FNAME); 76 | } -------------------------------------------------------------------------------- /cw07/zad1/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "shared_memory.h" 9 | #include "queue.h" 10 | #include "common.h" 11 | #include "sem.h" 12 | 13 | static Semaphore sem_queue; 14 | static Semaphore sem_chairs; 15 | static Semaphore sem_barbers; 16 | static Semaphore buffer_mutex; 17 | 18 | void open_semaphores(); 19 | char random_haircut(); 20 | 21 | int main(void) { 22 | srand(time(NULL) + getpid()); 23 | 24 | char *queue = attach_shared_memory(PROJECT_IDENTIFIER, BUFFER_SIZE); 25 | if (queue == NULL) { 26 | fprintf(stderr, "[ERROR] Can't open queue.\n"); 27 | exit(EXIT_FAILURE); 28 | } 29 | 30 | open_semaphores(); 31 | 32 | if (strlen(queue) >= QUEUE_SIZE) { 33 | printf("\t[CLIENT-%d] Queue full. Leaving.\n", getpid()); 34 | fflush(stdout); 35 | } 36 | 37 | aquire(sem_queue); 38 | 39 | aquire(buffer_mutex); 40 | char haircut = random_haircut(); 41 | printf("\t[CLIENT-%d] New client with haircut no. %d \n", getpid(), haircut); 42 | fflush(stdout); 43 | queue_push(queue, haircut); 44 | release(buffer_mutex); 45 | 46 | release(sem_barbers); 47 | aquire(sem_chairs); 48 | 49 | printf("\t[CLIENT-%d] Client done.\n", getpid()); 50 | fflush(stdout); 51 | 52 | detach_shared_memory(queue); 53 | 54 | return EXIT_SUCCESS; 55 | } 56 | 57 | void open_semaphores() 58 | { 59 | sem_queue = open_semaphore(SEM_QUEUE_FNAME); 60 | sem_chairs = open_semaphore(SEM_CHAIRS_FNAME); 61 | sem_barbers = open_semaphore(SEM_BARBERS_FNAME); 62 | buffer_mutex = open_semaphore(SEM_BUFFER_MUTEX_FNAME); 63 | } 64 | 65 | char random_haircut() { 66 | return (char) (rand() % 128); 67 | } -------------------------------------------------------------------------------- /cw07/zad1/common.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMMON_H__ 2 | #define __COMMON_H__ 3 | 4 | #include 5 | 6 | #ifdef POSIX 7 | #define PROJECT_IDENTIFIER "SHARED" 8 | #define SEM_QUEUE_FNAME "/sem_queue" 9 | #define SEM_CHAIRS_FNAME "/sem_chairs" 10 | #define SEM_BARBERS_FNAME "/sem_barbers" 11 | #define SEM_BUFFER_MUTEX_FNAME "/sem_mutex" 12 | #endif 13 | #ifdef SYSTEM_V 14 | #define PROJECT_IDENTIFIER getenv("HOME") 15 | #define SEM_QUEUE_FNAME "0" 16 | #define SEM_CHAIRS_FNAME "1" 17 | #define SEM_BARBERS_FNAME "2" 18 | #define SEM_BUFFER_MUTEX_FNAME "3" 19 | #endif 20 | 21 | // Simulation config 22 | #define BARBER_TOTAL 3 // M 23 | #define CHAIR_TOTAL 2 // N 24 | #define QUEUE_SIZE 5 // P 25 | #define CUSTOMERS_TOTAL 6 26 | #define BUFFER_SIZE 4096 27 | 28 | #endif -------------------------------------------------------------------------------- /cw07/zad1/queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "queue.h" 5 | #include "common.h" 6 | 7 | char queue_pop(char *queue) { 8 | if(queue_empty(queue)) { 9 | fprintf(stderr, "[ERROR] Failed to pop from queue. Queue empty.\n"); 10 | return '\0'; 11 | } 12 | char byte = queue[0]; 13 | memcpy(queue, queue + 1, strlen(queue) + 1); 14 | return byte; 15 | } 16 | 17 | void queue_push(char *queue, char byte) { 18 | if(queue_full(queue)) { 19 | fprintf(stderr, "[ERROR] Failed to push to queue. Queue full.\n"); 20 | return; 21 | } 22 | int size = strlen(queue); 23 | queue[size] = byte; 24 | queue[size + 1] = '\0'; 25 | } 26 | 27 | bool queue_full(char *queue) { 28 | return (strlen(queue) + 1 == BUFFER_SIZE); 29 | } 30 | 31 | bool queue_empty(char *queue) { 32 | return (strlen(queue) == 0); 33 | } -------------------------------------------------------------------------------- /cw07/zad1/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef __QUEUE_H__ 2 | #define __QUEUE_H__ 3 | 4 | #include 5 | 6 | char queue_pop(char *queue); 7 | void queue_push(char *queue, char byte); 8 | bool queue_full(char *queue); 9 | bool queue_empty(char *queue); 10 | 11 | #endif -------------------------------------------------------------------------------- /cw07/zad1/sem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "sem.h" 5 | 6 | #ifdef POSIX 7 | 8 | Semaphore create_semaphore(const char *filename, int initial) { 9 | Semaphore s = sem_open(filename, O_CREAT | O_EXCL, 0644, initial); 10 | if (s == SEM_FAILED) { 11 | fprintf(stderr, "[ERROR] Failed at semaphore %s.\n", filename); 12 | return NULL; 13 | } 14 | return s; 15 | } 16 | 17 | Semaphore open_semaphore(const char *filename) { 18 | Semaphore s = sem_open(filename, 0); 19 | if (s == SEM_FAILED) { 20 | fprintf(stderr, "[ERROR] Failed at semaphore %s.\n", filename); 21 | return NULL; 22 | } 23 | return s; 24 | } 25 | 26 | void close_semaphore(Semaphore sem) { 27 | sem_close(sem); 28 | } 29 | 30 | void unlink_semaphore(const char* filename) { 31 | sem_unlink(filename); 32 | } 33 | 34 | void aquire(Semaphore sem) { 35 | sem_wait(sem); 36 | } 37 | 38 | void release(Semaphore sem) { 39 | sem_post(sem); 40 | } 41 | 42 | #endif 43 | 44 | #ifdef SYSTEM_V 45 | 46 | Semaphore create_semaphore(const char *filename, int initial) { 47 | key_t key = ftok(getenv("HOME"), filename[0]); 48 | if (key == -1) { 49 | perror("Creating a semaphore failed on ftok"); 50 | return -1; 51 | } 52 | Semaphore semid = semget(key, 1, 0664 | IPC_CREAT); 53 | if (semid == -1) { 54 | perror("Creating a semaphore failed on semid"); 55 | return -1; 56 | } 57 | if(semctl(semid, 0, SETVAL, initial) == -1) { 58 | perror("Creating a semaphore failed on semctl"); 59 | return -1; 60 | } 61 | return semid; 62 | } 63 | 64 | Semaphore open_semaphore(const char *filename) { 65 | // This is hacky but I really don't want a different function signature 66 | key_t key = ftok(getenv("HOME"), filename[0]); 67 | if (key == -1) { 68 | return -1; 69 | } 70 | return semget(key, 1, 0); 71 | } 72 | 73 | void close_semaphore(Semaphore sem) {} 74 | 75 | void unlink_semaphore(const char* filename) { 76 | Semaphore semid = open_semaphore(filename); 77 | if( semid == -1) { 78 | fprintf(stderr, "[ERROR] Failed to unlink semaphore.\n"); 79 | return; 80 | } 81 | semctl(semid, 0, IPC_RMID); 82 | } 83 | 84 | void aquire(Semaphore sem) { 85 | struct sembuf operation = { 0, -1, 0 }; 86 | if(semop(sem, &operation, 1) == -1) { 87 | perror("aquire"); 88 | } 89 | } 90 | 91 | void release(Semaphore sem) { 92 | struct sembuf operation = { 0, 1, 0 }; 93 | if(semop(sem, &operation, 1) == -1){ 94 | perror("aquire"); 95 | } 96 | } 97 | 98 | #endif -------------------------------------------------------------------------------- /cw07/zad1/sem.h: -------------------------------------------------------------------------------- 1 | #ifndef __SEM_H__ 2 | #define __SEM_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | #ifdef POSIX 13 | typedef sem_t* Semaphore; 14 | #endif 15 | #ifdef SYSTEM_V 16 | typedef int Semaphore; 17 | #endif 18 | 19 | Semaphore create_semaphore(const char *filename, int initial); 20 | Semaphore open_semaphore(const char *filename); 21 | void close_semaphore(Semaphore sem); 22 | void unlink_semaphore(const char* filename); 23 | void aquire(Semaphore sem); 24 | void release(Semaphore sem); 25 | 26 | #endif -------------------------------------------------------------------------------- /cw07/zad1/shared_memory.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include "shared_memory.h" 11 | 12 | #ifdef SYSTEM_V 13 | static int get_shared_memory(const char *filename, int size) { 14 | key_t key = ftok(filename, 0); 15 | if (key == -1) { 16 | return -1; 17 | } 18 | return shmget(key, size, 0644 | IPC_CREAT); 19 | } 20 | 21 | char *attach_shared_memory(const char* filename, int size) { 22 | int shared_memory_id = get_shared_memory(filename, size); 23 | char *shared_memory; 24 | if (shared_memory_id == -1) { 25 | fprintf(stderr, "[ERROR] No identifier for file: %s\n", filename); 26 | return NULL; 27 | } 28 | shared_memory = shmat(shared_memory_id, NULL, 0); 29 | if (shared_memory == (char*)(-1)) { 30 | fprintf(stderr, "[ERROR] Failed to load block with id %d\n", shared_memory_id); 31 | return NULL; 32 | } 33 | return shared_memory; 34 | } 35 | 36 | bool detach_shared_memory(char *shared_memory) { 37 | return (shmdt(shared_memory) != -1); 38 | } 39 | 40 | bool destroy_shared_memory(const char *filename) { 41 | int shared_memory_id = get_shared_memory(filename, 0); 42 | if(shared_memory_id == -1) { 43 | return false; 44 | } 45 | return (shmctl(shared_memory_id, IPC_RMID, NULL) != -1); 46 | } 47 | #endif 48 | 49 | 50 | 51 | #ifdef POSIX 52 | #include 53 | 54 | static int get_shared_memory(const char* filename, int size) { 55 | int descriptor = shm_open(filename, O_CREAT | O_RDWR, 0644); 56 | if (descriptor == -1) { 57 | return -1; 58 | } 59 | if(ftruncate(descriptor, size) == -1) { 60 | perror("[ERROR] ftruncate() call"); 61 | return -1; 62 | } 63 | return descriptor; 64 | } 65 | 66 | char *attach_shared_memory(const char* filename, int size) { 67 | int shared_memory_id = get_shared_memory(filename, size); 68 | if (shared_memory_id == -1) { 69 | fprintf(stderr, "[ERROR] Can't get file descriptor for: %s\n", filename); 70 | return NULL; 71 | } 72 | char *shared_memory = (char*) mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_memory_id, 0); 73 | return shared_memory; 74 | } 75 | 76 | bool detach_shared_memory(char *shared_memory) { 77 | return (shmdt(shared_memory) != -1); 78 | } 79 | 80 | bool destroy_shared_memory(const char *filename) { 81 | return (shm_unlink(filename) != -1); 82 | } 83 | #endif -------------------------------------------------------------------------------- /cw07/zad1/shared_memory.h: -------------------------------------------------------------------------------- 1 | #ifndef __SHARED_MEMORY_H__ 2 | #define __SHARED_MEMORY_H__ 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | char *attach_shared_memory(const char* filename, int size); 14 | bool detach_shared_memory(char *shared_memory); 15 | bool destroy_shared_memory(const char *filename); 16 | 17 | #endif -------------------------------------------------------------------------------- /cw07/zad1/simulation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "common.h" 9 | #include "shared_memory.h" 10 | #include "queue.h" 11 | #include "sem.h" 12 | 13 | #ifdef POSIX 14 | #define BARBER_EXEC "./barber_POSIX" 15 | #define CLIENT_EXEC "./client_POSIX" 16 | #endif 17 | #ifdef SYSTEM_V 18 | #define BARBER_EXEC "./barber_SYSTEM_V" 19 | #define CLIENT_EXEC "./client_SYSTEM_V" 20 | #endif 21 | 22 | static Semaphore sem_queue; 23 | static Semaphore sem_chairs; 24 | static Semaphore sem_barbers; 25 | static Semaphore buffer_mutex; 26 | 27 | void close_semaphores(void); 28 | void unlink_semaphores(void); 29 | void create_semaphores(void); 30 | 31 | int main(void) { 32 | printf("[SIMULATION] barbers total: %d, chairs total: %d, queue size: %d, customers waiting: %d\n", 33 | BARBER_TOTAL, 34 | CHAIR_TOTAL, 35 | QUEUE_SIZE, 36 | CUSTOMERS_TOTAL); 37 | fflush(stdout); 38 | 39 | char *shared = attach_shared_memory(PROJECT_IDENTIFIER, BUFFER_SIZE); 40 | if(shared == NULL) { 41 | exit(EXIT_FAILURE); 42 | } 43 | shared[0] = '\0'; 44 | 45 | unlink_semaphores(); 46 | create_semaphores(); 47 | 48 | for(int i=0;i 0); 62 | 63 | if (!destroy_shared_memory(PROJECT_IDENTIFIER)) { 64 | fprintf(stderr, "[ERROR] Failed to release shared memory.\n"); 65 | exit(EXIT_FAILURE); 66 | } 67 | close_semaphores(); 68 | printf("[SIMULATION] Simulation finished.\n"); 69 | fflush(stdout); 70 | return EXIT_SUCCESS; 71 | } 72 | 73 | 74 | void close_semaphores(void) { 75 | close_semaphore(sem_queue); 76 | close_semaphore(sem_chairs); 77 | close_semaphore(sem_barbers); 78 | close_semaphore(buffer_mutex); 79 | } 80 | 81 | void unlink_semaphores(void) { 82 | unlink_semaphore(SEM_QUEUE_FNAME); 83 | unlink_semaphore(SEM_CHAIRS_FNAME); 84 | unlink_semaphore(SEM_BARBERS_FNAME); 85 | unlink_semaphore(SEM_BUFFER_MUTEX_FNAME); 86 | } 87 | 88 | void create_semaphores(void) { 89 | sem_queue = create_semaphore(SEM_QUEUE_FNAME, CHAIR_TOTAL); 90 | sem_chairs = create_semaphore(SEM_CHAIRS_FNAME, 0); 91 | sem_barbers = create_semaphore(SEM_BARBERS_FNAME, 0); 92 | buffer_mutex = create_semaphore(SEM_BUFFER_MUTEX_FNAME, 1); 93 | } -------------------------------------------------------------------------------- /cw07/zad2/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | OTHER=sem.c shared_memory.c queue.c 5 | 6 | all: 7 | make simulation STANDARD=POSIX 8 | make simulation STANDARD=SYSTEM_V 9 | 10 | simulation: client barber 11 | $(CC) $(CFLAGS) $(SOURCES) simulation.c $(OTHER) -D$(STANDARD) -lrt -lpthread -o simulation_$(STANDARD) 12 | 13 | client: 14 | $(CC) $(CFLAGS) $(SOURCES) client.c $(OTHER) -D$(STANDARD) -lrt -lpthread -o client_$(STANDARD) 15 | 16 | barber: 17 | $(CC) $(CFLAGS) $(SOURCES) barber.c $(OTHER) -D$(STANDARD) -lrt -lpthread -o barber_$(STANDARD) 18 | 19 | clean: 20 | rm -rf barber_* client_* simulation_* -------------------------------------------------------------------------------- /cw07/zad2/barber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "common.h" 9 | #include "shared_memory.h" 10 | #include "queue.h" 11 | #include "sem.h" 12 | 13 | #define HAIRCUT_TIME 1000 14 | #define TIMEOUT 1000000 15 | 16 | static Semaphore sem_queue; 17 | static Semaphore sem_chairs; 18 | static Semaphore sem_barbers; 19 | static Semaphore buffer_mutex; 20 | 21 | void open_semaphores(); 22 | 23 | int main(void) 24 | { 25 | char *queue = attach_shared_memory(PROJECT_IDENTIFIER, BUFFER_SIZE); 26 | if (queue == NULL) 27 | { 28 | fprintf(stderr, "[ERROR] Can't open queue.\n"); 29 | exit(EXIT_FAILURE); 30 | } 31 | open_semaphores(); 32 | 33 | printf("\t[BARBER-%d] Spawned\n", getpid()); 34 | fflush(stdout); 35 | 36 | while (true) 37 | { 38 | 39 | aquire(sem_barbers); 40 | 41 | release(buffer_mutex); 42 | char haircut = queue_pop(queue); 43 | release(buffer_mutex); 44 | 45 | printf("\t[BARBER-%d] Processing hairuct no. %d\n", getpid(), haircut); 46 | fflush(stdout); 47 | 48 | usleep(HAIRCUT_TIME); 49 | 50 | printf("\t[BARBER-%d] Done with hairuct no. %d\n", getpid(), haircut); 51 | fflush(stdout); 52 | 53 | release(sem_chairs); 54 | release(sem_queue); 55 | 56 | if (queue_empty(queue)) 57 | { 58 | usleep(TIMEOUT); 59 | if (queue_empty(queue)) 60 | break; 61 | } 62 | } 63 | printf("\t[BARBER-%d] Queue empty. Closing. \n", getpid()); 64 | fflush(stdout); 65 | 66 | detach_shared_memory(queue); 67 | return EXIT_SUCCESS; 68 | } 69 | 70 | void open_semaphores() 71 | { 72 | sem_queue = open_semaphore(SEM_QUEUE_FNAME); 73 | sem_chairs = open_semaphore(SEM_CHAIRS_FNAME); 74 | sem_barbers = open_semaphore(SEM_BARBERS_FNAME); 75 | buffer_mutex = open_semaphore(SEM_BUFFER_MUTEX_FNAME); 76 | } -------------------------------------------------------------------------------- /cw07/zad2/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "shared_memory.h" 9 | #include "queue.h" 10 | #include "common.h" 11 | #include "sem.h" 12 | 13 | static Semaphore sem_queue; 14 | static Semaphore sem_chairs; 15 | static Semaphore sem_barbers; 16 | static Semaphore buffer_mutex; 17 | 18 | void open_semaphores(); 19 | char random_haircut(); 20 | 21 | int main(void) { 22 | srand(time(NULL) + getpid()); 23 | 24 | char *queue = attach_shared_memory(PROJECT_IDENTIFIER, BUFFER_SIZE); 25 | if (queue == NULL) { 26 | fprintf(stderr, "[ERROR] Can't open queue.\n"); 27 | exit(EXIT_FAILURE); 28 | } 29 | 30 | open_semaphores(); 31 | 32 | if (strlen(queue) >= QUEUE_SIZE) { 33 | printf("\t[CLIENT-%d] Queue full. Leaving.\n", getpid()); 34 | fflush(stdout); 35 | } 36 | 37 | aquire(sem_queue); 38 | 39 | aquire(buffer_mutex); 40 | char haircut = random_haircut(); 41 | printf("\t[CLIENT-%d] New client with haircut no. %d \n", getpid(), haircut); 42 | fflush(stdout); 43 | queue_push(queue, haircut); 44 | release(buffer_mutex); 45 | 46 | release(sem_barbers); 47 | aquire(sem_chairs); 48 | 49 | printf("\t[CLIENT-%d] Client done.\n", getpid()); 50 | fflush(stdout); 51 | 52 | detach_shared_memory(queue); 53 | 54 | return EXIT_SUCCESS; 55 | } 56 | 57 | void open_semaphores() 58 | { 59 | sem_queue = open_semaphore(SEM_QUEUE_FNAME); 60 | sem_chairs = open_semaphore(SEM_CHAIRS_FNAME); 61 | sem_barbers = open_semaphore(SEM_BARBERS_FNAME); 62 | buffer_mutex = open_semaphore(SEM_BUFFER_MUTEX_FNAME); 63 | } 64 | 65 | char random_haircut() { 66 | return (char) (rand() % 128); 67 | } -------------------------------------------------------------------------------- /cw07/zad2/common.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMMON_H__ 2 | #define __COMMON_H__ 3 | 4 | #include 5 | 6 | #ifdef POSIX 7 | #define PROJECT_IDENTIFIER "SHARED" 8 | #define SEM_QUEUE_FNAME "/sem_queue" 9 | #define SEM_CHAIRS_FNAME "/sem_chairs" 10 | #define SEM_BARBERS_FNAME "/sem_barbers" 11 | #define SEM_BUFFER_MUTEX_FNAME "/sem_mutex" 12 | #endif 13 | #ifdef SYSTEM_V 14 | #define PROJECT_IDENTIFIER getenv("HOME") 15 | #define SEM_QUEUE_FNAME "0" 16 | #define SEM_CHAIRS_FNAME "1" 17 | #define SEM_BARBERS_FNAME "2" 18 | #define SEM_BUFFER_MUTEX_FNAME "3" 19 | #endif 20 | 21 | // Simulation config 22 | #define BARBER_TOTAL 3 // M 23 | #define CHAIR_TOTAL 2 // N 24 | #define QUEUE_SIZE 5 // P 25 | #define CUSTOMERS_TOTAL 6 26 | #define BUFFER_SIZE 4096 27 | 28 | #endif -------------------------------------------------------------------------------- /cw07/zad2/queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "queue.h" 5 | #include "common.h" 6 | 7 | char queue_pop(char *queue) { 8 | if(queue_empty(queue)) { 9 | fprintf(stderr, "[ERROR] Failed to pop from queue. Queue empty.\n"); 10 | return '\0'; 11 | } 12 | char byte = queue[0]; 13 | memcpy(queue, queue + 1, strlen(queue) + 1); 14 | return byte; 15 | } 16 | 17 | void queue_push(char *queue, char byte) { 18 | if(queue_full(queue)) { 19 | fprintf(stderr, "[ERROR] Failed to push to queue. Queue full.\n"); 20 | return; 21 | } 22 | int size = strlen(queue); 23 | queue[size] = byte; 24 | queue[size + 1] = '\0'; 25 | } 26 | 27 | bool queue_full(char *queue) { 28 | return (strlen(queue) + 1 == BUFFER_SIZE); 29 | } 30 | 31 | bool queue_empty(char *queue) { 32 | return (strlen(queue) == 0); 33 | } -------------------------------------------------------------------------------- /cw07/zad2/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef __QUEUE_H__ 2 | #define __QUEUE_H__ 3 | 4 | #include 5 | 6 | char queue_pop(char *queue); 7 | void queue_push(char *queue, char byte); 8 | bool queue_full(char *queue); 9 | bool queue_empty(char *queue); 10 | 11 | #endif -------------------------------------------------------------------------------- /cw07/zad2/sem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "sem.h" 5 | 6 | #ifdef POSIX 7 | 8 | Semaphore create_semaphore(const char *filename, int initial) { 9 | Semaphore s = sem_open(filename, O_CREAT | O_EXCL, 0644, initial); 10 | if (s == SEM_FAILED) { 11 | fprintf(stderr, "[ERROR] Failed at semaphore %s.\n", filename); 12 | return NULL; 13 | } 14 | return s; 15 | } 16 | 17 | Semaphore open_semaphore(const char *filename) { 18 | Semaphore s = sem_open(filename, 0); 19 | if (s == SEM_FAILED) { 20 | fprintf(stderr, "[ERROR] Failed at semaphore %s.\n", filename); 21 | return NULL; 22 | } 23 | return s; 24 | } 25 | 26 | void close_semaphore(Semaphore sem) { 27 | sem_close(sem); 28 | } 29 | 30 | void unlink_semaphore(const char* filename) { 31 | sem_unlink(filename); 32 | } 33 | 34 | void aquire(Semaphore sem) { 35 | sem_wait(sem); 36 | } 37 | 38 | void release(Semaphore sem) { 39 | sem_post(sem); 40 | } 41 | 42 | #endif 43 | 44 | #ifdef SYSTEM_V 45 | 46 | Semaphore create_semaphore(const char *filename, int initial) { 47 | key_t key = ftok(getenv("HOME"), filename[0]); 48 | if (key == -1) { 49 | perror("Creating a semaphore failed on ftok"); 50 | return -1; 51 | } 52 | Semaphore semid = semget(key, 1, 0664 | IPC_CREAT); 53 | if (semid == -1) { 54 | perror("Creating a semaphore failed on semid"); 55 | return -1; 56 | } 57 | if(semctl(semid, 0, SETVAL, initial) == -1) { 58 | perror("Creating a semaphore failed on semctl"); 59 | return -1; 60 | } 61 | return semid; 62 | } 63 | 64 | Semaphore open_semaphore(const char *filename) { 65 | // This is hacky but I really don't want a different function signature 66 | key_t key = ftok(getenv("HOME"), filename[0]); 67 | if (key == -1) { 68 | return -1; 69 | } 70 | return semget(key, 1, 0); 71 | } 72 | 73 | void close_semaphore(Semaphore sem) {} 74 | 75 | void unlink_semaphore(const char* filename) { 76 | Semaphore semid = open_semaphore(filename); 77 | if( semid == -1) { 78 | fprintf(stderr, "[ERROR] Failed to unlink semaphore.\n"); 79 | return; 80 | } 81 | semctl(semid, 0, IPC_RMID); 82 | } 83 | 84 | void aquire(Semaphore sem) { 85 | struct sembuf operation = { 0, -1, 0 }; 86 | if(semop(sem, &operation, 1) == -1) { 87 | perror("aquire"); 88 | } 89 | } 90 | 91 | void release(Semaphore sem) { 92 | struct sembuf operation = { 0, 1, 0 }; 93 | if(semop(sem, &operation, 1) == -1){ 94 | perror("aquire"); 95 | } 96 | } 97 | 98 | #endif -------------------------------------------------------------------------------- /cw07/zad2/sem.h: -------------------------------------------------------------------------------- 1 | #ifndef __SEM_H__ 2 | #define __SEM_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | #ifdef POSIX 13 | typedef sem_t* Semaphore; 14 | #endif 15 | #ifdef SYSTEM_V 16 | typedef int Semaphore; 17 | #endif 18 | 19 | Semaphore create_semaphore(const char *filename, int initial); 20 | Semaphore open_semaphore(const char *filename); 21 | void close_semaphore(Semaphore sem); 22 | void unlink_semaphore(const char* filename); 23 | void aquire(Semaphore sem); 24 | void release(Semaphore sem); 25 | 26 | #endif -------------------------------------------------------------------------------- /cw07/zad2/shared_memory.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include "shared_memory.h" 11 | 12 | #ifdef SYSTEM_V 13 | static int get_shared_memory(const char *filename, int size) { 14 | key_t key = ftok(filename, 0); 15 | if (key == -1) { 16 | return -1; 17 | } 18 | return shmget(key, size, 0644 | IPC_CREAT); 19 | } 20 | 21 | char *attach_shared_memory(const char* filename, int size) { 22 | int shared_memory_id = get_shared_memory(filename, size); 23 | char *shared_memory; 24 | if (shared_memory_id == -1) { 25 | fprintf(stderr, "[ERROR] No identifier for file: %s\n", filename); 26 | return NULL; 27 | } 28 | shared_memory = shmat(shared_memory_id, NULL, 0); 29 | if (shared_memory == (char*)(-1)) { 30 | fprintf(stderr, "[ERROR] Failed to load block with id %d\n", shared_memory_id); 31 | return NULL; 32 | } 33 | return shared_memory; 34 | } 35 | 36 | bool detach_shared_memory(char *shared_memory) { 37 | return (shmdt(shared_memory) != -1); 38 | } 39 | 40 | bool destroy_shared_memory(const char *filename) { 41 | int shared_memory_id = get_shared_memory(filename, 0); 42 | if(shared_memory_id == -1) { 43 | return false; 44 | } 45 | return (shmctl(shared_memory_id, IPC_RMID, NULL) != -1); 46 | } 47 | #endif 48 | 49 | 50 | 51 | #ifdef POSIX 52 | #include 53 | 54 | static int get_shared_memory(const char* filename, int size) { 55 | int descriptor = shm_open(filename, O_CREAT | O_RDWR, 0644); 56 | if (descriptor == -1) { 57 | return -1; 58 | } 59 | if(ftruncate(descriptor, size) == -1) { 60 | perror("[ERROR] ftruncate() call"); 61 | return -1; 62 | } 63 | return descriptor; 64 | } 65 | 66 | char *attach_shared_memory(const char* filename, int size) { 67 | int shared_memory_id = get_shared_memory(filename, size); 68 | if (shared_memory_id == -1) { 69 | fprintf(stderr, "[ERROR] Can't get file descriptor for: %s\n", filename); 70 | return NULL; 71 | } 72 | char *shared_memory = (char*) mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_memory_id, 0); 73 | return shared_memory; 74 | } 75 | 76 | bool detach_shared_memory(char *shared_memory) { 77 | return (shmdt(shared_memory) != -1); 78 | } 79 | 80 | bool destroy_shared_memory(const char *filename) { 81 | return (shm_unlink(filename) != -1); 82 | } 83 | #endif -------------------------------------------------------------------------------- /cw07/zad2/shared_memory.h: -------------------------------------------------------------------------------- 1 | #ifndef __SHARED_MEMORY_H__ 2 | #define __SHARED_MEMORY_H__ 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | char *attach_shared_memory(const char* filename, int size); 14 | bool detach_shared_memory(char *shared_memory); 15 | bool destroy_shared_memory(const char *filename); 16 | 17 | #endif -------------------------------------------------------------------------------- /cw07/zad2/simulation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "common.h" 9 | #include "shared_memory.h" 10 | #include "queue.h" 11 | #include "sem.h" 12 | 13 | #ifdef POSIX 14 | #define BARBER_EXEC "./barber_POSIX" 15 | #define CLIENT_EXEC "./client_POSIX" 16 | #endif 17 | #ifdef SYSTEM_V 18 | #define BARBER_EXEC "./barber_SYSTEM_V" 19 | #define CLIENT_EXEC "./client_SYSTEM_V" 20 | #endif 21 | 22 | static Semaphore sem_queue; 23 | static Semaphore sem_chairs; 24 | static Semaphore sem_barbers; 25 | static Semaphore buffer_mutex; 26 | 27 | void close_semaphores(void); 28 | void unlink_semaphores(void); 29 | void create_semaphores(void); 30 | 31 | int main(void) { 32 | printf("[SIMULATION] barbers total: %d, chairs total: %d, queue size: %d, customers waiting: %d\n", 33 | BARBER_TOTAL, 34 | CHAIR_TOTAL, 35 | QUEUE_SIZE, 36 | CUSTOMERS_TOTAL); 37 | fflush(stdout); 38 | 39 | char *shared = attach_shared_memory(PROJECT_IDENTIFIER, BUFFER_SIZE); 40 | if(shared == NULL) { 41 | exit(EXIT_FAILURE); 42 | } 43 | shared[0] = '\0'; 44 | 45 | unlink_semaphores(); 46 | create_semaphores(); 47 | 48 | for(int i=0;i 0); 62 | 63 | if (!destroy_shared_memory(PROJECT_IDENTIFIER)) { 64 | fprintf(stderr, "[ERROR] Failed to release shared memory.\n"); 65 | exit(EXIT_FAILURE); 66 | } 67 | close_semaphores(); 68 | printf("[SIMULATION] Simulation finished.\n"); 69 | fflush(stdout); 70 | return EXIT_SUCCESS; 71 | } 72 | 73 | 74 | void close_semaphores(void) { 75 | close_semaphore(sem_queue); 76 | close_semaphore(sem_chairs); 77 | close_semaphore(sem_barbers); 78 | close_semaphore(buffer_mutex); 79 | } 80 | 81 | void unlink_semaphores(void) { 82 | unlink_semaphore(SEM_QUEUE_FNAME); 83 | unlink_semaphore(SEM_CHAIRS_FNAME); 84 | unlink_semaphore(SEM_BARBERS_FNAME); 85 | unlink_semaphore(SEM_BUFFER_MUTEX_FNAME); 86 | } 87 | 88 | void create_semaphores(void) { 89 | sem_queue = create_semaphore(SEM_QUEUE_FNAME, CHAIR_TOTAL); 90 | sem_chairs = create_semaphore(SEM_CHAIRS_FNAME, 0); 91 | sem_barbers = create_semaphore(SEM_BARBERS_FNAME, 0); 92 | buffer_mutex = create_semaphore(SEM_BUFFER_MUTEX_FNAME, 1); 93 | } -------------------------------------------------------------------------------- /cw08/report.txt: -------------------------------------------------------------------------------- 1 | Ukonczone 100% 2 | 3 | Zad 1. 4 | Dodane funkcja obslugujaca gre wielowatkowo, 5 | podanie ujemnej ilosci watkow skutkuje jednym watkiem na kazde pole, 6 | a wiec jest zgodne z poleceniem. 7 | 8 | Zad 2. 9 | Kod zadania 1 + dodany argument - ilosc watkow 10 | 11 | 😳 12 | -------------------------------------------------------------------------------- /cw08/zad1/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall 3 | 4 | all: life 5 | 6 | life: grid.c life.c 7 | $(CC) $(CFLAGS) grid.c life.c -o life -lncursesw -lpthread 8 | 9 | clean: 10 | rm -f life 11 | -------------------------------------------------------------------------------- /cw08/zad1/grid.c: -------------------------------------------------------------------------------- 1 | #include "grid.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | const int grid_width = 30; 10 | const int grid_height = 30; 11 | 12 | char *create_grid() 13 | { 14 | return malloc(sizeof(char) * grid_width * grid_height); 15 | } 16 | 17 | void destroy_grid(char *grid) 18 | { 19 | free(grid); 20 | } 21 | 22 | void draw_grid(char *grid) 23 | { 24 | for (int i = 0; i < grid_height; ++i) 25 | { 26 | // Two characters for more uniform spaces (vertical vs horizontal) 27 | for (int j = 0; j < grid_width; ++j) 28 | { 29 | if (grid[i * grid_width + j]) 30 | { 31 | mvprintw(i, j * 2, "■"); 32 | mvprintw(i, j * 2 + 1, " "); 33 | } 34 | else 35 | { 36 | mvprintw(i, j * 2, " "); 37 | mvprintw(i, j * 2 + 1, " "); 38 | } 39 | } 40 | } 41 | 42 | refresh(); 43 | } 44 | 45 | void init_grid(char *grid) 46 | { 47 | for (int i = 0; i < grid_width * grid_height; ++i) 48 | grid[i] = rand() % 2 == 0; 49 | } 50 | 51 | bool is_alive(int row, int col, char *grid) 52 | { 53 | 54 | int count = 0; 55 | for (int i = -1; i <= 1; i++) 56 | { 57 | for (int j = -1; j <= 1; j++) 58 | { 59 | if (i == 0 && j == 0) 60 | { 61 | continue; 62 | } 63 | int r = row + i; 64 | int c = col + j; 65 | if (r < 0 || r >= grid_height || c < 0 || c >= grid_width) 66 | { 67 | continue; 68 | } 69 | if (grid[grid_width * r + c]) 70 | { 71 | count++; 72 | } 73 | } 74 | } 75 | 76 | if (grid[row * grid_width + col]) 77 | { 78 | if (count == 2 || count == 3) 79 | return true; 80 | else 81 | return false; 82 | } 83 | else 84 | { 85 | if (count == 3) 86 | return true; 87 | else 88 | return false; 89 | } 90 | } 91 | 92 | void update_grid(char *src, char *dst) 93 | { 94 | for (int i = 0; i < grid_height; ++i) 95 | { 96 | for (int j = 0; j < grid_width; ++j) 97 | { 98 | dst[i * grid_width + j] = is_alive(i, j, src); 99 | } 100 | } 101 | } 102 | 103 | void ignore_handler(int signo, siginfo_t* info, void* context) {} 104 | 105 | typedef struct { 106 | char* src; 107 | char* dst; 108 | int start; 109 | int end; 110 | } UpdateGridArgs; 111 | 112 | void* update_grid_region(void* _args) { 113 | UpdateGridArgs* args = (UpdateGridArgs*) _args; 114 | 115 | while (1) { 116 | for (int i = args->start; i < args->end; i++) { 117 | args->dst[i] = is_alive(i / grid_width, i % grid_width, args->src); 118 | } 119 | pause(); 120 | 121 | char* tmp = args->src; 122 | args->src = args->dst; 123 | args->dst = tmp; 124 | } 125 | 126 | return NULL; 127 | } 128 | 129 | void update_grid_multithreaded(char* src, char* dst, int n_threads) { 130 | static pthread_t* _threads = NULL; 131 | static int _n_threads = -1; 132 | 133 | struct sigaction action; 134 | sigemptyset(&action.sa_mask); 135 | action.sa_sigaction = ignore_handler; 136 | sigaction(SIGUSR1, &action, NULL); 137 | 138 | if (!_threads) { 139 | int n2 = grid_width*grid_height; 140 | 141 | // If a negative number was provided, use one thread for each tile 142 | n_threads = n_threads < 0 ? n2 : n_threads; 143 | 144 | _threads = malloc(sizeof(pthread_t) * n_threads); 145 | _n_threads = n_threads; 146 | 147 | int block = n2/n_threads + (n2 % n_threads ? 1 : 0); 148 | 149 | for (int i = 0; i < n2; i += block) { 150 | 151 | // ending the program will release this memory, it's executed only once so it's not leaking 152 | UpdateGridArgs* args = malloc(sizeof(UpdateGridArgs)); 153 | args->src = src; 154 | args->dst = dst; 155 | args->start = i; 156 | args->end = (i+block+block > n2) ? n2 : i+block; 157 | 158 | pthread_create( 159 | _threads+(i/block), 160 | NULL, 161 | update_grid_region, 162 | (void*) args 163 | ); 164 | } 165 | } 166 | 167 | for (int i=0; i < _n_threads; i++) { 168 | pthread_kill(_threads[i], SIGUSR1); 169 | } 170 | } -------------------------------------------------------------------------------- /cw08/zad1/grid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | char *create_grid(); 5 | void destroy_grid(char *grid); 6 | void draw_grid(char *grid); 7 | void init_grid(char *grid); 8 | bool is_alive(int row, int col, char *grid); 9 | void update_grid(char *src, char *dst); 10 | void update_grid_multithreaded(char* src, char* dst, int n_threads); 11 | -------------------------------------------------------------------------------- /cw08/zad1/life.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "grid.h" 6 | #include 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | srand(time(NULL)); 13 | setlocale(LC_CTYPE, ""); 14 | initscr(); // Start curses mode 15 | 16 | char *foreground = create_grid(); 17 | char *background = create_grid(); 18 | char *tmp; 19 | 20 | init_grid(foreground); 21 | 22 | while (true) 23 | { 24 | draw_grid(foreground); 25 | usleep(500 * 1000); 26 | 27 | // Step simulation 28 | update_grid_multithreaded(foreground, background, -1); 29 | 30 | tmp = foreground; 31 | foreground = background; 32 | background = tmp; 33 | } 34 | 35 | endwin(); // End curses mode 36 | destroy_grid(foreground); 37 | destroy_grid(background); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /cw08/zad2/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall 3 | 4 | all: life 5 | 6 | life: grid.c life.c 7 | $(CC) $(CFLAGS) grid.c life.c -o life -lncursesw -lpthread 8 | 9 | clean: 10 | rm -f life 11 | -------------------------------------------------------------------------------- /cw08/zad2/grid.c: -------------------------------------------------------------------------------- 1 | #include "grid.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | const int grid_width = 30; 10 | const int grid_height = 30; 11 | 12 | char *create_grid() 13 | { 14 | return malloc(sizeof(char) * grid_width * grid_height); 15 | } 16 | 17 | void destroy_grid(char *grid) 18 | { 19 | free(grid); 20 | } 21 | 22 | void draw_grid(char *grid) 23 | { 24 | for (int i = 0; i < grid_height; ++i) 25 | { 26 | // Two characters for more uniform spaces (vertical vs horizontal) 27 | for (int j = 0; j < grid_width; ++j) 28 | { 29 | if (grid[i * grid_width + j]) 30 | { 31 | mvprintw(i, j * 2, "■"); 32 | mvprintw(i, j * 2 + 1, " "); 33 | } 34 | else 35 | { 36 | mvprintw(i, j * 2, " "); 37 | mvprintw(i, j * 2 + 1, " "); 38 | } 39 | } 40 | } 41 | 42 | refresh(); 43 | } 44 | 45 | void init_grid(char *grid) 46 | { 47 | for (int i = 0; i < grid_width * grid_height; ++i) 48 | grid[i] = rand() % 2 == 0; 49 | } 50 | 51 | bool is_alive(int row, int col, char *grid) 52 | { 53 | 54 | int count = 0; 55 | for (int i = -1; i <= 1; i++) 56 | { 57 | for (int j = -1; j <= 1; j++) 58 | { 59 | if (i == 0 && j == 0) 60 | { 61 | continue; 62 | } 63 | int r = row + i; 64 | int c = col + j; 65 | if (r < 0 || r >= grid_height || c < 0 || c >= grid_width) 66 | { 67 | continue; 68 | } 69 | if (grid[grid_width * r + c]) 70 | { 71 | count++; 72 | } 73 | } 74 | } 75 | 76 | if (grid[row * grid_width + col]) 77 | { 78 | if (count == 2 || count == 3) 79 | return true; 80 | else 81 | return false; 82 | } 83 | else 84 | { 85 | if (count == 3) 86 | return true; 87 | else 88 | return false; 89 | } 90 | } 91 | 92 | void update_grid(char *src, char *dst) 93 | { 94 | for (int i = 0; i < grid_height; ++i) 95 | { 96 | for (int j = 0; j < grid_width; ++j) 97 | { 98 | dst[i * grid_width + j] = is_alive(i, j, src); 99 | } 100 | } 101 | } 102 | 103 | void ignore_handler(int signo, siginfo_t* info, void* context) {} 104 | 105 | typedef struct { 106 | char* src; 107 | char* dst; 108 | int start; 109 | int end; 110 | } UpdateGridArgs; 111 | 112 | void* update_grid_region(void* _args) { 113 | UpdateGridArgs* args = (UpdateGridArgs*) _args; 114 | 115 | while (1) { 116 | for (int i = args->start; i < args->end; i++) { 117 | args->dst[i] = is_alive(i / grid_width, i % grid_width, args->src); 118 | } 119 | pause(); 120 | 121 | char* tmp = args->src; 122 | args->src = args->dst; 123 | args->dst = tmp; 124 | } 125 | 126 | return NULL; 127 | } 128 | 129 | void update_grid_multithreaded(char* src, char* dst, int n_threads) { 130 | static pthread_t* _threads = NULL; 131 | static int _n_threads = -1; 132 | 133 | struct sigaction action; 134 | sigemptyset(&action.sa_mask); 135 | action.sa_sigaction = ignore_handler; 136 | sigaction(SIGUSR1, &action, NULL); 137 | 138 | if (!_threads) { 139 | int n2 = grid_width*grid_height; 140 | 141 | // If a negative number was provided, use one thread for each tile 142 | n_threads = n_threads < 0 ? n2 : n_threads; 143 | 144 | _threads = malloc(sizeof(pthread_t) * n_threads); 145 | _n_threads = n_threads; 146 | 147 | int block = n2/n_threads + (n2 % n_threads ? 1 : 0); 148 | 149 | for (int i = 0; i < n2; i += block) { 150 | 151 | // ending the program will release this memory, it's executed only once so it's not leaking 152 | UpdateGridArgs* args = malloc(sizeof(UpdateGridArgs)); 153 | args->src = src; 154 | args->dst = dst; 155 | args->start = i; 156 | args->end = (i+block+block > n2) ? n2 : i+block; 157 | 158 | pthread_create( 159 | _threads+(i/block), 160 | NULL, 161 | update_grid_region, 162 | (void*) args 163 | ); 164 | } 165 | } 166 | 167 | for (int i=0; i < _n_threads; i++) { 168 | pthread_kill(_threads[i], SIGUSR1); 169 | } 170 | } -------------------------------------------------------------------------------- /cw08/zad2/grid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | char *create_grid(); 5 | void destroy_grid(char *grid); 6 | void draw_grid(char *grid); 7 | void init_grid(char *grid); 8 | bool is_alive(int row, int col, char *grid); 9 | void update_grid(char *src, char *dst); 10 | void update_grid_multithreaded(char* src, char* dst, int n_threads); 11 | -------------------------------------------------------------------------------- /cw08/zad2/life.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "grid.h" 6 | #include 7 | #include 8 | #include 9 | 10 | int main(int argc, char** argv) 11 | { 12 | int n; 13 | 14 | if (argc != 2) { 15 | fprintf(stderr, "[LIFE] Invalid argument count, please provide the number of threads.\n"); 16 | exit(1); 17 | } 18 | 19 | n = atoi(argv[1]); 20 | 21 | srand(time(NULL)); 22 | setlocale(LC_CTYPE, ""); 23 | initscr(); // Start curses mode 24 | 25 | char *foreground = create_grid(); 26 | char *background = create_grid(); 27 | char *tmp; 28 | 29 | init_grid(foreground); 30 | 31 | while (true) 32 | { 33 | draw_grid(foreground); 34 | usleep(500 * 1000); 35 | 36 | // Step simulation 37 | update_grid_multithreaded(foreground, background, n); 38 | 39 | tmp = foreground; 40 | foreground = background; 41 | background = tmp; 42 | } 43 | 44 | endwin(); // End curses mode 45 | destroy_grid(foreground); 46 | destroy_grid(background); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /cw09/report.txt: -------------------------------------------------------------------------------- 1 | Ukonczone 100% 2 | 3 | Zad 1. 4 | Zadanie wykonane z uzyciem barier 5 | 6 | 💍 7 | -------------------------------------------------------------------------------- /cw09/zad1/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: santa.c 6 | $(CC) $(CFLAGS) santa.c -o santa -lpthread 7 | 8 | clean: 9 | rm -f santa 10 | -------------------------------------------------------------------------------- /cw09/zad1/santa.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define REINDEER_TOTAL 9 10 | #define ELVES_TOTAL 10 11 | #define ELVES_REQUIRED 3 12 | 13 | typedef struct { 14 | pthread_mutex_t mutex; 15 | pthread_cond_t condition; 16 | unsigned int elves, reindeer; 17 | } Monitor; 18 | 19 | static Monitor monitor = { 20 | .mutex = PTHREAD_MUTEX_INITIALIZER, 21 | .condition = PTHREAD_COND_INITIALIZER, 22 | .reindeer = 0, 23 | .elves = 0 24 | }; 25 | static pthread_barrier_t reindeer_barrier, elves_barrier; 26 | 27 | pid_t get_thread_id() { 28 | return syscall(__NR_gettid); 29 | } 30 | 31 | void *reindeer_routine(void *arg) { 32 | while (true) { 33 | printf("[REINDEER] Reindeer [%d] is on vacation \n", get_thread_id()); 34 | sleep(5 + (rand() % 6)); 35 | 36 | pthread_mutex_lock(&monitor.mutex); 37 | monitor.reindeer++; 38 | if (monitor.reindeer == REINDEER_TOTAL) { 39 | printf("[REINDEER] Reindeer [%d] wakes up Santa.\n", get_thread_id()); 40 | } 41 | else { 42 | printf("[REINDEER] Reindeer [%d] came back from vacation\n", get_thread_id()); 43 | } 44 | pthread_cond_signal(&monitor.condition); 45 | pthread_mutex_unlock(&monitor.mutex); 46 | 47 | pthread_barrier_wait(&reindeer_barrier); 48 | } 49 | return NULL; 50 | } 51 | 52 | void *elf_routine(void *arg) { 53 | while(true) { 54 | sleep(2 + (rand() % 4)); 55 | 56 | pthread_mutex_lock(&monitor.mutex); 57 | if (monitor.elves < ELVES_REQUIRED ) { 58 | monitor.elves += 1; 59 | if (monitor.elves == ELVES_REQUIRED) { 60 | printf("[ELF] [%d] wakes up santa\n", get_thread_id()); 61 | } 62 | else { 63 | printf("[ELF] Elf [%d] is waiting for santa.\n", get_thread_id()); 64 | } 65 | pthread_cond_signal(&monitor.condition); 66 | } 67 | else { 68 | printf("[ELF] Elf [%d] solves the problem on their own.\n", get_thread_id()); 69 | pthread_mutex_unlock(&monitor.mutex); 70 | continue; 71 | } 72 | pthread_mutex_unlock(&monitor.mutex); 73 | pthread_barrier_wait(&elves_barrier); 74 | 75 | } 76 | return NULL; 77 | } 78 | 79 | int main(int argc, char** argv) { 80 | pthread_barrier_init(&reindeer_barrier, NULL, REINDEER_TOTAL + 1); 81 | pthread_barrier_init(&elves_barrier, NULL, ELVES_REQUIRED + 1); 82 | 83 | pthread_t reindeer_ids[REINDEER_TOTAL]; 84 | for(int i=0;i 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #define safe(expr) \ 21 | ({ \ 22 | typeof(expr) __tmp = expr; \ 23 | if (__tmp == -1) { \ 24 | printf("%s:%d "#expr" failed: %s\n", __FILE__, __LINE__, strerror(errno));\ 25 | exit(EXIT_FAILURE); \ 26 | } \ 27 | __tmp; \ 28 | }) 29 | #define loop for(;;) 30 | #define find(init, cond) ({ int index = -1; for (init) if (cond) { index = i; break; } index; }) 31 | #define repeat(n) for(int i = 0; i < n; i++) 32 | #define print(x) write(STDOUT_FILENO, x, sizeof(x)) -------------------------------------------------------------------------------- /cw10/zad1/message.h: -------------------------------------------------------------------------------- 1 | #define MSG_LEN 256 2 | 3 | typedef enum { 4 | msg_ping, 5 | msg_username_taken, 6 | msg_server_full, 7 | msg_disconnect, 8 | msg_get, 9 | msg_init, 10 | msg_list, 11 | msg_tone, 12 | msg_tall, 13 | msg_stop, 14 | } message_type; 15 | 16 | typedef struct { 17 | message_type type; 18 | char text[MSG_LEN]; // INCLUDES NICKNAME 19 | char other_nickname[MSG_LEN]; 20 | } message; 21 | -------------------------------------------------------------------------------- /cw10/zad2/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | CC=gcc 3 | CFLAGS=-Wall -O3 4 | 5 | all: 6 | make client 7 | make server 8 | 9 | client: client.c 10 | $(CC) $(CFLAGS) client.c -o client -lpthread 11 | 12 | server: server.c 13 | $(CC) $(CFLAGS) server.c -o server -lpthread 14 | 15 | clean: 16 | rm -f client server -------------------------------------------------------------------------------- /cw10/zad2/client.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "message.h" 3 | 4 | int _; 5 | 6 | int connect_unix(char* path, char* user) { 7 | struct sockaddr_un addr, bind_addr; 8 | memset(&addr, 0, sizeof(addr)); 9 | bind_addr.sun_family = AF_UNIX; 10 | addr.sun_family = AF_UNIX; 11 | snprintf(bind_addr.sun_path, sizeof bind_addr.sun_path, "/tmp/%s%ld", user, time(NULL)); 12 | strncpy(addr.sun_path, path, sizeof addr.sun_path); 13 | 14 | int sock = safe (socket(AF_UNIX, SOCK_DGRAM, 0)); 15 | safe (bind(sock, (void*) &bind_addr, sizeof addr)); 16 | safe (connect(sock, (struct sockaddr*) &addr, sizeof addr)); 17 | 18 | return sock; 19 | } 20 | 21 | int connect_web(char* ipv4, int port) { 22 | struct sockaddr_in addr; 23 | memset(&addr, 0, sizeof(addr)); 24 | addr.sin_family = AF_INET; 25 | addr.sin_port = htons(port); 26 | if (inet_pton(AF_INET, ipv4, &addr.sin_addr) <= 0) { 27 | print("Invalid address\n"); 28 | exit(0); 29 | } 30 | 31 | int sock = safe (socket(AF_INET, SOCK_DGRAM, 0)); 32 | safe (connect(sock, (struct sockaddr*) &addr, sizeof addr)); 33 | 34 | return sock; 35 | } 36 | 37 | int sock; 38 | void on_SIGINT(int _) { 39 | message msg = { .type = msg_disconnect }; 40 | sendto(sock, &msg, sizeof msg, 0, NULL, sizeof(struct sockaddr_in)); 41 | exit(0); 42 | } 43 | 44 | int main(int argc, char** argv) { 45 | char* nickname; 46 | if (argc > 2) nickname = argv[1]; 47 | if (argc == 5 && strcmp(argv[2], "web") == 0) sock = connect_web(argv[3], atoi(argv[4])); 48 | else if (argc == 4 && strcmp(argv[2], "unix") == 0) sock = connect_unix(argv[3], nickname); 49 | else { 50 | print("Usage [nick] [web|unix] [ip port|path]\n"); 51 | exit(0); 52 | } 53 | 54 | signal(SIGINT, on_SIGINT); 55 | 56 | message msg = { .type = msg_connect }; 57 | strncpy(msg.nickname, nickname, sizeof msg.nickname); 58 | send(sock, &msg, sizeof msg, 0); 59 | 60 | int epoll_fd = safe (epoll_create1(0)); 61 | 62 | struct epoll_event stdin_event = { 63 | .events = EPOLLIN | EPOLLPRI, 64 | .data = { .fd = STDIN_FILENO } 65 | }; 66 | epoll_ctl(epoll_fd, EPOLL_CTL_ADD, STDIN_FILENO, &stdin_event); 67 | 68 | struct epoll_event socket_event = { 69 | .events = EPOLLIN | EPOLLPRI | EPOLLHUP, 70 | .data = { .fd = sock } 71 | }; 72 | epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &socket_event); 73 | 74 | struct epoll_event events[2]; 75 | loop { 76 | int nread = safe (epoll_wait(epoll_fd, events, 2, 1)); 77 | repeat(nread) { 78 | if (events[i].data.fd == STDIN_FILENO) { 79 | // puts("got stdin"); 80 | // fflush(NULL); 81 | 82 | char buffer[512] = {}; 83 | 84 | // POWAŻNY PARSER 85 | 86 | int x = read(STDIN_FILENO, &buffer, 512); 87 | buffer[x] = 0; 88 | 89 | char korektor[] = " \t\n"; 90 | char *token; 91 | int idx = 0; 92 | token = strtok( buffer, korektor ); 93 | 94 | message_type type = -1; 95 | char other_nickname[MSG_LEN] = {}; 96 | char text[MSG_LEN] = {}; 97 | 98 | bool broke = false; 99 | 100 | if (token == NULL) 101 | continue; 102 | 103 | while( token != NULL ) { 104 | // puts("TOKEN:"); 105 | // puts(token); 106 | // fflush(NULL); 107 | 108 | switch (idx) { 109 | case 0: 110 | if (strcmp(token, "LIST") == 0) type = msg_list; 111 | else if (strcmp(token, "2ALL") == 0) type = msg_tall; 112 | else if (strcmp(token, "2ONE") == 0) type = msg_tone; 113 | else if (strcmp(token, "STOP") == 0) type = msg_stop; 114 | else { 115 | broke = true; 116 | puts("Invalid command"); 117 | type = -1; 118 | } 119 | break; 120 | case 1: 121 | memcpy(text, token, strlen(token)*sizeof(char)); 122 | text[strlen(token)] = 0; 123 | break; 124 | case 2: 125 | memcpy(other_nickname, token, strlen(token)*sizeof(char)); 126 | other_nickname[strlen(token)] = 0; 127 | break; 128 | case 3: 129 | broke = true; 130 | break; 131 | } 132 | 133 | if (broke) 134 | break; 135 | 136 | idx++; 137 | token = strtok( NULL, korektor ); 138 | } 139 | if (broke) 140 | continue; 141 | 142 | // puts("parsed"); 143 | // fflush(NULL); 144 | 145 | message msg; 146 | msg.type = type; 147 | memcpy(&msg.other_nickname, other_nickname, strlen(other_nickname)+1); 148 | memcpy(&msg.text, text, strlen(text)+1); 149 | 150 | sendto(sock, &msg, sizeof msg, 0, NULL, sizeof(struct sockaddr_in)); 151 | /// 152 | 153 | 154 | 155 | } else { // TODO(rad) 156 | // puts("got msg"); 157 | // fflush(NULL); 158 | message msg; 159 | recvfrom(sock, &msg, sizeof msg, 0, NULL, NULL); 160 | 161 | if (msg.type == msg_username_taken) { 162 | puts("This username is already taken\n"); 163 | close(sock); 164 | exit(0); 165 | } else if (msg.type == msg_server_full) { 166 | puts("Server is full\n"); 167 | close(sock); 168 | exit(0); 169 | } else if (events[i].events & EPOLLHUP) { 170 | puts("Disconnected\n"); 171 | exit(0); 172 | } else if (msg.type == msg_ping) { 173 | sendto(sock, &msg, sizeof msg, 0, NULL, sizeof(struct sockaddr_in)); 174 | } else if (msg.type == msg_stop) { 175 | puts("Stopping\n"); 176 | close(sock); 177 | exit(0); 178 | } else if (msg.type == msg_get) { 179 | puts(msg.text); 180 | } 181 | } 182 | 183 | 184 | } 185 | } 186 | } -------------------------------------------------------------------------------- /cw10/zad2/common.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #define safe(expr) \ 21 | ({ \ 22 | typeof(expr) __tmp = expr; \ 23 | if (__tmp == -1) { \ 24 | printf("%s:%d "#expr" failed: %s\n", __FILE__, __LINE__, strerror(errno));\ 25 | exit(EXIT_FAILURE); \ 26 | } \ 27 | __tmp; \ 28 | }) 29 | #define loop for(;;) 30 | #define find(init, cond) ({ int index = -1; for (init) if (cond) { index = i; break; } index; }) 31 | #define repeat(n) for(int i = 0; i < n; i++) 32 | #define print(x) write(STDOUT_FILENO, x, sizeof(x)) -------------------------------------------------------------------------------- /cw10/zad2/message.h: -------------------------------------------------------------------------------- 1 | #define MSG_LEN 256 2 | 3 | typedef enum { 4 | msg_ping, 5 | msg_username_taken, 6 | msg_server_full, 7 | msg_disconnect, 8 | msg_get, 9 | msg_init, 10 | msg_list, 11 | msg_tone, 12 | msg_tall, 13 | msg_stop, 14 | msg_connect, 15 | } message_type; 16 | 17 | typedef struct { 18 | message_type type; 19 | char text[MSG_LEN]; // INCLUDES NICKNAME 20 | char nickname[MSG_LEN]; 21 | char other_nickname[MSG_LEN]; 22 | } message; 23 | --------------------------------------------------------------------------------