Skip to content

Instantly share code, notes, and snippets.

@andersonfraga
Last active April 26, 2017 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonfraga/607749c41e7e284aa0c84b3956d90847 to your computer and use it in GitHub Desktop.
Save andersonfraga/607749c41e7e284aa0c84b3956d90847 to your computer and use it in GitHub Desktop.
#include <string.h>
#include "headers.h"
int main(int argc, char const *argv[])
{
int id_shared, id_server;
char *psh_msg;
sem_t *sem_server;
const char *message = argv[1];
key_t sh_key = SHKEY;
key_t srv_key = SRVKEY;
SHM_GET_MAT(sh_key, srv_key);
// message será copiado para psh_msg, inicializado em SHM_GET_MAT
strcpy(psh_msg, message);
sem_post(&(*sem_server));
return 0;
}
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#define SHM_GET_MAT(sh_key, srv_key) \
if ((id_shared = shmget(sh_key, SHMSZ, IPC_CREAT | 0666)) < 0) { \
perror("shmget id_shared"); \
return 1; \
} \
\
if ((id_server = shmget(srv_key, sizeof(sem_t), IPC_CREAT | 0666)) < 0) { \
perror("shmget id_server"); \
return 1; \
} \
\
if ((psh_msg = shmat(id_shared, NULL, 0)) == (char *) -1) { \
perror("shmat psh_msg"); \
return 1; \
} \
\
if ((sem_server = (sem_t*) shmat(id_server, NULL, 0)) < 0) { \
perror("shmat sem_server"); \
return 1; \
} \
#define SHKEY 2345
#define SRVKEY 3456
#define SHMSZ 27
#include "headers.h"
int main(int argc, char const *argv[])
{
int id_shared, id_server;
char *psh_msg, *message;
sem_t *sem_server;
key_t sh_key = SHKEY;
key_t srv_key = SRVKEY;
SHM_GET_MAT(sh_key, srv_key);
message = psh_msg;
*message = (long) NULL;
sem_init(&(*sem_server), 1, 1);
while (1) {
sem_wait(&(*sem_server));
for (message = psh_msg; *message != (long) NULL; message++) {
putchar(*message);
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment