Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 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. #define size 512;
  14.  
  15. char* readinput();
  16. char* encipher(char* BUF);
  17. void print_res(char* BUF);
  18.  
  19. int main(void)
  20. {
  21.  
  22.     char * BUF;
  23.     int shmid;
  24.     key_t key;
  25.     char *s;
  26.     key = 1234;
  27.     sem_t *sem1;
  28.     sem_t *sem2;
  29.     sem_t *sem3;
  30.     int semValue = 0;
  31.     //tworzymy segment
  32.     if ((shmid = shmget(key, sizeof(char), IPC_CREAT | 0666)) < 0) {
  33.         perror("shmget");
  34.         exit(1);
  35.     }
  36.     //dolaczamy zmienna do segmentu
  37.     if ((BUF = shmat(shmid, NULL, 0)) == (char *) -1) {
  38.         perror("shmat");
  39.         exit(1);
  40.     }
  41.  
  42.     //tworzymy semafor
  43.     //przygotowujemy do dzielenia miedzy procesami
  44.     sem1 = sem_open ("sharedSem1", O_CREAT | O_EXCL, 0666, semValue);
  45.     sem2 = sem_open ("sharedSem2", O_CREAT | O_EXCL, 0666, semValue);
  46.     sem3 = sem_open ("sharedSem3", O_CREAT | O_EXCL, 0666, semValue);
  47.     //sem unlink sprawia ze semafor bedzie istnial w nieskonczonosc;
  48.     sem_unlink("sharedSem");
  49.     sem_unlink("sharedSem2");
  50.     sem_unlink("sharedSem3");
  51.  
  52.     if(fork()==0)
  53.     {
  54.         printf("Proces %d %d utworzony.\n", getpid(), getppid());
  55.         while(1)
  56.         {
  57.             //BUF=readinput();
  58.             printf("1");
  59.             wait(1);
  60.             sem_post(sem2);
  61.             sem_wait(sem1);
  62.         }
  63.     }
  64.     if(fork()==0)
  65.     {
  66.         printf("Proces %d %d utworzony.\n", getpid(), getppid());
  67.         while(1)
  68.         {
  69.             sem_wait(sem2);
  70.             //BUF=encipher(BUF);
  71.             printf("2");
  72.             wait(1);
  73.             sem_post(sem3);
  74.         }
  75.     }
  76.     if(fork()==0)
  77.     {
  78.         printf("Proces %d %d utworzony.\n", getpid(), getppid());
  79.         while(1)
  80.         {
  81.             sem_wait(sem3);
  82.             //print_res(BUF);
  83.             printf("3");
  84.             wait(1);
  85.             sem_post(sem1);
  86.         }
  87.     }
  88.     pause();
  89.     return 0;
  90. }
  91.  
  92.  
  93. char* readinput()
  94. {
  95.     char* txt;
  96.     printf("Podaj tekst do zaszyfrowania: ");
  97.     scanf("%s", &txt);
  98.     printf("%s",txt);
  99.     return txt;
  100. }
  101.  
  102. char* encipher(char* BUF)
  103. {
  104.     int i;
  105.     int key=5;
  106.     char* etxt;
  107.     for(i=0;i<strlen(BUF);i++)
  108.     {
  109.         if((BUF[i]>='A') && (BUF[i]<='Z'))
  110.         {
  111.             etxt[i]=BUF[i]+key;
  112.             if(etxt[i]>'Z')
  113.                 etxt[i]=(etxt[i]%90)+65;
  114.         }
  115.         else
  116.         {
  117.             if((BUF[i]>='a') && (BUF[i]<='z'))
  118.             {
  119.                 etxt[i]=BUF[i]+key;
  120.                 if(etxt[i]>'z')
  121.                     etxt[i]=(etxt[i]%122)+97;
  122.             }
  123.             else
  124.                 etxt[i]=BUF[i];
  125.         }
  126.         printf("%c",etxt[i]);
  127.     }
  128. }
  129.  
  130. void print_res(char* BUF)
  131. {
  132.     printf("zaszyfrowany tekst: %s\n\n", BUF);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement