c++试图添加Peterson算法以避免共享内存中的竞争条件

标签 c++ linux algorithm struct

我写了两个程序(程序 1 和程序 2)使用共享内存相互通信。程序 1 从文件中读取一个句子,并在修改后将其传递给下一个程序(程序 2),以获取每个单词的首字母及其大小。我遇到了竞争条件问题。我添加了 Peterson 算法,但是一旦我执行了 2 个程序,一个在前台,一个在后台,我没有得到任何结果。

-一旦我删除了 Peterson 算法,我的程序就可以运行了

-我正在使用 C++ 在 Linux 中工作

程序 1

#include<iostream>
#include<fstream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

using namespace std;

int filesize(){

ifstream input;
input.open("file1.txt");

string temp;

int i = 0;

while(input>>temp){i++;}

input.close();  

return i;
}

struct shdata
{   
char c;      
    int n;
    int size;
    bool flag[2];
    int turn;
};

int main(){
 ifstream input;
     input.open("file1.txt");   

int shmid;
key_t key = 8006;

struct shdata *shm;



shmid = shmget(key, sizeof(struct shdata), IPC_CREAT | 0666);

 if(shmid < 0){
     cout<<"Error .. Can not get memory\n";
     exit(0);
 }

     shm = (struct shdata *)shmat (shmid, NULL, 0);

     if(shm <= (struct shdata *)(0))
     {
      cout<<"Errors.. Can not attach\n";
      exit(1);
     }
     shm->flag[0]=false;
     shm->flag[1]=true; 
     string temp;


     while(input>>temp){
     shm->flag[0]=true;
     shm->turn = 1;          
     while(shm->flag[1]== true && shm-> turn == 1 );

     shm->c=temp[0];
     shm->n=temp.size();
     shm->size = filesize();


     shm->flag[0]=false;  
     sleep(1);
     }


return 0;
}

计划 2

    #include<iostream>
    #include<fstream>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <unistd.h>

    using namespace std;

    int filesize(){

ifstream input;
input.open("file1.txt");

string temp;

int i = 0;

while(input>>temp){i++;}

input.close();  

return i;
    }

    struct shdata
    {   
char c;      
    int n;
    int size;
    bool flag[2];
    int turn;
    };

    int main(){

int shmid;
key_t key = 8006;

struct shdata *shm;



    shmid = shmget(key, sizeof(struct shdata), 0);
if(shmid < 0)
   {
  cout<<"Error .. Can not get memory\n";
  exit(0);
   }

   shm = (struct shdata *)shmat (shmid,0, 0);

   if(shm <= (struct shdata *)(0))
   {
   cout<<"Error .. Can not attach\n";
   exit(1);
   }
   int c =0;
   while(c<shm->size){

shm->flag[1] = true;
    shm->turn=0; 

 while( shm->flag[0]==false && shm->turn == 0);

sleep(1);     
 for(int i = 0; i < shm->n ;i++)
 {
    cout<<shm->c;
 }
 cout<<endl;

 shm->flag[1]=false;

 c++;
 }
shmctl(shmid, IPC_RMID, NULL);

return 0;
}        

最佳答案

程序 2 永远不会进入 while(c<shm->size)循环因为在那一点 shm->size是 0。为了绕过它,程序 1 应该初始化 shm->size在程序 2 达到该点之前。这可能会导致另一个竞争条件,因为似乎没有任何机制可以确保共享内存在程序 2 开始使用之前由程序 1 初始化。

它似乎在没有 Peterson 算法的情况下工作,因为在那种情况下程序 1 不会等待标志并初始化 shm->size进一步深入循环。

关于c++试图添加Peterson算法以避免共享内存中的竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30327886/

相关文章:

c++ - OpenCV图像变换和透视变化

c++ - 错误 : default argument given for parameter 2

c++ - 在 txt c++ 中读取行

c - 套接字程序无法 POST - C (Linux)

python - Pygame 中的动画故障

c++ - C++ 中 Main 参数的 Const-Qualification

linux - 在 Linux 终端中运行 ffmpeg

使用 OpenSSL ssl_conn 错误进行编译

python - Python 中点画派绘画的随机化算法

algorithm - 流体模拟如何集成到刚体 phisix 引擎中?