Thread: characters wont appear in shared memory

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    20

    characters wont appear in shared memory

    I am creating a program that uses shared memory for comunication between processes but somehow i cannot get the characters to appear in the other process

    this is a header file with the struct

    "estrutura.h"
    Code:
    #define MAX 50
    #define         ShmKey          77
    #define         ShmSize         4 * 1024
    #define         ShmPerm         0777
    #define         ShmFlag         IPC_CREAT
    #include <semaphore.h>
    struct fila
    {
    char a[50];
    int cabeca, cauda;
    sem_t sem;
    };
    And here is the main file that i use to handle the communication

    Code:
    #include <unistd.h> 
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <sys/types.h> 
    #include <sys/ipc.h> 
    #include "estrutura.h" 
    #include "sharedMemory.h" 
     
    char erro(char *m)  {                 
        fprintf(stderr, "ERRO: %s\n", m); 
        exit(1); 
    } 
     
    void Sem_init(sem_t *sem, int pshared, unsigned int value) { 
      if (sem_init(sem, pshared, value)) 
             printf("Seminit Error"); 
    } 
    void inicializar(struct fila *f) { 
        f->cabeca = 0; 
        f->cauda = 0; 
        printf("Vector inicializado\n"); 
    } 
     
    main() 
    { 
        sem_t *sem2; 
        struct fila *f; 
        SharedMemory    shmId; 
        void    * shmAt; 
        ShmCreate(shmId, ShmKey, ShmSize); 
          ShmAttach(shmId, shmAt); 
        f = (struct fila *)shmAt; 
        Sem_init(&(f->sem),1,1); 
        inicializar(f); 
     
        switch (fork()) { 
        case -1: 
            erro("fork"); 
            break; 
        case 0: 
            execlp("/home/hyper/Documents/SO2/TP2/leitura", "leitura", 0); 
            erro("Voltou do ls"); 
            break; 
        default: 
            wait(); 
            switch(fork()) { 
                case 0: 
                    execlp("/home/hyper/Documents/SO2/TP2/teste", "teste", 0); 
                    //erro("Voltou do wc"); 
                    break; 
                 default:            
                    //execlp("/home/hyper/Documents/SO2/TP1/pork","pork",0); 
                    break; 
            } 
                    break; 
        } 
    }

    Here is one of the processes
    The one that writes into the shared memory
    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include "estrutura.h"
    #include "sharedMemory.h"
    void Sem_init(sem_t *sem, int pshared, unsigned int value) {
      if (sem_init(sem, pshared, value))
             printf("Seminit Error");
    }
    void P(sem_t *sem) {
      if (sem_wait(sem))
        printf("V");
    }
    
    void V(sem_t *sem) {
      if (sem_post(sem))
        printf("V");
    }
    void leitura(struct fila *f) {
        char a[50];
           int n = read(0,a,sizeof(a));
           a[n] = '\0';
        int i = 0;
        P(&(f->sem));
        while(a[i] != '\0') {
            if(a[i] != '\n') {
                 f->a[f->cabeca] = a[i];
              f->cabeca++;
                  i++;
            } else {i++;}
        }
        f->a[f->cabeca + 1] = '\0';
        V(&(f->sem));
    }
    main() {
        struct fila *f;
        SharedMemory    shmId;
        void    * shmAt;
        ShmGet(shmId, ShmKey, ShmSize);    
        ShmAttach(shmId, shmAt);
        f = (struct fila *)shmAt;
        leitura(f);
    }
    This is the process that is unable to read the characters that the other process put in the shared memory

    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <semaphore.h>
    #include "estrutura.h"
    #include "sharedMemory.h"
    void Sem_init(sem_t *sem, int pshared, unsigned int value) {
      if (sem_init(sem, pshared, value))
             printf("Seminit Error");
    }
    void P(sem_t *sem) {
      if (sem_wait(sem))
        printf("V");
    }
    
    void V(sem_t *sem) {
      if (sem_post(sem))
        printf("V");
    }
    
    /*void leitura(struct fila *f) {
        int i = 0;
        char a[50];
             P(&(f->sem));
          while((f->cabeca) > 0) {
              a[i] = f->a[f->cabeca];
              f->cabeca--;
              i++;
          }
         V(&(f->sem));
          a[i+1] = '\0';
    }*/
    main() {
        //printf("Segundo processo");
        struct fila *f;
        SharedMemory    shmId;
        void    * shmAt;
        ShmGet(shmId, ShmKey, ShmSize);
        ShmAttach(shmId, shmAt);
        f = (struct fila *)shmAt;
        putchar(f->a[0]);
        //this is the printf that is supposed to show the characters in the array this does not work
        printf("%s",f->a);
        //leitura(f);
        
    }
    This is the header file i use that cointains information about shared memory
    Code:
    /* 
        sharedMemory.h:    Types and routines for User-friendly Shared Memory 
        Paulo Afonso Lopes 
    */ 
     
     
    /* 
            Types ... 
    */ 
     
    #define         SharedMemory    int 
     
     
     
    /* 
            and routines ... 
    */ 
     
     
    #ifndef DEBUG 
     
     
    #define  ShmCreate(shmId, shmKey, shmSize)                \ 
           shmId = shmget(shmKey, shmSize, 0777|IPC_CREAT) 
     
     
    #define  ShmRemove(shmId)        shmctl(shmId, IPC_RMID, NULL) 
     
     
    #define  ShmGet(shmId, shmKey, shmSize)                    \ 
           shmId = shmget(shmKey, shmSize, 0777) 
     
     
    #define  ShmAttach(shmId, shmAt)    shmAt = (void *)shmat(shmId, 0, 0) 
     
     
    #define  ShmDetach(shmAt)        shmdt(shmAt) 
     
     
     
    #else 
     
     
     
    #define  ShmCreate(shmId, shmKey, shmSize)                \ 
        if ( (shmId = shmget(shmKey, shmSize, 0777|IPC_CREAT)) < 0 ) {    \ 
          perror("ShmCreate");                        \ 
          exit(errno);                            \ 
        } 
     
     
    #define  ShmRemove(shmId)                        \ 
        if ( shmctl(shmId, IPC_RMID, NULL) < 0 ) {            \ 
          perror("ShmRemove");                        \ 
          exit(errno);                            \ 
        } 
     
     
    #define  ShmGet(shmId, shmKey, shmSize)                    \ 
        if ( (shmId = shmget(shmKey, shmSize, 0777)) < 0 ) {        \ 
          perror("ShmGet");                        \ 
          exit(errno);                            \ 
        } 
     
     
    #define  ShmAttach(shmId, shmAt)                    \ 
        if ( (shmAt = (void *)shmat(shmId, 0, 0)) < 0 ) {        \ 
          perror("ShmAttach");                        \ 
          exit(errno);                            \ 
        } 
     
     
    #define  ShmDetach(shmAt)                        \ 
        if ( shmdt(shmAt) < 0 ) {                    \ 
          perror("ShmDetach");                        \ 
          exit(errno);                            \ 
        } 
     
    #endif

    Any help would be appreciated

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Aren't you suppose to initialize shmId to some value before you use it?

    Also, have you tested all your macros in isolation and confirmed that they are working correctly?

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    Quote Originally Posted by Subsonics View Post
    Aren't you suppose to initialize shmId to some value before you use it?

    Also, have you tested all your macros in isolation and confirmed that they are working correctly?
    Yes smget will give a value to shmId..
    every other thing works ..
    i just can't display characters..
    if i try to print any other value in the struct it will appear

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Hyp3rTension View Post
    Yes smget will give a value to shmId..
    every other thing works ..
    i just can't display characters..
    if i try to print any other value in the struct it will appear
    Yeah, the first argument to shmget is the key, which is what I thought this was since you have obfuscated all standard functions with your own macros. Why not create a generic error handling macro / function?

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    The thing is .. that when i try to change an int value that is in the structure ...
    and print it in the other process .. the values appear
    but whenever i try to print a char .... it shows as empty .. which means that only characters aren't being put into the shared memory

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shared Memory
    By schrei in forum C Programming
    Replies: 2
    Last Post: 12-07-2011, 11:02 AM
  2. Shared memory IPC Help!!!
    By liudaisuda in forum Linux Programming
    Replies: 3
    Last Post: 09-21-2011, 04:14 PM
  3. STL in Shared Memory
    By AlenDev in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2010, 04:04 AM
  4. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 07:48 AM
  5. using shared memory in c
    By flawildcat in forum C Programming
    Replies: 1
    Last Post: 04-09-2002, 12:25 PM