I am write a simple MPI program, I am confused at the output when we call MPI_Abort.
The code is
// main.cpp
#include "mpi.h"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
MPI_Init(&argc, &argv);
int x = 1;
if (x == 1)
{
MPI_Abort(MPI_COMM_WORLD, -100);
}
MPI_Finalize();
return 0;
}
I am using the follow command to compile the program.
mpic++ main.cpp
The operating system is Ubuntu 18.04
The compiler is MPICH, type mpic++ -v in terminal can obtain detailed information of compiler:
mpicxx for MPICH version 3.3a2
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
If I run the executable file by the follow command
./a.out
The output is
application called MPI_Abort(MPI_COMM_WORLD, -100) - process 0
[unset]: write_line error; fd=-1 buf=:cmd=abort exitcode=-100
:
system msg for write_line failure : Bad file descriptor
How to understand the write line error? write line failure? Indeed what happens?
If I run the executable file by the follow command
mpirun -np 2 ./a.out
The output is
application called MPI_Abort(MPI_COMM_WORLD, -100) - process 0
[cli_0]: readline failed
How to understand the read line failed? Indeed what happens?
Thanks for your time.
Related
Initializing the file name via pointer let's crash the gcc on Linux but not on MacOS, can someone explain this?
#include <stdio.h>
int doOpenFile( char *name, FILE **filehdl, char *option );
int doOpenFile( char *name, FILE **filehdl, char *option )
{
if( *filehdl ) fclose( *filehdl );
if( (*filehdl = fopen( name, option )) == NULL ) return( -1 );
return( 0 );
}
int main(int argc, char **argv)
{
char *file = "test.c";
FILE *filePtr;
doOpenFile(file, &filePtr, "r");
if(filePtr != NULL) fclose(filePtr);
printf("%s\n", file);
}
Linux gcc:
oot#1fa88ab5df9e:/build/open62541-server# gcc -o test test.c
root#1fa88ab5df9e:/build/open62541-server# ./test
Segmentation fault (core dumped)
root#1fa88ab5df9e:/build/open62541-server# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 10.2.1-6' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-gcn/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-mutex
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.1 20210110 (Debian 10.2.1-6)
root#1fa88ab5df9e:/build/open62541-server#
MacOS:
open62541-server % gcc test.c -o test
open62541-server % ./test
test.c
open62541-server % gcc -v
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
open62541-server %
Weird because even if I don't pass the variable, the gcc on Linux crash
doOpenFile("test.c", &filePtr, "r");
if( *filehdl ) invokes undefined behavior since you are accessing a non-initialized FILE pointer. Either document to the caller that the function assumes the parameter to either be a null pointer or a valid file pointer, or drop the if statement.
Unrelated to your problem, you should also use const char* everywhere in this code, for const correctness and because string literals are read-only anyway.
Like the others said. When you declare a variable in C it does contain a (seemingly) random value that happened to be in RAM at runtime. That value can be NULL (or nil if you print it on the console, those two words mean the same). I wrote a small program to show this:
#include <stdio.h>
int main() {
int* pointer[20];
for(int i = 0; i < 20; ++i) {
printf("%p\n", &pointer[i]);
}
}
Output:
(nil)
(nil)
(nil)
(nil)
(nil)
(nil)
(nil)
(nil)
(nil)
0x1007
0x5631f0cff040
0xd
0x7ffe17efbbf0
0x7ffe17efc019
0x7f512adab5e0
0x5631f0d0023d
0x7f512ad862e8
0x5631f0d001f0
(nil)
0x5631f0d00080
So you see that there is a chance that the value is NULL, and that's probably what happened in you MacOS try. But feel free to run my small program here to check if that is always the case on your Mac.
When you pass a pointer that is not NULL to fclose( *filehdl ); you get a segmentation fault because it points to an address that does not belong to your program.
The easiest way to fix your problem ist to just initialize:
FILE *filePtr = NULL;
I download libxml2 2.9.4 in here, when i ./configure; make, i get following error:
nanohttp.c: In function ‘xmlNanoHTTPConnectHost’:
nanohttp.c:1073:18: error: storage size of ‘hints’ isn’t known
struct addrinfo hints, *res, *result;
^
nanohttp.c:1079:11: warning: implicit declaration of function ‘getaddrinfo’ [-Wimplicit-function-declaration]
status = getaddrinfo (host, NULL, &hints, &result);
^
nanohttp.c:1079:2: warning: nested extern declaration of ‘getaddrinfo’ [-Wnested-externs]
status = getaddrinfo (host, NULL, &hints, &result);
^
nanohttp.c:1085:35: error: dereferencing pointer to incomplete type ‘struct addrinfo’
for (res = result; res; res = res->ai_next) {
^
nanohttp.c:1089:7: warning: implicit declaration of function ‘freeaddrinfo’ [-Wimplicit-function-declaration]
freeaddrinfo (result);
^
nanohttp.c:1089:7: warning: nested extern declaration of ‘freeaddrinfo’ [-Wnested-externs]
nanohttp.c:1073:18: warning: unused variable ‘hints’ [-Wunused-variable]
struct addrinfo hints, *res, *result;
^
the full output is here
here is my environment:
roroco#roroco ~/Downloads $ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 17.3 Rosa
Release: 17.3
Codename: rosa
roroco#roroco ~/Downloads $ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.5-2ubuntu1~14.04.1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.5 (Ubuntu 4.8.5-2ubuntu1~14.04.1)
I try version 2.9.2 and 2.9.3, i get same error, how to compile libxml2 success?
I find the solution:
in nanohttp.c, the error line:
#if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
{
int status;
struct addrinfo hints, *res, *result;
so i can disable ipv6 to don't compile this block code. I try following code will work:
./configure --enable-ipv6=no LIBS="-lpthread"; make
I find "--enable-ipv6" in "libxml2-src/configure"
For using "getaddrinfo" you must use POSIX.
For clean C, use gcc flag -std=gnu11 or -std=gnu99 instead с11/с99.
Checking for POSIX you can using this code:
#ifdef __USE_POSIX
#warning POSIX compatible
#else
#warning no support POSIX
#endif
The following code produces "Quit (core dumped)" when I run it and type control \. I don't even get to hit return. I got this code from a tutorial book.
I tried debugging with gcc but it does this: (At the blank line it's running getline and I'm typing control \.)
14 bytes_read = getline (&my_string, &nbytes, stdin); (gdb)
Program received signal SIGQUIT, Quit. 0x004011e5 in main () at
myfile.c:14 14 bytes_read = getline (&my_string, &nbytes,
stdin); (gdb)
...when I single step it.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int bytes_read;
int nbytes = 100;
char *my_string;
puts ("Please enter a line of text.");
/* These 2 lines are the heart of the program. */
my_string = (char *) malloc (nbytes + 1);
bytes_read = getline (&my_string, &nbytes, stdin);
if (bytes_read == -1)
{
puts ("ERROR!");
}
else
{
puts ("You typed:");
puts (my_string);
}
return 0;
}
I used command-line "gcc -g myfile.c".
I am on Cygwwin bash
$ uname --all CYGWIN_NT-5.1 LENOVO-3AA1E185 1.7.29(0.272/5/3)
2014-04-07 13:44 i686 Cygwin
$ gcc -v Using built-in specs. COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-cygwin/4.8.2/lto-wrapper.exe
Target: i686-pc-cygwin Configured with:
/cygdrive/i/szsz/tmpp/gcc4/gcc-4.8.2-2/src/gcc-4.8.2/configure
--srcdir=/cygdrive/i/szsz/tmpp/gcc4/gcc-4.8.2-2/src/gcc-4.8.2 --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/libexec --datadir=/usr/share --localstatedir=/var --sysconfdir=/etc --libdir=/usr/lib --datarootdir=/usr/share --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=i686-pc-cygwin --host=i686-pc-cygwin --target=i686-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --disable-__cxa_atexit --with-dwarf2 --with-arch=i686 --with-tune=generic --disable-sjlj-exceptions --enable-languages=ada,c,c++,fortran,java,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --disable-libitm --enable-libquadmath --enable-libquadmath-support --enable-libssp --enable-libada --enable-libjava --enable-libgcj-sublibs --disable-java-awt --disable-symvers --with-ecj-jar=/usr/share/java/ecj.jar --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --libexecdir=/usr/lib Thread model: posix gcc version 4.8.2 (GCC)
underneath cygwin is Microsoft Windows XP Professional 32-bit SP3
Control-C is a Quit Signal trigger on most systems :
The SIGQUIT signal is similar to SIGINT, except that it's controlled
by a different key—the QUIT character, usually C-\—and produces a core
dump when it terminates the process, just like a program error signal.
You can think of this as a program error condition “detected” by the
user.
quoted from GNU. So this behavior is expected: You program did receive the SIGQUIT signal
I am new to C and have a question. I think I made a mistake, but the compiler is not giving a warning and the program runs fine.
I believe there is an error in line 4:
#include <stdio.h>
void void_int(int x);
void void_int(x) { // <-- no type definition for parameter x
printf( "void_int: %d\n", x);
}
int main(int argc, char **argv) {
printf("Hello world\n");
// declare a function pointer
void (*by_ref_void_int)(int);
// assign pointer to a function
by_ref_void_int = &void_int;
// run referenced function
by_ref_void_int(2);
return 0;
}
I do not define a type for the first argument to the function definition void void_int(x) {, however, gcc is not giving me any errors. The program runs just fine.
I compile with: gcc -Wall -o fp fp.c.
Would anybody help me trying to understand? Is there a default type for function arguments and is it possibly int?
Thanks and best wishes
-S
$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc-4.7.real
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5)
to keep the compatibility with arcaic versions of the C language, if a declaration specifies no type, the default type is assumed to be int.
If you simply specify a version of the language, the compiler will surely help you spotting this
gcc -std=c99 a.c
a.c: In function ‘void_int’:
a.c:4:6: warning: type of ‘x’ defaults to ‘int’ [enabled by default]
void void_int(x) { // <-- no type definition for parameter x
^
try to specify a version of the language, it's always a good habit that will help you.
When I build the following code, it builds fine. If I change the code to comment out the "while", using the same command line, it does not build (see below)
#include <stdio.h>
#include <mqueue.h>
int main(int argc, char *argv[]) {
while (1) { }
mq_open("/YouSUCK", O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, NULL);
return 0;
}
dada#thud:~/RaspberryPI$ gcc -g -Wall -lrt -o mqtest mqtest.c
dada#thud:~/RaspberryPI$
#include <stdio.h>
#include <mqueue.h>
int main(int argc, char *argv[]) {
// while (1) { }
mq_open("/YouSUCK", O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, NULL);
return 0;
}
dada#thud:~/RaspberryPI$ gcc -g -Wall -lrt -o mqtest mqtest.c
/tmp/cccw376u.o: In function `main':
/home/dada/RaspberryPI/mqtest.c:7: undefined reference to `mq_open'
collect2: ld returned 1 exit status
dada#thud:~/RaspberryPI$
Any ideas ?
dada#thud:~/RaspberryPI$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
Update:
Looks like a compiler version issue, I built the same code on a different box, and it builds properly with either of the above files. So I guess I need a different compiler.
dada#JoesPi ~ $ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.6/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.3-14+rpi1' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 4.6.3 (Debian 4.6.3-14+rpi1)
'undefined reference to mq_open' collect2:
Link agaist librt. E.G. -lrt
You can compile it using $gcc -g -Wall -o mqtest mqtest.c -lrt to make it work without compilation errors as undefined reference for posix message queue apia.
(as mentioned in the comments )
Acutally if you use -lrt before c file there is error
root#embsys-VirtualBox:~# gcc -lrt test.c
/tmp/ccR93VIp.o: In function tfunc':
test.c:(.text+0x31): undefined reference tomq_getattr'
test.c:(.text+0x8f): undefined reference to mq_receive'
/tmp/ccR93VIp.o: In functionmain':
test.c:(.text+0x145): undefined reference to mq_open'
test.c:(.text+0x194): undefined reference tomq_notify'
collect2: error: ld returned 1 exit status
This works
root#embsys-VirtualBox:~# gcc test.c -lrt