Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 04-18-2010, 01:32 PM   #1
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:

*** glibc detected *** /home/it04/dahl0616/PA3/lib/matrix: free(): invalid next size (normal): 0x0000000001147030 ***
======= Backtrace: =========
======= 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/
7f988eb4e000-7f988ed4e000 ---p 00016000 08:02 32768                      /lib/
7f988ed4e000-7f988ed4f000 r--p 00016000 08:02 32768                      /lib/
7f988ed4f000-7f988ed50000 rw-p 00017000 08:02 32768                      /lib/
7f988ed50000-7f988eeb8000 r-xp 00000000 08:02 32923                      /lib/
7f988eeb8000-7f988f0b8000 ---p 00168000 08:02 32923                      /lib/
7f988f0b8000-7f988f0bc000 r--p 00168000 08:02 32923                      /lib/
7f988f0bc000-7f988f0bd000 rw-p 0016c000 08:02 32923                      /lib/
7f988f0bd000-7f988f0c2000 rw-p 7f988f0bd000 00:00 0 
7f988f0c2000-7f988f0e2000 r-xp 00000000 08:02 32919                      /lib/
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/
7f988f2e2000-7f988f2e3000 rw-p 00020000 08:02 32919                      /lib/
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");     

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");

   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;

   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
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,823

Rep: Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611
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
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...


java, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:51 AM.

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