// Main function
int main(int argc, char* argv[]) {
    int i;          // Iterator
    
    // We create a set of 1 semaphore    
    // ftok generates a key based on the program name and a char value
    // This avoid to pick an arbitrary key already existing
    semId = semget(ftok(argv[0], 'A'), 1, IPC_CREAT | PERMS);
    
    // Set the semaphore at index 0 to value 1 (= available for use)
    initSem(semId, 0, 1);
    
    pthread_t thread[N_THREAD];    // Array of threads

    // Creating threads
    for (i = 0 ; i < N_THREAD ; i++) {
        pthread_create(&thread[i], NULL, deposit, NULL);
    }

    // Wait until threads are all complete
    for (i = 0 ; i < N_THREAD ; i++) {
        pthread_join(thread[i], NULL);
    }
    printf("Account/N_THREAD(%d): %d\n", N_THREAD, account/N_THREAD);
    semctl(semId, 0, IPC_RMID, 0);    // Free the semaphore
    return 0;
}
示例#2
0
serverADT createServer()
{
	int semid;

	serverADT server = malloc(sizeof(struct serverCDT));
	if (server == NULL)
		return NULL;

	if ( (server->sockFd = socket(AF_UNIX, SOCK_DGRAM,0) ) == -1)
	{
		perror("Socket call failed");
		free(server);
		return NULL;
	}

	semid = initSem(1);
	if (semid == -1) {
		free(server);
		fprintf(stderr, "Server: Semaphore initialization failed.");
		return NULL;
	}
	server->semid = semid;

	return server;	
}
static void initialiserSemaphores(char *argv0, int *sem1, int *sem2,
				  int *sem3) {
  int init[] = { 1, 1, 0 };

  initSem(3, argv0, init);
  *sem1 = 0;
  *sem2 = 1;
  *sem3 = 2;
}
示例#4
0
int bmount(const char *camino) {
    fichero = open(camino, O_RDWR | O_CREAT, 0666);
    if (fichero < 0) {
        printf("bloques.c > bmount \n");
        return -1;
    }
    semaforo = initSem();
    return fichero;
}
示例#5
0
int main()
{
    RunQ = (struct queue *) malloc(sizeof(struct queue));
    empty = (struct semaphore *) malloc(sizeof(struct semaphore));
    full = (struct semaphore *) malloc(sizeof(struct semaphore));
    mutex = (struct semaphore *) malloc(sizeof(struct semaphore));
    initQueue(RunQ);
    initSem(full, 0);
    initSem(empty, N);
    initSem(mutex, 1);

    start_thread(&prod);
    start_thread(&cons);
    start_thread(&prod2);
    start_thread(&cons2);
    run();


    return 0;
}
示例#6
0
文件: main.c 项目: jungsagacity/ftp
int main()
{

    // monitor analysis center,update the gloable variable uploadList, is always running
    pid_t p_analysisCenterMonitor;
    // start up uoload function,transfer data from  analysis center to products&service center
    pid_t p_upload;
    // start up download function,transfer data from  data center to analysis center
    //pid_t download;
    // log file uploading or downloading action state whether success or failed
    pid_t p_log;
    // the most important thread, the other is actived by it,expect analysisCenterMonitorTread.
    pid_t p_timingTask;


    config("system.ini");

    traverseList();

    //initilise upload list
    initUploadlist();
    //initilise upload list
    //initDownloadloadlist();
    //initialise sem_upload,sem_download state.
    initSem();

    //create five processes,but never destroy them.
    {
        //create t analysis Center   Monitor process
        p_analysisCenterMonitor = fork();
        if( p_analysisCenterMonitor == 0)
        {
            analysisCenterMonitor();
            return 0;
        }

        //create the upload process
        p_upload = fork();
        if( p_upload == 0)
        {
            upload();
            return 0;
        }

        //create the download process
        //download = fork();
        //if( download == 0)
        //{
        //    download();
        //    return 0;
        //}

        //create the timingTask process
        p_timingTask = fork();
        if( p_timingTask == 0)
        {
            timingTask();
            return 0;
        }

        //create the log process
        p_log = fork();
        if( p_log == 0)
        {
            log();
            return 0;
        }
    }

    while(1);

    return 0;


}
示例#7
0
int main(int argc, char *argv[])
{
   int c, i;
   long tmask = 0, sslMode=0,sslOMode=0, tracelevel=0;
   char * tracefile = NULL;
#ifdef HAVE_UDS
   int enableUds=0;
#endif
   int enableHttp=0,enableHttps=0,useChunking=0,doBa=0,enableInterOp=0,httpLocalOnly=0;
   int syslogLevel=LOG_ERR;
   long dSockets,sSockets,pSockets;
   char *pauseStr;

   /* SF 3462309 - If there is an instance running already, return */
   int pid_found = 0;
   if ((pid_found = sfcb_is_running()) != 0) {
      mlogf(M_ERROR, M_SHOW, " --- A previous instance of sfcbd [%d] is running. Exiting.\n", pid_found);
      exit(1);
   }

   name = strrchr(argv[0], '/');
   if (name != NULL) ++name;
   else name = argv[0];

   collectStat=0;
   colorTrace=0;
   processName="sfcbd";
   provPauseStr=getenv("SFCB_PAUSE_PROVIDER");
   httpPauseStr=getenv("SFCB_PAUSE_CODEC");
   currentProc=sfcBrokerPid=getpid();
   restartArgc=argc;
   restartArgv=argv;


   exFlags = 0;

   static struct option const long_options[] =
       {
	   { "config-file",      required_argument, 0,        'c' },
	   { "daemon",           no_argument,       0,        'd' },
	   { "help",             no_argument,       0,        'h' },
	   { "color-trace",      no_argument,       0,        'k' },
	   { "collect-stats",    no_argument,       0,        's' },
	   { "syslog-level",     required_argument, 0,        'l' },
	   { "trace-components", required_argument, 0,        't' },
	   { "version",          no_argument,       0,        'v' },
           { "disable-repository-default-inst-provider", no_argument,       0,        'i' },
	   { 0, 0, 0, 0 }
       };

   while ((c = getopt_long(argc, argv, "c:dhkst:vil:", long_options, 0)) != -1)
   {
       switch(c)
       {
	   case 0:
	       break;

	   case 'c':
	       configfile = strdup(optarg);
	       break;

	   case 'd':
	       daemon(0, 0);
	       currentProc=sfcBrokerPid=getpid(); /* req. on some systems */
	       break;

	   case 'h':
	       usage(0);

	   case 'k':
	       colorTrace = 1;
	       break;

	   case 's':
	       collectStat = 1;
	       break;

	   case 't':
	       if (*optarg == '?') {
		   fprintf(stdout, "---   Traceable Components:     Int       Hex\n");
		   for (i = 0; traceIds[i].id; i++)
		       fprintf(stdout, "--- \t%18s:    %d\t0x%05X\n", traceIds[i].id, traceIds[i].code, traceIds[i].code);
		   exit(0);
	       } else if (isdigit(*optarg)) {
		   char *ep;
		   tmask = strtol(optarg, &ep, 0);
	       } else {
		   fprintf(stderr, "Try %s -t ? for a list of the trace components and bitmasks.\n", name);
		   exit(1);
	       }
	       break;

	   case 'v':
	       version();

           case 'i':
               disableDefaultProvider=1;
               break;

	   case 'l':
            if (strcmp(optarg,"LOG_ERR")==0) {
                syslogLevel=LOG_ERR;
            } else if (strcmp(optarg,"LOG_INFO")==0) {
                syslogLevel=LOG_INFO;
            } else if (strcmp(optarg,"LOG_DEBUG")==0) {
                syslogLevel=LOG_DEBUG;
            } else {
                fprintf(stderr,"Invalid value for syslog-level.\n");
	            usage(3);
            }
            break;
        
	   default:
	       usage(3);
       }
   }

   if (optind < argc)
   {
      fprintf(stderr,"SFCB not started: unrecognized config property %s\n",argv[optind]);
      usage(1);
   }

   startLogging(syslogLevel,1);

   mlogf(M_INFO,M_SHOW,"--- %s V" sfcHttpDaemonVersion " started - %d\n", name, currentProc);

   //get the creation timestamp for the sequence context
   struct timeval  tv;
   struct timezone tz;
   gettimeofday(&tv, &tz);
   struct tm cttm;
   sfcBrokerStart = (char *) malloc(15 * sizeof(char));
   memset((void *)sfcBrokerStart, 0, 15 * sizeof(char));
   if (gmtime_r(&tv.tv_sec, &cttm) != NULL) {
     strftime((char *)sfcBrokerStart, 15, "%Y%m%d%H%M%S", &cttm);
   }

   if (collectStat) {
       mlogf(M_INFO,M_SHOW,"--- Statistics collection enabled\n");
       remove("sfcbStat");
   }

   setupControl(configfile);      
   
   _SFCB_TRACE_INIT();

   if (tmask == 0) {
     /* trace mask not specified, check in config file */
     getControlNum("traceMask",&tmask);
   }
   
   if (tmask) {
     if (getControlNum("traceLevel",&tracelevel) || tracelevel == 0) {
       /* no tracelevel found in config file, use default */
       tracelevel = 1;
     }
     if (getenv("SFCB_TRACE_FILE") == NULL && 
	 getControlChars("traceFile",&tracefile) == 0) {
       /* only set tracefile from config file if not specified via env */
       _SFCB_TRACE_SETFILE(tracefile);
     }
     _SFCB_TRACE_START(tracelevel,tmask);
   }

//        SFCB_DEBUG
#ifndef SFCB_DEBUG
   if (tmask)
      mlogf(M_ERROR,M_SHOW,"--- SCFB_DEBUG not configured. -t %d ignored\n",tmask);
#endif

   if ((pauseStr=getenv("SFCB_PAUSE_PROVIDER"))) {
     printf("--- Provider pausing for: %s\n",pauseStr);
   }
      
   if (getControlBool("enableHttp", &enableHttp))
      enableHttp=1;

#ifdef HAVE_UDS
   if (getControlBool("enableUds", &enableUds))
      enableUds=1;
#endif
      
#if defined USE_SSL
   if (getControlBool("enableHttps", &enableHttps))
      enableHttps=0;

   sslMode=enableHttps;
#ifdef HAVE_UDS
   sslOMode=sslMode & !enableHttp & !enableUds;
#else
   sslOMode=sslMode & !enableHttp;
#endif
#else
   mlogf(M_INFO,M_SHOW,"--- SSL not configured\n");
   enableHttps=0;
   sslMode=0;
   sslOMode=0;
#endif
   
   if (getControlBool("useChunking", &useChunking))
      useChunking=0;
   if (useChunking==0)
         mlogf(M_INFO,M_SHOW,"--- Chunking disabled\n");

   if (getControlBool("doBasicAuth", &doBa))
      doBa=0;
   if (!doBa)
      mlogf(M_INFO,M_SHOW,"--- User authentication disabled\n");

   if (getControlBool("enableInterOp", &enableInterOp))
       enableInterOp=0;

   if (getControlNum("httpProcs", (long*)&dSockets))
      dSockets = 10;
   if (getControlNum("httpsProcs", (long*)&sSockets))
      sSockets = 10;
   if (getControlNum("provProcs", (long*)&pSockets))
      pSockets = 16;

   if (getControlBool("httpLocalOnly", &httpLocalOnly))
      httpLocalOnly=0;   
   if (httpLocalOnly)
      mlogf(M_INFO,M_SHOW,"--- External HTTP connections disabled; using loopback only\n");

   if (getControlNum("providerSampleInterval", (long*)&provSampleInterval))
      provSampleInterval = 30;
   if (getControlNum("providerTimeoutInterval", (long*)&provTimeoutInterval))
      provTimeoutInterval = 60;
   if (getControlBool("providerAutoGroup", (int*)&provAutoGroup))
      provAutoGroup=1;

   resultSockets=getSocketPair("sfcbd result");
   sfcbSockets=getSocketPair("sfcbd sfcb");

   if (enableInterOp==0)
       mlogf(M_INFO,M_SHOW,"--- InterOp namespace disabled\n");
   else
       exFlags = exFlags | 2;

   if ((enableInterOp && pSockets < 4 ) || pSockets < 3) {
     /* adjusting provider number */
     if (enableInterOp) {
       pSockets = 4;
     } else {
       pSockets = 3;
     }
     mlogf(M_INFO,M_SHOW,
	   "--- Max provider process number adjusted to %d\n", pSockets);
   }

   // Check for whitespace trimming option
   if (getControlBool("trimWhitespace", &trimws)) {
     trimws = 0;
   }

   if ((enableHttp || enableHttps) && dSockets > 0) {
     startHttp = 1;
   }
   
   initSem(dSockets,sSockets,pSockets);
   initProvProcCtl(pSockets);
   init_sfcBroker();
   initSocketPairs(pSockets,dSockets,sSockets);

   setSignal(SIGQUIT, handleSigquit,0);
   setSignal(SIGINT,  handleSigquit,0);
   
   setSignal(SIGTERM, handleSigquit,0);
   setSignal(SIGHUP,  handleSigHup,0);

   atexit(uninitGarbageCollector);

   startLocalConnectServer();
   
#ifndef LOCAL_CONNECT_ONLY_ENABLE
   if (startHttp) {
     startHttpd(argc, argv, sslMode);
   }
#endif // LOCAL_CONNECT_ONLY_ENABLE
   
#ifdef HAVE_JDBC
   //Start dbProtocol-Daemon
   if (startDbp) {
      if (sslMode)
         startDbpd(argc, argv,1);
      if (!sslOMode)
         startDbpd(argc, argv,0);
   }
#endif	


   setSignal(SIGSEGV, handleSigSegv,SA_ONESHOT);
   setSignal(SIGCHLD, handleSigChld,0);
   
   processProviderMgrRequests();

   return 0;
}