]>
Commit | Line | Data |
---|---|---|
d4a28ab0 CW |
1 | /***************************************************************************** |
2 | Excerpt from "Linux Programmer's Guide - Chapter 6" | |
3 | (C)opyright 1994-1995, Scott Burkett | |
4 | ***************************************************************************** | |
5 | MODULE: semstat.c | |
6 | ***************************************************************************** | |
7 | A companion command line tool for the semtool package. semstat displays | |
8 | the current value of all semaphores in the set created by semtool. | |
9 | *****************************************************************************/ | |
bd695173 CW |
10 | /* |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License | |
13 | * as published by the Free Software Foundation; either version 2 | |
14 | * of the License, or (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
24 | * | |
25 | * See the COPYING file for license information. | |
26 | */ | |
27 | ||
28 | #if HAVE_CONFIG_H | |
29 | # include "config.h" | |
30 | #endif | |
d4a28ab0 | 31 | |
d2b03e6a | 32 | #include "common.h" |
bd695173 CW |
33 | |
34 | #if HAVE_SYS_IPC_H | |
35 | # include <sys/ipc.h> | |
36 | #endif | |
37 | #if HAVE_SYS_SEM_H | |
38 | # include <sys/sem.h> | |
39 | #endif | |
d4a28ab0 CW |
40 | |
41 | /* arg for semctl system calls. */ | |
42 | union semun { | |
43 | int val; /* value for SETVAL */ | |
44 | struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */ | |
45 | ushort *array; /* array for GETALL & SETALL */ | |
46 | struct seminfo *__buf; /* buffer for IPC_INFO */ | |
47 | void *__pad; | |
48 | }; | |
49 | ||
50 | int get_sem_count(int sid); | |
51 | void show_sem_usage(int sid); | |
52 | int get_sem_count(int sid); | |
53 | void dispval(int sid); | |
54 | ||
55 | int main(int argc, char *argv[]) | |
56 | { | |
57 | key_t key; | |
58 | int semset_id; | |
59 | ||
60 | /* Create unique key via call to ftok() */ | |
61 | key = ftok(".", 's'); | |
62 | ||
63 | /* Open the semaphore set - do not create! */ | |
64 | if((semset_id = semget(key, 1, 0666)) == -1) | |
65 | { | |
66 | printf("Semaphore set does not exist\n"); | |
67 | exit(1); | |
68 | } | |
69 | ||
70 | show_sem_usage(semset_id); | |
71 | return(0); | |
72 | } | |
73 | ||
74 | void show_sem_usage(int sid) | |
75 | { | |
76 | int cntr=0, maxsems, semval; | |
77 | ||
78 | maxsems = get_sem_count(sid); | |
79 | ||
80 | while(cntr < maxsems) { | |
81 | semval = semctl(sid, cntr, GETVAL, 0); | |
82 | printf("Semaphore #%d: --> %d\n", cntr, semval); | |
83 | cntr++; | |
84 | } | |
85 | } | |
86 | ||
87 | int get_sem_count(int sid) | |
88 | { | |
89 | int rc; | |
90 | struct semid_ds mysemds; | |
91 | union semun semopts; | |
92 | ||
93 | /* Get current values for internal data structure */ | |
94 | semopts.buf = &mysemds; | |
95 | ||
96 | if((rc = semctl(sid, 0, IPC_STAT, semopts)) == -1) { | |
97 | perror("semctl"); | |
98 | exit(1); | |
99 | } | |
100 | ||
101 | /* return number of semaphores in set */ | |
102 | return(semopts.buf->sem_nsems); | |
103 | } | |
104 | ||
105 | void dispval(int sid) | |
106 | { | |
107 | int semval; | |
108 | ||
109 | semval = semctl(sid, 0, GETVAL, 0); | |
110 | printf("semval is %d\n", semval); | |
111 | } |