1

I have two processes running. One is the mainserver and the other is proxy server. They both communicate through a shared memory. Proxy server writes name of the file and mainserver reads it. Then main server writes the content of that file to be read by proxy server. Following are the codes: Main server:

   //receive data from client  
       memset(&buffer,'\0',sizeof(buffer));  
       bytes = read(c_fd, buffer, sizeof(buffer)); 
       if(!(bytes<0 && bytes==0 )){ 


  //SHARED MEMORY STUFF :) //

  for(int i=0; i<strlen(b);i++)
        printf("%c", b[i]);
  char c;
  int shmid;
  key_t key;
  char *shm;
  char *s= (char *)malloc (strlen(buffer)+1);
  //naming shared memory segment 5678
  key=5678;
  //Locate the segment
  if((shmid =shmget (key,SHMSZ,0666))<0){
       perror("shmget");
       exit(1);
 }
  //attaching the segment
 if((shm =shmat (shmid,NULL,0))==(char *) -1){
       perror("shmat");
       exit(1);

 }
 int count=0;
 s =shm;
 printf("File name received is :\n");

 for(s=shm ; *s!=NULL; s++){

       putchar (*s);
       if((*s >='a'    &&  *s<='z'  )  || *s=='.' ){

           b[count]=*s;
           count++;}
      // else printf("special character");




 }

// *shm = '*';
 //*s=NULL;
 //shmdt ((void *)shmid);

 //reading html file

  printf("coiunt%d",count);
  char *ss="page2.html"; 
  printf("%s",b);    
  char shared_buffer[69999]; 
  int handle = open(b, O_RDONLY);
     if (handle!=-1)    //FILE FOUND// change
        {
            printf("inside %s",b);

            send(c_fd, "File found\n\n", 17, 0);
                                //reading html file
                                if(read(handle, shared_buffer, 1024)!=-1)
                                         printf("READ");
                                //adding data to segment
                                //printf("sharedddd %c",shared_buffer[0]);
                        s= shm;

                                for(int i=0; i<strlen(shared_buffer); i++){
                                *s++= shared_buffer[i];
                                printf(" %s",s);
                               // printf("sharedddd %c",shared_buffer[i]);
                                }
                                *s= NULL;


            }
        else    {//write(c_fd, " Not Found\n", 23); //FILE NOT FOUND
                                }





  }



         fflush(stdout);  


 }

Proxy Server:

           //receive data from client  
           memset(&buffer,'\0',sizeof(buffer));  
           bytes = read(c_fd, buffer, sizeof(buffer)); 
           if(!(bytes<0 && bytes==0 )){ 


          //SHARED MEMORY STUFF :) //

          for(int i=0; i<strlen(b);i++)
                printf("%c", b[i]);
          char c;
          int shmid;
          key_t key;
          char *shm;
          char *s= (char *)malloc (strlen(buffer)+1);
          //naming shared memory segment 5678
          key=5678;
          //Locate the segment
          if((shmid =shmget (key,SHMSZ,0666))<0){
               perror("shmget");
               exit(1);
         }
          //attaching the segment
         if((shm =shmat (shmid,NULL,0))==(char *) -1){
               perror("shmat");
               exit(1);

         }
         int count=0;
         s =shm;
         printf("File name received is :\n");

         for(s=shm ; *s!=NULL; s++){

               putchar (*s);
               if((*s >='a'    &&  *s<='z'  )  || *s=='.' ){

                   b[count]=*s;
                   count++;}
              // else printf("special character");




         }

        // *shm = '*';
         //*s=NULL;
         //shmdt ((void *)shmid);

         //reading html file

          printf("coiunt%d",count);
          char *ss="page2.html"; 
          printf("%s",b);    
          char shared_buffer[69999]; 
          int handle = open(b, O_RDONLY);
             if (handle!=-1)    //FILE FOUND// change
                {
                    printf("inside %s",b);

                    send(c_fd, "File found\n\n", 17, 0);
                                        //reading html file
                                        if(read(handle, shared_buffer, 1024)!=-1)
                                                 printf("READ");
                                        //adding data to segment
                                        //printf("sharedddd %c",shared_buffer[0]);
                                s= shm;

                                        for(int i=0; i<strlen(shared_buffer); i++){
                                        *s++= shared_buffer[i];
                                        printf(" %s",s);
                                       // printf("sharedddd %c",shared_buffer[i]);
                                        }
                                        *s= NULL;


                    }
                else    {//write(c_fd, " Not Found\n", 23); //FILE NOT FOUND
                                        }





          } 
             fflush(stdout);  

 }  

I have only added the relevant code where memory is being created and shared. Please note that both of these codes are in separate files. I need to implement mutex and a condition variable such that when main server can only read once the proxy server has written to memory and so on. I have searched codes on the internet but they all seem to be working for a single file. How can they be shared among two files? Kindly inform about some tutorial or anything to get me started.

cranberry
  • 55
  • 7
  • 1
    Possible duplicate. See https://stackoverflow.com/questions/32153151/how-to-create-semaphores-in-shared-memory-in-c – Devstr Feb 17 '18 at 10:18
  • 2
    Possible duplicate of [How do I synchronize access to shared memory in LynxOS/POSIX?](https://stackoverflow.com/questions/2584678/how-do-i-synchronize-access-to-shared-memory-in-lynxos-posix) – n. m. could be an AI Feb 17 '18 at 10:21
  • @n.m. Is there a link for SysV api? – cranberry Feb 17 '18 at 10:49
  • 1
    You could place POSIX threads' conditions and mutexes inside the shared memory and use it by both processes. See [this answer for details](https://stackoverflow.com/a/20326345/694576). – alk Feb 17 '18 at 12:48

0 Answers0