C++ Eisenberg-McGuire算法在C语言中的实现
我试图理解Eisenberg-McGuire算法,我发现了这个实现它的程序,但是当我运行这个程序时,我发现了一个分割错误C++ Eisenberg-McGuire算法在C语言中的实现,c++,segmentation-fault,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 "C
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;
}
Eisenberg-McGuire算法:N进程的软件方法
相互排斥。
有关Eisenberg-McGuire算法的说明,请参见第261页,共页
并发系统-操作系统、数据库和分布式系统
系统:一种不整合的方法/Jean Bacon——第二版”。
版权(c)2001小张*/
#包括
#包括
#包括
使用名称空间std;
/**********************************************************************/
/*N进程互斥的Eisenberg-McGuire算法*/
/**********************************************************************/
类eis_mcg_mutex_t{
私人:
int n;
枚举procphase{out_cr,want_cr,claim_cr}*procphase;
内翻;
公众:
/*初始化由N个进程共享的互斥数据*/
eis_mcg_mutex_t(int nproc)
{
n=nproc;
procphase=新枚举procphase[n];
srand(时间(0));
回合=(整数)(1.0*n*rand()/(rand_MAX+1.0));
对于(int i=0;ifor (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)i);
(i=0;ifor (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)&i);
(i=0;ifor (i = 0; i < N; i++) pthread_create(&tid[i], NULL, doit, (void *)i);
(i=0;iintdoit(void*arg)
中,更改了inttid=*((int*)(&arg));
它现在工作得很好。或者可能更改强制转换
doit()
,而不是传递指向主线程中正在快速修改的变量的指针……请注意,传递地址并不能保证线程看到不同的值;这取决于线程的调度。您最好使用原始的(void*)调用中的i
,以及int tid=(int)arg;
中的doit()
。每个线程都以这种方式保证其自身的值。但如果我执行该错误,则会出现以下错误:从指针转换到较小类型“int”会丢失信息int tid=(int)我修正了这个错误,程序现在运行得很好。请看更新。谢谢。莱弗勒,德米特里。<代码>类< /C> >不是C中的关键字;你必须写C++。