简体   繁体   English

setjmp / longjmp 64位

[英]setjmp/longjmp in 64 bit

I have tried to use setjmp/longjmp in 64 bit Ubuntu, but it doesn't properly work, while it works alright in 32 bit Ubuntu. 我试图在64位Ubuntu中使用setjmp / longjmp ,但是它不能正常工作,而在32位Ubuntu中可以正常使用。 Any idea, what is happening. 任何想法,正在发生什么。 The following is the code, I was trying to execute. 以下是我尝试执行的代码。

On 64 bit, it hangs, at the point where it gets back after longjmp. 在64位上,它挂起,在longjmp之后返回的位置。 In wikipedia's article about setcontext , it says it doesn't work properly with 64 bit. 在Wikipedia的有关setcontext的文章中,它说64位不能正常工作。 Do we have this same problem with setjmp ? setjmp是否也有同样的问题? Infact, I was trying to use setjmp , to avoid the problem with setcontext , but it seems to have the same issue on 64 bit. 实际上,我尝试使用setjmp来避免setcontext的问题,但在64位上似乎也有相同的问题。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <string>
#include <setjmp.h>

#define NOFTHREADS 2

#define DATASIZE        500
#define SETSIZE         (DATASIZE / NOFTHREADS)  

int data[DATASIZE];
pthread_mutex_t sumMutex;
pthread_mutex_t prodMutex;
int sum;
double prod;
static jmp_buf buf;
int jmp_onced[NOFTHREADS+1];

#define lock pthread_mutex_lock
#define unlock pthread_mutex_unlock

void *SumThread( void *pParam )
{
  unsigned int tid = *((unsigned int*)(pParam));
  int si = tid * SETSIZE;
  int i, j, oi, local_sum;
  double local_prod;
  pthread_attr_t attr;

  if ( setjmp(buf) ) 
    printf( "%d: tid %u back! <<<<<<<<<\n", getpid(), tid );
  if ( jmp_onced[tid] )
    goto end_this;

  printf( "%d: >>>>>>>>>>>>> tid %u, starting <<<<<<<<<<<<\n\n", getpid(), tid );

  for( oi = 0; oi < 5; oi++ )
  {
    local_sum = 0;
    local_prod = 1.0;
    for( i = si; i < (si+SETSIZE); i++ )
    {
      local_sum = local_sum + data[i];
      if ( data[i ] )
      local_prod *= 0.005 * data[i];
    }

    lock( &sumMutex );
    sum += local_sum;
    unlock( &sumMutex );

    lock( &prodMutex );
    prod *= local_prod;
    unlock( &prodMutex );
  }

  printf( "%d: !!!!!!!!!!!!!!tid %u done!!!!!!!!!!!!!!\n\n", getpid(), tid );
  jmp_onced[tid] = 1;
  longjmp( buf, 1 );

end_this:
  printf( "%d: ****** tid %u is exiting! ******\n", getpid(), tid ); 
  return 0;
}

void real_main()
{
  pthread_t hThread[NOFTHREADS];
  int index[NOFTHREADS];
  int i, pid, err;
  time_t t1 = time(NULL);

  printf( "%d: Inside real_main, primary thread = %lx!\n", getpid(), pthread_self() );
  for( i = 0; i < NOFTHREADS; i++ )
  {
      index[i] = i;
      pthread_create( &hThread[i], NULL, SumThread, &index[i] ); 
  }
  for( i = 0; i < NOFTHREADS; i++ )
      pthread_join( hThread[i], NULL );

  printf( "Sum of numbers from 1 to %d is %d\n", DATASIZE, sum ); 
  printf( "Prod of numbers from 1 to %d is %g\n", DATASIZE, prod );
  printf( "\n\n[[[[[ %d(child of %d): Time taken = %lu seconds ]]]]]\n\n", getpid(), getppid(), time(NULL) - t1 );
}

int main(int argc, char **argv)
{
  int pid, i, err;

  printf( "size of long is %d\n", sizeof( long ) );

  sumMutex = PTHREAD_MUTEX_INITIALIZER;
  prodMutex = PTHREAD_MUTEX_INITIALIZER;
  printf( "pid = %d, @sumMutex = %lx, @prodMutex = %lx\n", getpid(), (long)&sumMutex, (long)&prodMutex );   

  for( i = 0; i < DATASIZE; i++ )
    data[i] = i+1;

  switch(pid = fork())
  {
  case -1:
    printf("fork failed");
    break;
  case 0: // Child
    printf( "Child pid() = %d\n", getpid() );
    real_main();
    break;
  default:// Leader
    printf( "Parent pid() = %d\n", getpid() );
    real_main();
  }

  printf( "getppid() = %d, getpid() = %d\n", getppid(), getpid() );
  return 0;
}

You need to use a separate jmp_buf for each thread. 您需要为每个线程使用单独的jmp_buf Currently you're re-using the same one. 目前,您正在重复使用同一张图片。

If you don't declare your local variables volatile , and you change them after calling setjmp() , then they are not guaranteed to be preserved when you call longjmp() . 如果您未将局部变量声明为volatile ,而是在调用setjmp()之后对其进行了更改,则不能保证在调用longjmp()时保留它们。

(7.13.2.1/3 in the C99 standard.) (在C99标准中为7.13.2.1/3。)

But that doesn't seem to apply here, since si and tid aren't changed. 但这似乎并不适用于此,因为sitid均未更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM