7

Hello I have the following code, which I compile with gcc (>4.2) with -fopenmp flag:

int main(void)
{
#pragma omp parallel for
    int i; 
    for(i=0;i<4;i++) while(1);

    return 0;
}

I get a SIGSEGV on OSX Lion (ver 1.7.3, llvm-gcc 4.2.1) and CentOS 6.2 . What am I doing wrong here? Thanks

Steve Blackwell
  • 5,904
  • 32
  • 49
sfa
  • 101
  • 4
  • I get the same, Win7/cygwin, gcc 4.5.0. I ran it though gdb: it creates the threads and then I get `Program received signal SIGSEGV, Segmentation fault. 0x63602726 in omp_get_max_active_levels ()`. It runs fine without the `while(1)`. How is OpenMP treating that infinite loop? – Steve Blackwell Feb 06 '12 at 16:37
  • You're using an undeclared variable. But that should generate a compilation error, not a segfault. But with `i` declared I also get a segfault, gcc-4.5.1, openSuSE 11.4. – Daniel Fischer Feb 06 '12 at 16:41
  • I forgot to add.. int i.. I wrote the code in a hurry :D. – sfa Feb 06 '12 at 20:58

3 Answers3

2

Not sure if this is relevant to the compiler version and configuration but while(true){} terminates

More precisely, if you write a loop which

  • makes no calls to library I/O functions, and
  • does not access or modify volatile objects, and
  • performs no synchronization operations (1.10) or atomic operations (Clause 29)

and does not terminate, you have undefined behaviour.

This may end up not applying to your situation, but as C++11 becomes more established, watch out.

Community
  • 1
  • 1
spraff
  • 32,570
  • 22
  • 121
  • 229
  • Irrelevant, this is C, and in C, the loop may only be assumed to terminate if the controlling expression is **not a constant expression**. Since `while(1);` is controlled by a constant expression, the loop must not terminate. – Daniel Fischer Feb 06 '12 at 19:56
  • Interesting, I didn't realise C had a not-constant-expression quality like that. Can you point to the standard, please? – spraff Feb 07 '12 at 08:32
  • Hello spraff, I don't see any undefined behavior in C => while(1); (point to the C standard please). It should be an unconditional jump (jmp) jumping at its own address. If I create 4 threads (with pthread_create) and I put in the start function a "while(1);" I get no SIGSEGV, and my 4 cores are at 100%. I wanted to achieve the same behavior with openmp, but it seems it's not that reliable as I thought. – sfa Feb 07 '12 at 09:35
  • 2
    I'm going by n1570 since I don't have the actual standard, in 6.8.5 (6): "An iteration statement whose controlling expression is not a constant expression,that performs no input/output operations, ...". It's possible that the 'not a constant expression' qualifier has been removed again, but since it has been added later to the paragraph, I don't expect that. – Daniel Fischer Feb 07 '12 at 10:10
1

Very interesting.

I changed the code a little so

int main(void)
{
int i;
#pragma omp parallel 
  {
        while(1);
    }
    return 0;
} 

and so

inline void func() {
    while (1) ;
}

int main(void)
{
int i;
#pragma omp parallel for 
    for(i=0;i<8;i++) {
        func();
    }
    return 0;
}

And they both worked OK.

mikithskegg
  • 806
  • 6
  • 10
  • I know, I also tried the latter. (also works without the inline). But what do you think is wrong with the code I wrote,(except that is missing a... int i). On my CentOS gomp_loop_static_start() gets called, with parameters like => chunk_size=140737488347560, istart=0x7fffffffe1b0, iend=0xca. The last one is a pointer which gets dereferenced.. hence getting the SIGSEGV. If I explicitly use schedule() to set chunk size to a value, the iend pointer is a valid address.. and I get no SIG 11. Also if I debug your latter code(with the inline fnc) gomp_loop_static_start() doesn't get called at all. – sfa Feb 06 '12 at 21:33
  • if you compile with optimizations enabled then you also get segfault for `func()` case. – jfs Feb 06 '12 at 23:14
  • @J.F.Sebastian Not here, with `func()`, it properly does nothing at 390+% CPU, from -O0 to -O3 and -Os. Segfaults with an unadorned `while(1);` on all optimisation levels. gcc is 4.5.1. Very strange. – Daniel Fischer Feb 07 '12 at 00:18
  • gcc 4.6.1: `-O0` no segfault, `-O1+` -- segfault. – jfs Feb 07 '12 at 00:57
  • @sfa, I really don't know what happens. Think one should look at the generated assembler code. Latter i will try to use Intel C++ compiler, now i have no time. – mikithskegg Feb 07 '12 at 11:06
  • I tried compilation with Intel C++ Composer 12.1 and everything WORKS properly !!! – mikithskegg Feb 09 '12 at 21:19
  • Thanks mikithskegg, the problem was in gcc. – sfa Feb 22 '12 at 11:49
1

There was a bug in the gcc regarding this issue, I reported it and they will provide a fix. Here is the link: GCC bug

sfa
  • 101
  • 4