System Functions (ss_xxx)
[The msystem.h & system.c]


Functions

midas_thread_t ss_thread_create (INT(*thread_func)(void *), void *param)
INT ss_thread_kill (midas_thread_t thread_id)
DWORD ss_millitime ()
DWORD ss_time ()
INT ss_sleep (INT millisec)


Function Documentation

DWORD ss_millitime  ) 
 

Returns the actual time in milliseconds with an arbitrary origin. This time may only be used to calculate relative times.

Overruns in the 32 bit value don't hurt since in a subtraction calculated with 32 bit accuracy this overrun cancels (you may think about!)..

...
DWORD start, stop:
start = ss_millitime();
  < do operations >
stop = ss_millitime();
printf("Operation took %1.3lf seconds\n",(stop-start)/1000.0);
...
Returns:
millisecond time stamp.

Definition at line 1979 of file system.c.

Referenced by bm_check_buffers(), bm_open_buffer(), close_buffers(), cm_cleanup(), cm_get_watchdog_info(), cm_set_watchdog_params(), cm_shutdown(), db_open_database(), dm_buffer_create(), register_equipment(), scan_fragment(), scheduler(), send_event(), and tr_stop().

INT ss_sleep INT  millisec  ) 
 

Suspend the calling process for a certain time.

The function is similar to the sleep() function, but has a resolution of one milliseconds. Under VxWorks the resolution is 1/60 of a second. It uses the socket select() function with a time-out. See examples in ss_time()

Parameters:
millisec Time in milliseconds to sleep. Zero means infinite (until another process calls ss_wake)
Returns:
SS_SUCCESS

Definition at line 2205 of file system.c.

Referenced by cm_shutdown(), main(), read_trigger_event(), and register_equipment().

midas_thread_t ss_thread_create INT(*)(void *)  thread_func,
void *  param
 

Execute command in a separate process, close all open file descriptors invoke ss_exec() and ignore pid.

{ ...
  char cmd[256];
  sprintf(cmd,"%s %s %i %s/%s %1.3lf %d",lazy.commandAfter,
     lazy.backlabel, lazyst.nfiles, lazy.path, lazyst.backfile,
     lazyst.file_size/1024.0/1024.0, blockn);
  cm_msg(MINFO,"Lazy","Exec post file write script:%s",cmd);
  ss_system(cmd);
}
...
\encode
@param command Command to execute.
@return SS_SUCCESS or ss_exec() return code
*/
INT ss_system(char *command)
{
#ifdef OS_UNIX
   INT childpid;

   return ss_exec(command, &childpid);

#else

   system(command);
   return SS_SUCCESS;

#endif
}

/**dox***************************************************************/
#ifndef DOXYGEN_SHOULD_SKIP_THIS

/*------------------------------------------------------------------*/
INT ss_exec(char *command, INT * pid)
/********************************************************************\

  Routine: ss_exec

  Purpose: Execute command in a separate process, close all open
           file descriptors, return the pid of the child process.

  Input:
    char * command    Command to execute
    INT  * pid        Returned PID of the spawned process.
  Output:
    none

  Function value:
    SS_SUCCESS       Successful completion
    SS_ABORT         fork() was not successful, or other problem

\********************************************************************/
{
#ifdef OS_UNIX

   /* only implemented for UNIX */
   int i, fd;

   if ((*pid = fork()) < 0)
      return SS_ABORT;
   else if (*pid != 0) {
      /* avoid <defunc> parent processes */
      signal(SIGCHLD, catch_sigchld);
      return SS_SUCCESS;        /* parent returns */
   }

   /* child continues here... */

   /* close all open file descriptors */
   for (i = 0; i < 256; i++)
      close(i);

   /* try and use up stdin, stdout and stderr, so other
      routines writing to stdout etc won't cause havoc */
   for (i = 0; i < 3; i++) {
      fd = open("/dev/null", O_RDWR, 0);
      if (fd < 0)
         fd = open("/dev/null", O_WRONLY, 0);
      if (fd < 0) {
         cm_msg(MERROR, "ss_exec", "Can't open /dev/null");
         return SS_ABORT;
      }
      if (fd != i) {
         cm_msg(MERROR, "ss_exec", "Did not get file descriptor");
         return SS_ABORT;
      }
   }

   setsid();                    /* become session leader */
   /* chdir("/"); *//* change working directory (not on NFS!) */
   umask(0);                    /* clear our file mode createion mask */

   /* execute command */
   execl("/bin/sh", "sh", "-c", command, NULL);

#else

   system(command);

#endif

   return SS_SUCCESS;
}

/**dox***************************************************************/
#endif                          /* DOXYGEN_SHOULD_SKIP_THIS */

/********************************************************************/
/**
Creates and returns a new thread of execution. 

Note the difference when calling from vxWorks versus Linux and Windows.
The parameter pointer for a vxWorks call is a VX_TASK_SPAWN structure, whereas
for Linux and Windows it is a void pointer.
Early versions returned SS_SUCCESS or SS_NO_THREAD instead of thread ID.

Example for VxWorks
\code
...
VX_TASK_SPAWN tsWatch = {"Watchdog", 100, 0, 2000,  (int) pDevice, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
midas_thread_t thread_id = ss_thread_create((void *) taskWatch, &tsWatch);
if (thread_id == 0) {
  printf("cannot spawn taskWatch\n");
}
...
Example for Linux
...
midas_thread_t thread_id = ss_thread_create((void *) taskWatch, pDevice);
if (thread_id == 0) {
  printf("cannot spawn taskWatch\n");
}
...
Parameters:
(*thread_func) Thread function to create.
param a pointer to a VX_TASK_SPAWN structure for vxWorks and a void pointer for Unix and Windows
Returns:
the new thread id or zero on error

Definition at line 1419 of file system.c.

Referenced by dm_buffer_create().

INT ss_thread_kill midas_thread_t  thread_id  ) 
 

Destroys the thread identified by the passed thread id. The thread id is returned by ss_thread_create() on creation.

...
midas_thread_t thread_id = ss_thread_create((void *) taskWatch, pDevice);
if (thread_id == 0) {
  printf("cannot spawn taskWatch\n");
}
...
ss_thread_kill(thread_id);
...
Parameters:
thread_id the thread id of the thread to be killed.
Returns:
SS_SUCCESS if no error, else SS_NO_THREAD

Definition at line 1493 of file system.c.

DWORD ss_time  ) 
 

Returns the actual time in seconds since 1.1.1970 UTC.

...
DWORD start, stop:
start = ss_time();
  ss_sleep(12000);
stop = ss_time();
printf("Operation took %1.3lf seconds\n",stop-start);
...
Returns:
Time in seconds

Definition at line 2046 of file system.c.

Referenced by al_trigger_alarm(), bm_compose_event(), cm_synchronize(), cm_time(), cm_yield(), db_get_key_time(), db_set_data(), db_set_data_index(), db_set_value(), scheduler(), and send_event().


Midas DOC Version 1.9.5 ---- PSI Stefan Ritt ----
Contributions: Pierre-Andre Amaudruz - Suzannah Daviel - Doxygen - Peter Green - Qing Gu - Greg Hackman - Gertjan Hofman - Paul Knowles - Rudi Meier - Glenn Moloney - Dave Morris - John M O'Donnell - Konstantin Olchanski - Renee Poutissou - Andreas Suter - Jan M.Wouters - Piotr Adam Zolnierczuk