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>
- #define size 512;
- char* readinput();
- char* encipher(char* BUF);
- void print_res(char* BUF);
- int main(void)
- {
- char * BUF;
- int shmid;
- key_t key;
- char *s;
- key = 1234;
- sem_t *sem1;
- sem_t *sem2;
- sem_t *sem3;
- int semValue = 0;
- //tworzymy segment
- if ((shmid = shmget(key, sizeof(char), IPC_CREAT | 0666)) < 0) {
- perror("shmget");
- exit(1);
- }
- //dolaczamy zmienna do segmentu
- if ((BUF = shmat(shmid, NULL, 0)) == (char *) -1) {
- perror("shmat");
- exit(1);
- }
- //tworzymy semafor
- //przygotowujemy do dzielenia miedzy procesami
- sem1 = sem_open ("sharedSem1", O_CREAT | O_EXCL, 0666, semValue);
- 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("sharedSem");
- sem_unlink("sharedSem2");
- sem_unlink("sharedSem3");
- if(fork()==0)
- {
- printf("Proces %d %d utworzony.\n", getpid(), getppid());
- while(1)
- {
- //BUF=readinput();
- printf("1");
- wait(1);
- sem_post(sem2);
- sem_wait(sem1);
- }
- }
- if(fork()==0)
- {
- printf("Proces %d %d utworzony.\n", getpid(), getppid());
- while(1)
- {
- sem_wait(sem2);
- //BUF=encipher(BUF);
- printf("2");
- wait(1);
- sem_post(sem3);
- }
- }
- if(fork()==0)
- {
- printf("Proces %d %d utworzony.\n", getpid(), getppid());
- while(1)
- {
- sem_wait(sem3);
- //print_res(BUF);
- printf("3");
- wait(1);
- sem_post(sem1);
- }
- }
- pause();
- return 0;
- }
- char* readinput()
- {
- char* txt;
- printf("Podaj tekst do zaszyfrowania: ");
- scanf("%s", &txt);
- printf("%s",txt);
- return txt;
- }
- char* encipher(char* BUF)
- {
- int i;
- int key=5;
- char* etxt;
- for(i=0;i<strlen(BUF);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];
- }
- printf("%c",etxt[i]);
- }
- }
- void print_res(char* BUF)
- {
- printf("zaszyfrowany tekst: %s\n\n", BUF);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement