[PHP] Re: [PHP-DEV] Re: Re: sysvsem extention question

2001-08-24 Thread Tom May

Chris Chabot <[EMAIL PROTECTED]> writes:

> Hey Jason, i do see your point.
> 
> However, something still illudes me in my reasoning i think..
> 
> In a web envirioment, you are most likely to be in one of two situations when using 
>semaphores.
> 
> - Plain standard lock (with ability of doing resource count)
> - All web servers connect to a external process that handles a service (like printer)
> - The web processes them selves are the 'external resource' which handle the 
>decreasing of lock-count
> 
> The notes in the original source code of the php extention explained that the 
>second+third lock were used
> for:
> - Resource count
> - Be able to set initial max-resource count
> 
> However, when i follow this reasoning, two things come to mind
> - Resource count is a API provided by the sysvsem implimentation via semctl (# 
>waiting, etc)
> - if you try to set the semaphore's resource count, and it fails (other process 
>connected to it and locked
> it?) then wouldnt it be safe to assume that that 'other process' is another web 
>process, which sets the same
> resource counters... so we end up with an good situation anyways...
> 
> So if you would do a semget with IPC_CREATE + IPC_EXCL. If this succeeds, do the 
>SETALL/SETVAL routine via
> semctl, if it fails on EEXISTS, do a second semget without create+excl and attach to 
>it..

I haven't looked at the code for years, but I may have been thinking
that the semaphore lives on after all the processes using it have
died.  So, if you change the allowed resource usage in your php code
and restart the server, you want the new value to take effect and not
be stuck with the old value from the last run.

> Donno if it would work, or how-much overhead it would add, but it sounds like it 
>could ;-)
> 
> -- Chris
> 
> 
> Jason Greene wrote:
> 
> > - Original Message -
> > From: "Sascha Schumann" <[EMAIL PROTECTED]>
> > To: "Jason T.Greene" <[EMAIL PROTECTED]>
> > Cc: "Tom May" <[EMAIL PROTECTED]>; "Chris Chabot" <[EMAIL PROTECTED]>; 
><[EMAIL PROTECTED]>;
> > <[EMAIL PROTECTED]>
> > Sent: Friday, August 24, 2001 1:49 AM
> > Subject: Re: [PHP-DEV] Re: Re: sysvsem extention question
> >
> > > > parent process right before fork.  There is no parent
> > > > initializer for a php module author
> > >
> > > MINIT() is called from the parent process in forking servers.
> > > The mm storage handler uses this hook to instantiate a shared
> > > memory segment and propagate the handle to all child
> > > processes.
> > Sascha is right...
> > Allow me to reword..
> > There is no way to allow a php file author the ability to execute php source during
> > module startup of a forking web server. The module would have to allocate and
> > initialize all semaphores before the php source is parsed. This defeats the purpose
> > of a semaphore extension in a forking webserver environment, becuase the php source
> > author would be limited to just the semaphors allocated by the php module.
> >
> > -Jason
> >
> > > - Sascha Experience IRCG
> > >   http://schumann.cx/http://schumann.cx/ircg
> > >

Tom.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Re: sysvsem extention question

2001-08-21 Thread Tom May

First off, before you get the wrong impression of my answer, let me
say that your observation "It all seems kinda kludgy and quick-hack
for a specific > project, and not 'sysv semaphores implimented in
php'" is right on.

Chris Chabot <[EMAIL PROTECTED]> writes:

> I have been playing with the idea of making some application backend
> services using pcntl (for singaling and fork)+ sysvsem's (for
> synchronisation).

Cool.  But see below.

> However while reading thru the sysvsem source code, i noticed the
> behaviour of the semaphores in PHP is basicly as a 'lock' (as one would
> use a file lock w/ flock()). From what i understand from my advanced
> unix programming book, and the linux man pages, this is not what
> semaphores are supposed to be (though they can be abused to be so).
>
> The way one would -expect- semaphores to function on a unix platform
> is best described in "Linux Programmers Guide" at
> 
>http://uiarchive.uiuc.edu/mirrors/ftp/ftp.ibiblio.org/pub/Linux/docs/linux-doc-project/programmers-guide/lpg-0.4.pdf
> (page 46 and on).
> 
> A short sumary would be: a semaphore is a resource counter (not an
> absolute lock).

A lock is just a degenerate case.  A slightly less degenerate case
(that can't be implemented with flock) is when you want to allow N
users of the resource instead of just one.

> The main this is that a process other then the process that acquired
> the semaphore can decrease the semaphore count, thus releasing it for
> another client. Another big difference is that a process can set the
> semaphore count lower then zero (0 == locked). This sometimes can be
> usefull for Client / Server synchonisation.
> 
> Example usage for a typical Client / Server program flow using
> semaphores / signals :

I admit I haven't looked at this hard enough to understand all the
details, but could message queues help you out here?  You could get
higher throughput since the server fills the buffer and calls msgsnd()
beforehand, whereas in your signalling implementation clients will
stall between the time they signal the server and the time the server
has filled a buffer for them.  Also, you need some additional overhead
to manage the buffer handoff from client to server.  And doing things
in signal handlers can get dicey.

If a message queue isn't big enough for you, you can pass references
to shared memory in your messages.

> Server :
> create sem
> sem acquire
> setup envirioment
> fork children
> 
> Multiple Clients:
> repeat (wait for sem acquire) {
> send signal to Server
> communicate with server, get 'buffer'
> process buffer
> }
> 
> Server:
> 
> while (Fill data into buffer) {
> semaphore release (!);
> Sleep(infinite or untill time out);
> }
> 
> -> Sleeps untill interupted by signal (signal send by Client/Child)
> -> In signal handler:
> Send 'buffer' to client that acquired semaphore
> return;
>will cause the program to go back to main loop, sleep was interupted,
> so goes to while (fill buffer) again. Also note that the Client -never-
> calls semaphore release. The server does that once the resource is
> available again.
> 
> Rinse and Repeat till end of time, or end of data :-) This will
> distribute the data to be processed over all the different clients
> (since semaphores guarantee a linear processing of clients, so all
> clients get there equal share).
> 
> Last, i don't see why the implimentation as exists, requires 3
> semaphores.

I don't remember why either.  I did that code somewhere around the end
of 1998 . . .

> It all seems kinda kludgy and quick-hack for a specific
> project, and not 'sysv semaphores implimented in php'. (please forgive
> my rude assumptions)

Bingo.

> Does the maintainer (Tom May) want to work on this, or anyone else? I'd
> be happy to help out, but my last C coding days are over 6 years behind
> me, so i don't feel very confident to lead this project.. So any/all
> help and/or comments would be apreciated.

I no longer use php or maintain any of the stuff I helped with (aside
from answering the occaisional question, like this one.

> Also i noticed a potential security hole in the exisiting source, at
> line 190 of sysvsem.c it uses
> 
> semget(key, 3, perm|IPC_CREATE);
> 
> However, perm is not zero'd to 7 bits before being used, thus allowing
> extra flags being added to the call, which presumably is unintentional,
> since it allows nasty flags to be passed to this call. perm is gotton
> from arg_perm, which is a lval. What i imagine you 'should' do is zer