Created
March 29, 2017 00:23
-
-
Save axsddlr/4c74ca8c1299f78b0d149f91b2211107 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
#include <errno.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
static long int count(long int *a); // prototype counting function | |
#ifndef MAX_ELEMENTS | |
#define MAX_ELEMENTS 10 | |
#endif /* MAX_ELEMENTS */ | |
#ifndef CLOCKS_PER_SEC | |
#error CLOCKS_PER_SEC not defined on this platform | |
#endif /* CLOCKS_PER_SEC */ | |
int main(int argc, char **argv) { | |
int pid; | |
int state; | |
long int arr[MAX_ELEMENTS], i, cnt; | |
clock_t start_time, end_time; | |
double total_time; | |
FILE *fp; | |
pid = fork(); // creating child process | |
if (pid == 0) { // starting child process | |
printf("my parent's pid = %d\n", getppid()); | |
switch ( argc ) { | |
case 1: | |
fp = stdin; | |
break; | |
case 2: | |
if ( (fp = fopen(argv[1], "r")) == NULL ) { | |
fprintf(stderr, "Could not open input file: %s\n", strerror(errno)); | |
return 2; | |
}; | |
break; | |
default: | |
fprintf(stderr, "Usage: %s [ FILE ]\n", argv[0]); | |
return 1; | |
break; | |
}; | |
start_time = clock(); | |
printf("Starting of the program: = %ld\n", start_time); | |
for ( i = 0; i < MAX_ELEMENTS; i++ ) { | |
fscanf(fp,"%ld", &arr[i]); | |
printf("%ld\n", arr[i]); | |
}; | |
fclose(fp); | |
cnt = count(arr); | |
end_time = clock(); | |
printf("Result: %ld\n", cnt); | |
total_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; | |
printf("Total time taken by CPU: %f\n", total_time); | |
return 0; | |
_exit(0); // end child process | |
} | |
/* Now back to the parent code */ | |
if (pid < 0) { | |
fprintf(stderr, "\tfork failed\n\n"); | |
_exit(1); | |
} | |
printf("Child pid = %d\n", pid); | |
//wait(&state); /* wait for the child process to end */ | |
//printf("The state value is: %d\n ", state); | |
//return; | |
} | |
static long int count(long int *a) { | |
int counting, i, j, k; | |
counting = 0; | |
for ( i = 0; i < MAX_ELEMENTS - 2; i++ ) | |
for ( j = i + 1; j < MAX_ELEMENTS - 1; j++ ) | |
for ( k = j + 1; k < MAX_ELEMENTS; k++ ) | |
if ( (a[i] + a[j] + a[k]) == 0 ) { | |
counting++; | |
printf("Triplet Found : %ld, %ld, %ld\n", a[i], a[j], a[k]); | |
}; | |
return counting; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment