-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperationSystems.c
256 lines (226 loc) · 6.66 KB
/
OperationSystems.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdio.h> //Temel C Komutlarini Kullanabilmek Icin Ekledigimiz Header Dosyasi
#include <stdlib.h> //exit Fonksiyonu Icin Header Dosyasi
#include <unistd.h> //Fork ve pid_t Tanimlarini Kullanabilmek Icin Ekledigimiz Header Dosyasi
#include <string.h> //String(Metin) Islemleri Icin Kullandigimiz Header Dosyasi
#include <sys/wait.h> //wait() Fonksiyonu
#include <pthread.h> //Thread Tanimlayabilmek Ve Kullanabilmemiz Icin Header
#include <sys/shm.h> //Shared Memory Kullanimi Icin
//Pipe icin global degiskenler
#define BUFFER_SIZE 400
#define READ_END 0
#define WRITE_END 1
int offset = 7;
//Thread Kullanimini Kolaylastirmak Icin Struct YApisi
typedef struct thread_data {
int arraySize;
int indis;
int choise;
char* text;
}thread_data;
//Bolmek Icin Kullanacagimiz Dort Parca
thread_data part[4];
//Dinamik dizi icin bellekten alan tahsisi.
void createArray(thread_data* d, int sizeVal, int choise)
{
d->text = (char*)malloc(sizeVal * sizeof(char));
d->arraySize = sizeVal;
d->indis = 0;
d->choise = choise;
}
//Dinamik dizi degerinin ilk tanimdan buyuk olmasi durumunda genisletme ve bellekten ek alan alma.
void expandArray(thread_data* d)
{
if (d->indis == d->arraySize) //Yine de maksimum indis degeri ile tanimli dizi genisligini karsilastiyoruz.
{
char* cntrl;
d->arraySize++;
//Bellekten alan alinamamasi durumunda uyari almasi icin duzenliyoruz.
cntrl = (char*)realloc(d->text, sizeof(char) * d->arraySize);
if (cntrl == NULL)
{
printf("Memory Fault!");
exit(1);
}
d->text = cntrl;
}
}
//Dinamik diziye bellek ekleme ve indis degerinin arttirilamasi.
void addArray(thread_data* d, char v)
{
expandArray(d);
d->text[d->indis++] = v;
d->choise = 1;
}
//Dinamik dizi alanini tamimiyla silinerek bellege iadesi.
void freeArray(thread_data* d)
{
free(d->text);
d->arraySize = 0;
d->indis = 0;
d->choise = 0;
}
//Kullanicidan Sifrelenecek Olan Metinin Alinmasi
void getText(char temp[])
{
printf("\n--------------Hi! Welcome To The Encryption Program--------------");
printf("\n---Please Remember That You Can Enter a Maximum Of 400 Letters!(Englisch Alphabet Only Use)---");
printf("\nPlease Enter The Text To Be Encrypted:");
fgets(temp,400,stdin);
if (strlen(temp) > 400 || strlen(temp)<5)
{
printf("You Have Exceeded The Maximum Chracter(400) Limit! Try Again.");
exit(0);
}
}
//Disaridan aldigi metin verisini, ikinci parametre olarak aldigimiz kaydirma sayisina ve operasyon turune gore Sezar Sifrelemesi uygular veya cozer.
void* CrypteOperations(void *arg)
{
thread_data *tdata=(thread_data *)arg;
char text[400];
strcpy(text,tdata->text);
int operationChoise = tdata->choise;
//Encrypte -> 1 | Decrypt ->0
if (operationChoise == 0)
{
offset = -offset;
}
int i = 0;
while (text[i] != '\0')
{
if (text[i] == ' ')
{
i++;
}
if (text[i] >= 65 && text[i] <= 90)
{
text[i] = ((text[i] + offset - 65) % 90) + 65;
}
else
{
text[i] = ((text[i] + offset - 97) % 122) + 97;
}
i++;
}
strcpy(tdata->text,text);
}
//Pipe Yazma Ve Okuma Islemleri
void PipeWrite(int fd[],char * msg)
{
close(fd[READ_END]);
write(fd[WRITE_END], msg ,strlen(msg)+1);
close(fd[WRITE_END]);
printf("\nData Send-Write Pipe: %s",msg);
wait(NULL);
}
char* PipeRead(int fd[], char*msg)
{
close(fd[WRITE_END]);
read(fd[READ_END], msg ,BUFFER_SIZE);
close(fd[READ_END]);
printf("\nData Read Pipe-Close Pipe: %s",msg);
return msg;
}
//Metini dort parcaya threadler icin ayiriyoruz.
void textPartition(char* tempText)
{
int len = strlen(tempText);
for(int i=0;i<4;i++)
{
createArray(&part[i],len,1);
}
int a=0, counter=0;
while(a<len)
{
if(a<len/4)
addArray(&part[0],tempText[a]);
else if(a>=len/4 && a<(len/4)*2)
addArray(&part[1],tempText[a]);
else if(a>=(len/4)*2 && a<(len/4)*3)
addArray(&part[2],tempText[a]);
else
addArray(&part[3],tempText[a]);
a++;
}
}
//Shared Memory Yazma Ve Okuma
void sharedMemorySender(void* shared_memory, char* buff, int shmid)
{
shmid = shmget((key_t)1122,1024,0666|IPC_CREAT);
shared_memory = shmat(shmid,NULL,0);
strcpy(shared_memory,buff);
printf("\nData Write Shared Memory Area: %s",(char *)shared_memory);
}
char* sharedMemoryReceiver(void *shared_memory,int shmid)
{
shmid = shmget((key_t)1122,1024,0666);
shared_memory = shmat(shmid,NULL,0);
printf("\nRead Data Shared Memory Area: %s", (char *)shared_memory);
return (char *)shared_memory;
}
int main()
{
char write_msg[BUFFER_SIZE], read_msg[BUFFER_SIZE], tempText[BUFFER_SIZE];
int pass=0;
//Pipe ve Thread Islemi Icin Tanimlar
int fd[2];
pid_t pid;
//Shared Memory Kullanimi Icin Gerekli Tanimlar
void *shared_memory;
char buff[BUFFER_SIZE];
int shmid=0;
//Pipe'in Kontrolu
if(pipe(fd)==-1)
{
fprintf(stderr,"Pipe Failed!");
return 1;
}
//Parent-Child iliskisi olusturmak icin process'i catalliyoruz(fork).
pid = fork();
//Fork'un Kontrolu
if(pid<0)
{
fprintf(stderr,"Fork Failed!");
return 1;
}
//Parent Process (pid=1)
if(pid>0)
{
//Sifrelenecek olan metini ve kaydirma degerini kullanicidan aliyoruz.
getText(write_msg);
PipeWrite(fd,write_msg);
//Shared Memory'den Alip Sifrelemeyi Cozuyoruz.
part[0].text = sharedMemoryReceiver(shared_memory,shmid);
part[0].choise = 0;
printf("\n\nCrypted Text: %s",part[0].text);
CrypteOperations(&part[0]);
printf("\nDecrypted Text: %s",part[0].text);
pthread_exit(NULL);
}
//Child Process (pid=0)
else
{
//Thread Olusturuyoruz
pthread_t tid[4];
//Pipe uzerinden veriyi okuduk ve tempText icerisine attik.
strcpy(tempText, PipeRead(fd,read_msg));
textPartition(tempText);
for(int i=0;i<4;i++)
{
pthread_create(&(tid[i]), NULL, CrypteOperations, (void *)&part[i]);
}
for(int a=0;a<4;a++)
{
pthread_join(tid[a], NULL);
}
for(int i=1;i<4;i++)
{
for(int a=0;a<part[i].indis;a++)
{
addArray(&part[0],part[i].text[a]);
}
}
sharedMemorySender(shared_memory, part[0].text ,shmid);
pthread_exit(NULL);
}
return 0;
}