-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_reader_writer_writer.c
More file actions
71 lines (35 loc) · 985 Bytes
/
first_reader_writer_writer.c
File metadata and controls
71 lines (35 loc) · 985 Bytes
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
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string.h>
#include<stdlib.h>
#define SEG_SIZE 1024
typedef struct Data70{
sem_t critical70;
int cnt70;
}SharedData70;
SharedData70 *data_ptr70;
void *writer(void *arg)
{
sem_wait(&(data_ptr70->critical70));
data_ptr70->cnt70 = data_ptr70->cnt70+1;
printf("cnt value modified by Writer ,new cnt = %d\n",data_ptr70->cnt70);
sem_post(&(data_ptr70->critical70));
}
int main()
{
pthread_t write[5];
key_t key = ftok("shmfile",65);
int shmid70= shmget(key, SEG_SIZE, 0666|IPC_CREAT);
data_ptr70 = (SharedData70*)shmat(shmid70,NULL,0);
for(int i = 0; i <5; i++) {
pthread_create(&write[i], NULL, (void *)writer, NULL);
}
for(int i = 0; i <5; i++) {
pthread_join(write[i], NULL);
}
sem_destroy(&(data_ptr70->critical70));
return 0;
}