Index: trunk/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- trunk/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 640)
+++ trunk/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 652)
@@ -1669,4 +1669,6 @@
   }
 
+
+  // Define each env variable in runing environment
   maps *curs = getMaps (m, "env");
   if (curs != NULL)
@@ -1728,7 +1730,5 @@
 #endif
 
-  /**
-   * Need to check if we need to fork to load a status enabled 
-   */
+  // Need to check if we need to fork to load a status enabled 
   r_inputs = NULL;
   map *store = getMap (request_inputs, "storeExecuteResponse");
@@ -1787,6 +1787,4 @@
   _tmpMaps->name = zStrdup ("lenv");
   char tmpBuff[100];
-  semid lid = getShmLockId (NULL, 1);
-  lockShm (lid);
   struct ztimeval tp;
   if (zGettimeofday (&tp, NULL) == 0)
@@ -1794,7 +1792,5 @@
   else
     sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
-  unlockShm (lid);
-  removeShmLock (NULL, 1);
-  _tmpMaps->content = createMap ("usid", tmpBuff);
+  _tmpMaps->content = createMap ("osid", tmpBuff);
   _tmpMaps->next = NULL;
   sprintf (tmpBuff, "%i", cpid);
@@ -1802,4 +1798,5 @@
   char* tmpUuid=get_uuid();
   addToMap (_tmpMaps->content, "uusid", tmpUuid);
+  addToMap (_tmpMaps->content, "usid", tmpUuid);
   free(tmpUuid);
   addToMap (_tmpMaps->content, "status", "0");
@@ -1910,5 +1907,5 @@
     }
 #endif
-  char *fbkp, *fbkp1;
+  char *fbkp, *fbkpid, *fbkp1, *flog;
   FILE *f0, *f1;
   if (status != NULL)
@@ -1977,25 +1974,47 @@
 	   * process answer to http client.
 	   */
-#ifndef WIN32
-          zSleep (1);
-#endif
-          r_inputs = getMapFromMaps (m, "lenv", "usid");
+	  map* usid = getMapFromMaps (m, "lenv", "uusid");
+          r_inputs = getMapFromMaps (m, "lenv", "osid");
           int cpid = atoi (r_inputs->value);
           r_inputs = getMapFromMaps (m, "main", "tmpPath");
-          //map *r_inputs1 = getMap (s1->content, "ServiceProvider");
-		  map* r_inputs1 = createMap("ServiceName", s1->name);
-
+	  map* r_inputs1 = createMap("ServiceName", s1->name);
+
+	  // Create PID file referencing the OS process identifier
+          fbkpid =
+            (char *)
+            malloc ((strlen (r_inputs->value) +
+                     strlen (usid->value) + 7) * sizeof (char));
+          sprintf (fbkpid, "%s/%s.pid", r_inputs->value, usid->value);
+
+          f0 = freopen (fbkpid, "w+", stdout);
+	  fprintf(stdout,"%d",getpid());
+	  fflush(stdout);
+
+	  // Create SID file referencing the semaphore name
           fbkp =
             (char *)
             malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
-                     1024) * sizeof (char));
-          sprintf (fbkp, "%s/%s_%d.xml", r_inputs->value, r_inputs1->value,
-                   cpid);
-          char *flog =
+                     strlen (usid->value) + 7) * sizeof (char));
+          sprintf (fbkp, "%s/%s.sid", r_inputs->value, usid->value);
+
+          FILE* f2 = fopen (fbkp, "w+");
+	  map* tmpm=getMapFromMaps (m, "lenv", "osid");
+	  fprintf(f2,"%s",tmpm->value);
+	  fflush(f2);
+	  fclose(f2);
+	  free(fbkp);
+
+          fbkp =
             (char *)
             malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
-                     1024) * sizeof (char));
-          sprintf (flog, "%s/%s_%d_error.log", r_inputs->value,
-                   r_inputs1->value, cpid);
+                     strlen (usid->value) + 7) * sizeof (char));
+          sprintf (fbkp, "%s/%s_%s.xml", r_inputs->value, r_inputs1->value,
+                   usid->value);
+          flog =
+            (char *)
+            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
+                     strlen (usid->value) + 13) * sizeof (char));
+          sprintf (flog, "%s/%s_%s_error.log", r_inputs->value,
+                   r_inputs1->value, usid->value);
 #ifdef DEBUG
           fprintf (stderr, "RUN IN BACKGROUND MODE \n");
@@ -2005,19 +2024,5 @@
 #endif
           freopen (flog, "w+", stderr);
-          semid lid = getShmLockId (m, 1);
           fflush (stderr);
-          if (lid < 0)
-            {
-              return errorException (m, _("Lock failed"),
-			      "InternalError", NULL);
-            }
-          else
-            {
-              if (lockShm (lid) < 0)
-                {
-		  return errorException (m, _("Lock failed"),
-					 "InternalError", NULL);
-                }
-            }
           f0 = freopen (fbkp, "w+", stdout);
           rewind (stdout);
@@ -2036,15 +2041,18 @@
                                 request_output_real_format);
           fflush (stdout);
-          unlockShm (lid);
+#ifdef RELY_ON_DB
+	  init_sql(m);
+	  recordServiceStatus(m);
+	  recordResponse(m,fbkp);
+#endif
           fflush (stderr);
           fbkp1 =
             (char *)
             malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
-                     1024) * sizeof (char));
-          sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value,
-                   r_inputs1->value, cpid);
+                     strlen (usid->value) + 13) * sizeof (char));
+          sprintf (fbkp1, "%s/%s_final_%s.xml", r_inputs->value,
+                   r_inputs1->value, usid->value);
 
           f1 = freopen (fbkp1, "w+", stdout);
-          free (flog);
 
 	  if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
@@ -2062,6 +2070,6 @@
 	    free (tmpmaps);
 	    fflush (stdout);
-	    unlockShm (lid);
 	    fflush (stderr);
+	    unhandleStatus (m);
 	    return -1;
 	  }
@@ -2073,8 +2081,8 @@
       else
         {
-      /**
-       * error server don't accept the process need to output a valid 
-       * error response here !!!
-       */
+	  /**
+	   * error server don't accept the process need to output a valid 
+	   * error response here !!!
+	   */
           eres = -1;
           errorException (m, _("Unable to run the child process properly"),
@@ -2109,14 +2117,16 @@
       fclose (stdout);
       fclose (stderr);
-    /**
-     * Dump back the final file fbkp1 to fbkp
-     */
+      /**
+       * Dump back the final file fbkp1 to fbkp
+       */
       fclose (f0);
       fclose (f1);
       FILE *f2 = fopen (fbkp1, "rb");
+#ifndef RELY_ON_DB
       semid lid = getShmLockId (m, 1);
       if (lid < 0)
         return -1;
       lockShm (lid);
+#endif
       FILE *f3 = fopen (fbkp, "wb+");
       free (fbkp);
@@ -2129,9 +2139,17 @@
       fclose (f2);
       fclose (f3);
-      unlockShm (lid);
+      unlink (fbkpid);
+#ifndef RELY_ON_DB
+      removeShmLock (m, 1);
+#else
+      recordResponse(m,fbkp1);
+#endif
       unlink (fbkp1);
+      unlink (flog);
+      unhandleStatus (m);
+      free(fbkpid);
+      free (flog);
       free (fbkp1);
       free (tmps1);
-      unhandleStatus (m);
     }
 
