Category : C Source Code
Archive   : SEM10.ZIP
Filename : RWSDEMO.C

 
Output of file : RWSDEMO.C contained in archive : SEM10.ZIP
/* Citadel */
/* 241 East Eleventh Street * Brookville, IN 47012 * 317-647-4720 */
/* BBS 317-647-2403 */

/* #ident "rwsdemo.c 1.1 - 89/10/16" */

/* select operating system */
#define UNIX (1)
#define MSDOS (2)
#define HOST MSDOS

#include
#include
#if HOST == UNIX
#include "syscalkr.h"
void exit();
#define EXIT_SUCCESS (0)
#define EXIT_FAILURE (1)
#elif HOST == MSDOS
#include
#include
#define S_IPERM (S_IREAD | S_IWRITE | S_IEXEC)
#endif
#include "rwsem.h"

#define min(a,b) (((a) < (b)) ? (a) : (b))

#define RWSDIR ("rwsdir") /* r/w semaphore set directory */
#define RWSC (10) /* r/w semaphore count */

/*man---------------------------------------------------------------------------
NAME
rwsemdemo - rwsem demo program

SYNOPSIS
rwsdemo

DESCRIPTION
Demonstation program for rwsem routines. Creates a read/write
semaphore set and allows interactive manipulation of locks.
This demonstration is most effective if two instances of rwsdemo
are run simultaneously, either in two windows or on two
terminals.

SEE ALSO
rwsem, semaphor.

------------------------------------------------------------------------------*/
int main(argc, argv)
int argc;
char *argv[];
{
char lockheld[RWSC]; /* locks held by calling process */
int rwsno = 0; /* r/w semaphore number */
rwsset_t *rwsp = NULL; /* rwsem pointer */
char buf[256]; /* input buffer */
int n = 0; /* scanf return */
char cmd[2]; /* command */

/* initialize lockheld array */
for (rwsno = 0; rwsno < RWSC; rwsno++) {
lockheld[rwsno] = 'U';
}

/* open r/w semaphore set */
rwsp = rwsopen(RWSDIR, RWS_CREAT | S_IPERM, RWSC);
if (rwsp == NULL) {
perror("rwsopen");
exit(EXIT_FAILURE);
}

/* main loop */
while (1) {
/* display states of locks held by calling process */
printf("\nlocks held: ");
for (rwsno = 0; rwsno < min(RWSC, rwscount(rwsp)); rwsno++) {
printf("[%d-%c] ", rwsno, (int)lockheld[rwsno]);
}
/* get command */
printf("\nCommand: ");
gets(buf);
n = sscanf(buf, "%1s%d", cmd, &rwsno);
*cmd = toupper(*cmd);
if ((n == 1) && (*cmd == 'Q')) {
break;
}
if (n != 2) {
printf("Invalid entry. Enter U, R, or W followed by a r/w semaphore number, or Q to quit.\n");
continue;
}
if ((rwsno < 0) || (rwsno >= min(RWSC, rwscount(rwsp)))) {
printf("r/w semaphore number must be in the range [0,%d].\n", min(RWSC, rwscount(rwsp)));
continue;
}
/* perform specified operation */
switch (*cmd) {
case 'U': /* unlock */
printf("Unlocking r/w semaphore number %d.\n", rwsno);
if (rwslock(rwsp, rwsno, RWS_UNLCK) == -1) {
perror("rwslock");
exit(EXIT_FAILURE);
}
lockheld[rwsno] = 'U';
break;
case 'R': /* read lock */
printf("Read locking r/w semaphore number %d.\n", rwsno);
if (rwslock(rwsp, rwsno, RWS_RDLCK) == -1) {
if (errno != EAGAIN) {
perror("rwslock");
exit(EXIT_FAILURE);
}
printf("Read lock on r/w semaphore %d is blocked.\n", rwsno);
} else {
lockheld[rwsno] = 'R';
}
break;
case 'W': /* write lock */
printf("Write locking r/w semaphore number %d.\n", rwsno);
if (rwslock(rwsp, rwsno, RWS_WRLCK) == -1) {
if (errno != EAGAIN) {
perror("rwslock");
exit(EXIT_FAILURE);
}
printf("Write lock on r/w semaphore %d is blocked.\n", rwsno);
} else {
lockheld[rwsno] = 'W';
}
break;
default:
printf("Invalid entry. Enter U, R, or W followed by a r/w semaphore number, or Q to quit.\n");
continue;
break;
}
}

/* release all locks */
for (rwsno = 0; rwsno < min(RWSC, rwscount(rwsp)); rwsno++) {
if (rwslock(rwsp, rwsno, RWS_UNLCK) == -1) {
perror("rwslock");
exit(EXIT_FAILURE);
}
}

/* close r/w semaphore set */
if (rwsclose(rwsp) == -1) {
perror("rwsclose");
exit(EXIT_FAILURE);
}

printf("Do you wish to remove the read/write semaphore set?");
n = scanf("%1s", cmd);
if (toupper(*cmd) == 'Y') {
printf("Removing read/write semaphore set %s.\n", RWSDIR);
if (rwsremove(RWSDIR) == -1) {
perror("rwsremove");
exit(EXIT_FAILURE);
}
}

exit(EXIT_SUCCESS);
}


  3 Responses to “Category : C Source Code
Archive   : SEM10.ZIP
Filename : RWSDEMO.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/