Article View: comp.sys.amiga.tech
Article #5393Re: Amiga semaphores (HELP!)
From: Jim_Garrison
Date: Fri, 21 Sep 1990 16:10
Date: Fri, 21 Sep 1990 16:10
86 lines
2941 bytes
2941 bytes
Hello again,
I tried adding the LocalAddSemaphores() which John Suchs suggested to my
program. It still doesn't work! Please note that I am using NAMED semaphores
here, maybe that's what's causing my problems. Even after I have run this
program once (or more) FindSemaphore(name) still returns NULL. Do named
semaphores work? Or am I still not initializing my named semaphore
incorrectly? What I want to happen is that if two of these programs are run
at one time, only one will get (Create) the semaphore and the other will wait
(sleep) until the first one calls ReleaseSemaphore(sem). This way, I can
queue up any number of copies of this program, and each one will wait it's
turn for the resource (the printer) which cannot be shared.
Thanks again,
Jim Garrison (jimg@hpiatmh)
Hewlett Packard
Industrial Applications Center
(408) 746-5349
PS - I have sent in for the ordering information, so I can get the autodocs.
Sample program follows:
/* IPC.C - This is a test program, so I can see how Amiga semaphores work.
*
* 09/13/90 - Created by Jim Garrison.
* 09/18/90 - Added in LocalAddSemaphore() suggested by John Suchs.
*/
#include <stdio.h>
#include <libraries/dosextens.h>
#include <exec/execbase.h>
#include <exec/semaphores.h>
#include <exec/memory.h>
extern ExecBase *SysBase;
struct IntuitionBase *IB;
main()
{
struct SignalSemaphore *sem;
int error;
/* This next ALWAYS returns a NULL, no matter how many times I run this
* program. */
sem = FindSemaphore("test1");
if (!sem)
{
/* I'm typing this in from memory, so this next line might not be right
* but I am doing it correctly in the code on my Amiga at home. */
if ((IB = OpenLibrary("intuition.library", LIBRARY_REV)) == NULL)
exit(1);
sem = AllocMem(sizeof(struct SignalSemaphore), MEMF_PUBLIC | MEMF_CLEAR);
printf("Making a semaphore\n");
LocalAddSemaphore(sem, "test1");
}
else
printf("Got an existing semaphore\n");
ObtainSemaphore(sem);
printf("Pausing ....\n");
/* Do something with some resource. */
ReleaseSemaphore(sem);
printf("Done\n");
}
void LocalAddSemaphore(struct SignalSemaphore *SignalSemaphore, char *name)
{
SignalSemaphore->ss_Link.ln_Type=NT_SIGNALSEM;
SignalSemaphore->ss_Link.ln_Name = name;
SignalSemaphore->ss_Link.ln_Pri = 1;
InitSemaphore(SignalSemaphore);
Forbid();
Enqueue(&SysBase->SemaphoreList,SignalSemaphore);
Permit();
}
Technical Details
Message-ID:
<2290006@hp-ptp.HP.COM>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!hplabs!hpcc05!hp-ptp!jimgReferences:
<2290004@hp-ptp.HP.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!hplabs!hpcc05!hp-ptp!jimg From: jimg@hp-ptp.HP.COM (Jim_Garrison) Newsgroups: comp.sys.amiga.tech Subject: Re: Amiga semaphores (HELP!) Message-ID: <2290006@hp-ptp.HP.COM> Date: 21 Sep 90 16:10:58 GMT References: <2290004@hp-ptp.HP.COM> Organization: HP Pacific Technology Park - Sunnyvale, Ca. Lines: 86