Advertisement
Sbovvo

Untitled

Feb 23rd, 2019
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/sem.h>
  6. #include <sys/shm.h>
  7. #include "semafori.h"
  8.  
  9. #define SHM_KEY (key_t)1234
  10. #define SEM_KEY1 (key_t)5678
  11. #define SEM_KEY2 (key_t)9012
  12.  
  13. #define TEXT_SIZE 2048
  14. char text[TEXT_SIZE];
  15.  
  16. int main(int argc, char* argv[]){
  17.     int continua=1;
  18.     int ShmID;
  19.     int SEM_USER_1, SEM_USER_2;
  20.     void *ShmP=(void*)0;
  21.     char   *buf;
  22.     int mySem;
  23.     int yourSem;
  24.     printf("Benvenuti nella chat, scrivi fine per terminare.\n");
  25.    
  26.     if (argc<2){
  27.         ShmID = shmget(SHM_KEY, TEXT_SIZE, 0666 | IPC_CREAT);
  28.         if(ShmID==-1){
  29.             perror("errore di shmget");
  30.             exit(EXIT_FAILURE);
  31.         }
  32.         ShmP= shmat(ShmID, (void *)0, 0);
  33.         if(ShmP==(void *)-1){
  34.             perror("errore shmat");
  35.             exit(EXIT_FAILURE);
  36.         }
  37.        
  38.         buf =(char *)ShmP;
  39.         printf(buf, " ");
  40.        
  41.         SEM_USER_1=semget(SEM_KEY1, 1, 0666 | IPC_CREAT);
  42.         if(SEM_USER_1==-1){
  43.             perror("Errore semget 1");
  44.             exit(EXIT_FAILURE);
  45.         }
  46.         SEM_USER_2=semget(SEM_KEY1, 1, 0666 | IPC_CREAT);
  47.         if(SEM_USER_2==-1){
  48.             perror("Errore semget 1");
  49.             exit(EXIT_FAILURE);
  50.         }
  51.        
  52.         if(SEM_SET(SEM_USER_2, 0)==-1) exit(EXIT_FAILURE);
  53.         if(SEM_SET(SEM_USER_1, 1)==-1) exit(EXIT_FAILURE);
  54.  
  55.         mySem=SEM_USER_2;
  56.         yourSem=SEM_USER_1;
  57.         printf("Tu sei l'utente 2\n");
  58.     } else
  59.         {
  60.         ShmID = shmget(SHM_KEY, TEXT_SIZE, 0666 | IPC_CREAT);
  61.         if(ShmID==-1){
  62.             perror("errore di shmget");
  63.             exit(EXIT_FAILURE);
  64.         }
  65.         ShmP= shmat(ShmID, (void *)0, 0);
  66.         if(ShmP==(void *)-1){
  67.             perror("errore shmat");
  68.             exit(EXIT_FAILURE);
  69.         }
  70.        
  71.         buf=(char *) ShmP;
  72.        
  73.         SEM_USER_1=semget(SEM_KEY1, 1, 0666 | IPC_CREAT);
  74.         if(SEM_USER_1==-1){
  75.             perror("Errore semget 1");
  76.             exit(EXIT_FAILURE);
  77.         }
  78.         SEM_USER_2=semget(SEM_KEY1, 1, 0666 | IPC_CREAT);
  79.         if(SEM_USER_2==-1){
  80.             perror("Errore semget 1");
  81.             exit(EXIT_FAILURE);
  82.         }
  83.        
  84.         mySem=SEM_USER_1;
  85.         yourSem=SEM_USER_2;
  86.         printf("Tu sei l'utente 1\n");
  87.     }
  88.    
  89.     while (continua){
  90.         SEM_P(mySem);
  91.         if(strncmp(buf, "fine", 4)==0) continua=0;
  92.         else{
  93.             if(strlen(buf)>0)
  94.                 printf("Risposta: %s\n", buf);
  95.             printf("> ");
  96.             fgets(buf, TEXT_SIZE, stdin);
  97.             if(strncmp(buf, "fine", 4)==0) continua=0;
  98.         }
  99.         SEM_V(yourSem);
  100.     }
  101.    
  102.     if(mySem==SEM_USER_2){
  103.         if(shmdt(ShmP)==-1){
  104.             perror("Shmdt fallita\n");
  105.             exit(EXIT_FAILURE);
  106.         }
  107.         if(shmctl(ShmID,     IPC_RMID, 0) ==-1){
  108.             perror("Shmctl errore");
  109.             exit(EXIT_FAILURE);
  110.         }
  111.         SEM_DEL(SEM_USER_1);
  112.         SEM_DEL(SEM_USER_2);
  113.     }
  114.     if(mySem==SEM_USER_1){
  115.         if(shmdt(ShmP)==-1){
  116.             perror("Shmdt fallita \n");
  117.             exit(EXIT_FAILURE);
  118.         }
  119.     }
  120.     exit(EXIT_SUCCESS);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement