Thread: column duplicated in IPC shared memory

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    8

    Angry column duplicated in IPC shared memory

    Hello, i have tried everything i can imagine but i cant figure out whats going wrong here.

    in the player struct , there is a 6x6 matrix, which i fill in the create_player function but when i print it i got 7 columns? then i try to place a '0' in the matrix with the place_ship function, but when the column is 5 it add 2 ceros?

    if i reduce the j iterations then i got duplicated in the column 4? or 3 or 2...

    is this really a duplicated column? or am i doing something wrong in the code? i searched and tried different things the hole day but cant get it

    hope anyone can help me,
    Thanks in advice!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <pthread.h>
    #include <signal.h>     
    
    
    #define SHMEM_SIZE 1024
    #define SHM_KEY 1210
    #define SHM_V_KEY 1012
    
    
    typedef struct{
          char board[6][6]; 
          int thread_ret;
          pthread_t thread;
    }player;
    
    
    player *shm;
    int *shm_v;
    
    
    void *shoot(void *target){
          printf("under construction\n");
    }
    
    
    //shm[shm_v[1]]->thread_ret = pthread_create(&shm[shm_v[1]]->thread, NULL, shoot, );
    
    
    void fill_matrix(){
          for (size_t i = 0; i < 6; i++) {
                for (size_t j = 0; j < 6; j++) {
                      shm[shm_v[1]].board[i][j] = '*';
                }
          }
    }
    
    
    void place_ship(){
          int x, y;      srand(time(NULL));
          x = rand() & 5;
          y = rand() & 5;
          printf("x = %d\ny = %d\n", x,y);
          shm[shm_v[1]]->board[3][5] = '0';
    }
    
    
    void print_player(player *p){
          for (size_t i = 0; i < 6; i++) {
                for (size_t j = 0; j < 6; j++) {
                      printf("|%c", shm[shm_v[1]].board[i][j]);
                      if (j == 5) {
                            printf("|%c|\n", shm[shm_v[1]].board[i][j]);
                      }
                }
          }
    }
    
    
    void new_player(){
          for (size_t i = 0; i < 6; i++) {
                for (size_t j = 0; j < 6; j++) {
                      shm[shm_v[1]].board[i][j] = '*';
                }
          }
    }
    
    
    int main(int argc, char *argv[]) {
          int id_shm, id_shm_v;
    
    
          id_shm  = shmget(SHM_KEY, SHMEM_SIZE, 0777 | IPC_CREAT);
          id_shm_v  = shmget(SHM_V_KEY, SHMEM_SIZE, 0777 | IPC_CREAT);
    
    
          if (id_shm == -1 || id_shm_v == -1) {
                perror("No se pudo reservar la memoria");
                exit(-1);
          }
    
    
          shm = (player *) shmat(id_shm, 0, 0);
          shm_v = (int *) shmat(id_shm_v, 0, 0);
    
    
    
    
          if(shm == -1 || shm_v == -1){
                perror("No se pudo acceder a la memoria");
                exit(-1);
          }
          new_player();
          print_player(&shm[shm_v[1]]);
          shm_v[1]++;
    
    
          //fill_matrix();
          //print_player(&shm[shm_v[1]]);
    
    
          while (1) {
                printf("Id del servidor: %d, cantida de jugadores actualmente %d\n", shm_v[0],shm_v[1]);
                sleep(2);
          }
    
    
          //shmdt(shm); //
          //shmctl(id_shm, IPC_RMID, 0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
                      printf("|%c", shm[shm_v[1]].board[i][j]);
                      if (j == 5) {
                            printf("|%c|\n", shm[shm_v[1]].board[i][j]);
                      }
    Because you always print the last column twice.

    Also, storing a thread id in shared memory doesn't buy you anything. The result isn't usable in any other process context.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    8
    Quote Originally Posted by Salem View Post
    Code:
                      printf("|%c", shm[shm_v[1]].board[i][j]);
                      if (j == 5) {
                            printf("|%c|\n", shm[shm_v[1]].board[i][j]);
                      }
    Because you always print the last column twice.

    Also, storing a thread id in shared memory doesn't buy you anything. The result isn't usable in any other process context.
    really thank you buddy that was the thing -.-
    about the thread id you mean the shv_v[0]? im doing this to know who is the server process so i can send from other process signals

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-04-2014, 10:09 AM
  2. Shared Memory
    By schrei in forum C Programming
    Replies: 2
    Last Post: 12-07-2011, 11:02 AM
  3. arbitrary column arrangement of memory debug window?
    By m37h0d in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2009, 11:31 AM
  4. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 02:29 AM
  5. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 07:48 AM

Tags for this Thread