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
|
/****************************************************************
* *
* Copyright (c) 2001-2018 Fidelity National Information *
* Services, Inc. and/or its subsidiaries. All rights reserved. *
* *
* This source code contains the intellectual property *
* of its copyright holder(s), and is made available *
* under a license. If you do not know the terms of *
* the license, please stop and do not read further. *
* *
****************************************************************/
/* semstat2.c - display more information about semaphores (than semstat did)
*
* semval - current value of the semaphore
* semncnt - count of processes waiting for the semaphore value to
* become greater than its current value.
* semzcnt - count of processes waiting for the semaphore value to
* become zero.
* sempid - last process id to perform an operation on the
* semaphore.
*
* Usage: semstat2 <semid1> <semid2> ... <semidn>
*/
#include "main_pragma.h"
#include <errno.h>
#include <sys/types.h>
#include "gtm_ipc.h"
#include <sys/sem.h>
#include "gtm_sem.h"
#include "gtm_stdlib.h"
#include "gtm_stdio.h"
#undef EXIT
#define EXIT exit /* Use system "exit" (not gtm_image_exit) directly since this is a standalone module */
#define OUT_LINE 80 + 1
static void usage (char *prog);
static void usage (char *prog)
{
FPRINTF(stderr, "%s: Report information about semaphores\n", prog);
FPRINTF(stderr, "\nUsage-\n");
FPRINTF(stderr, "\t%s <semid1> <semid2> ... <semidn>\n\n", prog);
FPRINTF(stderr, "information returned for each semaphore in set:\n");
FPRINTF(stderr, "\tsemval\t\tcurrent value of semaphore\n");
FPRINTF(stderr, "\tsemncnt\t\t# of procs waiting for semval to increase\n");
FPRINTF(stderr, "\tsemzcnt\t\t# of procs waiting for semval to become zero\n");
FPRINTF(stderr, "\tsempid\t\tPID of last proc performing operation on semaphore\n");
}
int main (int argc, char *argv[])
{
int i, j;
char s[OUT_LINE];
int sem, semval, semncnt, semzcnt, sempid;
struct sembuf sop;
struct semid_ds semstat;
union semun semarg;
if (argc == 1)
{
usage(argv[0]);
EXIT(EXIT_FAILURE);
}
semarg.buf = &semstat;
for(i=1; i< argc; i++)
{
sem = ATOI(argv[i]);
if ( semctl(sem, 0, IPC_STAT, semarg) == -1 )
{
FPRINTF(stderr, "Error obtaining semaphore status.\n");
SNPRINTF(s, OUT_LINE, "semctl(%d)", sem);
PERROR(s);
continue;
}
PRINTF("semid %d: %hu semaphores in the set\n", sem, (unsigned short int)semarg.buf->sem_nsems);
for(j=0; j < semarg.buf->sem_nsems; j++)
{
if ( (semval = semctl(sem, j, GETVAL)) == -1 )
{
FPRINTF(stderr, "Error obtaining semaphore %d value.\n", j);
SNPRINTF(s, OUT_LINE, "semctl(%d)", sem);
PERROR(s);
continue;
}
PRINTF("sem %d: (semval=%d, ", j, semval);
if ( (semncnt = semctl(sem, j, GETNCNT)) == -1 )
{
FPRINTF(stderr, "\nError obtaining semaphore %d ncnt.\n", j);
SNPRINTF(s, OUT_LINE, "semctl(%d)", sem);
PERROR(s);
continue;
}
PRINTF("semncnt=%d, ", semncnt);
if ( (semzcnt = semctl(sem, j, GETZCNT)) == -1 )
{
FPRINTF(stderr, "\nError obtaining semaphore %d zcnt.\n", j);
SNPRINTF(s, OUT_LINE, "semctl(%d)", sem);
PERROR(s);
continue;
}
PRINTF("semzcnt=%d, ", semzcnt);
if ( (sempid= semctl(sem, j, GETPID)) == -1 )
{
FPRINTF(stderr, "\nError obtaining semaphore %d PID.\n", j);
SNPRINTF(s, OUT_LINE, "semctl(%d)", sem);
PERROR(s);
continue;
}
PRINTF("sempid=%d)\n", sempid);
}
}
EXIT(EXIT_SUCCESS);
}
|