Usando shared memory
Publicado por Perfil removido 23/11/2004
[ Hits: 8.763 ]
O GNU/Linux (e todo SO respeitável) oferece um recurso interessante para quem quer criar programas que conversem entre si. Esse recurso é a memória compartilhada, ou shared memory. Esse primeiro programa cria uma área de memória compartilhada que pode ser acessada por qualquer programa, desde que respeite as políticas de permissão, e é claro, saiba a chave, ou key, da área compartilhada. Num segundo programa estarei mostrando como se faz o acesso a essa área.
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
char *s;
int run = 1;
void
exit_handler(int signum)
{
run = 0;
}
void
usr1_handler(int signum)
{
printf("%s\n", s);
}
main()
{
int shmid;
struct shmid_ds buf;
struct sigaction sig;
char *s;
sig.sa_flags = 0;
sigemptyset(&sig.sa_mask);
sig.sa_handler = exit_handler;
sigaction(SIGTERM, &sig, NULL);
sig.sa_handler = usr1_handler;
sigaction(SIGUSR1, &sig, NULL);
shmid = shmget(0xFAB10, 100, IPC_CREAT | 0600);
if ( shmid == -1 )
{
printf("can't creat shared memory\n");
perror("shmget");
exit(1);
}
if ( shmctl(shmid, IPC_STAT, &buf) != 0 )
{
printf("can't stat shared memory\n");
perror("shmctl");
exit(2);
}
s = (char *) shmat(shmid, 0, 0);
if ( s == (void *)-1 )
{
printf("can't attach shared memory\n");
perror("shmat");
exit(3);
}
strcpy(s, "ola mundo");
while( run )
;
shmdt(s);
shmctl(shmid, IPC_RMID, 0);
exit(0);
}
Converter arquivos Bitmap para ASCII-art
Gerenciamento de Área de Alocação Dinâmica (Listas Encadeadas)
Exemplo de sistema especialista usando Inteligência Artificial
LazyDocker – Interface de Usuário em Tempo Real para o Docker
Instalando COSMIC no Linux Mint
Turbinando o Linux Mint: o poder das Nemo Actions
Inteligência Artificial no desenvolvimento de software: quando começar a usar?
[Resolvido] Algo deu errado ao abrir seu perfil
Usando o VNSTAT para medir o seu consumo de internet
Habilitando clipboard manager no ambiente COSMIC
Problema com som no laptop (5)
Quando vocês pararam de testar distros? (11)
Não estou conseguindo fazer funcionar meu Postfix na versão 2.4 no Deb... (2)









