diff --git a/lab0/hello/empty b/lab0/hello/empty new file mode 100644 index 000000000..e69de29bb diff --git a/lab0/hello/newhello.c b/lab0/hello/newhello.c new file mode 100755 index 000000000..c71ae2565 --- /dev/null +++ b/lab0/hello/newhello.c @@ -0,0 +1,6 @@ +#include + +int main() +{ + printf("Hello World!\n"); +} \ No newline at end of file diff --git a/lab1/src/p.txt b/lab1/src/p.txt new file mode 100644 index 000000000..566084644 --- /dev/null +++ b/lab1/src/p.txt @@ -0,0 +1 @@ +hello world and all people diff --git a/lab1/src/script.sh b/lab1/src/script.sh new file mode 100755 index 000000000..b2dd522ce --- /dev/null +++ b/lab1/src/script.sh @@ -0,0 +1,3 @@ +dt=$(date '+%d/%m/%Y %H:%M:%S'); +echo "$dt" +echo $path diff --git a/lab1/src/with_cake.txt b/lab1/src/with_cake.txt new file mode 100644 index 000000000..61bbb8f60 --- /dev/null +++ b/lab1/src/with_cake.txt @@ -0,0 +1,381 @@ +Here is some milk and oaten cake. +And a halfpenny cake, +I'll give you a cake +She takes the cake - +One day it's honey and raisin cake, +Don't that take the cake +With the big bad devil's food cake. +Sweet as a cake +I say let 'em all eat cake. +And baked him a cake +Is it the baker who gave you a cake +Was sugar cake +We ask them for bread and get ?let them eat cake? +when it comes to brains I take the cake +The sacramental cake: +Well, I'll be a lawman's piece of cake +And the maid comes in with coffee and cake +is just icing on the cake, +Nibblin' on sponge cake +And when you stand before the candles on a cake +There's no bread, let them eat cake +In the wedding cake? +I want to be the girl with the most cake +This is what you call presidential cake up +I light the candles and I cut the cake +I put my face down in the cake +Back inside Sam, throw away your cake +What's up? They all want the cake +And I will bake a sugar cake +Baby Cham and Foxy Brown, ah we take the cake +A piece of your cake? +He was the one afraid to cut the cake +Right, look, I was out of town getting cake +Well did he leave your pretty fingers lying in the wedding cake +A sell dem sell very cheap fi fruit cake +But I wake, mountains pop out of the cake +Rubberbands round cash im makin cake +We get a bit of the good life, a piece of the cake +Mary antoinette enjoyed her cake +After Canada the USA's a piece of cake +And ain't no fake gonna steal my cake +Homie over here's tryin to take his cake +makes you humble for the pot, I know he's accustomed to the cake +You traded in your real man for trailer life and funnel cake +Want a big piece of the cake +Is sweeter than the cake +I like cake, +I'll cut out cake +Don't need candles and cake +I'm gonna be on you like a fat kid on cake! +Becomes a piece of cake +So damp and rich a cake- +Back the cake, +The way John Goodman needs cake +this dessert, takes the cake, +Becomes a piece of cake +While he gorges on truffles and cake. +When there was still one piece of cake +when it comes to brains I take the cake +That's nice, but it's just icing on the cake. +Not an enticing effect to be icing your face like a cake +Some take just apple pie, some take just cake +Happy birthday darling, I've no present, no fancy cake +Vanilla fudge and wedding cake +We ought to bake a sunshine cake, +[We ought to bake a sunshine cake] +[So why not bake a sunshine cake,] +I thought the world was sugar cake +before the candles on a cake +He's a piece of cake +It's as easy as pie, and as simple as cake +Matter fact make me a birthday cake +Celebrate, drink up, get your cake up, +Did you leave your pretty fingers flying in the wedding cake? +Spaghetti and cookies and cake +About the time the couple cut the cake +and janey's fingers were in the cake +While blowing out the candles on a birthday cake +Blow out the candles on the cake +Running this hotel is no piece of cake +some folks like cake +He said don't worry it's a piece of cake +I popped out from a cake +Poor old papa carved the turkey and little sister cut the cake +Too many reaching for a piece of cake +you sick little cup cake, +Now it's time to light another candle on the cake +Why don't you take a stone and eat it just as if it were a cake +It ain't hard to tell, you know I get cake +It all comes with the wedding cake +But you take the cake +I live in a cake, +I make spaghetti, she brings cake +I had to leave before they even cut the cake. +I'm the one that get the cake +The streets is talkin and they tellin me to go get that cake +I swear it ain't been the same ever since I got cake +I'm on a paper chase for pies, yeah that cookies and cake +I'm on a paper chase for pies, yeah that cookies and cake +And you're filling your fat face with every different kind of cake +You serve me up with cake +Well all that icing and all that cake, +in the wedding cake? +This is what you call presidential cake up +This is what you call presidential cake up +Remember I'm gonna spend my cake +Yeah I VV'd the stones, dog I'm into cake +You like the frosting, you just bought the cake +The kids won't eat my charcoal cake +Playin' patty cake (patty cake) +Thinking about you is the icing on the cake. +Now they're out in the woods eating ice-cream and cake +Sweeter than a buckwheat cake +About the time the couple cut the cake +It's time to get wise, rise, and cut the cake +iceing on a cake +Carved our initials on top of the cake +Here on earth I'll have my cake +I'm the candles on you're cake +when do i jump out of the cake +famous for his cake +Where we're all gonna have a cake. +Can I have another piece of chocolate cake? +Why my homeys ain't get that cake +Half baked, eatin' ganja cake +Janey's fingers were in the cake +If I told you what I make, everytime I flip a cake +i lived in a cake +Lost in coma and covered in cake. +All's fair in love and war, piece of cake +Claiming the got backs but they never share their cake +Give me a cake +And a piece of the wedding cake +And Janey's fingers were in the cake +Ain't no piece of cake +it hasn't always been a piece of cake +Did you leave your pretty fingers lying in the wedding cake? +on my way up, gettin my cake up +Just icing on the cake +Want you to come get a file in your cake +He lost his slice of the eternal cake +The girl in the cake +As we keep you wired, so up the stakes, cut the cake +dem sin a pile up on dem and it a cake up +Won't feed the birds bread boys, let them eat cake +I'll make my money, set my cake up +Now, let's set it straight, B-I-G don't cuff or cake +People say you can't have your cake +Some songs sound like cake +straight to the hood to see some new cake +I gotta stay awake when I hold the cake +Those who could relate know the world ain't cake +Where you said put the cake +soup and his kingly cake +spaghetti and cookies and cake +He said you try it, I said it's a piece of cake +Too much sugar and too much cake +Are you made or broken by the birthday cake? +Thirty-nine (39) candles burnt out on a cake +Sweet carob rice cake +Well, she go to see the baker when she wants a cake +Traveling and stacking the cake +I've got some mochomo cake +now my cake up +Man, this really takes the cake +You bake my cake +Cutting the cake +I'm cake +see the sugar bowl do a tootsi roll and the bad devils food cake +I wanna have my piece of cake +Yeah and I'm about cake +Violet wants ice cream, Violet wants cake +I want the whole of the cake +For the birthday cake +Been like this ever since I got my cake up +I flashed a grin and said, Jack this is gonna be a piece of cake +They do whatever it takes to keep a lil' cake +Baking up a Martian cake +but the something on the side is what takes the cake +These limitations that burden me must be the icing on your cake +To much icind on the cake +But your kiss is sweeter than the cake +My heart, my soul, my candy, my cake +I'm a bake a hater cake today +seems like a cake +Being that we ugly, it ain't a piece of cake. +I'm selfish got to have the pie and the whole cake +comin' around for devil's cake +There were no balloons or cake +Dark, rich and thick like a fresh baked cake +I'm craving the taste of the cake +Where you goin'? Where I'm goin? Finna get the cake!/ +If I knew you were comin' I'd've baked a cake +Ate up all of the wedding cake, Uh-huh. +Eat it like a piece of cake +Her name is Little Debbie, and I love her cake +Light a fuse, bake a cake +Thirty-one candles on her birthday cake +You'll miss my carrot cake +I love you like a fat kid love cake +First they brought in tay and cake +Antagonizing pressure is the icing on the cake +I eat my cake +The kids won't eat my charcoal cake +But this job ain't all cookies and cake +They brought in tea and whisky cake. +All the world is birthday cake +I ain't tryina sell my soul but I'm tryina get this cake +And I will bake a sugar cake +Consider my raw demeanor the icing on the cake +Life is just a piece of cake +'cause wit whipped cream on em, they taste just like cake +Most friends were broke, only a couple had cake +I'd celebrate at your wake, I'd bake myself a cake +I'm just tryin' to be the great, tryin' to get a piece of cake +You can't get a slice if you don't bake the cake +If 50 was Puffy, you'd run and go get him a cheese cake +But bein an entertainer puts the icin on the cake +Your pockets we shake for cake +So just cut the stupid cake +But this one takes the cake +And ain't no fake gonna steal my cake +makes you humble for the pot, I know he's accustomed to the cake +I'm a man - I insist you insist on a piece of cake +You're a man - You insist I insist on a piece of cake +"Let 'em eat cake." +I don't want a piece of your cake! +Tryin' not to go to jail, tryin' not to cake up +We smash for cake +In an old age pension-less cake +Vanilla fudge and wedding cake +(You are just the icing to put on my cake) +Eat a crocodile cake +You have let us eat the cake +To make up for that piece of cake +Matter fact make me a birthday cake +And start to bake a sugar cake +ha get your cake up +Get cha cake up +Somebody's been digging 'round in my cake. +When a waiter brought out a big strawberry cake +And I quietly finished my strawberry cake +There's too much motherfuckin' paper to cake up +stake and a layer cake. +The jingoistic cake +Black the colour icing on our cake +and janey's fingers were in the cake +The layers on the cake +I'm a cup of hot coffee, I'm a piece of cake. +I can fill your larder, I'm a piece of cake. +you can bowl me over, I'm a piece of cake. +Been piece of cake +He said don't worry it's a piece of cake +Four and twenty blackbirds in a cake +odd years without having my cake up +You dudes is cake +Then blow out the candles, and have a piece of cake! +All I found was that old piece of our wedding cake. +The few hours off to eat wedding cake +But I hear tell someone's been diggin round in my cake +Come with the wedding cake +Comes with the wedding cake +a piece of cake +Red Velvet cake +Joy and tears come with the wedding cake. +It all comes with the wedding cake... +I eat it up and take the cake +In time I've earned my cake, +Today my son birthday and I can't even buy a cake +I had to leave before they even cut the cake. +She in her birthday suit cause of the damn cake +You serve me up with cake +Who ate every crumb of the wedding cake. +Cuz wit whipped cream on em, they taste just like cake +The safe in the floor with a life time of cake +Him and Genius flew back from, Uganda black, gettin' that cake +Just the sweet watermelon and the buckwheat cake +Gotta get my cake up +Marie said "Just let them eat cake" +Four and twenty blackbirds in a cake +for a cup of cold coffee and a piece of cake. +I will be the file in the cake +getting that independent cake +I'm gonna bake me her favorite cake +But wait, he got singles in his cake +I got locked by the jake, you was right I had to take the whole cake +I'll screw my only brother for even a glimpse at a piece of the cake +Leaving all the yellow roses on her wedding cake +They just get the news and the ones in power always get the cake +Thinking about you is the icing on the cake. +It's time to get wise, rise, and cut the cake +Becomes a piece of cake +Did we mention each meal would be full of fun cake? +Mek all mi allstar cake +It's gonna melt the icing on our cake +Mind right, cake up +Birthday wordplay, piece of cake +heaven is a beef-cake. +Beef cake? +Triple beam wit da lean, the man wit da cake +I believe in birthday cake +And I can't be runnin late cause I'm about that cake +Sweet potatoe cake +The only thing she'd make was cake, +Four and twenty blackbirds in a cake +Janey's fingers were in the cake +I bake Donald Trump cake +so i get up off my bump and go get that cake +But im still gettin cake +I'm your cream cake +Before you hibernate and you come out a crab cake" +When I was nine I said a rhyme by putting candles on my cake +I love you like a fat kid love cake +If you bring enough for them, you can have your cake +And Janey's fingers were in the cake +You take the cake. +Your style is all tissue, chocolate fudge cream cake +Luck seemed to serve us a slice of life's cake +Baby don't stop, take it to the top, eat it like a piece of cake +She might pop out of the top of a birthday cake +Come with the wedding cake. +I didn't know this was the place I'd get my piece of the cake +Or feed you poison birthday cake +i`m popping out of my rebirthday cake +I bang up like a birthday cake +Watch me bake my own cake +Ended up in folsom but the file was in the cake +The camel's tellin' you to come on, you had to cake it +You have sex with cake +dem sin a pile up on dem and it a cake up +Won't feed the birds bread boys, let them eat cake +From "Fruitcake" by Eraserheads: +Life is a piece of cake +And start to bake a sugar cake +It's no piece of cake, +I say stick together God until y'all cake up +That way they get the biggest slice of the cake +ni mamul bbetnun kosun negenun piece of a cake +Those who could relate know the world ain't cake +Ice cream and cake. +Whether we have not or have cake +For a slice of cake +Are you made or broken by the birthday cake? +And I share her love of wine and cake +Same knife that cuts your throat divides the cake +well, first she brought out tea and cake, +Still you want to have and eat your cake +Well I found a match and wrote these words on your cake +and the brook valley man dem need a slice a di cake +And a red-head freckled face blows out the candles on his birthday cake +Cutting the cake +Becomes a piece of cake +But your friends are hungry so just cut the stupid cake. +You just keep on trying till you run out of cake +Gave up your lot for a bite of the cake +If I kill, yo, I'm doin' so for the cake +My piece of the cake +It's a'one big... one big... one big... cake! +Like dropping out of weight while you're trying to have your cake +You know it's all for the cake, yeah it's all for the cake +That's when the voice began to barter with the promise of cake. +She could almost taste the cake. +on my birthday give me a burger not a cake +Love burns all and hate takes the cake +And we would sail under the stars eating strawberry cake +I'm a bake a hater cake today +Then its kissy smiley, fake fake fake, Like a cake +And dream of mama's chocolate cake +Hey producer cut me on the cake +Everyday is a grind, gotta get the cake up +We drank a lot of stout and ate a lot of cake! +He said baby you'r so hott you could bake my cake +till you run out of cake. +I'm craving the taste of the cake +I shake the jake to get the cake +Baked inside a fudge cake. +Ain't nothing but a fruit cake +Out of a ginger cake +Won't even buy me cheap, cheap cake +Let's spend some cake +Jimmy wanna piece o' your chocolate cake! diff --git a/lab5/src/deadlock b/lab5/src/deadlock new file mode 100644 index 000000000..591e25e98 Binary files /dev/null and b/lab5/src/deadlock differ diff --git a/lab5/src/deadlock.c b/lab5/src/deadlock.c new file mode 100644 index 000000000..d44b3f649 --- /dev/null +++ b/lab5/src/deadlock.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include + +int X = 2; +int Y = 3; + +pthread_mutex_t mut1 = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER; + +void xy() +{ + int x; + int y; + pthread_mutex_lock(&mut1); + x = X; + printf("X = %d was put in th1\n", x); + sleep(1); + pthread_mutex_lock(&mut2); + y = Y; + printf("Y = %d was put in th1\n", y); + printf("X*Y = %d\n", x * y); + pthread_mutex_unlock(&mut2); + pthread_mutex_unlock(&mut1); +} + +void yx() +{ + int x; + int y; + pthread_mutex_lock(&mut2); + y = Y; + printf("Y = %d was put in th2\n", y); + sleep(1); + pthread_mutex_lock(&mut1); + y = Y; + printf("X = %d was put in th2\n", x); + printf("Y*X = %d\n", y * x); + pthread_mutex_unlock(&mut1); + pthread_mutex_unlock(&mut2); +} + +int main() { + pthread_t thread1, thread2; + + if (pthread_create(&thread1, NULL, (void*)xy, NULL) != 0) + { + perror("pthread_create"); + exit(1); + } + + if (pthread_create(&thread2, NULL, (void*)yx, NULL) != 0) + { + perror("pthread_create"); + exit(1); + } + + if (pthread_join(thread1, NULL) != 0) + { + perror("pthread_join"); + exit(1); + } + + if (pthread_join(thread2, NULL) != 0) + { + perror("pthread_join"); + exit(1); + } + + return 0; +} \ No newline at end of file diff --git a/lab5/src/factorial b/lab5/src/factorial new file mode 100644 index 000000000..5807c5939 Binary files /dev/null and b/lab5/src/factorial differ diff --git a/lab5/src/factorial.c b/lab5/src/factorial.c new file mode 100644 index 000000000..f43eaa4a8 --- /dev/null +++ b/lab5/src/factorial.c @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include + +struct Segment +{ + int begin; + int end; + int mod; +}; + +int result = 1; + +pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; + +void factorial(void* segments) +{ + + struct Segment* segment = (struct Segment*)segments; + int temp_result = 1; + for (int i = (*segment).begin; i <= (*segment).end; i++) + { + temp_result *= ((i % (*segment).mod) == 0 ? 1 : i); + sleep(1); + + } + pthread_mutex_lock(&mut); + result *= temp_result; + pthread_mutex_unlock(&mut); +} + +int main(int argc, char** argv) +{ + int k = -1; + int pnum = -1; + int mod = -1; + + while (true) + { + int current_optind = optind ? optind : 1; + + static struct option options[] = { { "k", required_argument, 0, 0}, + { "pnum", required_argument, 0, 0}, + { "mod", required_argument, 0, 0}, + { 0, 0, 0, 0} }; + + int option_index = 0; + int c = getopt_long(argc, argv, "f", options, &option_index); + + if (c == -1) break; + + switch (c) + { + case 0: + switch (option_index) + { + case 0: + k = atoi(optarg); + if (k <= 0) + { + printf("k is a positive number\n\n"); + return 1; + } + break; + case 1: + pnum = atoi(optarg); + if (pnum <= 0) + { + printf("pnum is a positive number\n"); + return 1; + } + break; + case 2: + mod = atoi(optarg); + if (mod <= 0) + { + printf("mod is a positive number\n"); + return 1; + } + break; + + defalut: + printf("Index %d is out of options\n", option_index); + } + break; + + case 'f': + break; + + case '?': + break; + + default: + printf("getopt returned character code 0%o?\n", c); + } + } + + if (optind < argc) + { + printf("Has at least one no option argument\n"); + return 1; + } + + if (k == -1 || pnum == -1 || mod == -1) + { + printf("Usage: %s --k \"num\" --pnum \"num\" --mod \"num\" \n", + argv[0]); + return 1; + } + + pthread_t threads[pnum]; + struct Segment segment[pnum]; + for (int i = 0; i < pnum; i++) + { + segment[i].begin = (k / pnum) * i + 1; + segment[i].end = ((i != pnum - 1) ? (k / pnum) * (i + 1) : k); + segment[i].mod = mod; + if (pthread_create(&threads[i], NULL, (void*)factorial, (void*)(segment + i)) != 0) + { + printf("Error: pthread_create failed!\n"); + return 1; + } + } + + for (int i = 0; i < pnum; i++) + { + if (pthread_join(threads[i], NULL) != 0) + { + perror("pthread_join"); + exit(1); + } + } + printf("\nResult: %d\n", result); + return 0; +} \ No newline at end of file diff --git a/lab5/src/mutex b/lab5/src/mutex new file mode 100644 index 000000000..693f04cf7 Binary files /dev/null and b/lab5/src/mutex differ diff --git a/lab5/src/mutex2 b/lab5/src/mutex2 new file mode 100644 index 000000000..dc7e0d4c4 Binary files /dev/null and b/lab5/src/mutex2 differ diff --git a/lab6/src/Server.txt b/lab6/src/Server.txt new file mode 100644 index 000000000..3f52ed81b --- /dev/null +++ b/lab6/src/Server.txt @@ -0,0 +1,2 @@ +127.0.0.1:20001 +127.0.0.1:20002 \ No newline at end of file diff --git a/lab6/src/client.c b/lab6/src/client.c index 91b35af32..0934ac0b3 100755 --- a/lab6/src/client.c +++ b/lab6/src/client.c @@ -12,24 +12,51 @@ #include #include -struct Server { - char ip[255]; - int port; +#include +#include "pthread.h" +#include "multmodulo.h" + +struct ParallelArgs { + uint64_t begin; + uint64_t end; + uint64_t mod; + int sck; }; -uint64_t MultModulo(uint64_t a, uint64_t b, uint64_t mod) { - uint64_t result = 0; - a = a % mod; - while (b > 0) { - if (b % 2 == 1) - result = (result + a) % mod; - a = (a * 2) % mod; - b /= 2; - } +uint64_t GetAnswer(const struct ParallelArgs *args) { + char task[sizeof(uint64_t) * 3]; + memcpy(task, &args->begin, sizeof(uint64_t)); + memcpy(task + sizeof(uint64_t), &args->end, sizeof(uint64_t)); + memcpy(task + 2 * sizeof(uint64_t), &args->mod, sizeof(uint64_t)); - return result % mod; + if (send(args->sck, task, sizeof(task), 0) < 0) { + fprintf(stderr, "Send failed\n"); + exit(1); + } + + + char response[sizeof(uint64_t)]; + if (recv(args->sck, response, sizeof(response), 0) < 0) { + fprintf(stderr, "Recieve failed\n"); + exit(1); + } + + uint64_t cur_answer = 0; + memcpy(&cur_answer, response, sizeof(uint64_t)); + + return cur_answer; } +void *ThreadServer(void *args) { + struct ParallelArgs *fargs = (struct ParallelArgs *)args; + return (void *)(uint64_t *)GetAnswer(fargs); +} + +struct Server { + char ip[255]; + int port; +}; + bool ConvertStringToUI64(const char *str, uint64_t *val) { char *end = NULL; unsigned long long i = strtoull(str, &end, 10); @@ -69,14 +96,24 @@ int main(int argc, char **argv) { switch (option_index) { case 0: ConvertStringToUI64(optarg, &k); - // TODO: your code here + if (k < 0) { + printf("k is not negative number\n"); + return 1; + } break; case 1: ConvertStringToUI64(optarg, &mod); - // TODO: your code here + if (mod <= 0) { + printf("mod is a positive number\n"); + return 1; + } break; case 2: - // TODO: your code here + if(access(optarg, R_OK)) + { + printf("Cannot access file\nFile path format: /path/to/file\n"); + return 1; + } memcpy(servers, optarg, strlen(optarg)); break; default: @@ -98,12 +135,23 @@ int main(int argc, char **argv) { return 1; } - // TODO: for one server here, rewrite with servers from file + FILE *fp; + fp = fopen(servers, "r"); unsigned int servers_num = 1; + fscanf(fp, "%d\n", &servers_num); struct Server *to = malloc(sizeof(struct Server) * servers_num); + + // TODO: delete this and parallel work between servers - to[0].port = 20001; - memcpy(to[0].ip, "127.0.0.1", sizeof("127.0.0.1")); + for (int i = 0; i < servers_num; i++) + { + fscanf(fp, "%s : %d\n", to[i].ip, &to[i].port); + } + + pthread_t threads[servers_num]; + struct ParallelArgs args[servers_num]; + uint64_t answer = 1; + uint64_t part_size = k / servers_num; // TODO: work continiously, rewrite to make parallel for (int i = 0; i < servers_num; i++) { @@ -131,33 +179,29 @@ int main(int argc, char **argv) { // TODO: for one server // parallel between servers - uint64_t begin = 1; - uint64_t end = k; - - char task[sizeof(uint64_t) * 3]; - memcpy(task, &begin, sizeof(uint64_t)); - memcpy(task + sizeof(uint64_t), &end, sizeof(uint64_t)); - memcpy(task + 2 * sizeof(uint64_t), &mod, sizeof(uint64_t)); - - if (send(sck, task, sizeof(task), 0) < 0) { - fprintf(stderr, "Send failed\n"); - exit(1); + args[i].begin = i*part_size + 1; + if (i + 1 == servers_num) + { + args[i].end = k; } - - char response[sizeof(uint64_t)]; - if (recv(sck, response, sizeof(response), 0) < 0) { - fprintf(stderr, "Recieve failed\n"); - exit(1); + else + { + args[i].end = (i + 1)*part_size; + } + args[i].mod = mod; + args[i].sck = sck; + if (pthread_create(&threads[i], NULL, ThreadServer, (void *)(args + i))) { + printf("Error: pthread_create failed!\n"); + return 1; } - - // TODO: from one server - // unite results - uint64_t answer = 0; - memcpy(&answer, response, sizeof(uint64_t)); - printf("answer: %llu\n", answer); - - close(sck); } + for (uint32_t i = 0; i < servers_num; i++) { + uint64_t result = 0; + pthread_join(threads[i], (void **)&result); + answer = MultModulo(answer, result, mod); + close(args[i].sck); + } + printf("answer: %lu\n", answer); free(to); return 0; diff --git a/lab6/src/makefile b/lab6/src/makefile new file mode 100644 index 000000000..f7abc7306 --- /dev/null +++ b/lab6/src/makefile @@ -0,0 +1,19 @@ +CC = gcc +CFLAG = -I. + +all : server client + +server : + $(CC) server.c -lpthread -L../multmodulo -lmultmodulo -o ./server $(CFLAG) + +client : + $(CC) client.c -lpthread -L../multmodulo -lmultmodulo -o ./client $(CFLAG) + +server.o : + $(CC) server.c -o server.o $(CFLAG) + +client.o : + $(CC) client.c -o client.o $(CFLAG) + +clean : + rm server client diff --git a/lab6/src/multmodulo.c b/lab6/src/multmodulo.c new file mode 100644 index 000000000..b964cf634 --- /dev/null +++ b/lab6/src/multmodulo.c @@ -0,0 +1,15 @@ +#include +#include "multmodulo.h" + +uint64_t multmodulo(uint64_t a, uint64_t b, uint64_t mod) { + uint64_t result = 0; + a = a % mod; + while (b > 0) { + if (b % 2 == 1) + result = (result + a) % mod; + a = (a * 2) % mod; + b /= 2; + } + + return result % mod; +} diff --git a/lab6/src/multmodulo.h b/lab6/src/multmodulo.h new file mode 100644 index 000000000..b58ab7bb5 --- /dev/null +++ b/lab6/src/multmodulo.h @@ -0,0 +1,2 @@ +#include "stdint.h" +uint64_t multmodulo(uint64_t a, uint64_t b, uint64_t mod); \ No newline at end of file diff --git a/lab6/src/server.c b/lab6/src/server.c index 4c5f53abb..3325456f7 100755 --- a/lab6/src/server.c +++ b/lab6/src/server.c @@ -12,6 +12,7 @@ #include #include "pthread.h" +#include "multmodulo.h" struct FactorialArgs { uint64_t begin; @@ -19,24 +20,14 @@ struct FactorialArgs { uint64_t mod; }; -uint64_t MultModulo(uint64_t a, uint64_t b, uint64_t mod) { - uint64_t result = 0; - a = a % mod; - while (b > 0) { - if (b % 2 == 1) - result = (result + a) % mod; - a = (a * 2) % mod; - b /= 2; - } - - return result % mod; -} uint64_t Factorial(const struct FactorialArgs *args) { uint64_t ans = 1; - // TODO: your code here - + for (int i = args->begin; i <= args->end; i++) + { + ans = multmodulo(ans, i, args->mod); + } return ans; } @@ -67,11 +58,17 @@ int main(int argc, char **argv) { switch (option_index) { case 0: port = atoi(optarg); - // TODO: your code here + if (port <= 0) { + printf("port is a positive number\n"); + return 1; + } break; case 1: tnum = atoi(optarg); - // TODO: your code here + if (tnum <= 0) { + printf("tnum is a positive number\n"); + return 1; + } break; default: printf("Index %d is out of options\n", option_index); @@ -154,17 +151,23 @@ int main(int argc, char **argv) { memcpy(&end, from_client + sizeof(uint64_t), sizeof(uint64_t)); memcpy(&mod, from_client + 2 * sizeof(uint64_t), sizeof(uint64_t)); - fprintf(stdout, "Receive: %llu %llu %llu\n", begin, end, mod); - + fprintf(stdout, "Receive: %lu %lu %lu\n", begin, end, mod); + + int part_size = (end - begin + 1) / tnum; struct FactorialArgs args[tnum]; for (uint32_t i = 0; i < tnum; i++) { - // TODO: parallel somehow - args[i].begin = 1; - args[i].end = 1; + args[i].begin = begin + i*part_size; + if (i + 1 == tnum) + { + args[i].end = end; + } + else + { + args[i].end = begin + (i + 1)*part_size - 1; + } args[i].mod = mod; - if (pthread_create(&threads[i], NULL, ThreadFactorial, - (void *)&args[i])) { + (void *)(args + i))) { printf("Error: pthread_create failed!\n"); return 1; } @@ -174,10 +177,10 @@ int main(int argc, char **argv) { for (uint32_t i = 0; i < tnum; i++) { uint64_t result = 0; pthread_join(threads[i], (void **)&result); - total = MultModulo(total, result, mod); + total = multmodulo(total, result, mod); } - printf("Total: %llu\n", total); + printf("Total: %lu\n", total); char buffer[sizeof(total)]; memcpy(buffer, &total, sizeof(total)); diff --git a/lab7/src/makefile b/lab7/src/makefile new file mode 100644 index 000000000..11d8b97b1 --- /dev/null +++ b/lab7/src/makefile @@ -0,0 +1,28 @@ +all: tcpclient tcpserver udpclient udpserver + +tcpserver : tcpserver.o + gcc -o tcpserver tcpserver.o + +tcpserver.o : tcpserver.c + gcc -c tcpserver.c + +tcpclient : tcpclient.o + gcc -o tcpclient tcpclient.o + +tcpclient.o : tcpclient.c + gcc -c tcpclient.c + +udpserver : udpserver.o + gcc -o udpserver udpserver.o + +udpserver.o : udpserver.c + gcc -c udpserver.c + +udpclient : udpclient.o + gcc -o udpclient udpclient.o + +tcpclient.o : tcpclient.c + gcc -c tcpclient.c + +clean : + rm tcpclient.o tcpserver.o udpclient.o udpserver.o tcpclient tcpserver udpclient udpserver \ No newline at end of file diff --git a/lab7/src/tcpclient.c b/lab7/src/tcpclient.c index 44fb2f48b..d958ddf59 100755 --- a/lab7/src/tcpclient.c +++ b/lab7/src/tcpclient.c @@ -7,48 +7,142 @@ #include #include -#define BUFSIZE 100 +#include +#include +#include + + +//#define BUFSIZE 100 #define SADDR struct sockaddr #define SIZE sizeof(struct sockaddr_in) -int main(int argc, char *argv[]) { - int fd; - int nread; - char buf[BUFSIZE]; - struct sockaddr_in servaddr; - if (argc < 3) { - printf("Too few arguments \n"); - exit(1); - } - - if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - perror("socket creating"); - exit(1); - } - - memset(&servaddr, 0, SIZE); - servaddr.sin_family = AF_INET; - - if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) { - perror("bad address"); - exit(1); - } - - servaddr.sin_port = htons(atoi(argv[2])); - - if (connect(fd, (SADDR *)&servaddr, SIZE) < 0) { - perror("connect"); - exit(1); - } - - write(1, "Input message to send\n", 22); - while ((nread = read(0, buf, BUFSIZE)) > 0) { - if (write(fd, buf, nread) < 0) { - perror("write"); - exit(1); - } - } - - close(fd); - exit(0); + + + +bool ConvertStringToUI64(const char* str, uint64_t* val) { + char* end = NULL; + unsigned long long i = strtoull(str, &end, 10); + if (errno == ERANGE) { + fprintf(stderr, "Out of uint64_t range: %s\n", str); + return false; + } + + if (errno != 0) + return false; + + *val = i; + return true; } + +int main(int argc, char* argv[]) { + int fd; + int nread; + + int BUFSIZE = -1; + char servadr[255] = { '\0' }; + int port = -1; + while (1) + { + int current_optind = optind ? optind : 1; + + static struct option options[] = + { + {"bufsize", required_argument, 0, 0}, + {"serv", required_argument, 0, 0}, + {"port", required_argument, 0, 0}, + {0, 0, 0, 0} + }; + + int option_index = 0; + int c = getopt_long(argc, argv, "", options, &option_index); + + if (c == -1) + break; + + switch (c) + { + case 0: + { + switch (option_index) + { + case 0: + ConvertStringToUI64(optarg, &BUFSIZE); + if (BUFSIZE <= 0) + { + printf("bufsize is a positive number\n"); + return 1; + } + break; + case 1: + memcpy(servadr, optarg, strlen(optarg)); + if (strlen(servadr) == sizeof('\0')) + { + printf("bufsize is a positive number\n"); + return 1; + } + break; + case 2: + ConvertStringToUI64(optarg, &port); + if (port <= 0) + { + printf("bufsize is a positive number\n"); + return 1; + } + break; + default: + printf("Index %d is out of options\n", option_index); + } + } + break; + + case '?': + printf("Arguments error\n"); + break; + default: + fprintf(stderr, "getopt returned character code 0%o?\n", c); + } + } + + if (BUFSIZE == -1 || !strlen(servadr) || port == -1) { + fprintf(stderr, "Using: %s --bufsize 100 --serv 127.0.0.1 --port 10050\n", argv[0]); + return 1; + } + + char buf[BUFSIZE]; + struct sockaddr_in servaddr; + if (argc < 3) { + printf("Too few arguments \n"); + exit(1); + } + + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("socket creating"); + exit(1); + } + + memset(&servaddr, 0, SIZE); + servaddr.sin_family = AF_INET; + + if (inet_pton(AF_INET, servadr, &servaddr.sin_addr) <= 0) { + perror("bad address"); + exit(1); + } + + servaddr.sin_port = htons(port); + + if (connect(fd, (SADDR*)&servaddr, SIZE) < 0) { + perror("connect"); + exit(1); + } + + write(1, "Input message to send\n", 22); + while ((nread = read(0, buf, BUFSIZE)) > 0) { + if (write(fd, buf, nread) < 0) { + perror("write"); + exit(1); + } + } + + close(fd); + exit(0); +} \ No newline at end of file diff --git a/lab7/src/tcpserver.c b/lab7/src/tcpserver.c index 01489864e..4aa8a4048 100755 --- a/lab7/src/tcpserver.c +++ b/lab7/src/tcpserver.c @@ -7,56 +7,114 @@ #include #include -#define SERV_PORT 10050 -#define BUFSIZE 100 +#include +#include + + +//#define SERV_PORT 10050 +//#define BUFSIZE 100 #define SADDR struct sockaddr -int main() { - const size_t kSize = sizeof(struct sockaddr_in); - - int lfd, cfd; - int nread; - char buf[BUFSIZE]; - struct sockaddr_in servaddr; - struct sockaddr_in cliaddr; - - if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - perror("socket"); - exit(1); - } - - memset(&servaddr, 0, kSize); - servaddr.sin_family = AF_INET; - servaddr.sin_addr.s_addr = htonl(INADDR_ANY); - servaddr.sin_port = htons(SERV_PORT); - - if (bind(lfd, (SADDR *)&servaddr, kSize) < 0) { - perror("bind"); - exit(1); - } - - if (listen(lfd, 5) < 0) { - perror("listen"); - exit(1); - } - - while (1) { - unsigned int clilen = kSize; - - if ((cfd = accept(lfd, (SADDR *)&cliaddr, &clilen)) < 0) { - perror("accept"); - exit(1); +int main(int argc, char **argv) { + const size_t kSize = sizeof(struct sockaddr_in); + + int lfd, cfd; + int nread; + + + int SERV_PORT = -1; + int BUFSIZE = -1; + while (true) { + int current_optind = optind ? optind : 1; + + static struct option options[] = { {"port", required_argument, 0, 0}, + {"bufsize", required_argument, 0, 0}, + {0, 0, 0, 0} }; + + int option_index = 0; + int c = getopt_long(argc, argv, "", options, &option_index); + + if (c == -1) + break; + + switch (c) { + case 0: { + switch (option_index) { + case 0: + SERV_PORT = atoi(optarg); + // TODO: your code here + if (SERV_PORT <= 0) + { + printf("port is a positive number"); + } + break; + case 1: + BUFSIZE = atoi(optarg); + // TODO: your code here + if (BUFSIZE < 0) + { + printf("bufsize is a positive number"); + } + break; + default: + printf("Index %d is out of options\n", option_index); + } + } break; + + case '?': + printf("Unknown argument\n"); + break; + default: + fprintf(stderr, "getopt returned character code 0%o?\n", c); + } } - printf("connection established\n"); - while ((nread = read(cfd, buf, BUFSIZE)) > 0) { - write(1, &buf, nread); + if (SERV_PORT == -1 || BUFSIZE == -1) { + fprintf(stderr, "Using: %s --port 10050 --bufsize 100\n", argv[0]); + return 1; } - if (nread == -1) { - perror("read"); - exit(1); + char buf[BUFSIZE]; + struct sockaddr_in servaddr; + struct sockaddr_in cliaddr; + + if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("socket"); + exit(1); + } + + memset(&servaddr, 0, kSize); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(SERV_PORT); + + if (bind(lfd, (SADDR*)&servaddr, kSize) < 0) { + perror("bind"); + exit(1); + } + + if (listen(lfd, 5) < 0) { + perror("listen"); + exit(1); + } + + while (1) { + unsigned int clilen = kSize; + + if ((cfd = accept(lfd, (SADDR*)&cliaddr, &clilen)) < 0) { + perror("accept"); + exit(1); + } + printf("connection established\n"); + + while ((nread = read(cfd, buf, BUFSIZE)) > 0) { + write(1, &buf, nread); + } + + if (nread == -1) { + perror("read"); + exit(1); + } + close(cfd); } - close(cfd); - } -} +} \ No newline at end of file diff --git a/lab7/src/udpclient.c b/lab7/src/udpclient.c index 2c02b8d72..82307cb02 100755 --- a/lab7/src/udpclient.c +++ b/lab7/src/udpclient.c @@ -9,49 +9,148 @@ #include #include -#define SERV_PORT 20001 -#define BUFSIZE 1024 +#include +#include +#include +#include +#include + +#include +#include + + + +//#define SERV_PORT 20001 +//#define BUFSIZE 1024 #define SADDR struct sockaddr #define SLEN sizeof(struct sockaddr_in) -int main(int argc, char **argv) { - int sockfd, n; - char sendline[BUFSIZE], recvline[BUFSIZE + 1]; - struct sockaddr_in servaddr; - struct sockaddr_in cliaddr; - - if (argc != 2) { - printf("usage: client \n"); - exit(1); - } - - memset(&servaddr, 0, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - servaddr.sin_port = htons(SERV_PORT); - - if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) < 0) { - perror("inet_pton problem"); - exit(1); - } - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - perror("socket problem"); - exit(1); - } - - write(1, "Enter string\n", 13); - - while ((n = read(0, sendline, BUFSIZE)) > 0) { - if (sendto(sockfd, sendline, n, 0, (SADDR *)&servaddr, SLEN) == -1) { - perror("sendto problem"); - exit(1); - } - if (recvfrom(sockfd, recvline, BUFSIZE, 0, NULL, NULL) == -1) { - perror("recvfrom problem"); - exit(1); +bool ConvertStringToUI64(const char* str, uint64_t* val) { + char* end = NULL; + unsigned long long i = strtoull(str, &end, 10); + if (errno == ERANGE) { + fprintf(stderr, "Out of uint64_t range: %s\n", str); + return false; } - printf("REPLY FROM SERVER= %s\n", recvline); - } - close(sockfd); + if (errno != 0) + return false; + + *val = i; + return true; } + + +int main(int argc, char** argv) { + int sockfd, n; + + int SERV_PORT = -1; + int BUFSIZE = -1; + char servadr[255] = { '\0' }; + while (true) + { + int current_optind = optind ? optind : 1; + + static struct option options[] = + { + {"serv", required_argument, 0, 0}, + {"port", required_argument, 0, 0}, + {"bufsize", required_argument, 0, 0}, + {0, 0, 0, 0} + }; + + int option_index = 0; + int c = getopt_long(argc, argv, "", options, &option_index); + + if (c == -1) + break; + + switch (c) + { + case 0: + { + switch (option_index) + { + case 0: + memcpy(servadr, optarg, strlen(optarg)); + if (strlen(servadr) == sizeof('\0')) + { + printf("bufsize is a positive number\n"); + return 1; + } + break; + case 1: + ConvertStringToUI64(optarg, &SERV_PORT); + if (SERV_PORT <= 0) + { + printf("port is a positive number\n"); + return 1; + } + break; + case 2: + ConvertStringToUI64(optarg, &BUFSIZE); + if (BUFSIZE <= 0) + { + printf("bufsize is a positive number\n"); + return 1; + } + break; + default: + printf("Index %d is out of options\n", option_index); + } + } + break; + + case '?': + printf("Arguments error\n"); + break; + default: + fprintf(stderr, "getopt returned character code 0%o?\n", c); + } + } + + if (!strlen(servadr) || SERV_PORT == -1 || BUFSIZE == -1) { + fprintf(stderr, "Using: %s --serv 127.0.0.1 --port 20001 --bufsize 1024\n", argv[0]); + return 1; + } + + char sendline[BUFSIZE], recvline[BUFSIZE + 1]; + struct sockaddr_in servaddr; + struct sockaddr_in cliaddr; + + if (argc < 3) { + printf("usage: client \n"); + exit(1); + } + + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(SERV_PORT); + + if (inet_pton(AF_INET, servadr, &servaddr.sin_addr) < 0) { + perror("inet_pton problem"); + exit(1); + } + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + perror("socket problem"); + exit(1); + } + + write(1, "Enter string\n", 13); + + while ((n = read(0, sendline, BUFSIZE)) > 0) { + if (sendto(sockfd, sendline, n, 0, (SADDR*)&servaddr, SLEN) == -1) { + perror("sendto problem"); + exit(1); + } + + if (recvfrom(sockfd, recvline, BUFSIZE, 0, NULL, NULL) == -1) { + perror("recvfrom problem"); + exit(1); + } + + printf("REPLY FROM SERVER= %s\n", recvline); + } + close(sockfd); +} \ No newline at end of file diff --git a/lab7/src/udpserver.c b/lab7/src/udpserver.c index 2c7f2dd80..1782e849a 100755 --- a/lab7/src/udpserver.c +++ b/lab7/src/udpserver.c @@ -8,49 +8,114 @@ #include #include -#define SERV_PORT 20001 -#define BUFSIZE 1024 +#include +#include +#include +#include +#include + +#include +#include + +#include + +//#define SERV_PORT 20001 +//#define BUFSIZE 1024 #define SADDR struct sockaddr #define SLEN sizeof(struct sockaddr_in) -int main() { - int sockfd, n; - char mesg[BUFSIZE], ipadr[16]; - struct sockaddr_in servaddr; - struct sockaddr_in cliaddr; - - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - perror("socket problem"); - exit(1); - } - - memset(&servaddr, 0, SLEN); - servaddr.sin_family = AF_INET; - servaddr.sin_addr.s_addr = htonl(INADDR_ANY); - servaddr.sin_port = htons(SERV_PORT); - - if (bind(sockfd, (SADDR *)&servaddr, SLEN) < 0) { - perror("bind problem"); - exit(1); - } - printf("SERVER starts...\n"); - - while (1) { - unsigned int len = SLEN; - - if ((n = recvfrom(sockfd, mesg, BUFSIZE, 0, (SADDR *)&cliaddr, &len)) < 0) { - perror("recvfrom"); - exit(1); +int main(int argc, char **argv) { + int sockfd, n; + + int SERV_PORT = -1; + int BUFSIZE = -1; + while (true) { + int current_optind = optind ? optind : 1; + + static struct option options[] = { {"port", required_argument, 0, 0}, + {"bufsize", required_argument, 0, 0}, + {0, 0, 0, 0} }; + + int option_index = 0; + int c = getopt_long(argc, argv, "", options, &option_index); + + if (c == -1) + break; + + switch (c) { + case 0: { + switch (option_index) { + case 0: + SERV_PORT = atoi(optarg); + // TODO: your code here + if (SERV_PORT <= 0) + { + printf("port is a positive number"); + } + break; + case 1: + BUFSIZE = atoi(optarg); + // TODO: your code here + if (BUFSIZE < 0) + { + printf("bufsize is a positive number"); + } + break; + default: + printf("Index %d is out of options\n", option_index); + } + } break; + + case '?': + printf("Unknown argument\n"); + break; + default: + fprintf(stderr, "getopt returned character code 0%o?\n", c); + } + } + + if (SERV_PORT == -1 || BUFSIZE == -1) { + fprintf(stderr, "Using: %s --port 20001 --bufsize 1024\n", argv[0]); + return 1; + } + + + char mesg[BUFSIZE], ipadr[16]; + struct sockaddr_in servaddr; + struct sockaddr_in cliaddr; + + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + perror("socket problem"); + exit(1); + } + + memset(&servaddr, 0, SLEN); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(SERV_PORT); + + if (bind(sockfd, (SADDR*)&servaddr, SLEN) < 0) { + perror("bind problem"); + exit(1); } - mesg[n] = 0; + printf("SERVER starts...\n"); + + while (1) { + unsigned int len = SLEN; + + if ((n = recvfrom(sockfd, mesg, BUFSIZE, 0, (SADDR*)&cliaddr, &len)) < 0) { + perror("recvfrom"); + exit(1); + } + mesg[n] = 0; - printf("REQUEST %s FROM %s : %d\n", mesg, - inet_ntop(AF_INET, (void *)&cliaddr.sin_addr.s_addr, ipadr, 16), - ntohs(cliaddr.sin_port)); + printf("REQUEST %s FROM %s : %d\n", mesg, + inet_ntop(AF_INET, (void*)&cliaddr.sin_addr.s_addr, ipadr, 16), + ntohs(cliaddr.sin_port)); - if (sendto(sockfd, mesg, n, 0, (SADDR *)&cliaddr, len) < 0) { - perror("sendto"); - exit(1); + if (sendto(sockfd, mesg, n, 0, (SADDR*)&cliaddr, len) < 0) { + perror("sendto"); + exit(1); + } } - } -} +} \ No newline at end of file