Old stuff/ecole_etude_fac_de_pau/licence_3/systeme-d-exploitation/tp4/exo2b.c
(Deskargatu)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
void fonction(int n)
{
char* test;
key_t cle= ftok("./prout",0);
int id;
id = shmget(cle,0,0);
if (id == -1)
{
perror("shmget");
exit(-1);
}
// printf("Id = %d\n",id);
test = (char*)shmat(id,0, 0);
printf("On lit %s\n",test);
shmdt(test);
exit(-1);
}
int main(int argc, char** argv)
{
pid_t pid;
char* test=NULL;
key_t cle= ftok("./prout",0);
int id;
const char source[] = "salut";
id = shmget(cle,0,0);
if (id == -1)
{
perror("shmget");
exit(-1);
}
// printf("Id = %d\n",id);
test = (char*)shmat(id,0, 0);
if ((int)test == -1) { perror("error shmat"); exit(-1); }
char temp[100];
while(strcmp("exit\n",test) != 0)
{
sleep(1);
if (strcmp(temp,test)!=0)
{
printf("On lit : %s\n",test);
memcpy(temp,test,sizeof(char)*100);
}
}
return 1;
}