delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/11/20/11:22:38

X-Spam-Check-By: sourceware.org
Message-ID: <e2e108260511200822n7cf22c31n695726858db7a841@mail.gmail.com>
Date: Sun, 20 Nov 2005 17:22:28 +0100
From: Bart Van Assche <bart DOT vanassche AT gmail DOT com>
To: cygwin AT cygwin DOT com
Subject: 1.5.18: segmentation fault on combination of pthread_kill() and sigsuspend()
In-Reply-To: <e2e108260511200812h46024b30v24ad287515d4eb87@mail.gmail.com>
MIME-Version: 1.0
References: <e2e108260511200812h46024b30v24ad287515d4eb87 AT mail DOT gmail DOT com>
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

------=_Part_9306_27990119.1132503748587
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hello,

    I wrote a small program in order to get an idea of context switch
times of the Linux kernel. The program runs fine on Linux. Valgrind
2.2.0 does report the program to be clean. Just out of curiosity, I
tried to compile and run the same program with Cygwin. However, on
cygwin it segfaults. Did I do anything wrong, or is this a cygwin bug
?

 $ ./ctxtsw.exe
 Iterations: 5
 Segmentation fault (core dumped)

 $ cat ctxtsw.exe.stackdump
 Exception: STATUS_ACCESS_VIOLATION at eip=3D00000001
 eax=3D00000000 ebx=3D00446048 ecx=3D00000004 edx=3D00000000 esi=3D00000001=
 edi=3D18ACEF78
 ebp=3D18ACEF88 esp=3D18ACEF70
program=3DC:\cygwin\home\B\test\ctxtsw\ctxtsw.exe, pid 2044, thread
unknown (0x884)
 cs=3D001B ds=3D0023 es=3D0023 fs=3D003B gs=3D0000 ss=3D0023
 Stack trace:
 Frame     Function  Args
 18ACEF88  00000001  (00000000, FFFFFFFF, 18ACEFC8, 61003B7D)
 18ACEFC8  6109D17E  (004D07B0, 18ACF000, 6109D110, 00000000)
 18ACEFF8  61003E94  (00000000, 00000000, 00000000, 00000000)
 18ACFF98  61003EDA  (00000000, 00000000, 00000000, 00000000)
 End of stack trace

 $


     Cygwin DLL version info:
         DLL version: 1.5.18
         DLL epoch: 19
         DLL bad signal mask: 19005
         DLL old termios: 5
         DLL malloc env: 28
         API major: 0
         API minor: 132
         Shared data: 4
         DLL identifier: cygwin1
         Mount registry: 2
         Cygnus registry name: Cygnus Solutions
         Cygwin registry name: Cygwin
         Program options name: Program Options
         Cygwin mount registry name: mounts v2
         Cygdrive flags: cygdrive flags
         Cygdrive prefix: cygdrive prefix
         Cygdrive default prefix:
         Build date: Sat Jul 2 20:30:04 EDT 2005
         Shared id: cygwin1S4

------=_Part_9306_27990119.1132503748587
Content-Type: text/plain; name=ctxtsw.cpp; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="ctxtsw.cpp"

#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/time.h>


class CCounter
{
public:
  CCounter()
    : m_iCount(0)
  { pthread_mutex_init(&m_Mutex, 0); pthread_cond_init(&m_Cond, 0); }
  ~CCounter()
  { pthread_cond_destroy(&m_Cond); pthread_mutex_destroy(&m_Mutex); }
  void WaitUntilAtLeast(int const i)
  { pthread_mutex_lock(&m_Mutex); 
    while (m_iCount < i)
      pthread_cond_wait(&m_Cond, &m_Mutex);
    pthread_mutex_unlock(&m_Mutex); }
  void operator++()
  { pthread_mutex_lock(&m_Mutex); m_iCount++;
    pthread_cond_signal(&m_Cond);
    pthread_mutex_unlock(&m_Mutex); }
private:
  pthread_mutex_t m_Mutex;
  pthread_cond_t  m_Cond;
  int             m_iCount;
};

static volatile int s_iIterations;
static volatile pthread_t s_Thread1ID;
static volatile pthread_t s_Thread2ID;
static volatile timeval s_iTime[1000];
static CCounter s_ThreadsStarted;


void* Thread1(void*)
{
  ++s_ThreadsStarted;
  s_ThreadsStarted.WaitUntilAtLeast(2);

  sigset_t EmptyMask;
  sigemptyset(&EmptyMask);
  for (int i = 0; i < s_iIterations; i++)
  {
    gettimeofday(const_cast<timeval*>(&s_iTime[2*i]), 0);
    pthread_kill(s_Thread2ID, SIGALRM);
    sigsuspend(&EmptyMask);
  }
  return 0;
}

void* Thread2(void*)
{
  ++s_ThreadsStarted;
  s_ThreadsStarted.WaitUntilAtLeast(2);

  sigset_t EmptyMask;
  sigemptyset(&EmptyMask);
  sigsuspend(&EmptyMask);
  for (int i = 0; i < s_iIterations; i++)
  {
    gettimeofday(const_cast<timeval*>(&s_iTime[2*i+1]), 0);
    pthread_kill(s_Thread1ID, SIGALRM);
    sigsuspend(&EmptyMask);
  }
  return 0;
}

void SignalHandler(int const /*iSignal*/)
{
  // write(STDOUT_FILENO, "SIGALRM\n", 8);
}

int main(int argc, char** argv)
{
  sigset_t BlockedSignals;
  sigemptyset(&BlockedSignals);
  sigaddset(&BlockedSignals, SIGALRM);
  sigprocmask(SIG_BLOCK, &BlockedSignals, 0);
  struct sigaction Sigaction;
  memset(&Sigaction, 0, sizeof(Sigaction));
  Sigaction.sa_handler = SignalHandler;
  sigaction(SIGALRM, &Sigaction, 0);

  s_iIterations = (argc > 1) ? atoi(argv[1]) : 5;
  std::cout << "Iterations: " << s_iIterations << "\n";

  pthread_create(const_cast<pthread_t*>(&s_Thread1ID), 0, Thread1, 0);
  pthread_create(const_cast<pthread_t*>(&s_Thread2ID), 0, Thread2, 0);
  pthread_join(s_Thread1ID, 0);
  pthread_kill(s_Thread2ID, SIGALRM);
  pthread_join(s_Thread2ID, 0);
  for (int i = 1; i < 2 * s_iIterations; i++)
  {
    timeval Delta;
    timersub(&s_iTime[i], &s_iTime[i-1], &Delta);
    std::cout << i << ' ' << Delta.tv_usec * 1e-3 << " ms\n";
  }
  {
    timeval Timeval1;
    timeval Timeval2;
    timeval Delta;
    gettimeofday(&Timeval1, 0);
    gettimeofday(&Timeval2, 0);
    timersub(&Timeval2, &Timeval1, &Delta);
    std::cout << "Time required for one gettimeofday() call: "
	      << Delta.tv_usec * 1e-3 << " ms\n";
  }
  return 0;
}

// Local variables:
// compile-command: "g++ -Wall -W -g -O3 ctxtsw.cpp -o ctxtsw -lpthread"
// End:








------=_Part_9306_27990119.1132503748587
Content-Type: text/plain; charset=us-ascii

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/
------=_Part_9306_27990119.1132503748587--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019