From b5921f2165b86a325ebc032d03c5ab91475bedd2 Mon Sep 17 00:00:00 2001 From: Ankan Poddar Date: Sat, 22 Dec 2018 18:02:57 +0530 Subject: [PATCH] Assignment 2 --- half.c | 35 +++++++++++++++++++++++++++++++++++ square.c | 35 +++++++++++++++++++++++++++++++++++ twice.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 half.c create mode 100644 square.c create mode 100644 twice.c diff --git a/half.c b/half.c new file mode 100644 index 0000000..ee1e373 --- /dev/null +++ b/half.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +char* itoa(int val, int base){ + + static char buf[32] = {0}; + int i = 30; + for(; val && i ; --i, val /= base) + buf[i] = "0123456789abcdef"[val % base]; + return &buf[i+1]; + +} + +int main(int argc, char* argv[]) { + if (argc == 1) + exit(0); + + int n = atoi(argv[argc - 1]); + int res = n / 2; + printf("Halving, Current process id: %u, Current result: %d\n", getpid(), res); + + char* v[argc]; + for (int i = 1; i < (argc-1) ; i++) { + v[i-1] = argv[i]; + } + + v[argc-2] = itoa(res, 10); + v[argc-1] = NULL; + + execvp(v[0], v); +} + diff --git a/square.c b/square.c new file mode 100644 index 0000000..f9f7146 --- /dev/null +++ b/square.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +char* itoa(int val, int base){ + + static char buf[32] = {0}; + int i = 30; + for(; val && i ; --i, val /= base) + buf[i] = "0123456789abcdef"[val % base]; + return &buf[i+1]; + +} + +int main(int argc, char* argv[]) { + if (argc == 1) + exit(0); + + int n = atoi(argv[argc - 1]); + int res = n * n; + printf("Squaring, Current process id: %u, Current result: %d\n", getpid(), res); + + char* v[argc]; + for (int i = 1; i < (argc-1) ; i++) { + v[i-1] = argv[i]; + } + + v[argc-2] = itoa(res, 10); + v[argc-1] = NULL; + + execvp(v[0], v); +} + diff --git a/twice.c b/twice.c new file mode 100644 index 0000000..1dca0cb --- /dev/null +++ b/twice.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +char* itoa(int val, int base){ + + static char buf[32] = {0}; + int i = 30; + for(; val && i ; --i, val /= base) + buf[i] = "0123456789abcdef"[val % base]; + return &buf[i+1]; + +} + +int main(int argc, char* argv[]) { + if (argc == 1) + exit(0); + + int n = atoi(argv[argc - 1]); + int res = 2 * n; + printf("Doubling, Current process id: %u, Current result: %d\n", getpid(), res); + + char* v[argc]; + for (int i = 1; i < (argc-1) ; i++) { + v[i-1] = argv[i]; + } + + v[argc-2] = itoa(res, 10); + v[argc-1] = NULL; + + execvp(v[0], v); +} +