2aa66ee03d5e450c6bcdfbc94586b8c8babfaf45
[gapil.git] / sources / IPCTestId.c
1 /* IPCTestId.c
2  * 
3  * Copyright (C) 2001 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program IPCTestId.c: 
22  * Program to test IPC identifiers
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: ipctestid -h give all info's
28  *
29  * $Id: IPCTestId.c,v 1.2 2002/08/19 17:34:23 piccardi Exp $
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
35 #include <errno.h>       /* error definitions and routines */ 
36 #include <stdlib.h>      /* C standard library */
37 #include <unistd.h>      /* unix standard library */
38 #include <stdio.h>       /* standard I/O library */
39 #include <string.h>      /* string functions */
40 #include <sys/ipc.h>     /* SysV IPC functions */
41 #include <sys/msg.h>     /* SysV Message Queue */
42 #include <sys/sem.h>     /* SysV Semaphores */
43 #include <sys/shm.h>     /* SysV Shared Memory */
44
45 #include "macros.h"      /* My macros */
46 /* Help printing routine */
47 void usage(void);
48
49 int main(int argc, char *argv[])
50 {
51 /* 
52  * Variables definition  
53  */
54     int i; 
55     int n = 5;                                        /* default is 10 tryes */
56     char type='q';                                  /* default is use queues */
57     int id;
58     /*
59      * Input section: decode command line parameters 
60      * Use getopt function
61      */
62     opterr = 0;  /* don't want writing to stderr */
63     while ( (i = getopt(argc, argv, "hqsmn:")) != -1) {
64         switch (i) {
65         /* 
66          * Handling options 
67          */ 
68         case 'h':   /* help option */
69             printf("Wrong -h option use\n");
70             usage();
71             return -1;
72             break;
73         case 'q':   /* Message Queue */
74             debug("Message Queue\n");
75             type = i;
76             break;
77         case 's':   /* Semaphore */
78             debug("Semaphore\n");
79             type = i;
80             break;
81         case 'm':   /* Shared Memory */
82             debug("Shared Memory\n");
83             type = i;
84             break;
85         case 'n':   /* Number of tryes */
86             debug("Number of tryes\n");
87             n = strtol(optarg, NULL, 10);
88             break;
89         default:    /* should not reached */
90             usage();
91         }
92     }
93     /* ***********************************************************
94      * 
95      *           Options processing completed
96      *
97      *                Main code beginning
98      * 
99      * ***********************************************************/
100     switch (type) {
101     case 'q':   /* Message Queue */
102         debug("Message Queue Try\n");
103         for (i=0; i<n; i++) {
104             id = msgget(IPC_PRIVATE, IPC_CREAT|0666);
105             printf("Identifier Value %d \n", id);
106             msgctl(id, IPC_RMID, NULL);
107         }
108         break;
109     case 's':   /* Semaphore */
110         debug("Semaphore\n");
111         for (i=0; i<n; i++) {
112             id = semget(IPC_PRIVATE, 1, IPC_CREAT|0666);
113             printf("Identifier Value %d \n", id);
114             semctl(id, 0, IPC_RMID);
115         }
116         break;
117     case 'm':   /* Shared Memory */
118         debug("Shared Memory\n");
119         for (i=0; i<n; i++) {
120             id = shmget(IPC_PRIVATE, 1000, IPC_CREAT|0666);
121             printf("Identifier Value %d \n", id);
122             shmctl(id, IPC_RMID, NULL);
123         }
124         break;
125     default:    /* should not reached */
126         return -1;
127     }
128     return 0;
129 }
130 /*
131  * routine to print usage info and exit
132  */
133 void usage(void) {
134     printf("Program ipctestid : test IPC identifier number assignment\n");
135     printf("Usage:\n");
136     printf("  ipctestid [-h] [-qsm] [-n N] \n");
137     printf("  -h     print this help\n");
138     printf("  -q     try for message queue identifiers\n");
139     printf("  -s     try for semaphore identifiers\n");
140     printf("  -m     try for shared memory identifiers\n");
141     printf("  -n XX  try XX times\n");
142     
143     exit(1);
144 }
145