/
project3.c
196 lines (126 loc) · 2.99 KB
/
project3.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <semaphore.h>
#include <errno.h>
#include <signal.h>
#include <sys/shm.h>
#define TEXT_SZ 4096
struct shared_use_st
{
int pid;
int noOfProcess;
int read;
int dataWritten;
char text[TEXT_SZ];
};
void help(char * appName){
printf("Usage : %s\n",appName);
printf("type exit to terminate the program\n" );
return ;
}
void writeToShared(void * args,sem_t * sem_id){
struct shared_use_st * shared_stuff=(struct shared_use_st *)args;
char* in_line=NULL;
size_t read=0;
size_t capacity=0;
do{
read=getline(&in_line,&capacity,stdin);
if(strcmp(in_line,"exit\n")==0){
break;
}
if(read !=-1){
while(shared_stuff->dataWritten==1){
sleep(1);
}
sem_wait(sem_id);
shared_stuff->pid=getpid();
strncpy(shared_stuff->text,in_line,TEXT_SZ);
shared_stuff->dataWritten=1;
sem_post(sem_id);
}
}while(1);
}
void readFromShared(void * args,sem_t * sem_id){
struct shared_use_st * shared_stuff=(struct shared_use_st *)args;
pid_t localRead=0;
while(1){
if(shared_stuff->dataWritten ==1 && shared_stuff->pid != getppid() && localRead != shared_stuff->pid){
sem_wait(sem_id);
printf("Process %d sent : %s",(int)shared_stuff->pid,shared_stuff->text );
shared_stuff->read++;
if(shared_stuff->read==shared_stuff->noOfProcess-1){
shared_stuff->dataWritten=0;
shared_stuff->read=0;
}
localRead=shared_stuff->pid;
sem_post(sem_id);
}
if(shared_stuff->dataWritten==0){
localRead=0;
}
}
}
int main(int argc,char* argv[]){
if(argc>1){
if(strcmp(argv[1],"-h")==0){
help(argv[0]);
}
exit(EXIT_SUCCESS);
}
struct shared_use_st *shared_stuff;
void *shared_memory = (void *)0;
int shmid=0;
sem_t * sem_id;
pid_t child;
sem_id=sem_open("mysemaph", O_CREAT, 0600, 1);
if(sem_id == SEM_FAILED) {
perror("parent sem_open");
return;
}
shmid = shmget( (key_t)1234,sizeof(struct shared_use_st),0666 |IPC_CREAT );
if(shmid==-1){
perror("shmget: shmget failed");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid,(void *)0, 0);
if (shared_memory == (void *)-1) {
perror("shmat failed");
exit(EXIT_FAILURE);
}
shared_stuff = (struct shared_use_st *)shared_memory;
sem_wait(sem_id);
shared_stuff->noOfProcess++;
sem_post(sem_id);
if((child=fork())==-1){
perror("fork error");
exit(EXIT_FAILURE);
}
else if(child==0){
readFromShared(shared_stuff,sem_id);
}
else{
writeToShared(shared_stuff,sem_id);
kill(child,SIGKILL);
}
sem_wait(sem_id);
shared_stuff->noOfProcess--;
int num_process=shared_stuff->noOfProcess;
sem_post(sem_id);
if (shmdt(shared_memory) == -1)
{
perror("shmdt failed");
exit(EXIT_FAILURE);
}
if(num_process==0){
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
perror("shmctl error");
exit(EXIT_FAILURE);
}
}
exit(EXIT_SUCCESS);
}