Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <sys/shm.h>
- #include <errno.h>
- #include <semaphore.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <string.h>
- char* readinput();
- char* encipher(char* BUF);
- void print_res(char* BUF);
- int main(void)
- {
- char *BUF;
- int shmid;
- sem_t *sem1;
- sem_t *sem2;
- sem_t *sem3;
- int semValue = 0;
- //tworzymy segment
- if ((shmid = shmget(ftok("a",1), 100, IPC_CREAT | 0666)) < 0)
- {
- perror("shmget");
- exit(1);
- }
- //dolaczamy zmienna do segmentu
- if ((BUF=(char*)shmat(shmid,NULL,0))==NULL)
- {
- perror("shmat");
- exit(1);
- }
- //tworzymy semafor
- //przygotowujemy do dzielenia miedzy procesami
- sem1 = sem_open ("sharedSem1", O_CREAT | O_EXCL, 0666, 0);
- sem2 = sem_open ("sharedSem2", O_CREAT | O_EXCL, 0666, semValue);
- sem3 = sem_open ("sharedSem3", O_CREAT | O_EXCL, 0666, semValue);
- //sem unlink sprawia ze semafor bedzie istnial w nieskonczonosc;
- sem_unlink("sharedSem1");
- sem_unlink("sharedSem2");
- sem_unlink("sharedSem3");
- if(fork()==0)
- {
- printf("Proces %d %d utworzony.\n", getpid(), getppid());
- //if ((shmid = shmget(ftok(".",1), 100, 0666)) < 0)
- //{
- //perror("shmget");
- //exit(1);
- //}
- //if ((BUF = (char*)shmat(shmid,NULL,0)) == NULL)
- //{
- //perror("shmat");
- //exit(1);
- //}
- while(1)
- {
- BUF=readinput();
- sem_post(sem2);
- sem_wait(sem1);
- printf("%s\n", BUF);
- }
- }
- if(fork()==0)
- {
- printf("Proces %d %d utworzony.\n", getpid(), getppid());
- //if ((shmid = shmget(ftok(".",1), 100, 0666)) < 0)
- //{
- //perror("shmget");
- //exit(1);
- //}
- //if ((BUF = (char*)shmat(shmid,NULL,0)) == NULL)
- //{
- //perror("shmat");
- //exit(1);
- //}
- while(1)
- {
- sem_wait(sem2);
- BUF=encipher(BUF);
- sem_post(sem3);
- }
- }
- if(fork()==0)
- {
- printf("Proces %d %d utworzony.\n", getpid(), getppid());
- //if ((shmid = shmget(ftok(".",1), 100, 0666)) < 0)
- //{
- //perror("shmget");
- //exit(1);
- //}
- //if ((BUF = (char*)shmat(shmid,NULL,0)) == NULL)
- //{
- //perror("shmat");
- //exit(1);
- //}
- while(1)
- {
- sem_wait(sem3);
- print_res(BUF);
- sem_post(sem1);
- }
- }
- pause();
- return 0;
- }
- char* readinput()
- {
- char* txt=malloc(sizeof(char)*100);
- printf("Podaj tekst do zaszyfrowania: \n");
- scanf("%s", txt);
- return txt;
- }
- char* encipher(char* BUF)
- {
- int i;
- int key=5;
- char* etxt=malloc(sizeof(char)*100);
- for(i=0;i<100;i++)
- {
- if((BUF[i]>='A') && (BUF[i]<='Z'))
- {
- etxt[i]=BUF[i]+key;
- if(etxt[i]>'Z')
- etxt[i]=(etxt[i]%90)+65;
- }
- else
- {
- if((BUF[i]>='a') && (BUF[i]<='z'))
- {
- etxt[i]=BUF[i]+key;
- if(etxt[i]>'z')
- etxt[i]=(etxt[i]%122)+97;
- }
- else
- etxt[i]=BUF[i];
- }
- }
- return etxt;
- }
- void print_res(char* BUF)
- {
- printf("%s\n",BUF);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement