News:

OS2World.com Forum is back !!!

Remember to visit OS2World at:
http://www.os2world.com

Main Menu

4OS2 aliases and Session IDs

Started by Anton Monroe, July 13, 2026, 04:21:34 PM

Previous topic - Next topic

Anton Monroe

Has anyone noticed that 4OS2 aliases sometimes get lost or changed unexpectedly? It has been annoying me for years. I found the reason, but I haven't found a good solution.

Once in awhile one of my 4OS2 sessions suddenly loses its aliases. Usually a detached cron job that reports "unknown command" errors in its log file, but a couple of times I have been typing in a 4OS2 window when my aliases suddenly disappeared. Or rather, they were replaced with a different set of aliases that I use for batch files.
That was the clue.

I discovered it only happens if OS/2 assigns the same Session ID number to two 4OS2 sessions.
At boot, my startup.btm uses 'detach' to start several daemons, including a Cron daemon.
So part of the process list looks like this:


  [Type]    [SID]          [PID]  Process Name
  ______    _____          _____  ____________
              [0]            [0]  God, I suppose
      FS      [0]              |______[1]  SYSINIT
      FS      [1]              |      |_____[27]  F:\OS2\PMSHELL.EXE
      PM     [16]              |             |_____[32]  F:\OS2\PMSPOOL.EXE
      PM     [18]              |             |_____[34]  F:\OS2\PMSHELL.EXE
     VIO     [19]              |             |_____[35]  F:\SYS\APPS\4OS2\4OS2.EXE
      .       .                .
      .       .                .
      .       .                .
      DT     [17]              |_____[47]  F:\USR\SBIN\CUPSD.EXE                        (cupsd)
      DT     [19]              |____[104]  F:\USR\SBIN\DHCLIENT.EXE                     (dhclient)
      DT     [19]              |____[114]  F:\PROGRAMS\NDFS\NDCTL.EXE                   (ndctl)
      DT     [19]              |____[138]  F:\SYS\APPS\4OS2\4OS2.EXE                    (crond)



where PID 35 is the VIO window of startup.btm, with a Session ID of 19 and a window title of "[Init]".
Startup.btm detaches the DHCLIENT, NDCTL, and 4OS2 processes; note that they all have the same SID as startup.btm. The Cron daemon, PID 138, is a Rexx script called Cron.cmd, so the process name shows as "4OS2.EXE". I leave the [Init] window open after startup.btm finishes.

The problem comes when Cron.cmd detaches a 4OS2 session to run a batch file. That session is also given that same SID of 19. Most of my batch files start with

    setlocal
    unalias *
    <assign a new set of aliases suitable for use in batch files>

But the new aliases also take effect in the [Init] VIO window. When the Cron job finishes with 'endlocal',
the [Init] window's original aliases are restored.

