hi,
referring to my last question (http://stackoverflow.com/questions/876605/multiple-child-process), i am now trying to make an external sorting implementation using multiple child process.
...
fp = fopen(pathname, "r"); // open inputfile in r mode
fgets(trash, 10, fp); // ignore first line
for (i=0; i<numberOfProcess; ++i) {
#ifdef DBG
fprintf(stderr, "\nDBG: Calling fork()\n");
#endif
if ((pids[i] = fork()) < 0) {
perror("fork error");
exit(EXIT_FAILURE);
} else if (pids[i] == 0) { // Child Code
if (numbersToSort % numberOfProcess == 0) { // 16 % 4 = 0
partialDataSize = numbersToSort / numberOfProcess;
for (j=0; j<partialDataSize; j++) {
fscanf(fp, "%d", &arrayPartialData[j]);
qsort(arrayPartialData, partialDataSize, sizeof(int), (void *)comp_num);
//printf("%d\n", arrayPartialData[j]);
// TODO: qsort data until partialDataSize
}
}
printf("pid: %d child process %d outputs: ", getpid(), pids[i]);
printArray(arrayPartialData, partialDataSize);
//break;
exit(0);
}
}
/* Wait for children to exit. */
while (numberOfProcess > 0) {
pid = wait(&status);
--numberOfProcess;
}
fclose(fp);
but of course this code outputs the same sequence of sorted integers from inputfile because of fscanf.. for example if the beginning of input file includes 5 1 4, then it outputs:
(1st child) 1 4 5
(2nd child) 1 4 5
(with two child process).. because fscanf starts to read integers from the beginning of input stream.
my problem now is how can i continue to read the numbers starting from the point where the previous child process left? for example, if input file includes 5 1 4 8 5 10, then it can output:
(1st child) 1 4 5
(2nd child) 5 8 10
thanks in advance;)