LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-18-2010, 01:32 PM   #1
penguinGirl
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
Question Help Executing C Program From Java! (Ubuntu)


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:

Code:
*** 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:

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;
}
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!
 
Old 04-18-2010, 01:58 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Try using gdb or valgrind to debug. It's probably related to your malloc/free; did you malloc enough space?
 
Old 04-18-2010, 02:03 PM   #3
penguinGirl
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Yeah, I'm pretty sure I malloc'ed enough space because when I execute the code via the command line, it works just fine.

Also, I'm not sure how to use gdb or valgrind on this because the code is being called from within a Java program...
 
  


Reply

Tags
java, ubuntu



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a program in c daYz Programming 2 07-27-2007 03:34 PM
Executing program as non-root arip Programming 4 12-01-2005 05:41 AM
executing a program as root funkymunky Linux - General 2 05-06-2005 12:48 PM
Writing and executing a program Russb Linux - Software 1 07-25-2004 06:49 PM
executing a program in new terminal? kashifbilal Programming 7 04-26-2004 04:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration