]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/ipc/semstat.c
Make coding style consistent throughout
[cygwin-apps/cygutils.git] / src / ipc / semstat.c
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 *****************************************************************************/
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
31
32 #include "common.h"
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
40
41 /* arg for semctl system calls. */
42 union semun
43 {
44 int val; /* value for SETVAL */
45 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
46 ushort *array; /* array for GETALL & SETALL */
47 struct seminfo *__buf; /* buffer for IPC_INFO */
48 void *__pad;
49 };
50
51 int get_sem_count (int sid);
52 void show_sem_usage (int sid);
53 int get_sem_count (int sid);
54 void dispval (int sid);
55
56 int
57 main (int argc, char *argv[])
58 {
59 key_t key;
60 int semset_id;
61
62 /* Create unique key via call to ftok() */
63 key = ftok (".", 's');
64
65 /* Open the semaphore set - do not create! */
66 if ((semset_id = semget (key, 1, 0666)) == -1)
67 {
68 printf ("Semaphore set does not exist\n");
69 exit (1);
70 }
71
72 show_sem_usage (semset_id);
73 return (0);
74 }
75
76 void
77 show_sem_usage (int sid)
78 {
79 int cntr = 0, maxsems, semval;
80
81 maxsems = get_sem_count (sid);
82
83 while (cntr < maxsems)
84 {
85 semval = semctl (sid, cntr, GETVAL, 0);
86 printf ("Semaphore #%d: --> %d\n", cntr, semval);
87 cntr++;
88 }
89 }
90
91 int
92 get_sem_count (int sid)
93 {
94 int rc;
95 struct semid_ds mysemds;
96 union semun semopts;
97
98 /* Get current values for internal data structure */
99 semopts.buf = &mysemds;
100
101 if ((rc = semctl (sid, 0, IPC_STAT, semopts)) == -1)
102 {
103 perror ("semctl");
104 exit (1);
105 }
106
107 /* return number of semaphores in set */
108 return (semopts.buf->sem_nsems);
109 }
110
111 void
112 dispval (int sid)
113 {
114 int semval;
115
116 semval = semctl (sid, 0, GETVAL, 0);
117 printf ("semval is %d\n", semval);
118 }
This page took 0.041283 seconds and 5 git commands to generate.