-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutil.c
More file actions
executable file
·160 lines (139 loc) · 4.14 KB
/
util.c
File metadata and controls
executable file
·160 lines (139 loc) · 4.14 KB
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
/*
; Copyright (C) 2003-2004 A.R.Karthick
; <a_r_karthic@users.sourceforge.net>
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;
;
; Utility routines used by some common fragments of the Code
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
#include "util.h"
#include "sem.h"
#define BUFFER 1024
/*Not used that much now as it uses a lot of stack.*/
void message(int exit_status,FILE *fptr,const char *fmt,...) {
char buffer[BUFFER+1];
va_list ptr;
va_start(ptr,fmt);
vsnprintf(buffer,BUFFER,fmt,ptr);
va_end(ptr);
fprintf(stderr,"%s\n",buffer);
if(exit_status)
exit(exit_status);
return ;
}
/*
Lock the access to the semaphore section of threads.
This is needed to avoid context switches in the midst of a semaphore operation.
*/
void sem_lock() {
SET_SEMOP(sem_ipcids);
}
/*Unlock the access to the semaphore section of threads.*/
void sem_unlock() {
CLEAR_SEMOP(sem_ipcids);
}
/* Initialise the semaphore section for threads. */
void sem_init() {
sem_ipcids.max_id = -1;
sem_ipcids.entries = 0;//number of semaphore entries
sem_ipcids.semop_ind = 0; //clear the semop indicator bit
sem_ipcids.t_sem_array = ALLOC_MEM(sem_ipcids.t_sem_array,T_SEMMSL,0);
return ;
}
/* Initialise the IPC section for threads.*/
void ipc_init() {
sem_init(); //initialise the semaphore section for threads
}
/* Note: There is no grow array needed as we dont allow greater than SEMMSL number of semaphores.Allocate an id for a sem array.*/
struct t_sem_array *ipc_alloc(struct t_sem_array *sem_array) {
int i;
if(sem_ipcids.entries >= T_SEMMSL )
goto out; //failed to allocate an entry
for(i=0;i<=sem_ipcids.max_id;++i) {
if(! sem_ipcids.t_sem_array[i] )
goto found;
}
if(i >= T_SEMMSL)
goto out; //id not found
found: //id has been found
++sem_ipcids.entries; //increment the entries
if(i > sem_ipcids.max_id) sem_ipcids.max_id = i; //set the max.id
sem_ipcids.t_sem_array[i] = sem_array;
sem_array->id = i; //set the id of the semaphore
return sem_array;
out:
return NULL; //id not found
}
struct t_sem_array *ipc_find(int key) {
int i;
for(i=0;i<=sem_ipcids.max_id;++i) {
if(sem_ipcids.t_sem_array[i] && sem_ipcids.t_sem_array[i]->key == key)
goto found;
}
goto out; //not found
found :
return sem_ipcids.t_sem_array[i];
out:
return NULL;
}
/* Free up an ipc resource */
struct t_sem_array *ipc_free(int id) {
int i;
struct t_sem_array *ptr;
for(i=0;i<=sem_ipcids.max_id; ++i) {
if(sem_ipcids.t_sem_array[i] && sem_ipcids.t_sem_array[i]->id == id) {
sem_ipcids.t_sem_array[i]->key = -1; //invalidate the key
goto found;
}
}
goto out; //not found
found:
ptr = sem_ipcids.t_sem_array[i];
sem_ipcids.t_sem_array[i] = NULL;
--sem_ipcids.entries; //decrement the no. of entries
if( i == sem_ipcids.max_id) {
//readjust the max id of the semaphore
do {
--sem_ipcids.max_id ;
if(sem_ipcids.max_id < 0 ) break;
}while(! sem_ipcids.t_sem_array[sem_ipcids.max_id] );
}
return ptr;
out :
return NULL;
}
struct t_sem_array *ipc_get(int id) {
int i;
for(i=0;i<=sem_ipcids.max_id;++i) {
if(sem_ipcids.t_sem_array[i] && sem_ipcids.t_sem_array[i]->id == id)
goto found;
}
goto out;
found :
return sem_ipcids.t_sem_array[i];
out:
return NULL;
}
/* Release the t_sem_array for the IPC */
void ipc_release() {
if(sem_ipcids.t_sem_array)
free((void*)sem_ipcids.t_sem_array);
return ;
}