首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Eisenberg-McGuire算法分割故障: 11的C实现

Eisenberg-McGuire算法分割故障: 11的C实现
EN

Stack Overflow用户
提问于 2014-12-05 10:37:10
回答 1查看 467关注 0票数 1

我正在尝试理解Eisenberg-McGuire算法,我找到了这个实现它的程序,但是当我运行这个程序时,我得到了一个分割错误。

代码语言:javascript
复制
Segmentation fault: 11

下面是程序

代码语言:javascript
复制
/* 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;
}

我不明白是什么导致了分段错误。任何帮助都是非常感谢的。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-12-05 10:47:52

修好了。

代码语言:javascript
复制
for (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)i);

应该是

代码语言:javascript
复制
for (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)&i);

未找到地址的amperson操作符。

更新:

我现在没有传递地址。

代码语言:javascript
复制
for (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)i);

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

它现在可以完美地工作了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27307860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档