c++ - Eisenberg-McGuire算法段错误: 11 in C的实现

标签 c++ segmentation-fault

我试图理解 Eisenberg-McGuire 算法,我发现了这个实现它的程序,但是当我运行该程序时,我遇到了段错误。

Segmentation fault: 11

这是程序

/* Eisenberg-McGuire algorithm: a software approach to N-process
   mutual exclusion.

   For description of Eisenberg-McGuire algorithm, see page 261 of
   "Concurrent Systems - Operating Systems, Database and Distributed
   Systems: An Inegrated Approach / Jean Bacon -- 2nd Edition".

   Copyrigh (c) 2001 Xiao Zhang */

#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using namespace std;

/**********************************************************************/
/* Eisenberg-McGuire's algorithm for N-process mutual exclusion       */
/**********************************************************************/

class eis_mcg_mutex_t {

private:

    int n;
    enum procphase { out_cr, want_cr, claim_cr } *procphase;
    int turn;

public:

    /* Initialize the mutex data shared by N processes */

    eis_mcg_mutex_t(int nproc) 
    {
        n = nproc;
        procphase = new enum procphase [n];
        srand(time(0));
        turn = (int) (1.0 * n * rand() / (RAND_MAX + 1.0));
        for (int i = 0; i < n; i++) 
            procphase[i] = out_cr;
    }

  /* Entry protocol for process i */

    void mutex_lock(int i) {
        procphase[i] = want_cr;
        int j = turn;
        do 
        {
            while (j != i) 
            {
                if (procphase[j] == out_cr) 
                    j = (j + 1) % n;
                else 
                    j = turn;
            }
            procphase[i] = claim_cr;
            j = (j + 1) % n;

            while (procphase[j] != claim_cr) 
                j = (j + 1) % n;

        } while (!(j == i && (turn == i || procphase[turn] == out_cr)));
        turn = i;
    }

  /* Exit protocol for process i */

    void mutex_unlock(int i) 
    {
        int j = (turn + 1) % n;
        while (procphase[j] == out_cr) 
            j = (j + 1) % n;
        turn = j;
        procphase[i] = out_cr;
    }

};

/**********************************************************************/
/* To test the Eisenberg-McGuire's algorithm, we write a simple       */
/* program that creates N threads (processes) and then has each       */
/* thread increment a global variable `counter' NLOOP times. The      */
/* final value of `counter' is expected to be N * NLOOP.              */
/**********************************************************************/

#define N 4           /* number of threads */
#define NLOOP 1000    /* number of times each thread loops */

int counter;      /* this is cremented by the threads */
eis_mcg_mutex_t counter_in_use(N);

void *doit(void *arg)
{
  int i, val;
  int tid = *(int *)arg;

  /* Each thread fetches, prints and increments the counter NLOOP times.
     The value of the counter should increase monotonically. */

  for (i = 0; i < NLOOP; i++) {

    /* Replace pthread_mutex_lock() with Eisenberg-McGuire's
       enter-critical-section procedure. */
    counter_in_use.mutex_lock(tid);

    /* Here is critical section */
    val = counter;
    counter = val + 1;
    cout << tid << ": " << counter << endl;

    /* Replace pthread_mutex_unlock() with Eisenberg-McGuire's
       leave-critical-section procedure. */
    counter_in_use.mutex_unlock(tid);

  }

  return NULL;
}

int main()
{
  pthread_t tid[N];
  int i;

  for (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)i);
  for (i = 0; i < N; i++) pthread_join(tid[i], NULL);

  return 0;
}

我不明白是什么导致了段错误。任何帮助表示赞赏。谢谢。

最佳答案

已修复。

for (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)i);

应该是

for (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)&i);

错过了 amperson 接线员的地址。

更新:

我现在没有传地址。

for (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)i);

并且在 int doit(void *arg) 中,更改了 int tid = *((int*)(&arg));

它现在完美运行。

关于c++ - Eisenberg-McGuire算法段错误: 11 in C的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27307860/

相关文章:

std::vector<std::array<char, 2>> 的 C++ 数据连续性

c++ - C/C++ 中的基本多线程——提示、建议、教程、一些方向?

c++ - 2 位输入的表达式树

java - JNI,从 C++ 调用 Java,CallObjectMethod 上的 SIGSEGV

c - 分段故障

c++ - 在没有运行时库的情况下在 Linux 下编译 C++

c++ - 具有虚拟成员 : linker error 的模板类

c++ - 调用 sf::Window::close 后 SFML 中的段错误

c - 段错误总是在 3 个输入后发生

ruby-on-rails - OS X 10.8 上 Rails 应用程序中的段错误