Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/shm.h>
- void produttore(key_t key){
- int memZone;
- if (memZone=shmget(key,128,IPC_CREAT|IPC_EXCL|0666)==-1){
- printf("Errore...");
- exit(0);
- }
- void *robe;
- if ((robe= shmat(memZone,NULL,0)) == (void *) -1) {
- printf("Errore del produttore. Impossibile agganciare la memoria condivisa.\n");
- shmctl(memZone, IPC_RMID, NULL);
- exit(1);
- }
- scanf("%[^\n]",(char *)robe);
- shmdt(robe);
- }
- void consumatore(key_t key){
- int memZone=shmget(key,128,0);
- void *robine=shmat(memZone,NULL,0);
- printf("|%s|\n",(char *)robine);
- shmctl(memZone, IPC_RMID, NULL);
- shmdt(robine);
- }
- int main(){
- int status;
- key_t key = 1324;
- int son=fork();
- if (son==0){
- produttore(key);
- exit(0);
- }
- wait(&status);
- consumatore(key);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement