Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <fcntl.h>
  6. #include <sys/shm.h>
  7. #include <errno.h>
  8. #include <semaphore.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <string.h>
  12.  
  13. char* readinput();
  14. char* encipher(char* BUF);
  15. void print_res(char* BUF);
  16.  
  17. int main(void)
  18. {
  19.  
  20.     char *BUF;
  21.     int shmid;
  22.     sem_t *sem1;
  23.     sem_t *sem2;
  24.     sem_t *sem3;
  25.     int semValue = 0;
  26.        
  27.         //tworzymy segment
  28.     if ((shmid = shmget(ftok("a",1), 100, IPC_CREAT | 0666)) < 0)
  29.     {
  30.         perror("shmget");
  31.         exit(1);
  32.     }
  33.         //dolaczamy zmienna do segmentu
  34.     if ((BUF=(char*)shmat(shmid,NULL,0))==NULL)
  35.     {
  36.         perror("shmat");
  37.         exit(1);
  38.     }
  39.        
  40.     //tworzymy semafor
  41.     //przygotowujemy do dzielenia miedzy procesami
  42.     sem1 = sem_open ("sharedSem1", O_CREAT | O_EXCL, 0666, 0);
  43.     sem2 = sem_open ("sharedSem2", O_CREAT | O_EXCL, 0666, semValue);
  44.     sem3 = sem_open ("sharedSem3", O_CREAT | O_EXCL, 0666, semValue);
  45.     //sem unlink sprawia ze semafor bedzie istnial w nieskonczonosc;
  46.     sem_unlink("sharedSem1");
  47.     sem_unlink("sharedSem2");
  48.     sem_unlink("sharedSem3");
  49.  
  50.     if(fork()==0)
  51.     {
  52.         printf("Proces %d %d utworzony.\n", getpid(), getppid());
  53.        
  54.         //if ((shmid = shmget(ftok(".",1), 100, 0666)) < 0)
  55.         //{
  56.             //perror("shmget");
  57.             //exit(1);
  58.         //}
  59.         //if ((BUF = (char*)shmat(shmid,NULL,0)) == NULL)
  60.         //{
  61.             //perror("shmat");
  62.             //exit(1);
  63.         //}
  64.        
  65.         while(1)
  66.         {
  67.             BUF=readinput();
  68.             sem_post(sem2);
  69.             sem_wait(sem1);
  70.             printf("%s\n", BUF);
  71.         }
  72.     }
  73.    
  74.     if(fork()==0)
  75.     {
  76.         printf("Proces %d %d utworzony.\n", getpid(), getppid());
  77.        
  78.         //if ((shmid = shmget(ftok(".",1), 100, 0666)) < 0)
  79.         //{
  80.             //perror("shmget");
  81.             //exit(1);
  82.         //}
  83.         //if ((BUF = (char*)shmat(shmid,NULL,0)) == NULL)
  84.         //{
  85.             //perror("shmat");
  86.             //exit(1);
  87.         //}
  88.        
  89.            
  90.         while(1)
  91.         {
  92.             sem_wait(sem2);
  93.             BUF=encipher(BUF);
  94.             sem_post(sem3);
  95.         }
  96.     }
  97.    
  98.     if(fork()==0)
  99.     {
  100.         printf("Proces %d %d utworzony.\n", getpid(), getppid());
  101.        
  102.         //if ((shmid = shmget(ftok(".",1), 100, 0666)) < 0)
  103.         //{
  104.             //perror("shmget");
  105.             //exit(1);
  106.         //}
  107.         //if ((BUF = (char*)shmat(shmid,NULL,0)) == NULL)
  108.         //{
  109.             //perror("shmat");
  110.             //exit(1);
  111.         //}
  112.        
  113.         while(1)
  114.         {
  115.             sem_wait(sem3);
  116.             print_res(BUF);
  117.             sem_post(sem1);
  118.         }
  119.     }
  120.     pause();
  121.     return 0;
  122. }
  123.  
  124.  
  125. char* readinput()
  126. {
  127.     char* txt=malloc(sizeof(char)*100);
  128.     printf("Podaj tekst do zaszyfrowania: \n");
  129.     scanf("%s", txt);
  130.     return txt;
  131. }
  132.  
  133. char* encipher(char* BUF)
  134. {
  135.     int i;
  136.     int key=5;
  137.     char* etxt=malloc(sizeof(char)*100);
  138.     for(i=0;i<100;i++)
  139.     {
  140.         if((BUF[i]>='A') && (BUF[i]<='Z'))
  141.         {
  142.             etxt[i]=BUF[i]+key;
  143.             if(etxt[i]>'Z')
  144.                 etxt[i]=(etxt[i]%90)+65;
  145.         }
  146.         else
  147.         {
  148.             if((BUF[i]>='a') && (BUF[i]<='z'))
  149.             {
  150.                 etxt[i]=BUF[i]+key;
  151.                 if(etxt[i]>'z')
  152.                     etxt[i]=(etxt[i]%122)+97;
  153.             }
  154.             else
  155.                 etxt[i]=BUF[i];
  156.         }
  157.     }
  158.     return etxt;
  159. }
  160.  
  161. void print_res(char* BUF)
  162. {
  163.     printf("%s\n",BUF);
  164.     return;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement