Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/shm.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <wait.h>
- #include <fcntl.h>
- #include<sys/ipc.h>
- int main ()
- {
- key_t Clave;
- int Id_Memoria;
- int * Memoria;
- int aux;
- int maxNum = 10;
- int pid1, pid2;
- Clave = ftok ("/bin/ls", 33);
- if (Clave == -1)
- {
- printf("Error, no existe la clave\n");
- exit(EXIT_FAILURE);
- }
- Id_Memoria = shmget (Clave, sizeof(int)*100, 0777 | IPC_CREAT);
- if (Id_Memoria == -1)
- {
- printf("Error, no existe el ID para la memoria\n");
- exit(EXIT_FAILURE);
- }
- Memoria = shmat (Id_Memoria, 0, 0);
- if (Memoria < 0)
- {
- printf("Error, no existe la memoria compartida\n");
- exit(EXIT_FAILURE);
- }
- *Memoria = 0;
- shmdt(Memoria);
- printf("fin primera etapa\n");
- pid1 = fork();
- if(pid1 == -1)
- {
- printf("ERROR\n");
- exit(EXIT_FAILURE);
- }
- if (pid1 == 0)
- {
- printf("Soy H1, mi PID: %d\n Mi padre es: %d\n", getpid(), getppid());
- Memoria = shmat (Id_Memoria, 0, 0);
- if (Memoria < 0)
- {
- printf("Error no se consigue la memoria compartida\n");
- exit(EXIT_FAILURE);
- }
- while (*Memoria <= maxNum)
- {
- aux=*Memoria;
- if ((aux%2) == 0)
- {
- aux++;
- if (aux <= maxNum)
- printf("%d\n", aux);
- *Memoria = aux;
- }
- }
- shmdt (Memoria);
- exit(EXIT_SUCCESS);
- }
- else
- {
- pid2 = fork();
- if(pid2 == -1)
- {
- printf("ERROR\n");
- exit(EXIT_FAILURE);
- }
- if (pid2 == 0)
- {
- Memoria = shmat (Id_Memoria, 0, 0);
- if (Memoria < 0)
- {
- printf("Error, no existe la memoria compartida\n");
- exit(EXIT_FAILURE);
- }
- while (*Memoria <= maxNum)
- {
- aux=*Memoria;
- if ((aux%2) == 1)
- {
- aux++;
- if (aux <= maxNum)
- printf("%d\n", aux);
- *Memoria = aux;
- }
- }
- shmdt (Memoria);
- printf("Soy H2, mi PID: %d\n Mi padre es: %d\n", getpid(), getppid());
- exit(EXIT_SUCCESS);
- }
- exit(EXIT_SUCCESS);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement