LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2010, 08:45 AM   #1
awiles
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
Error on sending argv


Hi all,
I write a simple MPI program to send a text message to another process. The code is below.
(test.c)
Code:
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[]) {
    int dest, noProcesses, processId;
    MPI_Status status;
    
    char* buffer;
    
    char* text = "ABCDEF";
    
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &noProcesses);
    MPI_Comm_rank(MPI_COMM_WORLD, &processId);
    
    buffer = (char*) malloc(256 * sizeof(char));
    
    if (processId == 0) {
      fprintf(stdout, "Master: sending %s to %d\n", text, 1);
      MPI_Send((void *)&text, strlen(text) + 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
    } else {
      MPI_Recv(&buffer, 128, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
      fprintf(stdout, "Slave: received %s from %d\n", buffer, status.MPI_SOURCE);
    }
    MPI_Finalize();
    return 0;
}
After compiling and executing it I get the following output:
Code:
[root@cluster Desktop]# mpicc -o test test.c
[root@cluster Desktop]# mpirun -np 2 test
Master: sending ABCDEF to 1
Slave: received ABCDEF from 0
In the source code above, I replace
Code:
char* text = "ABCDEF";
by
Code:
char* text = argv[1];
then compile and execute it again with the following commands:
Code:
[root@cluster Desktop]# mpicc -o test test.c
[root@cluster Desktop]# mpirun -np 2 test ABCDEF
Then I get the following output:
Code:
Master: sending ABCDEF to 1
[cluster:03917] *** Process received signal ***
[cluster:03917] Signal: Segmentation fault (11)
[cluster:03917] Signal code: Address not mapped (1)
[cluster:03917] Failing at address: 0xbfa445a2
[cluster:03917] [ 0] [0x959440]
[cluster:03917] [ 1] /lib/libc.so.6(_IO_fprintf+0x22) [0x76be02]
[cluster:03917] [ 2] test(main+0x143) [0x80488b7]
[cluster:03917] [ 3] /lib/libc.so.6(__libc_start_main+0xdc) [0x73be8c]
[cluster:03917] [ 4] test [0x80486c1]
[cluster:03917] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 3917 on node cluster.hpc.org exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------
I’m very confused because the only difference between the two source codes is the difference between
Code:
char* text = "ABCDEF";
and
Code:
char* text = argv[1];
Can any one help me why the results are so different? How can I send argv[i] to another process?
Thank you very much!
 
Old 04-21-2010, 08:53 AM   #2
nowonmai
Member
 
Registered: Jun 2003
Posts: 481

Rep: Reputation: 48
If you go back over the compiler output, you'll probably have a better idea...
Also, brush up on indirection & string copying.
 
Old 04-21-2010, 09:00 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by awiles View Post
Hi all,
I write a simple MPI program to send a text message to another process. The code is below.
(test.c)
Code:
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[]) {
    int dest, noProcesses, processId;
    MPI_Status status;
    
    char* buffer;
    
    char* text = "ABCDEF";
    
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &noProcesses);
    MPI_Comm_rank(MPI_COMM_WORLD, &processId);
    
    buffer = (char*) malloc(256 * sizeof(char));
    
    if (processId == 0) {
      fprintf(stdout, "Master: sending %s to %d\n", text, 1);
      MPI_Send((void *)&text, strlen(text) + 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
    } else {
      MPI_Recv(&buffer, 128, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
      fprintf(stdout, "Slave: received %s from %d\n", buffer, status.MPI_SOURCE);
    }
    MPI_Finalize();
    return 0;
}
After compiling and executing it I get the following output:
Code:
[root@cluster Desktop]# mpicc -o test test.c
[root@cluster Desktop]# mpirun -np 2 test
Master: sending ABCDEF to 1
Slave: received ABCDEF from 0
In the source code above, I replace
Code:
char* text = "ABCDEF";
by
Code:
char* text = argv[1];
then compile and execute it again with the following commands:
Code:
[root@cluster Desktop]# mpicc -o test test.c
[root@cluster Desktop]# mpirun -np 2 test ABCDEF
Then I get the following output:
Code:
Master: sending ABCDEF to 1
[cluster:03917] *** Process received signal ***
[cluster:03917] Signal: Segmentation fault (11)
[cluster:03917] Signal code: Address not mapped (1)
[cluster:03917] Failing at address: 0xbfa445a2
[cluster:03917] [ 0] [0x959440]
[cluster:03917] [ 1] /lib/libc.so.6(_IO_fprintf+0x22) [0x76be02]
[cluster:03917] [ 2] test(main+0x143) [0x80488b7]
[cluster:03917] [ 3] /lib/libc.so.6(__libc_start_main+0xdc) [0x73be8c]
[cluster:03917] [ 4] test [0x80486c1]
[cluster:03917] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 3917 on node cluster.hpc.org exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------
I’m very confused because the only difference between the two source codes is the difference between
Code:
char* text = "ABCDEF";
and
Code:
char* text = argv[1];
Can any one help me why the results are so different? How can I send argv[i] to another process?
Thank you very much!

Do you really need the item in red ? I.e. do you really need address of pointer rather than the pointer itself ?

And regardless of your problem you must check what 'malloc' returns.

Anyway, put diagnostic

Code:
fprintf(stderr, ...);
messages in order to understand what is the last before segfault executed statement.
 
Old 04-21-2010, 09:50 AM   #4
awiles
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
Do you really need the item in red ? I.e. do you really need address of pointer rather than the pointer itself ?

And regardless of your problem you must check what 'malloc' returns.

Anyway, put diagnostic

Code:
fprintf(stderr, ...);
messages in order to understand what is the last before segfault executed statement.
Thanks for your reply. After removing the two ampersands, the application runs without any error.
Thanks again

Last edited by awiles; 04-21-2010 at 10:00 AM.
 
  


Reply



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
C cidr argv quantt Programming 11 01-26-2009 07:03 AM
Question about argv[] ghoughto Programming 6 10-25-2004 09:38 AM
argc argv linuxanswer Programming 8 10-25-2003 07:54 PM
Help with argv pilot1 Programming 7 08-23-2003 02:20 PM
Sending Error: 451 Error htimst Linux - General 1 09-03-2002 03:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:36 PM.

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