-
Notifications
You must be signed in to change notification settings - Fork 0
/
sem-lockunlock.c
199 lines (175 loc) · 4.2 KB
/
sem-lockunlock.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
/*
* Copyright (C) 1999, 2001, 2005, 2008, 2013 by Manfred Spraul.
* All rights reserved except the rights granted by the GPL.
*
* Redistribution of this file is permitted under the terms of the GNU
* General Public License (GPL) version 3 or later.
*/
#include <sys/sem.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <unistd.h>
#define SEM_LOCKUNLOCK_VERSION "0.01"
#define TRUE 1
#define FALSE 0
union semun {
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo* __buf;
};
#define barrier() __asm__ __volatile__("": : : "memory")
int g_loops;
int g_busy_in;
int g_busy_out;
int g_sem;
int g_completedsem;
static void thread_fnc(int id)
{
int i;
volatile int j;
int res;
struct sembuf sop[1];
for (i=0;i<g_loops;i++) {
sop[0].sem_num=id;
sop[0].sem_op=-1;
sop[0].sem_flg=0;
res = semop(g_sem,sop,1);
if(res==-1) {
printf("semop -1 failed, errno %d.\n", errno);
return;
}
for(j=0;j<g_busy_in;j++);
barrier();
sop[0].sem_num=id;
sop[0].sem_op=1;
sop[0].sem_flg=0;
res = semop(g_sem,sop,1);
if(res==-1) {
printf("semop +1 failed, errno %d.\n", errno);
return;
}
for(j=0;j<g_busy_out;j++);
barrier();
}
sop[0].sem_num=g_completedsem;
sop[0].sem_op=-1;
sop[0].sem_flg=IPC_NOWAIT;
res = semop(g_sem,sop,1);
if(res==-1) {
printf("semop -1 on completedsem returned %d, errno %d.\n", res, errno);
return;
}
return;
}
int main(int argc,char** argv)
{
int nsems;
int tasks;
int res;
pid_t *pids;
unsigned short *psems;
struct timeval t_before, t_after;
unsigned long long delta;
union semun arg;
int i;
printf(
"sem-lockunlock %s <sems> <tasks> <loops> <busy-in> <busy-out>\n",
SEM_LOCKUNLOCK_VERSION);
if(argc != 6) {
printf("Invalid parameters.\n");
printf("\n");
printf(" Sem-lockunlock create threads that perform lock/unlock with multiple sysv semaphores\n");
printf(" in one semaphore array.\n");
printf(" It is not guaranteed that each lock/unlock cycle causes\n");
printf(" a reschedule.\n");
printf(" No cpu binding is performed.\n");
printf(" \n");
return 1;
}
nsems=atoi(argv[1]);
tasks=atoi(argv[2]);
g_loops=atoi(argv[3]);
g_loops = (g_loops+tasks-1)/tasks;
g_busy_in=atoi(argv[4]);
g_busy_out=atoi(argv[5]);
g_completedsem = nsems;
res = semget(IPC_PRIVATE, nsems+1, 0777 | IPC_CREAT);
if(res == -1) {
printf(" create failed.\n");
return 1;
}
g_sem = res;
fflush(stdout);
pids = malloc(sizeof(pid_t)*tasks);
for (i=0;i<tasks;i++) {
res = fork();
if (res == 0) {
thread_fnc(i%nsems);
exit(0);
}
if (res == -1) {
printf("fork() failed, errno now %d.\n", errno);
return 1;
}
pids[i] = res;
}
printf("sem-lockunlock: using a semaphore array with %d semaphores.\n", nsems);
printf("sem-lockunlock: using %d tasks.\n", tasks);
printf("sem-lockunlock: each thread loops %d times\n", g_loops);
printf("sem-lockunlock: each thread busyloops %d loops outside and %d loops inside.\n", g_busy_out, g_busy_in);
fflush(stdout);
psems = malloc(sizeof(unsigned short)*nsems);
for (i=0;i<nsems;i++)
psems[i] = 1;
psems[i] = tasks;
{
struct sembuf sop[1];
gettimeofday(&t_before, NULL);
arg.array = psems;
semctl(g_sem, 0, SETALL, arg);
sop[0].sem_num=g_completedsem;
sop[0].sem_op=0;
sop[0].sem_flg=0;
res = semop(g_sem,sop,1);
if(res==-1) {
printf("semop 0 failed, errno %d.\n", errno);
return 1;
}
gettimeofday(&t_after, NULL);
}
for (i=0;i<tasks;i++) {
res = waitpid(pids[i], NULL, 0);
if (res != pids[i]) {
printf("waitpid() failed, errno now %d.\n", errno);
return 1;
}
}
delta = t_after.tv_sec - t_before.tv_sec;
delta = delta*1000000L;
delta += t_after.tv_usec - t_before.tv_usec;
printf("total execution time: %Ld.%03Ld%03Ld seconds for %d loops\n",
(delta/1000000),
(delta/1000)%1000,
(delta)%1000,
tasks*g_loops);
delta = delta*1000;
delta = delta/(tasks*g_loops);
printf("per loop execution time: %Ld.%03Ld usec\n",
(delta/1000),
(delta)%1000);
res = semctl(g_sem, 1, IPC_RMID, arg);
if(res == -1) {
printf(" semctl failed.\n");
return 1;
}
return 0;
}