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