TD8-9 Systeme, Ex1-2

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int creerMemp(key_t cle,int nb);
int accedeMemp(key_t cle,int nb);
void * attacheMemp(int shmid);
int detruitMemp(int shmid);
void * shmalloc(key_t cle,int size);

int shmfree(key_t cle);int creerMemp(key_t cle,int nb){
return shmget(cle,nb,IPC_CREAT|IPC_EXCL|0600);
}

int accedeMemp(key_t cle,int nb){
return shmget(cle,nb,0600);
}

void * attacheMemp(int shmid){
return shmat(shmid,0,0);
}

int detruitMemp(int shmid){
return shmctl(shmid,IPC_RMID,0);
}

void * shmalloc(key_t cle,int size){
void * res;
int id=creerMemp(cle,size);
if (id==-1)
return 0;
res=attacheMemp(id);
if (res==-1) {
detruitMemp(id);
return 0;
}
return res;
}

int shmfree(key_t cle){
return detruitMemp(shmget(cle,1,0));
}

int main(void){
key_t key;
int * ptr;
void * res;
pid_t pid;
int i;

key=ftok("/dc/password",0);
ptr=(int *) shmalloc(key,sizeof(int)); /* error */
if (!ptr){
printf("shmalloc ehec");
exit(1);
}

*ptr=0;
printf("Bonjour : %d\n",*ptr);

switch(pid=fork()) {
case -1: perror("erreur fork");
exit(1);
case 0:
*ptr=1;
printf("fils apres modif: %d\n",*ptr);
puts("Fin fils");
exit(0);
default:
wait(0);
printf("Pere : %d\n",*ptr);
shmfree(key);
puts("Fin du Pere\n");
}
return 0;
}

/* ©2003 All Rights Reserved to www.Software-DS.com */





Haut de la page - Page générée en 0.00174 sec.
 

1847337 visiteurs.   ©2001-2024 All Rights Reserved to Software-DS.com
Made with a mac  
Confidentialité