c ++ trying to add Peterson's algorithm to avoid competitive conditions in shared memory

advertisements

I have written two program (program 1 and program 2) to communicate with each other using shared memory. program 1 reads from a file a sentence and pass it after modification to get first letter of each word and its size to the next program ( program 2) . I faced race condition problem. I added Peterson algorithm but once I execute the 2 programs one in foreground and one in background I didn't get any result.

-once i remove the Peterson algorithm my programs work

-i'm working in linux using c++

program 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;
}

program 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;
}


program 2 never gets into the while(c<shm->size) loop because at that point shm->size is 0. To get around it, progran 1 should initialize shm->size before program 2 reaches that point. This might lead to another race condition because there doesn't seem to be any mechanism to ensure that the shared memory is initialized by program 1 before program 2 starts using it.

It seems to work without the Peterson algorithm because in that case program 1 doesn't wait on the flag and initializes shm->size further down in the loop.