-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinclass8.c
171 lines (137 loc) · 3.81 KB
/
inclass8.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/sem.h>
#define SIZE 16
int semaphore_create()
{
/* create a new semaphore set for use by parent and child processes */
int semID = semget(IPC_PRIVATE, 1, S_IWUSR);
if (semID < 0)
{
perror("semget: failed to create\n");
exit(EXIT_FAILURE);
}
/* initialize the semaphore set referenced by the previosly obtained semID handle */
if (semctl(semID, 0, SETVAL, 1) < 0)
{
perror("semctl: initialiation failure\n");
exit(EXIT_FAILURE);
}
return semID;
}
void semaphore_wait(int semaphoreID)
{
struct sembuf p = { 0, -1, SEM_UNDO};
if (semop(semaphoreID, &p, 1) < 0)
{
perror("semop: wait failed\n");
exit(EXIT_FAILURE);
}
}
void semaphore_signal(int semaphoreID)
{
struct sembuf v = { 0, +1, SEM_UNDO};
if (semop(semaphoreID, &v, 1) < 0)
{
perror("semop: wait failed\n");
exit(EXIT_FAILURE);
}
}
void semaphore_removal(int semaphoreID)
{
if (semctl(semaphoreID, 0, IPC_RMID) < 0)
{
perror("Unable to remove semaphore\n");
exit(EXIT_FAILURE);
}
}
int main(void)
{
int status;
long int i, loop, temp, *sharedMemoryPointer;
int sharedMemoryID;
pid_t pid;
/* printing values out */
printf("value of IRUSR: %o\n", S_IRUSR);
printf("value of IWUSR: %o\n", S_IWUSR);
printf("value of IXUSR: %o\n", S_IXUSR);
printf("value of IRGRP: %o\n", S_IRGRP);
printf("value of IWGRP: %o\n", S_IWGRP);
printf("value of IXGRP: %o\n", S_IXGRP);
printf("value of IROTH: %o\n", S_IROTH);
printf("value of IWOTH: %o\n", S_IWOTH);
printf("value of IXOTH: %o\n", S_IXOTH);
/* value of 0600, radix is octal */
printf("value of S_IWUSR | S_IRUSR: %o\n", S_IWUSR | S_IRUSR);
/* error: permission was denied when creating a semaphore with only S_IRUSR */
/* pass: program runs when creating a semaphore with only S_IWUSR */
/* controlled process synch */
sharedMemoryID = shmget(IPC_PRIVATE, SIZE, IPC_CREAT | S_IRUSR | S_IWUSR);
if(sharedMemoryID < 0)
{
perror("Unable to obtain shared memory\n");
exit(EXIT_FAILURE);
}
sharedMemoryPointer = shmat(sharedMemoryID, 0, 0);
if(sharedMemoryPointer == (void *) -1)
{
perror ("Unable to attach\n");
exit (1);
}
int semID = semaphore_create();
sharedMemoryPointer[0] = 10;
sharedMemoryPointer[1] = 11;
pid = fork();
if(pid < 0)
{
printf("Fork failed\n");
}
if(pid == 0)
{
/* wait our turn */
semaphore_wait(semID);
/* critical section */
temp = sharedMemoryPointer[0];
sharedMemoryPointer[0] = sharedMemoryPointer[1];
sharedMemoryPointer[1] = temp;
/* signal our turn's over */
semaphore_signal(semID);
if(shmdt(sharedMemoryPointer) < 0)
{
perror ("Unable to detach\n");
exit (1);
}
exit(0);
}
else
{
/* wait our turn */
semaphore_wait(semID);
/* critical section */
temp = sharedMemoryPointer[0];
sharedMemoryPointer[0] = sharedMemoryPointer[1];
sharedMemoryPointer[1] = temp;
/* signal our turn's over */
semaphore_signal(semID);
}
wait(&status);
printf("Values: %li\t%li\n", sharedMemoryPointer[0], sharedMemoryPointer[1]);
semaphore_removal(semID);
if(shmdt(sharedMemoryPointer) < 0)
{
perror("Unable to detach\n");
exit(EXIT_FAILURE);
}
if(shmctl(sharedMemoryID, IPC_RMID, 0) < 0)
{
perror("Unable to deallocate\n");
exit(EXIT_FAILURE);
}
return 0;
}