Hi All:
I am working on a project for school. It is a load-balanced distributed computing system. It is written in Java, and it consists of a Server, a Client, and various nodes. Each node runs on a separate physical machine.
The system works like this: the client is given a command name and a filename. The filename points to a file that contains information needed by the command.
This data is passed to the server, and the server then passes it to the various nodes. The nodes then execute the command as a separate process using Java's runtime.exec method.
As part of this project, we are required to write a simple program to be invoked as a command. I wrote a simple C program that generates matrices and multiplies them. The code compiles and works fine when I execute it at the command line.
I did my initial development using Mac OS X. When I executed my C program through Java, everything worked as expected. When I moved my code over to my campus's Ubuntu Linux boxes, it broke. The C code compiles and works just fine from the command line. But, when I execute it via the Java code, I get the following error:
*** glibc detected *** /home/it04/dahl0616/PA3/lib/matrix: free(): invalid next size (normal): 0x0000000001147030 ***
======= Backtrace: =========
/lib/libc.so.6[0x7f988edc7cb8]
/lib/libc.so.6(cfree+0x76)[0x7f988edca276]
/lib/libc.so.6[0x7f988edb8829]
/home/it04/dahl0616/PA3/lib/matrix[0x400afc]
/lib/libc.so.6(__libc_start_main+0xe6)[0x7f988ed6e5a6]
/home/it04/dahl0616/PA3/lib/matrix[0x400669]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:20 423064 /home/it04/dahl0616/PA3/lib/matrix
00600000-00601000 rw-p 00000000 00:20 423064 /home/it04/dahl0616/PA3/lib/matrix
01147000-01168000 rw-p 01147000 00:00 0 [heap]
7f9888000000-7f9888021000 rw-p 7f9888000000 00:00 0
7f9888021000-7f988c000000 ---p 7f9888021000 00:00 0
7f988eb38000-7f988eb4e000 r-xp 00000000 08:02 32768 /lib/libgcc_s.so.1
7f988eb4e000-7f988ed4e000 ---p 00016000 08:02 32768 /lib/libgcc_s.so.1
7f988ed4e000-7f988ed4f000 r--p 00016000 08:02 32768 /lib/libgcc_s.so.1
7f988ed4f000-7f988ed50000 rw-p 00017000 08:02 32768 /lib/libgcc_s.so.1
7f988ed50000-7f988eeb8000 r-xp 00000000 08:02 32923 /lib/libc-2.9.so
7f988eeb8000-7f988f0b8000 ---p 00168000 08:02 32923 /lib/libc-2.9.so
7f988f0b8000-7f988f0bc000 r--p 00168000 08:02 32923 /lib/libc-2.9.so
7f988f0bc000-7f988f0bd000 rw-p 0016c000 08:02 32923 /lib/libc-2.9.so
7f988f0bd000-7f988f0c2000 rw-p 7f988f0bd000 00:00 0
7f988f0c2000-7f988f0e2000 r-xp 00000000 08:02 32919 /lib/ld-2.9.so
7f988f2b9000-7f988f2bb000 rw-p 7f988f2b9000 00:00 0
7f988f2de000-7f988f2e1000 rw-p 7f988f2de000 00:00 0
7f988f2e1000-7f988f2e2000 r--p 0001f000 08:02 32919 /lib/ld-2.9.so
7f988f2e2000-7f988f2e3000 rw-p 00020000 08:02 32919 /lib/ld-2.9.so
7fff437e1000-7fff437f7000 rw-p 7ffffffe9000 00:00 0 [stack]
7fff437ff000-7fff43800000 r-xp 7fff437ff000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Here is my C code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> void matrix(int N) { int i, j, k; double A[N][N]; double B[N][N]; double C[N][N]; // Generate random input for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { A[i][j] = rand(); } } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { B[i][j] = rand(); } } for (j = 0; j < N; j++) { for (k = 0; k < N; k++) { for (i = 0; i < N; i++) { C[i][j] += A[i][k] * B[k][j]; } } } for (k = 0; k < N; k++) { for (j = 0; j < N; j++) { for (i = 0; i < N; i++) { C[i][j] += A[i][k] * B[k][j]; } } } printf("Done multiplying matrices.\n"); return; } int main(int argc, char *argv[]) { // Get filename char * filename = (char *)malloc(sizeof(argv[1])); strcpy(filename, argv[1]); // Open file FILE * fp = fopen(filename, "r"); free(filename); if (fp == NULL) { printf("Error - file not found.\n"); return 0; } // Read file contents int N; fscanf(fp, "%d", &N); printf("N is %d\n", N); // Close file if (fclose(fp)) { printf("Error - unable to close file.\n"); return 0; } matrix(N); return 1; }
And here is the corresponding Java code:
public void run() { Runtime runTime = Runtime.getRuntime(); Process process = null; String currentDir = System.getProperty("user.dir"); String cmd[] = { currentDir + '/' + jobCommand, currentDir + '/' + fileName }; System.out.println("Received job command: " + jobCommand); System.out.println("Received file name: " + fileName); try { long startTime, stopTime; double elapsed; // Execute process startTime = System.currentTimeMillis(); process = runTime.exec(cmd); process.waitFor(); stopTime = System.currentTimeMillis(); int retVal = process.exitValue(); process.destroy(); if (retVal == 0) { submitErrorToServer(BAD_FILENAME); return; } // Calculate the elapsed time elapsed = (stopTime - startTime) / 1000.0; System.out.println("Time elapsed: " + elapsed + " seconds"); System.out.println("Process complete - submitting result to server."); submitResultToServer(elapsed); } catch (Exception e) { System.out.println("Exception caught: " + e); submitErrorToServer(BAD_COMMAND); } }
The input file consists of just an integer that is used to determine the size of the matrices.
Any help would be GREATLY appreciated!!!! Thank you!