Skip to content

Commit

Permalink
Assignment 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ankan17 committed Dec 22, 2018
1 parent 6c4893d commit b5921f2
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
35 changes: 35 additions & 0 deletions half.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>

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);
}

35 changes: 35 additions & 0 deletions square.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>

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);
}

35 changes: 35 additions & 0 deletions twice.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>

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);
}

0 comments on commit b5921f2

Please sign in to comment.