Even worse, say I close the [Init] window. Then I open a new 4OS2 VIO window from the desktop. Sometimes, at unpredictable times, the new window is given an SID of 19 (and 4OS2's %_shell level is > 0). I don't understand why OS/2 does that. If the SID is 19, then the new window's aliases change whenever a Cron job starts. If I keep the [Init] window open, that seems to block OS/2 from assigning SID 19 to any new VIO window. Detached processes started by Cron.cmd are still given SID 19.

Running the Cron.cmd script under cmd.exe also gives duplicate SIDs. I have not tried another Cron daemon.

So my current workaround for avoiding alias conflicts is to leave the [Init] window open but unused, and to try to schedule my Cron jobs so they don't overlap.

Does anyone have ideas about this?  Apparently I do not understand what a "session" is. What is the difference between a "process" and a "session"? Should OS/2 be giving out duplicate Session IDs to unrelated processes?

Is there anything 4OS2 could do differently?  Somehow 4OS2 manages to limit the environment list to a single process. Could it do the same with the alias and history lists? I could open a ticket on the 4OS2 bug tracker, but I thought more people would see it if I asked here first.

No, I do not used shared aliases; this happens if and only if two processes have the same SID.

To demonstrate, try the attached todetach.btm. type
    echo %_sid
    alias
        --to see your SID and existing aliases
    detach todetach.btm
    alias
        --shows that your aliases have been changed, and that the detached
          process has the same SID
    set
        --shows that the environment has not changed
        --after 30 seconds, the detached batchfile will restore aliases
          with 'endlocal', beep, and exit
    alias
        --will now show that your original alias list has been restored
    history
        --will show that the detached batchfile added a command to the shared history
          (permanently, because history is not affected by setlocal/endlocal)

Oops, the message board will not let me attach a *.btm file. That's rude. Renamed it to todetach.cmd.




Dave Yeo

About sessions, from the Workplace Shell Programming Guide's glossary,
Quote
session -(1) A routing mechanism for user interaction via the console; a complete environment that determines how an application runs and how users interact with the application. OS/2 can manage more than one session at a time, and more than one process can run in a session. Each session has its own set of environment variables that determine where OS/2 looks for dynamic-link libraries and other important files. (2) (D of C) In the OS/2 operating system, one instance of a started program or command prompt. Each session is separate from all other sessions that might be running on the computer. The operating system is responsible for coordinating the resources that each session uses, such as computer memory, allocation of processor time, and windows on the screen.

Wonder if using a full screen session for certain stuff would be a workaround?

Steven Levine

Anton,

This behavior appears to be by design.  The relevant code is

c\os2init.c:304
    // create an extension consisting of the session number
    if ( gpIniptr->LocalAliases != 0 )
        sprintf( strend( szAliasSharename ), ".%03d", gpLIS->sgCurrent );

If you were using shared aliases, the shared memory segment would be named:

h\shralias.h:4
#define SHR_4OS2_ALIAS "\\SHAREMEM\\JP4OS2_A"

I suspect the reason for using session id rather than process id is so that processes started like

  4os2 /c ...

would inherit the aliases and so on.  You should be able to avoid non-unique session id by using start to start a new session.


Anton Monroe

Dave:

Definition (2) "one instance of a started program or command prompt" sounds like what I had assumed that "session" meant, but apparently it doesn't apply in this case. I am very puzzled by the way OS/2 assigns session IDs, but after all these years it is probably considered working as designed.

A full-screen session might help a little-- from some brief testing it looks like the full-screen sessions have single-digit SIDs starting at 5.  The sessions detached by startup.btm would share that SID, so any VIO windows I open later should have higher SIDs that would not cause an alias conflict, and I could safely close my startup.btm session. I normally never use full-screen anyway.  There would still be potential conflicts between overlapping Cron jobs.

Steve:

I suspected that changing anything in 4OS2 might break existing behavior that other people depend on, but it was worth asking. If anyone else notices the same problem, reading this thread might at least save them some trouble tracking it down.

Starting a new session with 'start' does the same thing as starting 4OS2 from the desktop-- OS/2 sometimes assigns it the same session ID as my detached sessions. I cannot use 'start' from a detached session, it gives
    SYS0418: The call to DosSMRegisterDD is not allowed.

Of course, I could run Cron.cmd in a window on the desktop and use 'start' to launch the jobs, but that seems contrary to the idea of a cron service running in the background.

RXU.DLL has some functions with names like RxExecPgm, RxStartSession, RxDetachRexxPgm, etc that might be worth investigating (?)

Thanks

Steven Levine

Hi Anton,

A change to using something like the process id to name the shared memory segment, would be a breaking change, but that is not necessarily a show stopper.  The behavior could be controlled by a 4OS2.INI configuration variable.

Also, 4OS2 is open source and easy to build with OpenWatcom.  You could do a
modified private build that handles your specific requirements.

However, I don't think using the process id is a solution for what you want to do.  Newly started processes would have a different segment name and would not inherit the aliases and history from the invoking process.

Another possible solution is to allow the memory segment name to be defined on the command line.  Your cooperating processes would arrange for the segment name to be passed to the new process.  This is similar to how we
handle REXX queues when we need to have multiple active queues.

A 4OS2 command like

  SHARENAME seg-name

should be relatively easy to implement.  Most of the work would be determining if the new segment already exists, and if not copying the content of the old shared memory segment to the new segment.

There's also the edge case where both the old and the new segment exist and have content.  This would need to be thought through.

Initially I was somewhat surprised that you were getting what appeared to be duplicate session ids,  but once I thought it the reason is clear.  Under the covers, you are starting your detached programs with with DosExecPgm. The docs call these programs detached processes which is correct.  They run in the same screen group as the process that started them.

DosStartSession does not support starting "detached" sessions.  It's easy to think of detached programs as separate sessions, but they are not.

The docs could explain this in more detail

I recommend you do some testcase-ing just to verify my thinking.  I suspect you are going to find that all your detached programs share the same screen group as the parent session, whatever that was.

If I were doing something similar to what you are trying to do, I would run cron minimized and have it start the detached processes via an intermediate transient session.  Your detached processes would get unique session ids.

What needs to be verified if this technique is to work as expected is that the session id is sticky.  I've never checked to see what happens when a transient session closes.  The session manager may keep the screen group alive until all the processes assigned to the screen group terminate or it may reassign the detached process to another screen group.

FWIW, there's nothing wrong with running cron detached, but as you have found there are tradeoffs required.

Anton Monroe

Thanks, Steve

I'll keep tinkering with it but I may be getting in over my head. If all else fails I'll fall back on running Cron minimized.

Lars

#6
This sounds a little like the "setenv"/"getenv" C functions vs. OS/2 global environment issue. C programs will inherit the OS/2 global environment and can use "getenv" to read these environment variables but under the hoods, these are a local copy of the OS/2 global environment. Using "setenv" will only change the local copy of the environment, it will never overwrite the OS/2 global environment. The OS/2 global environment is treated as "read-only" and I think this is a good thing.

In my opinion, a subordinate process should never be able to change the aliases of a parent process.
If a process overwrites aliases, then the aliases should be copied from the shared memory segment to a private memory segment and the process should use that.
An easier approach would be for the subordinate process to copy the whole shared memory segment to a private segment and continue reading/writing/manipulating aliases on that private segment.

Anton Monroe

Lars,

That reminds me of another puzzlement-- why does a detached session show a Parent Process ID of 0, rather than the PID of the process that started it? Add PPID to the list of things I don't understand.

Steven Levine

Detached processes are assigned PPID 0 so that they are not affected by actions that non-detached process.  IMO, this was a design choice.  This probably could have been handled by flags in the PTDA, but the developer's choice is not unreasonable.  While it's true that kernel has no record of the creating process I've never run into the need to know this myself and if I did, it's something that an application could handle on its own.

In some ways this is similar to how the kernel handles DLLs.  The kernel does not expose the PID that loaded the DLL while it does for other file types.