-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.c
172 lines (131 loc) · 2.84 KB
/
server.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
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
//Functions Declarations
void myfunc (void* ptr);
void getMsg();
void printMsg();
//Semaphore mutex lock
sem_t mutex;
//Shared memory pointers
char *shm, *s;
//Array for messages between Client-Server
char a[100];
int main()
{
//For Shared memory
int shmid,i,size=30;
key_t key;
//For thread passing
char *wel = "\n ------- Chat-Room Server<->Client --------\n";
//Key for data segment created by server
key = 1000;
// For Thread
int t;
//Forking
int f = fork();
int processID = getpid();
int parentID = getppid();
//Thread variable
pthread_t thread1;
//Initializing semaphore indicated by sem
sem_init(&mutex,0,1);
//Locate data segment using key and get its id
if ((shmid = shmget(key, size, IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}
//Attach pointer with memory block
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
printf("\n");
//Calling thread
t=pthread_create(&thread1, NULL, (void*) &myfunc , (void*) wel);
//Joining thread
pthread_join(thread1, NULL);
//Printing Child and Parent Processes IDs
printf("\nProcess ID = %d\n",processID);
printf("Parent ID = %d\n\n",parentID);
if(f==0) //Child Process
{
*shm = '!';
while(*shm!='*')
{
getMsg();
if(a[0]!='*'){
s = shm+1;
for (i=0;i<strlen(a);i++)
*s++ = a[i];
*s = '\0';
*shm = '@';
while(*shm!='#'&&*shm!='*')
sleep(1);
if(*shm!='*'){
printMsg();
}
else
printf("Connection Closed by client");
}
else
*shm = '*';
}
}
else if(f>0) //Parent process
{
//Detach shared memory
shmid = shmdt(shm);
//Wait for child process to finish
waitpid(f,0,0);
}
else
{
printf("Fork Error");
}
//Destroying semaphore indicated by sem
sem_destroy(&mutex);
exit(0);
}
void myfunc (void *ptr) //Thread Function
{
char *a = (char*) ptr;
printf("%s", a);
pthread_exit(0);
}
void getMsg()
{
//Semaphore Locking
sem_wait(&mutex);
printf("Enter Message : ");
fgets(a, 100, stdin);
//Filing
FILE * fp;
fp = fopen ("write.txt", "a");
fprintf(fp, "%s", "Server : ");
fprintf(fp, "%s", a);
fprintf(fp, "%s", "\n");
fclose(fp);
//Filing Close
//Semaphore Unlocking
sem_post(&mutex);
}
void printMsg()
{
//Semaphore Locking
sem_wait(&mutex);
printf("Client : ");
for (s = shm+1; *s != '\0'; s++)
putchar(*s);
putchar('\n');
//Semaphore Unlocking
sem_post(&mutex);
}