Advertisement
Sbovvo

Untitled

Feb 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/shm.h>
  5. #include <string.h>
  6. #include <sys/sem.h>
  7. #include "semafori.h"
  8.  
  9. #define SHM_KEY (key_t)1234
  10. #define SEM_KEYR (key_t)5678
  11. #define SEM_KEYW (key_t)9012
  12.  
  13.  
  14. struct DataCondivisa {
  15.     int intero;
  16.     float reale;
  17. };
  18.  
  19. int main(void){
  20.     int running= 1;
  21.     unsigned int count=0;
  22.     void * ShmP= (void *)0;
  23.     struct DataCondivisa *p;
  24.     int ShmID;
  25.     int WriteID, ReadID;
  26.    
  27.     WriteID= semget(SEM_KEYW, 1, 0666 | IPC_CREAT);
  28.     if (WriteID==-1){
  29.         perror("Errore dio ca\n");
  30.         exit(EXIT_FAILURE);
  31.     }
  32.     ReadID= semget(SEM_KEYR, 1, 0666 | IPC_CREAT);
  33.     if (ReadID==-1){
  34.         perror("Errore dio ca\n");
  35.         exit(EXIT_FAILURE);
  36.     }
  37.     ShmID= semget(SHM_KEY, sizeof(struct DataCondivisa), 0666 | IPC_CREAT);
  38.     if (ShmID==-1){
  39.         perror("Errore dio ca\n");
  40.         exit(EXIT_FAILURE);
  41.     }
  42.    
  43.     ShmP=shmat(ShmID, (void *)0, 0);
  44.     if (ShmP== (void *)-1) {
  45.         perror("Hai sbagliato orca madò\n");
  46.         exit(EXIT_FAILURE);
  47.     }
  48.    
  49.    
  50.     p=(struct DataCondivisa *) ShmP;
  51.     printf("La memoria si attacca al cazzo %d %f\n", p->intero, p->reale);
  52.    
  53.     while(running) {
  54.         if(SEM_P(WriteID)==-1) exit (EXIT_FAILURE);
  55.         printf("Inserisci un numero intero positivo oppure uno negativo per terminare\n");
  56.         scanf("%d", &p->intero);
  57.         if(p->intero<0){
  58.             running=0;
  59.         }else {
  60.             printf("Inserisci un numero reale\n");
  61.             scanf("%f", &p->reale);
  62.         }
  63.         if (SEM_V(ReadID)==-1) exit(EXIT_FAILURE);
  64.     }
  65.     if (shmdt(ShmP) ==-1){
  66.         perror("Errore dio ca\n");
  67.         exit (EXIT_FAILURE);
  68.     }
  69.     exit (EXIT_SUCCESS);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement