Created
February 26, 2015 04:36
-
-
Save ollien/ecc14d663f246d7512cf to your computer and use it in GitHub Desktop.
2/25/15 Homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#include <sys/types.h> | |
void forkAndCalculate(int num); | |
int main(int argc, char **argv){ | |
if (argc!=2 || argv[1]<0){ | |
printf("Malformed input!\n"); | |
return 1; | |
} | |
int status; | |
forkAndCalculate(atoi(argv[1])); | |
return 0; | |
} | |
void forkAndCalculate(int num){ | |
int result = fork(); | |
int status; | |
if (result==0){ | |
printf("%d ",num); | |
while (num!=1){ | |
if (num%2==0){ | |
num/=2; | |
} | |
else{ | |
num*=3; | |
num+=1; | |
} | |
printf("%d ",num); | |
} | |
printf("\n"); | |
exit(0); | |
} | |
else{ | |
wait(&status); | |
printf("Done!\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment