~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/port/qnx4/tstsem.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * tstsem.c
 
4
 *        Test of System V Semaphore Emulation
 
5
 *
 
6
 * Copyright (c) 1999, repas AEG Automation GmbH
 
7
 *
 
8
 *
 
9
 * IDENTIFICATION
 
10
 *        $PostgreSQL: pgsql/src/backend/port/qnx4/tstsem.c,v 1.10 2003-11-29 19:51:54 pgsql Exp $
 
11
 *
 
12
 *-------------------------------------------------------------------------
 
13
 */
 
14
 
 
15
#include "postgres.h"
 
16
 
 
17
#include <signal.h>
 
18
#include <unistd.h>
 
19
#include "storage/ipc.h"
 
20
#include <sys/mman.h>
 
21
#include <sys/sem.h>
 
22
 
 
23
 
 
24
#define SEMMAX  16
 
25
#define OPSMAX  1
 
26
 
 
27
int                     MaxBackends = SEMMAX;
 
28
 
 
29
static int      semid;
 
30
 
 
31
static void
 
32
sig_handler(int sig_no)
 
33
{
 
34
        union semun arg;
 
35
        int                     i = semctl(semid, 0, GETNCNT, arg);
 
36
 
 
37
        if (i == -1)
 
38
                perror("semctl");
 
39
        else
 
40
                printf("semval = %d\n", i);
 
41
}
 
42
 
 
43
void            on_proc_exit(void (*function) (), Datum arg)
 
44
{
 
45
        atexit(function);
 
46
}
 
47
int
 
48
main(int argc, char **argv)
 
49
{
 
50
        int                     c,
 
51
                                errflg = 0;
 
52
        char            s[80];
 
53
        key_t           key = IPC_PRIVATE;
 
54
        int                     nsems = SEMMAX;
 
55
        int                     semflg = 0;
 
56
        int                     unlink = 0;
 
57
        int                     i;
 
58
        struct sembuf sops[OPSMAX];
 
59
        u_short         array[SEMMAX];
 
60
        union semun arg;
 
61
 
 
62
        optarg = NULL;
 
63
        while (!errflg && (c = getopt(argc, argv, "k:n:cxu")) != -1)
 
64
        {
 
65
                switch (c)
 
66
                {
 
67
                        case 'k':
 
68
                                key = atoi(optarg);
 
69
                                break;
 
70
                        case 'n':
 
71
                                nsems = atoi(optarg);
 
72
                                break;
 
73
                        case 'c':
 
74
                                semflg |= IPC_CREAT;
 
75
                                break;
 
76
                        case 'x':
 
77
                                semflg |= IPC_EXCL;
 
78
                                break;
 
79
                        case 'u':
 
80
                                unlink = 1;
 
81
                                break;
 
82
                        default:
 
83
                                errflg++;
 
84
                }
 
85
        }
 
86
        if (errflg)
 
87
        {
 
88
                printf("usage: tstsem [-k key] [-n nsems] [-cxu]\n");
 
89
                exit(1);
 
90
        }
 
91
 
 
92
        if (unlink)
 
93
        {
 
94
                i = shm_unlink("SysV_Sem_Info");
 
95
                if (i == -1)
 
96
                        perror("shm_unlink");
 
97
                exit(i);
 
98
        }
 
99
 
 
100
        semid = semget(key, nsems, semflg);
 
101
        if (semid == -1)
 
102
        {
 
103
                perror("semget");
 
104
                exit(semid);
 
105
        }
 
106
 
 
107
        /* test signal interrupts */
 
108
        signal(SIGTERM, sig_handler);
 
109
 
 
110
        do
 
111
        {
 
112
                printf("(-)sem_op, (+)sem_op, (G)ETVAL, (S)ETVAL, GET(P)ID, GET(A)LL, SETA(L)L, GET(N)CNT, GET(Z)CNT, e(x)it: ");
 
113
                scanf("%s", s);
 
114
                switch (s[0])
 
115
                {
 
116
                        case '-':
 
117
                        case '+':
 
118
                                sops[0].sem_num = 0;
 
119
                                sops[0].sem_op = atoi(s);
 
120
                                if (sops[0].sem_op == 0)
 
121
                                        sops[0].sem_op = s[0] == '+' ? +1 : -1;
 
122
                                sops[0].sem_flg = 0;
 
123
                                if (semop(semid, sops, 1) == -1)
 
124
                                        perror("semop");
 
125
                                break;
 
126
 
 
127
                        case 'G':
 
128
                        case 'g':
 
129
                                i = semctl(semid, 0, GETVAL, arg);
 
130
                                if (i == -1)
 
131
                                        perror("semctl");
 
132
                                else
 
133
                                        printf("semval = %d\n", i);
 
134
                                break;
 
135
 
 
136
                        case 'S':
 
137
                        case 's':
 
138
                                printf("semval = ");
 
139
                                scanf("%d", &arg.val);
 
140
                                if (semctl(semid, 0, SETVAL, arg) == -1)
 
141
                                        perror("semctl");
 
142
                                break;
 
143
 
 
144
                        case 'P':
 
145
                        case 'p':
 
146
                                i = semctl(semid, 0, GETPID, arg);
 
147
                                if (i == -1)
 
148
                                        perror("semctl");
 
149
                                else
 
150
                                        printf("PID = %d\n", i);
 
151
                                break;
 
152
 
 
153
                        case 'A':
 
154
                        case 'a':
 
155
                                arg.array = array;
 
156
                                i = semctl(semid, 0, GETALL, arg);
 
157
                                if (i == -1)
 
158
                                        perror("semctl");
 
159
                                else
 
160
                                {
 
161
                                        for (i = 0; i < nsems; i++)
 
162
                                                printf("semval[%d] = %hu\n", i, arg.array[i]);
 
163
                                }
 
164
                                break;
 
165
 
 
166
                        case 'L':
 
167
                        case 'l':
 
168
                                arg.array = array;
 
169
                                for (i = 0; i < nsems; i++)
 
170
                                {
 
171
                                        printf("semval[%d] = ", i);
 
172
                                        scanf("%hu", &arg.array[i]);
 
173
                                }
 
174
                                if (semctl(semid, 0, SETALL, arg) == -1)
 
175
                                        perror("semctl");
 
176
                                break;
 
177
 
 
178
                        case 'N':
 
179
                        case 'n':
 
180
                                i = semctl(semid, 0, GETNCNT, arg);
 
181
                                if (i == -1)
 
182
                                        perror("semctl");
 
183
                                else
 
184
                                        printf("semval = %d\n", i);
 
185
                                break;
 
186
 
 
187
                        case 'Z':
 
188
                        case 'z':
 
189
                                i = semctl(semid, 0, GETZCNT, arg);
 
190
                                if (i == -1)
 
191
                                        perror("semctl");
 
192
                                else
 
193
                                        printf("semval = %d\n", i);
 
194
                                break;
 
195
                }
 
196
        }
 
197
        while (s[0] != 'x');
 
198
 
 
199
        if (semctl(semid, 0, IPC_RMID, arg) == -1)
 
200
        {
 
201
                perror("semctl");
 
202
                exit(-1);
 
203
        }
 
204
 
 
205
        exit(0);
 
206
}