#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#define SHMSZ 27
int main()
{
int shmid;
int *shm;
key_t key;
key =ftok("./",1);
if((shmid=shmget(key,SHMSZ,IPC_CREAT | 0666))<0){
perror("shmget");
return 1;
}
if ((shm=shmat(shmid,NULL,0))==(int *) -1){
perror("shmat");
return 1;
}
*shm=0;
switch(fork()){
case -1: {
perror("eroare fork\n");
break;
}
case 0:{
printf("%d - fiu\n",getpid());
switch(fork()){
case -1: {
perror("eroare fork\n");
break;
}
case 0:{
printf("%d - nepot\n",getpid());
char *str;
strcpy(str,"GOL");
while (strcmp(str,"EXIT")!=0)
{
printf("mesaj \n");
scanf("%s",str);
}
int nepot_id=getpid();
*shm=nepot_id;
break;
}
default:{
//fiuuuuuuuuuuuuuuuuuuuuuuuuuuuu
char *u;
while (*shm == 0)
sleep(1);
kill(*shm,9);
printf("nepot killarit la pid %d \n",*shm);
*shm=getpid();
kill(getpid(),9);
// printf("tata homorat \n");
break;
}
}
break;
}
default:{
printf("%d - tata\n",getpid());
sleep(2);
/*if (*shm!=0)
printf("tata killarit la pid %d \n",*shm);
*/ break;
}
}
sleep(10);
return 0;
}
评论0