コード例 #1
0
ファイル: import.c プロジェクト: USGS-CIDA/migratingToGit
void analyze_masters(int argc, char *argv[], 
			  import_options_t *analyzer, 
			  forest_t *forest)
/* main entry point; collect and parse CVS masters */
{
    char	    name[PATH_MAX];
    const char      *last = NULL;
    char	    *file;
    size_t	    i, j = 1;
    int		    c;
#ifdef THREADS
    pthread_attr_t  attr;

    /* Initialize and reinforce default thread non-detached attribute */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
#endif /* THREADS */

    striplen = analyzer->striplen;

    forest->textsize = forest->filecount = 0;
    progress_begin("Reading file list...", NO_MAX);
    for (;;)
    {
	struct stat stb;
	int l;
	if (argc < 2) {
	    /* coverity[tainted_data] Safe, never handed to exec */
	    if (fgets(name, sizeof(name), stdin) == NULL)
		break;
	    l = strlen(name);
	    if (name[l-1] == '\n')
		name[l-1] = '\0';
	    file = name;
	} else {
	    file = argv[j++];
	    if (!file)
		break;
	}

	if (stat(file, &stb) != 0)
	    continue;
	else if (S_ISDIR(stb.st_mode) != 0)
	    continue;
	else if (!analyzer->promiscuous)
	{
	    char *end = file + strlen(file);
	    if (end - file < 2 || end[-1] != 'v' || end[-2] != ',')
		continue;
	    if (strstr(file, "CVSROOT") != NULL)
		continue;
	}
	forest->textsize += stb.st_size;

	fn = xcalloc(1, sizeof(rev_filename), "filename gathering");
	*fn_tail = fn;
	fn_tail = (rev_filename **)&fn->next;
	if (striplen > 0 && last != NULL) {
	    c = strcommonendingwith(file, last, '/');
	    if (c < striplen)
		striplen = c;
	} else if (striplen < 0) {
	    striplen = 0;
	    for (i = 0; i < strlen(file); i++)
		if (file[i] == '/')
		    striplen = i + 1;
	}
	fn->file = atom(file);
	last = fn->file;
	total_files++;
	if (progress && total_files % 100 == 0)
	    progress_jump(total_files);
    }
    forest->filecount = total_files;

    generators = xcalloc(sizeof(generator_t), total_files, "Generators");
    sorted_files = xmalloc(sizeof(rev_file) * total_files, "sorted_files");
    cvs_masters = xcalloc(total_files, sizeof(cvs_master), "cvs_masters");
    rev_masters = xmalloc(sizeof(rev_master) * total_files, "rev_masters");
    fn_n = total_files;
    i = 0;
    rev_filename *tn;
    for (fn = fn_head; fn; fn = tn) {
	tn = fn->next;
	sorted_files[i].name = fn->file;
	sorted_files[i++].rectified = atom_rectify_name(fn->file);
	free(fn);
    }
#ifdef FILESORT
    /*
     * Sort list of files in path_deep_compare order of output name.
     * cvs_masters and rev_masters will be mainteined in this order.
     * This causes commits to come out in correct pack order.
     * It also causes operations to come out in correct fileop_sort order.
     * Note some output names are different to input names.
     * e.g. .cvsignore becomes .gitignore
     */
    qsort(sorted_files, total_files, sizeof(rev_file), file_compare);
#endif /*FILESORT */
	
    progress_end("done, %.3fKB in %d files",
		 (forest->textsize/1024.0), forest->filecount);

    /* things that must be visible to inner functions */
    load_current_file = 0;
    verbose = analyzer->verbose;

    /*
     * Analyze the files for CVS revision structure.
     *
     * The result of this analysis is a rev_list, each element of
     * which corresponds to a CVS master and points at a list of named
     * CVS branch heads (rev_refs), each one of which points at a list
     * of CVS commit structures (cvs_commit).
     */
#ifdef THREADS
    if (threads > 1)
	snprintf(name, sizeof(name), 
		 "Analyzing masters with %d threads...", threads);
    else
#endif /* THREADS */
	strcpy(name, "Analyzing masters...");
    progress_begin(name, total_files);
#ifdef THREADS
    if (threads > 1)
    {
	int i;

	workers = (pthread_t *)xcalloc(threads, sizeof(pthread_t), __func__);
	for (i = 0; i < threads; i++)
	    pthread_create(&workers[i], &attr, worker, NULL);

        /* Wait for all the threads to die off. */
	for (i = 0; i < threads; i++)
          pthread_join(workers[i], NULL);
        
	pthread_mutex_destroy(&enqueue_mutex);
	pthread_mutex_destroy(&revlist_mutex);
    }
    else
#endif /* THREADS */
	worker(NULL);

    progress_end("done, %d revisions", (int)total_revisions);
    free(sorted_files);

    forest->errcount = err;
    forest->total_revisions = total_revisions;
    forest->skew_vulnerable = skew_vulnerable;
    forest->cvs = cvs_masters;
    forest->generators = (generator_t *)generators;
}
コード例 #2
0
ファイル: init.c プロジェクト: DylanGriffith/julia
void julia_init(char *imageFile)
{
    jl_page_size = jl_getpagesize();
    jl_find_stack_bottom();
    jl_dl_handle = jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT);
#ifdef _OS_WINDOWS_
    uv_dlopen("ntdll.dll",jl_ntdll_handle); //bypass julia's pathchecking for system dlls
    uv_dlopen("Kernel32.dll",jl_kernel32_handle);
    uv_dlopen("msvcrt.dll",jl_crtdll_handle);
    uv_dlopen("Ws2_32.dll",jl_winsock_handle);
    _jl_exe_handle.handle = GetModuleHandleA(NULL);
    if (!DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
        GetCurrentProcess(), (PHANDLE)&hMainThread, 0,
        TRUE, DUPLICATE_SAME_ACCESS )) {
        JL_PRINTF(JL_STDERR, "Couldn't access handle to main thread\n");
    }
#if defined(_CPU_X86_64_)
    SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
    SymInitialize(GetCurrentProcess(), NULL, 1);
    needsSymRefreshModuleList = 0;
#endif
#endif
    jl_io_loop = uv_default_loop(); //this loop will internal events (spawining process etc.)
    init_stdio();

#if defined(__linux__)
    int ncores = jl_cpu_cores();
    if (ncores > 1) {
        cpu_set_t cpumask;
        CPU_ZERO(&cpumask);
        for(int i=0; i < ncores; i++) {
            CPU_SET(i, &cpumask);
        }
        sched_setaffinity(0, sizeof(cpu_set_t), &cpumask);
    }
#endif

#ifdef JL_GC_MARKSWEEP
    jl_gc_init();
    jl_gc_disable();
#endif
    jl_init_frontend();
    jl_init_types();
    jl_init_tasks(jl_stack_lo, jl_stack_hi-jl_stack_lo);
    jl_init_codegen();
    jl_an_empty_cell = (jl_value_t*)jl_alloc_cell_1d(0);

    jl_init_serializer();

    if (!imageFile) {
        jl_main_module = jl_new_module(jl_symbol("Main"));
        jl_main_module->parent = jl_main_module;
        jl_core_module = jl_new_module(jl_symbol("Core"));
        jl_core_module->parent = jl_main_module;
        jl_set_const(jl_main_module, jl_symbol("Core"),
                     (jl_value_t*)jl_core_module);
        jl_module_using(jl_main_module, jl_core_module);
        jl_current_module = jl_core_module;
        jl_init_intrinsic_functions();
        jl_init_primitives();
        jl_load("boot.jl");
        jl_get_builtin_hooks();
        jl_boot_file_loaded = 1;
        jl_init_box_caches();
    }

    if (imageFile) {
        JL_TRY {
            jl_restore_system_image(imageFile);
        }
        JL_CATCH {
            JL_PRINTF(JL_STDERR, "error during init:\n");
            jl_show(jl_stderr_obj(), jl_exception_in_transit);
            JL_PRINTF(JL_STDERR, "\n");
            jl_exit(1);
        }
    }

    // set module field of primitive types
    int i;
    void **table = jl_core_module->bindings.table;
    for(i=1; i < jl_core_module->bindings.size; i+=2) {
        if (table[i] != HT_NOTFOUND) {
            jl_binding_t *b = (jl_binding_t*)table[i];
            if (b->value && jl_is_datatype(b->value)) {
                jl_datatype_t *tt = (jl_datatype_t*)b->value;
                tt->name->module = jl_core_module;
            }
        }
    }

    // the Main module is the one which is always open, and set as the
    // current module for bare (non-module-wrapped) toplevel expressions.
    // it does "using Base" if Base is available.
    if (jl_base_module != NULL) {
        jl_add_standard_imports(jl_main_module);
    }
    // eval() uses Main by default, so Main.eval === Core.eval
    jl_module_import(jl_main_module, jl_core_module, jl_symbol("eval"));
    jl_current_module = jl_main_module;


#ifndef _OS_WINDOWS_
    signal_stack = malloc(SIGSTKSZ);
    struct sigaction actf;
    memset(&actf, 0, sizeof(struct sigaction));
    sigemptyset(&actf.sa_mask);
    actf.sa_handler = fpe_handler;
    actf.sa_flags = 0;
    if (sigaction(SIGFPE, &actf, NULL) < 0) {
        JL_PRINTF(JL_STDERR, "sigaction: %s\n", strerror(errno));
        jl_exit(1);
    }
#if defined(_OS_LINUX_)
    stack_t ss;
    ss.ss_flags = 0;
    ss.ss_size = SIGSTKSZ;
    ss.ss_sp = signal_stack;
    if (sigaltstack(&ss, NULL) < 0) {
        JL_PRINTF(JL_STDERR, "sigaltstack: %s\n", strerror(errno));
        jl_exit(1);
    }

    struct sigaction act;
    memset(&act, 0, sizeof(struct sigaction));
    sigemptyset(&act.sa_mask);
    act.sa_sigaction = segv_handler;
    act.sa_flags = SA_ONSTACK | SA_SIGINFO;
    if (sigaction(SIGSEGV, &act, NULL) < 0) {
        JL_PRINTF(JL_STDERR, "sigaction: %s\n", strerror(errno));
        jl_exit(1);
    }

    if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) {
        JL_PRINTF(JL_STDERR, "Couldn't set SIGPIPE\n");
        jl_exit(1);
    }
#elif defined (_OS_DARWIN_)
    kern_return_t ret;
    mach_port_t self = mach_task_self();
    ret = mach_port_allocate(self,MACH_PORT_RIGHT_RECEIVE,&segv_port);
    HANDLE_MACH_ERROR("mach_port_allocate",ret);
    ret = mach_port_insert_right(self,segv_port,segv_port,MACH_MSG_TYPE_MAKE_SEND);
    HANDLE_MACH_ERROR("mach_port_insert_right",ret);

    // Alright, create a thread to serve as the listener for exceptions
    pthread_t thread;
    pthread_attr_t attr;
    if (pthread_attr_init(&attr) != 0)
    {
        JL_PRINTF(JL_STDERR, "pthread_attr_init failed");
        jl_exit(1);  
    }
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    if (pthread_create(&thread,&attr,mach_segv_listener,NULL) != 0)
    {
        JL_PRINTF(JL_STDERR, "pthread_create failed");
        jl_exit(1);  
    }     
    pthread_attr_destroy(&attr);

    ret = task_set_exception_ports(self,EXC_MASK_BAD_ACCESS,segv_port,EXCEPTION_DEFAULT,MACHINE_THREAD_STATE);
    HANDLE_MACH_ERROR("task_set_exception_ports",ret);
#endif
#else
    if (signal(SIGFPE, (void (__cdecl *)(int))fpe_handler) == SIG_ERR) {
        JL_PRINTF(JL_STDERR, "Couldn't set SIGFPE\n");
        jl_exit(1);
    }
#endif


#ifdef JL_GC_MARKSWEEP
    jl_gc_enable();
#endif
}
コード例 #3
0
ファイル: rmt_server.c プロジェクト: likev/CodeOrpgPub
void
  RMTD_server_main
  (
) {
    int port_number, sig;            /* parent port number */

    /* Get a port number for the server */
    port_number = PNUM_get_port_number ();
    if (port_number == FAILURE) {
        MISC_log ("Can not find a port number\n");
        exit (1);
    }

    /* initialize client registration module */
    if (CLRG_initialize (N_child) == FAILURE)
        exit (1);

    /* open the message server sockets */
    if (MSGD_open_msg_server (port_number) == FAILURE)
        exit (2);

    /* open the RPC server socket */
    if ((Rpc_pfd = SOCD_open_server (port_number)) == FAILURE) {
        MISC_log ("Opening server failed\n");
        exit (2);
    }

    MISC_log ("Max number of children set to %d", N_child);
    MISC_log ("The port number is %d", port_number);

    /* go to background */
    if (Run_in_background)
        Goto_background ();
  
    RMT_access_disc_file (1, " \n", 3);

    /* Catch SIGCLD for calling wait to remove dead child */
    if (Set_signal_action (SIGCLD, Sigcld_int) == FAILURE)
        exit (1);

    for (sig = 1; sig <= 32; sig++) {	/* catch other signals */
	if (sig == SIGCLD || sig == SIGKILL || sig == SIGSTOP)
	    continue;
	if (sig == SIGPIPE)
	    Set_signal_action (sig, SIG_IGN);
	else
	    Set_signal_action (sig, Termination_exit);
    }

    /* write the PID to the file */
    MISC_log ("PID: %d", (int) getpid());

    MISC_log ("%s starts operation\n", Prog_name);
    while (1) {		/* The main loop. It never ends. */
	int sfd[2];     /* socket fd pairs created by the stream pipe */

	if (Iamchild == RMT_TRUE) {	/* The child */

#ifdef THREADED
	    int rtn;
	    if ((rtn = pthread_attr_init (&pthread_custom_attr)) != 0) {
                MISC_log ("pthread_attr_init failed %d:%s\n", 
						rtn, strerror (rtn));
                exit(0);
            }
	    if (( rtn = pthread_attr_setdetachstate (&pthread_custom_attr,
                                       PTHREAD_CREATE_DETACHED)) != 0) {
                MISC_log ("pthread_attr_setdetachstate failed %d:%s\n", 
					rtn,strerror(rtn));
                exit(0);
            }
#else
	    void *buf;
	    Manage_clients (MC_INIT, 0, &buf);
#endif

	    while (1) {
		int fd;		/* client fd */
	
#ifdef THREADED
		{		/* waiting for a new fd from the parent */
		    fd_set readfds;
		    FD_ZERO (&readfds);
		    FD_SET (sfd[0], &readfds);
		    select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
		}
		if ((fd = Receive_pipe_msg_from_parent (sfd[0])) < 0)
		    continue;
		pthread_mutex_lock (&countMutex);
		Num_threads++;
		pthread_mutex_unlock (&countMutex);
                pthread_create (&newThread, &pthread_custom_attr, 
						Process_child, (void *)fd);
#else
		fd = Manage_clients (MC_POLL, sfd[0], &buf);
		if (fd >= 0) {
		    int ret = Process_child (fd);
		    if (ret < 0)
			Manage_clients (MC_DELETE, fd, &buf);
		}
#endif
	    }
	}
	else {				/* parent */
	    int cpid;
	    int fd;		/* client fd */

	    sigrelse (SIGCHLD);
	    if (RMT_reread_config) {
		MISC_log ("Re-read configuration file %s\n", Conf_name);
		GCLD_initialize_host_table (Conf_name);
		MSGD_process_msgs (-1); 
		RMT_reread_config = 0;
		Conf_update_count++;
	    }
	    MSGD_process_msgs (Rpc_pfd); 
	    sighold (SIGCHLD);

	    /* accept new clients */
	    while (1) {
		int cl_type, cl_pid;
		unsigned int cl_addr;	/* LBO */
		int ret = 0;
		Client_regist newCl;

		if ((fd = GCLD_get_client (Rpc_pfd, &cl_type, 
					&cl_pid, &cl_addr)) == FAILURE)
		    break;

		if (cl_type == RMT_MULTIPLE_CLIENT && 
			(ret = CLRG_get_client_info (cl_pid, cl_addr)) > 0) {
		    Send_pipe_msg_to_child (ret, fd);
		    close (fd);			/* close client socket */
		}
		else {	/* no child for this client */

		    /* create a pipe to the child */
		    if (socketpair (AF_UNIX, SOCK_STREAM, 0, sfd) < 0) {
			MISC_log ("Pipe creation failed (errno = %d)", errno);
			close (fd);		/* close client socket */
			continue;
		    }
		    if (Send_pipe_msg_to_child (sfd[1], fd) == FAILURE) {
			close (fd);		/* close client socket */
		    	close (sfd[0]);		/* close pipe */
		    	close (sfd[1]);		/* close pipe */
			continue;
		    }

		    if (cl_type == RMT_SINGLE_CLIENT && cl_pid != 0)
			CLRG_term_client (cl_pid, cl_addr);

		    /* fork a child */
		    Cl_addr = cl_addr;
		    if ((cpid = fork ()) == 0) {
			int sig;
			for (sig = 1; sig <= 32; sig++) {
			    if (sig == SIGPIPE)
				Set_signal_action (sig, SIG_IGN);
			    else
				Set_signal_action (sig, SIG_DFL);
			}
		    	close (Rpc_pfd); 	/* Close parent socket fd */
			MSGD_close_msg_clients ();	/* close msg fds */
			RMTSM_close_msg_hosts ();	/* close msg fds */
		    	CLRG_close_other_client_fd (sfd[0]);
			close (fd);		/* close client socket */
		    	close (sfd[1]);		/* close parent side pipe */
			RMT_access_disc_file (0, NULL, 0);
		    	Iamchild = RMT_TRUE;	/* we must set this after 
						   	stopping timer */
		    	break;
		    }
		    else if (cpid < 0) {
		    	close (fd);	      /* close client socket */
		    	close (sfd[0]);       /* close pipe */
		    	close (sfd[1]);	      /* close pipe */
		    	MISC_log ("fork failed (errno = %d)",errno);
		    }
		    else {
			/* register the new client */
			newCl.childPid = cpid;		/* child pid */
			newCl.addr = cl_addr;		/* client addr */
			newCl.clientPid = cl_pid;	/* client pid */
			newCl.pipeFd = sfd[1];		/* to-child pipe fd */
		    	if (CLRG_register_client (&newCl) != SUCCESS)
			    close (sfd[1]);
			close (sfd[0]);		/* close child side pipe */
		    	close (fd);		/* close client socket */
		    }
		}
	    }
	}
    }
}
コード例 #4
0
ファイル: multistresstest.c プロジェクト: Fooway/proj.4
int main( int argc, char **argv )

{
/* -------------------------------------------------------------------- */
/*      Our first pass is to establish the correct answers for all      */
/*      the tests.                                                      */
/* -------------------------------------------------------------------- */
    int i, test_count = sizeof(test_list) / sizeof(TestItem); 

    for( i = 0; i < test_count; i++ )
    {
        TestItem *test = test_list + i;

        projPJ src_pj, dst_pj;

        src_pj = pj_init_plus( test->src_def );
        dst_pj = pj_init_plus( test->dst_def );

        if( src_pj == NULL )
        {
            printf( "Unable to translate:\n%s\n", test->src_def );
            test->skip = 1;
            continue;
        }

        if( dst_pj == NULL )
        {
            printf( "Unable to translate:\n%s\n", test->dst_def );
            test->skip = 1;
            continue;
        }
        
        test->dst_x = test->src_x;
        test->dst_y = test->src_y;
        test->dst_z = test->src_z;

        test->dst_error = pj_transform( src_pj, dst_pj, 1, 0, 
                                        &(test->dst_x), 
                                        &(test->dst_y),
                                        &(test->dst_z) );
     
        pj_free( src_pj );
        pj_free( dst_pj );

        test->skip = 0;

#ifdef notdef
        printf( "Test %d - output %.14g,%.14g,%g\n", i, test->dst_x, test->dst_y, test->dst_z );
#endif
    }

    printf( "%d tests initialized.\n", test_count );

/* -------------------------------------------------------------------- */
/*      Now launch a bunch of threads to repeat the tests.              */
/* -------------------------------------------------------------------- */
#ifdef _WIN32

	{ //Scoped to workaround lack of c99 support in VS
		HANDLE ahThread[num_threads];

		for( i = 0; i < num_threads; i++ )
		{
			active_thread_count++;

			ahThread[i] = CreateThread(NULL, 0, WinTestThread, NULL, 0, NULL);
			
			if (ahThread[i] == 0)
			{
				printf( "Thread creation failed.");
				return 1;
			}
		}

		printf( "%d test threads launched.\n", num_threads );

		WaitForMultipleObjects(num_threads, ahThread, TRUE, INFINITE);
	}

#else
    {
	pthread_t ahThread[num_threads];
	pthread_attr_t hThreadAttr;

	pthread_attr_init( &hThreadAttr );
	pthread_attr_setdetachstate( &hThreadAttr, PTHREAD_CREATE_DETACHED );

	for( i = 0; i < num_threads; i++ )
	{
		active_thread_count++;

		pthread_create( &(ahThread[i]), &hThreadAttr, 
			PosixTestThread, NULL );
	}

	printf( "%d test threads launched.\n", num_threads );

	while( active_thread_count > 0 )				       
		sleep( 1 );
    }
#endif

    printf( "all tests complete.\n" );

    return 0;
}
コード例 #5
0
ファイル: rpc_mgr.c プロジェクト: HDOD/slurm
/* Process incoming RPCs. Meant to execute as a pthread */
extern void *rpc_mgr(void *no_data)
{
	pthread_attr_t thread_attr_rpc_req;
	slurm_fd_t sockfd, newsockfd;
	int i, retry_cnt, sigarray[] = {SIGUSR1, 0};
	slurm_addr_t cli_addr;
	slurmdbd_conn_t *conn_arg = NULL;

	slurm_mutex_lock(&thread_count_lock);
	master_thread_id = pthread_self();
	slurm_mutex_unlock(&thread_count_lock);

	(void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
	(void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

	/* threads to process individual RPC's are detached */
	slurm_attr_init(&thread_attr_rpc_req);
	if (pthread_attr_setdetachstate
	    (&thread_attr_rpc_req, PTHREAD_CREATE_DETACHED))
		fatal("pthread_attr_setdetachstate %m");

	/* initialize port for RPCs */
	if ((sockfd = slurm_init_msg_engine_port(get_dbd_port()))
	    == SLURM_SOCKET_ERROR)
		fatal("slurm_init_msg_engine_port error %m");

	/* Prepare to catch SIGUSR1 to interrupt accept().
	 * This signal is generated by the slurmdbd signal
	 * handler thread upon receipt of SIGABRT, SIGINT,
	 * or SIGTERM. That thread does all processing of
	 * all signals. */
	xsignal(SIGUSR1, _sig_handler);
	xsignal_unblock(sigarray);

	/*
	 * Process incoming RPCs until told to shutdown
	 */
	while ((i = _wait_for_server_thread()) >= 0) {
		/*
		 * accept needed for stream implementation is a no-op in
		 * message implementation that just passes sockfd to newsockfd
		 */
		if ((newsockfd = slurm_accept_msg_conn(sockfd,
						       &cli_addr)) ==
		    SLURM_SOCKET_ERROR) {
			_free_server_thread((pthread_t) 0);
			if (errno != EINTR)
				error("slurm_accept_msg_conn: %m");
			continue;
		}
		fd_set_nonblocking(newsockfd);

		conn_arg = xmalloc(sizeof(slurmdbd_conn_t));
		conn_arg->newsockfd = newsockfd;
		slurm_get_ip_str(&cli_addr, &conn_arg->orig_port,
				 conn_arg->ip, sizeof(conn_arg->ip));
		retry_cnt = 0;
		while (pthread_create(&slave_thread_id[i],
				      &thread_attr_rpc_req,
				      _service_connection,
				      (void *) conn_arg)) {
			if (retry_cnt > 0) {
				error("pthread_create failure, "
				      "aborting RPC: %m");
				close(newsockfd);
				break;
			}
			error("pthread_create failure: %m");
			retry_cnt++;
			usleep(1000);	/* retry in 1 msec */
		}
	}

	debug3("rpc_mgr shutting down");
	slurm_attr_destroy(&thread_attr_rpc_req);
	(void) slurm_shutdown_msg_engine(sockfd);
	_wait_for_thread_fini();
	pthread_exit((void *) 0);
	return NULL;
}
コード例 #6
0
ファイル: pthread-mutex.c プロジェクト: 41i/ZeroSample
int main (int argc, char *argv[])
{
   int i;
   double *a, *b;
   int status;
   pthread_attr_t attr;
   int ret_count;


  printf("\n\t\t---------------------------------------------------------------------------");
  printf("\n\t\t Centre for Development of Advanced Computing (C-DAC)");
  printf("\n\t\t Email : RarchK");
  printf("\n\t\t---------------------------------------------------------------------------");
  printf("\n\t\t Objective : To perform dot product of vector using Mutex.\n ");
  printf("\n\t\t..........................................................................\n");


   /* Assign storage and initialize values */
   a = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double));
   b = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double));
  
   for (i=0; i<VECLEN*NUMTHRDS; i++)
    {
     a[i]=1.0;
     b[i]=a[i];
    }

   dotstr.veclen = VECLEN; 
   dotstr.a = a; 
   dotstr.b = b; 
   dotstr.sum=0;

   ret_count=pthread_mutex_init(&mutexsum, NULL);
   if (ret_count)
   {
         printf("ERROR; return code from pthread_mutex_init() is %d\n", ret_count);
         exit(-1);
   }
         
   /* Create threads to perform the dotproduct  */
   ret_count=pthread_attr_init(&attr);
   if (ret_count)
   {
       printf("ERROR; return code from pthread_attr_init() is %d\n", ret_count);
       exit(-1);
   }

   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

	for(i=0; i<NUMTHRDS; i++)
        {
	/* 
	Each thread works on a different set of data.
	The offset is specified by 'i'. The size of
	the data for each thread is indicated by VECLEN.
	*/
	    ret_count=pthread_create( &callThd[i], &attr, dotprod, (void *)i);
      	    if (ret_count)
      	    {
         	printf("ERROR; return code from pthread_create() is %d\n", ret_count);
         	exit(-1);
      	    }
	}

 	ret_count=pthread_attr_destroy(&attr);
        if(ret_count)
	{
		printf("\n ERROR : return code from pthread_attr_destroy() is %d\n",ret_count);
		exit(-1);
	}
                


        /* Wait on the other threads */
	/*for(i=0; i<NUMTHRDS; i++)
        {
	  ret_count=pthread_join( callThd[i], (void **)&status);
	  if (ret_count)
      	  {
         	printf("ERROR; return code from pthread_join() is %d\n", ret_count);
         	exit(-1);
      	  }
	
	}*/



	for(i=0; i<NUMTHRDS; i++)
		pthread_join( callThd[i], (void **)&status);

   /* After joining, print out the results and cleanup */
   printf ("Sum =  %f \n", dotstr.sum);
   free (a);
   free (b);
   ret_count=pthread_mutex_destroy(&mutexsum);
   if (ret_count)
   {
         printf("ERROR; return code from pthread_mutex_destroy() is %d\n", ret_count);
         exit(-1);
   }
   pthread_exit(NULL);
}   
コード例 #7
0
ファイル: rdma_thread.c プロジェクト: li-ch/rdma-examples
int start_rdma_threads(struct sock_t *sock, struct rdma_resource_t *rdma_resource,
	struct sock_bind_t *sock_bind)
{
	int i, j;
	int rc;
	int ret = 0;
	unsigned long exit_code;
	struct thread_context_t *t_ctx;
	struct user_param_t *user_param = &(rdma_resource->user_param);
	pthread_attr_t attr;

	pthread_attr_init(&attr);
	if (user_param->server_ip) {
		rdma_resource->client_ctx = 
			(struct thread_context_t*)malloc(sizeof(struct thread_context_t) * user_param->num_of_thread);
	} else {
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	}

	i = 0;

	while (1) {
		if (user_param->server_ip) {
			t_ctx = &rdma_resource->client_ctx[i];
		} else {
			t_ctx = (struct thread_context_t*)malloc(sizeof(struct thread_context_t));
		}

		t_ctx->rdma_resource = rdma_resource;
		rc = sock_connect_multi(sock_bind, &(t_ctx->sock));
		if (rc) {
			ERROR("Failed to open connection between the 2 sides.\n");
			goto failure_1;
		}

		rc = pthread_create(&(t_ctx->thread), &attr, rdma_thread, t_ctx);
		if (rc != 0) {
			ERROR("Failed to create thread.\n");
			break;
		}

		i++;
		if (i >= user_param->num_of_thread && (user_param->server_ip)) {
			break;
		}
	}

	if (user_param->server_ip) {
		if (i != user_param->num_of_thread) {
			for (j = 0; j < i; j++) {
				t_ctx = &rdma_resource->client_ctx[i];
				pthread_cancel(t_ctx->thread);
				pthread_join(t_ctx->thread, (void *)&exit_code);
			}

			ret = 1;
		}

		for (i = 0; i < user_param->num_of_thread; i++) {
			t_ctx = &rdma_resource->client_ctx[i];

			rc = pthread_join(t_ctx->thread, (void *)&exit_code);
			if ((rc != 0) || (exit_code != 0)) {
				ERROR("Failed to wait for thread[%d] termination.\n", i);
				ret = 1;
			} else {
				INFO("Thread[%d] finished with return value %lu\n", i, exit_code);

				if (t_ctx->min_lat < rdma_resource->min_lat) {
					rdma_resource->min_lat          = t_ctx->min_lat;
					rdma_resource->min_lat_iter_num = t_ctx->min_lat_iter_num;
				}

				if (t_ctx->max_lat > rdma_resource->max_lat) {
					rdma_resource->max_lat          = t_ctx->max_lat;
					rdma_resource->max_lat_iter_num = t_ctx->max_lat_iter_num;
				}

				for (j = 0; j < LAT_LEVEL; j++) {
					rdma_resource->lat[j] += t_ctx->lat[j];
				}
			}

			sock_close_multi(&(t_ctx->sock), sock_bind);
		}
		free(rdma_resource->client_ctx);

		INFO("Got min lat %f when sending NO.%u packet.\n", rdma_resource->min_lat, rdma_resource->min_lat_iter_num);
		INFO("Got max lat %f when sending NO.%u packet.\n", rdma_resource->max_lat, rdma_resource->max_lat_iter_num);
		for (i = 0; i < LAT_LEVEL; i++) {
			if (i < 7) {
				INFO("The number of Lat < %4dus is %3d \n", (1 + i), rdma_resource->lat[i]);
			} else {
				INFO("The number of Lat < %4dus is %3d \n", (1 << (i - 4)), rdma_resource->lat[i]);
			}
		}
	}

failure_1:
	return ret;
}
コード例 #8
0
ファイル: museum.c プロジェクト: wojtex/studia-so-archeo
int server(
      void *query, size_t query_length,
      void **response, size_t *response_length,
      int source, void *arg) {
  if(query_length <= 0) {
    error("Empty message - something went wrong.");
    return -1;
  }
  char cmd = ((char*)query)[0];
  if(cmd == ACT_MUSEUM_GET_ESTIMATIONS) {
    int pL, pP, pT, pB;
    if(match(query, query_length, "@c %i %i %i %i", ACT_MUSEUM_GET_ESTIMATIONS, &pL, &pP, &pT, &pB) == 0) {
      if(!(1 <= pL && pL <= pP && pP <= Dlugosc && 1 <= pT && pT <= pB && pB <= Glebokosc)) {
        combine(response, response_length, "%c %c", ACT_ERROR, ERR_INVALID_ARGUMENT);
        return 0;
      } else if((pP - pL + 1) * (pB - pT + 1) * sizeof(int) > 999) {
        combine(response, response_length, "%c %c", ACT_ERROR, ERR_INVALID_ARGUMENT);
        return 0;
      } else {
        pP--; pL--; pB--; pT--;
        int *res = malloc((pP - pL + 1) * (pB - pT + 1) * sizeof(int));
        int w = pP - pL + 1;
        for(int i = pL, ii = 0; i <= pP; i++, ii++) {
          for(int j = pT, jj = 0; j <= pB; j++, jj++) {
            res[ii * w + jj] = ((Wydobyte[i] < j)?Szacunek[i*Glebokosc + j]:0);
          }
        }
        combine(response, response_length, "%c %b", ACT_OK, res, (pP - pL + 1) * (pB - pT + 1) * sizeof(int));
        free(res);
        return 0;
      }
    } else {
      combine(response, response_length, "%c %c", ACT_ERROR, ERR_WRONG_MESSAGE);
      return 0;
    }
  } else if(cmd == ACT_MUSEUM_BUY_TERRAIN) {
    /*
     * idx (int), id (int), k (int), z (int) -> l (int), d (int), dip(long)
     */
    int k, z, idx, id;
    if(match(query, query_length, "@c %i %i %i %i", ACT_MUSEUM_BUY_TERRAIN, &idx, &id, &k, &z) == 0) {
      // Try to allocate and return result
      if(z < OplataStala) {
        combine(response, response_length, "%c", ACT_REJECT);
        return 0;
      }
      int ret = change_money_of_company(bQ, idx, id, -OplataStala, museum_ip);
      if(ret == 1 || z < OplataStala || Delegaci[idx].zajety != 0) {
        combine(response, response_length, "%c", ACT_REJECT);
        return 0;
      } else if(ret == -1) {
        combine(response, response_length, "%c", ACT_ERROR);
        return 0;
      }
      z -= OplataStala;
      pthread_mutex_lock(&ochrona);
      // find first such block of terrain
      int l = -1;
      int g = 0;
      for(int i = 0; i < Dlugosc - k; i++) {
        l = i;
        for(int j = 0; j < k; j++) {
          if(Rezerwacje[i+j] > 0) {
            l = -1;
            i = i + j;
            break;
          }
        }
        if(l + k == Dlugosc) {
          combine(response, response_length, "%c", ACT_REJECT);
          pthread_mutex_unlock(&ochrona);
          return 0;
        }
        if(l != -1) {
          int sumcost = 0;
          int ndw = 0;
          for(int ii = 0; ii < Dlugosc; ii++) {
            if(Wydobyte[l+ii] < Glebokosc) {
              ndw = 1;
              break;
            }
          }
          if(ndw == 0) {
            continue;
          }
          for(int i = 0; i < Glebokosc; i++) {
            int line_cost = 0;
            for(int j = 0; j < k; j++) {
              if(Wydobyte[j+l] > i) continue;
              line_cost += Szacunek[(j+l)*Glebokosc+i];
              if(line_cost > z) {
                line_cost = z + 1;
                break;
              }
            }
            if(line_cost == z + 1) break;
            else if(line_cost + sumcost > z) break;
            sumcost += line_cost;
            g = i + 1;
          }
          if(g == 0 || sumcost > z) {
            continue;
          }
        }
      }
      int ret2 = change_money_of_company(bQ, idx, id, -z, museum_ip);
      if(ret2 == 1) {
        combine(response, response_length, "%c", ACT_REJECT);
        pthread_mutex_unlock(&ochrona);
        return 0;
      } else if(ret2 == -1) {
        combine(response, response_length, "%c", ACT_ERROR);
        pthread_mutex_unlock(&ochrona);
        return 0;
      }
      for(int i = 0; i < k; i++) {
        Rezerwacje[i+l] = g;
        Uswiadomione[i+l] = 0;
      }
      pthread_mutex_unlock(&ochrona);
      // OK - create delegate
      Delegaci[idx].zajety = 1;
      Delegaci[idx].ip = delegate_base_ip + idx;
      Delegaci[idx].l = l;
      Delegaci[idx].g = g;
      Delegaci[idx].do_exit = 0;
      Delegaci[idx].k = k;
      
      pthread_attr_t attrs;
      pthread_attr_init(&attrs);
      pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
      
      int ret3 = pthread_create(&(Delegaci[idx].thread), &attrs, delegate, Delegaci+idx);
      if(ret3 != 0) {
        error("Failed with creating thread.");
        pthread_attr_destroy(&attrs);
        return -1;
      }
      pthread_attr_destroy(&attrs);
      combine(response, response_length, "%c %i %i %l", ACT_OK, l, g, delegate_base_ip + idx);
      return 0;
    } else {
      combine(response, response_length, "%c", ACT_ERROR);
      return 0;
    }
  } else if(cmd == ACT_MUSEUM_REPORT_INIT || cmd == ACT_MUSEUM_SEND_REPORT_PART
         || cmd == ACT_MUSEUM_COMPANY_EXIT) {
    if(report_server(query, query_length, response, response_length, source, arg) == 0)
      return 0;
    else
      return -1;
  } else {
    warning("Unsupported command 0x%x.", cmd);
    combine(response, response_length, "%c", ACT_ERROR);
    return 0;
  }
  assert(0);
  return -1;
}
コード例 #9
0
ファイル: susp.cpp プロジェクト: keyz182/BOINC-7.0
int
main (int argc, char *argv[])
{
    pthread_t threads[THREAD_COUNT];
    pthread_attr_t detach;
    int status;
    void *result;
    int i;

    status = pthread_attr_init (&detach);
    if (status != 0)
        err_abort (status, "Init attributes object");
    status = pthread_attr_setdetachstate (
        &detach, PTHREAD_CREATE_DETACHED);
    if (status != 0)
        err_abort (status, "Set create-detached");

    for (i = 0; i< THREAD_COUNT; i++) {
        status = pthread_create (
            &threads[i], &detach, thread_routine, (void *)i);
        if (status != 0)
            err_abort (status, "Create thread");
    }

    sleep (1);

    for (i = 0; i < THREAD_COUNT/2; i++) {
        printf ("Suspending thread %d.\n", i);
        status = thd_suspend (threads[i]);
        if (status != 0)
            err_abort (status, "Suspend thread");
    }

    printf ("Sleeping ...\n");
    sleep (1);

    for (i = 0; i < THREAD_COUNT/2; i++) {
        printf ("Continuing thread %d.\n", i);
        status = thd_continue (threads[i]);
        if (status != 0)
            err_abort (status, "Suspend thread");
    }

    for (i = THREAD_COUNT/2; i < THREAD_COUNT; i++) {
        printf ("Suspending thread %d.\n", i);
        status = thd_suspend (threads[i]);
        if (status != 0)
            err_abort (status, "Suspend thread");
    }

    printf ("Sleeping ...\n");
    sleep (1);

    for (i = THREAD_COUNT/2; i < THREAD_COUNT; i++) {
        printf ("Continuing thread %d.\n", i);
        status = thd_continue (threads[i]);
        if (status != 0)
            err_abort (status, "Continue thread");
    }

    /*
     * Request that each thread terminate. We don't bother waiting for them;
     * just trust that they will terminate in "reasonable time". When the last
     * thread exits, the process will exit.
     */
    for (i = 0; i < THREAD_COUNT; i++)
	pthread_cancel (threads[i]);

    pthread_exit (NULL);        /* Let threads finish */
}
コード例 #10
0
ファイル: daemon.c プロジェクト: Jumbo88888/rpcapd-linux
/*
	\param plen: the length of the current message (needed in order to be able
	to discard excess data in the message, if present)
*/
pcap_t *daemon_startcapture(SOCKET sockctrl, pthread_t *threaddata, char *source, int active, struct rpcap_sampling *samp_param, uint32 plen, char *errbuf)
{
char portdata[PCAP_BUF_SIZE];		// temp variable needed to derive the data port
char peerhost[PCAP_BUF_SIZE];		// temp variable needed to derive the host name of our peer
pcap_t *fp= NULL;					// pcap_t main variable
unsigned int nread;					// number of bytes of the payload read from the socket
char sendbuf[RPCAP_NETBUF_SIZE];	// temporary buffer in which data to be sent is buffered
int sendbufidx= 0;					// index which keeps the number of bytes currently buffered

// socket-related variables
SOCKET sockdata= 0;					// socket descriptor of the data connection
struct addrinfo hints;				// temp, needed to open a socket connection
struct addrinfo *addrinfo;			// temp, needed to open a socket connection
struct sockaddr_storage saddr;		// temp, needed to retrieve the network data port chosen on the local machine
socklen_t saddrlen;					// temp, needed to retrieve the network data port chosen on the local machine

pthread_attr_t detachedAttribute;	// temp, needed to set the created thread as detached

// RPCAP-related variables
struct rpcap_startcapreq startcapreq;		// start capture request message
struct rpcap_startcapreply *startcapreply;	// start capture reply message
int serveropen_dp;							// keeps who is going to open the data connection

	addrinfo= NULL;

	if ( (nread= sock_recv(sockctrl, (char *) &startcapreq, sizeof(struct rpcap_startcapreq), SOCK_RECEIVEALL_YES, errbuf, PCAP_ERRBUF_SIZE)) == -1)
		return NULL;

	startcapreq.flags= ntohs(startcapreq.flags);

	// Open the selected device
	if ( (fp= pcap_open(source, 
			ntohl(startcapreq.snaplen),
			(startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_PROMISC) ? PCAP_OPENFLAG_PROMISCUOUS : 0 /* local device, other flags not needed */, 
			ntohl(startcapreq.read_timeout),
			NULL /* local device, so no auth */,
			errbuf)) == NULL)
	{
		rpcap_senderror(sockctrl, errbuf, PCAP_ERR_OPEN, NULL);
		return NULL;
	}

	// Apply sampling parameters
	fp->rmt_samp.method= samp_param->method;
	fp->rmt_samp.value= samp_param->value;

	/*
	We're in active mode if:
	- we're using TCP, and the user wants us to be in active mode
	- we're using UDP
	*/
	serveropen_dp= (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_SERVEROPEN) || (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_DGRAM) || active;

	/*
	Gets the sockaddr structure referred to the other peer in the ctrl connection

	We need that because:
	- if we're in passive mode, we need to know the address family we want to use 
	(the same used for the ctrl socket)
	- if we're in active mode, we need to know the network address of the other host 
	we want to connect to
	*/
	saddrlen = sizeof(struct sockaddr_storage);
	if (getpeername(sockctrl, (struct sockaddr *) &saddr, &saddrlen) == -1)
	{
		sock_geterror("getpeername(): ", errbuf, PCAP_ERRBUF_SIZE);
		goto error;
	}

	memset(&hints, 0, sizeof(struct addrinfo) );
	hints.ai_socktype = (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
	hints.ai_family = saddr.ss_family;

	// Now we have to create a new socket to send packets
	if (serveropen_dp)		// Data connection is opened by the server toward the client
	{
		sprintf(portdata, "%d", ntohs(startcapreq.portdata) );

		// Get the name of the other peer (needed to connect to that specific network address)
		if (getnameinfo( (struct sockaddr *) &saddr, saddrlen, peerhost, 
				sizeof(peerhost), NULL, 0, NI_NUMERICHOST) )
		{
			sock_geterror("getnameinfo(): ", errbuf, PCAP_ERRBUF_SIZE);
			goto error;
		}

		if (sock_initaddress(peerhost, portdata, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
			goto error;

		if ( (sockdata= sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errbuf, PCAP_ERRBUF_SIZE)) == -1)
			goto error;
	}
	else		// Data connection is opened by the client toward the server
	{
		hints.ai_flags = AI_PASSIVE;

		// Let's the server socket pick up a free network port for us
		if (sock_initaddress(NULL, "0", &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
			goto error;

		if ( (sockdata= sock_open(addrinfo, SOCKOPEN_SERVER, 1 /* max 1 connection in queue */, errbuf, PCAP_ERRBUF_SIZE)) == -1)
			goto error;

		// get the complete sockaddr structure used in the data connection
		saddrlen = sizeof(struct sockaddr_storage);
		if (getsockname(sockdata, (struct sockaddr *) &saddr, &saddrlen) == -1)
		{
			sock_geterror("getsockname(): ", errbuf, PCAP_ERRBUF_SIZE);
			goto error;
		}

		// Get the local port the system picked up
		if (getnameinfo( (struct sockaddr *) &saddr, saddrlen, NULL, 
				0, portdata, sizeof(portdata), NI_NUMERICSERV) )
		{
			sock_geterror("getnameinfo(): ", errbuf, PCAP_ERRBUF_SIZE);
			goto error;
		}
	}

	// addrinfo is no longer used
	freeaddrinfo(addrinfo);
	addrinfo= NULL;

	// save the socket ID for the next calls
	fp->rmt_sockctrl= sockctrl;	// Needed to send an error on the ctrl connection

	// Now I can set the filter
	if ( daemon_unpackapplyfilter(fp, &nread, &plen, errbuf) )
		goto error;


	// Now, I can send a RPCAP start capture reply message
	if ( sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
		RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE) == -1)
		goto error;

	rpcap_createhdr( (struct rpcap_header *) sendbuf, RPCAP_MSG_STARTCAP_REPLY, 0, sizeof(struct rpcap_startcapreply) );

	startcapreply= (struct rpcap_startcapreply *) &sendbuf[sendbufidx];
	
	if ( sock_bufferize(NULL, sizeof(struct rpcap_startcapreply), NULL,
		&sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE) == -1)
		goto error;

	memset(startcapreply, 0, sizeof(struct rpcap_startcapreply) );
	startcapreply->bufsize= htonl(fp->bufsize);

	if (!serveropen_dp)
	{
		unsigned short port = (unsigned short)strtoul(portdata,NULL,10);
		startcapreply->portdata= htons(port);
	}

	if ( sock_send(sockctrl, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
		goto error;

	if (!serveropen_dp)
	{
	SOCKET socktemp;	// We need another socket, since we're going to accept() a connection

		// Connection creation
		saddrlen = sizeof(struct sockaddr_storage);

		socktemp= accept(sockdata, (struct sockaddr *) &saddr, &saddrlen);
		
		if (socktemp == -1)
		{
			sock_geterror("accept(): ", errbuf, PCAP_ERRBUF_SIZE);
			goto error;
		}

		// Now that I accepted the connection, the server socket is no longer needed
		sock_close(sockdata, errbuf, PCAP_ERRBUF_SIZE);
		sockdata= socktemp;
	}

	fp->rmt_sockdata= sockdata;

	/* GV we need this to create the thread as detached. */
	/* GV otherwise, the thread handle is not destroyed  */
	pthread_attr_init(&detachedAttribute); 
	pthread_attr_setdetachstate(&detachedAttribute, PTHREAD_CREATE_DETACHED);
	
	// Now we have to create a new thread to receive packets
	if ( pthread_create(threaddata, &detachedAttribute, (void *) daemon_thrdatamain, (void *) fp) )
	{
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error creating the data thread");
		pthread_attr_destroy(&detachedAttribute);
		goto error;
	}

	pthread_attr_destroy(&detachedAttribute);
	// Check if all the data has been read; if not, discard the data in excess
	if (nread != plen)
		sock_discard(sockctrl, plen - nread, NULL, 0);

	return fp;

error:
	rpcap_senderror(sockctrl, errbuf, PCAP_ERR_STARTCAPTURE, NULL);

	if (addrinfo)
		freeaddrinfo(addrinfo);

	if (threaddata)
		pthread_cancel(*threaddata);

	if (sockdata)
		sock_close(sockdata, NULL, 0);

	// Check if all the data has been read; if not, discard the data in excess
	if (nread != plen)
		sock_discard(sockctrl, plen - nread, NULL, 0);

	if (fp)
	{
		pcap_close(fp);
		fp= NULL;
	}

	return NULL;
}
コード例 #11
0
ファイル: termExec.cpp プロジェクト: Smileyt/vimtouch
static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, char **envp,
    int* pProcessId)
{
    char *devname;
    int ptm;
    pid_t pid;
    char tmpdir[PATH_MAX];
    char terminfodir[PATH_MAX];

    sprintf((char*)default_vimruntime_dir, "%s/vim/", cmd);
    sprintf((char*)default_vim_dir, "%s/vim/", cmd);
    sprintf(tmpdir, "%s/tmp", cmd);
    sprintf(terminfodir, "%s/terminfo", cmd);

    pipe(fake_gpm_fd);

    ptm = open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
    if(ptm < 0){
        LOGE("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
        return -1;
    }
    fcntl(ptm, F_SETFD, FD_CLOEXEC);

    if(grantpt(ptm) || unlockpt(ptm) ||
       ((devname = (char*) ptsname(ptm)) == 0)){
        LOGE("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
        return -1;
    }

    setenv("TMPDIR", tmpdir, 1);
    setenv("TERM", "linux", 1);
    //setenv("HOME", cmd, 1);
    setenv("TERMINFO", terminfodir, 1);

    if (envp) {
        for (; *envp; ++envp) {
            putenv(*envp);
        }
    }


    char** thread_arg = (char**)malloc(sizeof(char*)*2);
    thread_arg[0] = strdup(devname);
    if(arg0)
        thread_arg[1] = strdup(arg0);
    else 
        thread_arg[1] = NULL;

    pthread_t thread_id;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_mutex_init(&global_mutex, NULL);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    pthread_create(&thread_id, &attr, thread_wrapper, (void*)thread_arg);
    pthread_attr_destroy(&attr);
    *pProcessId = (int) thread_id;
    thread_data.insert(std::make_pair(thread_id, thread_arg));

    return ptm;

    /*
    pid = fork();
    if(pid < 0) {
        LOGE("- fork failed: %s -\n", strerror(errno));
        return -1;
    }

    if(pid == 0){
        close(ptm);

        int pts;

        setsid();
        
        pts = open(devname, O_RDWR);
        if(pts < 0) exit(-1);

        dup2(pts, 0);
        dup2(pts, 1);
        dup2(pts, 2);

        char* argv[3];
        argv[0] = (char*)"vim";
        argv[1] = (char*)arg0;
        argv[2] = NULL;

        AndroidMain(2, (char**)argv);
        exit(-1);
    } else {
        *pProcessId = (int) pid;
        return ptm;
    }
    */
}
コード例 #12
0
// Initliase MDSM parameters, return poiinter to input buffer where
// input data will be stored
void initialiseMDSM(SURVEY* input_survey)
{
    unsigned i, j, k;

    // Initialise survey
    survey = input_survey;

    // Initialise devices
    devices = call_initialise_devices(input_survey);

    // Calculate temporary DM-shifts, maxshift and nsamp per beam
    unsigned greatest_maxshift = 0;
    for (i = 0; i < survey -> nbeams; i++)
    {
        // Calculate temporary shifts
        BEAM *beam = &(survey -> beams[i]);
        beam -> dm_shifts = (float *) safeMalloc(survey -> nchans * sizeof(float));
        for (j = 0; j < survey -> nchans; j++)
            beam -> dm_shifts[j] = dmdelay(beam -> fch1 + (beam -> foff * j), beam ->fch1);

        // Calculate maxshift
        float    high_dm   = survey -> lowdm + survey -> dmstep * (survey -> tdms - 1);
        unsigned maxshift  = beam -> dm_shifts[survey -> nchans - 1] * high_dm / survey -> tsamp;

        greatest_maxshift = ( maxshift > greatest_maxshift) 
                            ? maxshift : greatest_maxshift;

        // Round up maxshift to the nearest multiple of 256
        greatest_maxshift = (greatest_maxshift / 256) * 256 + 256;
    }

    // TEMPORARY: ASSIGN ALL BEAMS SAME MAXSHIFT
    for(i = 0; i < survey -> nbeams; i++)
    {
        BEAM *beam = &(survey -> beams[i]);
        beam -> maxshift = greatest_maxshift;
    }

    printf("============================================================================\n");

    // Calculate global nsamp for all beams
    size_t inputsize, outputsize;
    survey -> nsamp = calculate_nsamp(greatest_maxshift, &inputsize, &outputsize, 
                                      devices -> minTotalGlobalMem);

    // When beamforming, antenna data will be copied to the output buffer, so it's size
    // should be large enough to accommodate both
    if (survey -> apply_beamforming)
    {
        if (survey -> nantennas == 0)
        {
            printf("Number of antennas should not be 0!\n");
            exit(0);
        }

        size_t beamforming_size = survey -> nantennas * survey -> nchans * survey -> nsamp * sizeof(unsigned char);
        outputsize = max(outputsize, beamforming_size);

         printf("Memory Required (per GPU): Input = %d MB; Output = %d MB\n", 
              (int) (survey -> nbeams * inputsize / 1024 / 1024 / survey -> num_gpus), (int) (outputsize/1024/1024));
    }
    else
    {
        printf("Memory Required (per beam): Input = %d MB; Output = %d MB\n", 
              (int) (inputsize / 1024 / 1024), (int) (outputsize/1024/1024));
    }

    // Allocate input buffer (MDSM_STAGES separate buffer to allow dumping to disk
    // during any iteration)
    input_buffer = (float **) safeMalloc(MDSM_STAGES * sizeof(float *));
    for(i = 0; i < MDSM_STAGES; i++)
        input_buffer[i] = (float *) safeMalloc(survey -> nbeams * survey -> nsamp * 
                                               survey -> nchans * sizeof(float));

    // Allocate output buffer (one for each beam)
    output_buffer = (float **) safeMalloc(survey -> nbeams * sizeof(float *));

    // Allocate antenna buffer if beamforming
    antenna_buffer = NULL;
    if (survey -> apply_beamforming)
        antenna_buffer = (unsigned char *) malloc(survey -> nantennas * survey -> nchans * survey -> nsamp * sizeof(unsigned char));

    // Create writer buffer 
    // TODO: Provide kernel suggestions for high speed I/O
    writer_buffer = (float *) safeMalloc(survey -> nbeams * survey -> nsamp * 
                                         survey -> nchans * sizeof(float));

    // Log parameters
    printf("Observation Params: ndms = %d, maxDM = %f\n", survey -> tdms, 
                survey -> lowdm + survey -> dmstep * (survey -> tdms - 1));

    printf("Observation Params: nchans = %d, nsamp = %d, tsamp = %f\n", 
            survey -> nchans, survey -> nsamp, survey -> tsamp);

    printf("Beam Params:\n");
    for(i = 0; i < survey -> nbeams; i++)
    {
        BEAM beam = survey -> beams[i];
        printf("    Beam %d: fch1 = %f, foff = %f, maxshift = %d, gpuID = %d\n", 
                    beam.beam_id, beam.fch1, beam.foff, beam.maxshift, beam.gpu_id);
    }


    printf("\n");
    if (survey -> apply_beamforming) printf("- Performing beamforming\n");
    if (survey -> apply_rfi_clipper ) printf("- Clipping RFI\n");
    printf("- Performing dedispersion\n");
    if (survey -> apply_detrending ) printf("- Performing detrending\n");
    if (survey -> apply_median_filter ) printf("- Performing median filtering\n");
    if (survey -> dump_to_disk) printf("- Dump to disk mode enabled\n");
    if (survey -> apply_clustering) printf("- Applying DBSCAN\n");
    if (survey -> tbb_enabled) 	printf("- TBB mode enabled\n");
    printf("\n");

    printf("============================================================================\n");

    // Initialise processing threads
    pthread_attr_init(&thread_attr);
    pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
    survey -> num_threads = survey -> nbeams;
    threads = (pthread_t *) calloc(sizeof(pthread_t), survey -> num_threads);

    threads_params = (THREAD_PARAMS **) safeMalloc(survey -> num_threads * sizeof(THREAD_PARAMS*));
    for(i = 0; i < survey -> num_threads; i++)
        threads_params[i] = (THREAD_PARAMS *) safeMalloc(sizeof(THREAD_PARAMS));

    // Initialise barriers
    if (pthread_barrier_init(&input_barrier, NULL, survey -> num_threads + 2))
        { fprintf(stderr, "Unable to initialise input barrier\n"); exit(0); }

    if (pthread_barrier_init(&output_barrier, NULL, survey -> num_threads + 2))
        { fprintf(stderr, "Unable to initialise output barrier\n"); exit(0); }

    // Create GPU objects and allocate beams/threads to them
    GPU **gpus = (GPU **) malloc(survey -> num_gpus * sizeof(GPU *));
    for(i = 0; i < survey -> num_gpus; i++)
    {
        gpus[i] = (GPU *) malloc(sizeof(GPU));
        gpus[i] -> device_id   = survey -> gpu_ids[i];
        gpus[i] -> num_threads = 0;
        gpus[i] -> primary_thread = 0;
        gpus[i] -> thread_ids = (unsigned *) malloc(8 * sizeof(unsigned));        
    }

    // Allocate GPUs to beams (split beams among GPUs, one beam cannot be processed on more than 1 GPU)
    for(i = 0; i < survey -> num_threads; i++)
    {
        unsigned gpu = (survey -> gpu_ids)[i % survey -> num_gpus];

        // Update GPU properties
        for(j = 0; j < survey -> num_gpus; j++)
        {
            if (gpus[j] -> device_id == gpu)
            {
                if (gpus[j] -> num_threads == 0) gpus[j] -> primary_thread = i;
                threads_params[i] -> gpu_index = j;
                gpus[j] -> thread_ids[gpus[j] -> num_threads] = i;
                gpus[j] -> num_threads++;
                break;
            }
        }
    }  

    // Initialise GPU barriers
    for(i = 0; i < survey -> num_gpus; i++)
        if (pthread_barrier_init(&(gpus[i] -> barrier), NULL, gpus[i] -> num_threads))
            { fprintf(stderr, "Unable to initialise input barrier\n"); exit(0); }

    // Create output params and output file
    output_params.nthreads = survey -> num_threads;
    output_params.iterations = 3;
    output_params.maxiters = 2;
    output_params.output_buffer = output_buffer;
    output_params.input_buffer = input_buffer;
    output_params.stop = 0;
    output_params.rw_lock = &rw_lock;
    output_params.input_barrier = &input_barrier;
    output_params.output_barrier = &output_barrier;
    output_params.start = start;
    output_params.survey = survey;
    output_params.writer_mutex = &writer_mutex;
    output_params.writer_buffer = writer_buffer;
    output_params.writer_params = &writer_params;

    // Create output thread 
    if (pthread_create(&output_thread, &thread_attr, process_output, (void *) &output_params))
        { fprintf(stderr, "Error occured while creating output thread\n"); exit(0); }

    // Create threads and assign devices
    for(k = 0; k < survey -> num_threads; k++) {

        // Create THREAD_PARAMS for thread, based on input data and DEVICE_INFO
        // Input and output buffer pointers will point to beginning of beam
        threads_params[k] -> iterations = 2;
        threads_params[k] -> maxiters = 2;
        threads_params[k] -> stop = 0;
        threads_params[k] -> output = output_buffer;
        threads_params[k] -> input = input_buffer;
        threads_params[k] -> antenna_buffer = antenna_buffer;
        threads_params[k] -> thread_num = k;
        threads_params[k] -> num_threads = survey -> num_threads;
        threads_params[k] -> rw_lock = &rw_lock;
        threads_params[k] -> input_barrier = &input_barrier;
        threads_params[k] -> output_barrier = &output_barrier;
        threads_params[k] -> start = start;
        threads_params[k] -> survey = survey;
        threads_params[k] -> inputsize = inputsize;
        threads_params[k] -> outputsize = outputsize;
        threads_params[k] -> gpus = gpus;
        threads_params[k] -> cpu_threads = threads_params;

         // Create thread (using function in dedispersion_thread)
         if (pthread_create(&threads[k], &thread_attr, call_dedisperse, (void *) threads_params[k]))
            { fprintf(stderr, "Error occured while creating thread\n"); exit(0); }
    }

    // If we are writing to file, initialise and launch writer thread
    if (survey -> tbb_enabled || survey -> dump_to_disk)
    {
        // Initialiase and start the Data Writer thread, if required
        writer_params.survey   = survey;
        writer_params.start    = start;
        writer_params.stop     = 0;
        writer_params.writer_buffer   = writer_buffer;
        writer_params.writer_mutex    = &writer_mutex;
        writer_params.create_new_file = false;
        writer_params.data_available  = false;
        if (pthread_create(&writer_thread, &thread_attr, write_to_disk, (void *) &writer_params))
            { fprintf(stderr, "Error occured while creating thread\n"); exit(0); }
    }

    // If we're writing all the incoming data stream, it takes precedence over TBB
    if (survey -> dump_to_disk)
        survey -> tbb_enabled = false;

    // Wait input barrier (for dedispersion_manager, first time)
    int ret = pthread_barrier_wait(&input_barrier);
    if (!(ret == 0 || ret == PTHREAD_BARRIER_SERIAL_THREAD))
        { fprintf(stderr, "Error during barrier synchronisation\n");  exit(0); }
}
コード例 #13
0
ファイル: rpcapd.c プロジェクト: EIChaoYang/libpcap
/*!
	\brief 'true' main of the program.

	It must be in a separate function because:
	- if we're in 'console' mode, we have to put the main thread waiting for a Ctrl+C
	(in order to be able to stop everything)
	- if we're in daemon mode, the main program must terminate and a new child must be 
	created in order to create the daemon

	\param ptr: it keeps the main socket handler (what's called 'sockmain' in the main()), that
	represents the socket used in the main connection. It is a 'void *' just because pthreads
	want this format.
*/
static void main_passive(void *ptr)
{
	char errbuf[PCAP_ERRBUF_SIZE + 1];	// keeps the error string, prior to be printed
	SOCKET sockctrl;			// keeps the socket ID for this control connection
	struct sockaddr_storage from;		// generic sockaddr_storage variable
	socklen_t fromlen;			// keeps the length of the sockaddr_storage variable
	SOCKET sockmain;

#ifndef USE_THREADS
	pid_t pid;
#endif

	sockmain = *((SOCKET *) ptr);

	// Delete the pointer (which has been allocated in the main)
	free(ptr);

	// Initialize errbuf
	memset(errbuf, 0, sizeof(errbuf));

	// main thread loop
	while (1)
	{
#ifdef USE_THREADS
		pthread_t threadId;		// Pthread variable that keeps the thread structures
		pthread_attr_t detachedAttribute;
#endif
		struct daemon_slpars *pars;	// parameters needed by the daemon_serviceloop()

		// Connection creation
		fromlen = sizeof(struct sockaddr_storage);

		sockctrl = accept(sockmain, (struct sockaddr *) &from, &fromlen);
		
		if (sockctrl == -1)
		{
			// The accept() call can return this error when a signal is catched
			// In this case, we have simply to ignore this error code
			// Stevens, pg 124
#ifdef _WIN32
			if (WSAGetLastError() == WSAEINTR)
#else
			if (errno == EINTR)
#endif
				continue;

			// Don't check for errors here, since the error can be due to the fact that the thread 
			// has been killed
			sock_geterror("accept(): ", errbuf, PCAP_ERRBUF_SIZE);
			SOCK_ASSERT(errbuf, 1);
			continue;
		}

		// checks if the connecting host is among the ones allowed
		if (sock_check_hostlist(hostlist, RPCAP_HOSTLIST_SEP, &from, errbuf, PCAP_ERRBUF_SIZE) < 0)
		{
			rpcap_senderror(sockctrl, errbuf, PCAP_ERR_HOSTNOAUTH, NULL);
			sock_close(sockctrl, NULL, 0);
			continue;
		}


#ifdef USE_THREADS
		// in case of passive mode, this variable is deallocated by the daemon_serviceloop()
		pars = (struct daemon_slpars *) malloc (sizeof(struct daemon_slpars));
		if (pars == NULL)
		{
			snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
			continue;
		}

		pars->sockctrl = sockctrl;
		pars->activeclose = 0;		// useless in passive mode
		pars->isactive = 0;
		pars->nullAuthAllowed = nullAuthAllowed;

		/* GV we need this to create the thread as detached. */
		/* GV otherwise, the thread handle is not destroyed  */
		pthread_attr_init(&detachedAttribute); 
		pthread_attr_setdetachstate(&detachedAttribute, PTHREAD_CREATE_DETACHED);
		if (pthread_create(&threadId, &detachedAttribute, (void *) &daemon_serviceloop, (void *) pars))
		{
			SOCK_ASSERT("Error creating the child thread", 1);
			pthread_attr_destroy(&detachedAttribute);
			continue;
		}
		pthread_attr_destroy(&detachedAttribute);

#else
		if ((pid = fork()) == 0)	// I am the child
		{
			// in case of passive mode, this variable is deallocated by the daemon_serviceloop()
			pars = (struct daemon_slpars *) malloc (sizeof(struct daemon_slpars));
			if (pars == NULL)
			{
				snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
				exit(0);
			}

			pars->sockctrl = sockctrl;
			pars->activeclose = 0;		// useless in passive mode
			pars->isactive = 0;
			pars->nullAuthAllowed = nullAuthAllowed;

			// Close the main socket (must be open only in the parent)
			closesocket(sockmain);

			daemon_serviceloop((void *) pars);
			exit(0);
		}

		// I am the parent
		// Close the childsocket (must be open only in the child)
		closesocket(sockctrl);
#endif

		// loop forever, until interrupted
	}
}
コード例 #14
0
ファイル: rpcapd.c プロジェクト: EIChaoYang/libpcap
void main_startup(void)
{
	char errbuf[PCAP_ERRBUF_SIZE + 1];	// keeps the error string, prior to be printed
	struct addrinfo *addrinfo;		// keeps the addrinfo chain; required to open a new socket
	int i;
#ifdef USE_THREADS
	pthread_t threadId;			// Pthread variable that keeps the thread structures
	pthread_attr_t detachedAttribute;	// PThread attribute needed to create the thread as detached
#else
	pid_t pid;
#endif

	i = 0;
	addrinfo = NULL;
	memset(errbuf, 0, sizeof(errbuf));

	// Starts all the active threads
	while ((activelist[i].address[0] != 0) && (i < MAX_ACTIVE_LIST))
	{
		activelist[i].ai_family = mainhints.ai_family;
		
#ifdef USE_THREADS
		/* GV we need this to create the thread as detached. */
		/* GV otherwise, the thread handle is not destroyed  */
		pthread_attr_init(&detachedAttribute); 
		pthread_attr_setdetachstate(&detachedAttribute, PTHREAD_CREATE_DETACHED);

		if (pthread_create(&threadId, &detachedAttribute, (void *) &main_active, (void *) &activelist[i]))
		{
			SOCK_ASSERT("Error creating the active child thread", 1);
			pthread_attr_destroy(&detachedAttribute);
			continue;
		}
		pthread_attr_destroy(&detachedAttribute);
#else
		if ((pid = fork()) == 0)	// I am the child
		{
			main_active((void *) &activelist[i]);
			exit(0);
		}
#endif
		i++;
	}

	/*
	 * The code that manages the active connections is not blocking;
	 * the code that manages the passive connection is blocking.
	 * So, if the user does not want to run in passive mode, we have
	 * to block the main thread here, otherwise the program ends and
	 * all threads are stopped.
	 *
	 * WARNING: this means that in case we have only active mode,
	 * the program does not terminate even if all the child thread
	 * terminates. The user has always to press Ctrl+C (or send a
	 * SIGTERM) to terminate the program.
	 */
	if (passivemode)
	{
		struct addrinfo *tempaddrinfo;

		// Do the work
		if (sock_initaddress((address[0]) ? address : NULL, port, &mainhints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
		{
			SOCK_ASSERT(errbuf, 1);
			return;
		}

		tempaddrinfo = addrinfo;

		while (tempaddrinfo)
		{
			SOCKET *socktemp;

			if ((sockmain = sock_open(tempaddrinfo, SOCKOPEN_SERVER, SOCKET_MAXCONN, errbuf, PCAP_ERRBUF_SIZE)) == -1)
			{
				SOCK_ASSERT(errbuf, 1);
				tempaddrinfo = tempaddrinfo->ai_next;
				continue;
			}

			// This trick is needed in order to allow the child thread to save the 'sockmain' variable
			// withouth getting it overwritten by the sock_open, in case we want to open more than one waiting sockets
			// For instance, the pthread_create() will accept the socktemp variable, and it will deallocate immediately that variable
			socktemp = (SOCKET *) malloc (sizeof (SOCKET));
			if (socktemp == NULL)
				exit(0);

			*socktemp = sockmain;

#ifdef USE_THREADS
			/* GV we need this to create the thread as detached. */
			/* GV otherwise, the thread handle is not destroyed  */
			pthread_attr_init(&detachedAttribute); 
			pthread_attr_setdetachstate(&detachedAttribute, PTHREAD_CREATE_DETACHED);

			if (pthread_create(&threadId, &detachedAttribute, (void *) &main_passive, (void *) socktemp))
			{
				SOCK_ASSERT("Error creating the passive child thread", 1);
				pthread_attr_destroy(&detachedAttribute);
				continue;
			}

			pthread_attr_destroy(&detachedAttribute);
#else
			if ((pid = fork()) == 0)	// I am the child
			{
				main_passive((void *) socktemp);
				return;
			}
#endif
			tempaddrinfo = tempaddrinfo->ai_next;
		}

		freeaddrinfo(addrinfo);
	}

	// All the previous calls are no blocking, so the main line of execution goes here
	// and I have to avoid that the program terminates
	while (1)
		pthread_suspend(10*60*1000); // it wakes up every 10 minutes; it seems to me reasonable
}
コード例 #15
0
//Main program
int main(void) {
	int receivedBytes;//, sentBytes;
	//unsigned char outBuff[BUFFER_MAX];
	unsigned char inBuff[BUFFER_MAX];
	//Reserve memory
	char* cmd = malloc((BUFFER_MAX+1) * sizeof(char));
	char* par = malloc((BUFFER_MAX+1) * sizeof(char));
	struct commandStructure command;
	
	//Threads variables
	pthread_t logThread;
	pthread_t emailThread;
	//pthread_t debugThread;
	pthread_t webcamThread;
	pthread_t emailPhotoThread;
	
	//Empty buffers
	init();
	
	// Open serial file descriptor
	int fd = openSerial();
		
	//Loop to scan
	while(1){
		receivedBytes = read(fd,inBuff,BUFFER_MAX);
		if(receivedBytes > 0){						// Data found!
			
			if(DEBUG){ 
				printf("\nPayload size: %d\n",receivedBytes);
				int i;
				for(i=0;i<receivedBytes;i++){
					printf("%c",inBuff[i]);
				}
				printf("\n");
			}

			getCmd(inBuff,cmd);
			getPar(inBuff,par);
			int pars = parseParameters(par);
			
			if(!validCommand(cmd)){
				printf("Invalid Command: %s\n\n",cmd);
				continue;
			}else{	
				//printf("Command: %s\n",cmd);
				//int i = 0;
				//printf("Parameters found: %d\n",pars);
				//for(i=0;i<pars;i++) printf("Parameter %d - %s\n",i,PARAMETERS[i]);			
			}
			
			if(compareText(cmd,"Debug")){ //thread is detached so resources can be recycled.
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				
				time_t now = time(NULL);
				printf("%s - %s",PARAMETERS[0],ctime(&now));

				/*
				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&debugThread,&attr,debugFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
				*/
			}

			if(compareText(cmd,"Log")){
				if(pars < 1){
					printf("Error: No message sent\n");
					continue;
				}
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];

				//=======Call logFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&logThread,&attr,logFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
			if(compareText(cmd,"Email")){
				if(pars < 3){  //Need at least the email address and a subject
					printf("Error: Need 3 parameters: address, subject and message\n");
					continue;
				}
				
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				command.par[1] = PARAMETERS[1];
				command.par[2] = PARAMETERS[2];
				
				//=======Call logFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&emailThread,&attr,emailFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
			if(compareText(cmd,"Webcam")){
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];

				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&webcamThread,&attr,webcamFunc,&command))){
					fprintf(stderr,"Error: Could not create webcam thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);				
			}

			if(compareText(cmd,"EmailPhoto")){
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				command.par[1] = PARAMETERS[1];
				command.par[2] = PARAMETERS[2];

				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&emailPhotoThread,&attr,emailPhotoFunc,&command))){
					fprintf(stderr,"Error: Could not create emailPhoto thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
		}else if(receivedBytes == 0){				//No data yet! go back to loop
			continue;					
		}else if(receivedBytes < 0){				//Error reading, exit.
			printf("Error reading from file!\n");
			perror("Error: " );
			close(fd);
			return -1;
		}
		usleep(UDOONEO_POLL_DELAY);	// poll time approx 50mS (faster crashes the app)
	}

	//Free reserved memory
	free((void*)cmd);
	free((void*)par);

	//Close serial's file descriptor
	close(fd);	
}
コード例 #16
0
ファイル: client.c プロジェクト: gesslerpd/udp-chat
int main(int argc, char *argv[]) {
    bzero(sendBuffer, BUF_SIZE);        //zero out both buffers
    bzero(receiveBuffer, BUF_SIZE + USERNAME_LEN + 2);

    int portnum;
    char username[USERNAME_LEN];

    if(argc != 4) {
        fprintf(stderr, "Usage: %s [server] [portnum] [username]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    portnum = atoi(argv[2]);
    strncpy(username, argv[3], USERNAME_LEN);

    printf("server: %s\n", argv[1]);
    printf("port: %d\n", portnum);
    printf("username: %s\n", username);

    //allow server to resolve hostnames or use ip's
    struct hostent *server_host;

    if((server_host = gethostbyname(argv[1])) == NULL) {        // get the host info
        fprintf(stderr, "Failed to resolve server host information\n");
        exit(EXIT_FAILURE);
    }

    printf("Host: %s\n", server_host->h_name);
    printf("IP Address of host: %s\n", inet_ntoa((struct in_addr)
                                                 *((struct in_addr *)
                                                   server_host->h_addr)));

    struct sockaddr_in server_addr;     // server's internet address used for all sends and receives

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd == -1) {          //socket() returns -1 on error
        close(sockfd);
        fprintf(stderr, "Failed to get socket file descriptor\n");
        exit(EXIT_FAILURE);
    }

    server_addr.sin_family = AF_INET;   // host byte order
    server_addr.sin_port = htons(portnum);      // short, network byte order
    server_addr.sin_addr = *((struct in_addr *) server_host->h_addr);
    memset(&(server_addr.sin_zero), '\0', 8);   // zero the rest of the struct

    //Make connection to server socket so we can use send() and recv() to read and write the server
    if(connect(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) == SYSERR) {
        close(sockfd);
        fprintf(stderr, "Failed to connect to remote server!\n");
        exit(EXIT_FAILURE);
    }

    // Create and send out open message to the server so it knows our username and we are identified as a connected client
    strcpy(sendBuffer, username);
    if(send(sockfd, sendBuffer, strlen(sendBuffer), 0) == SYSERR) {
        perror("send");
        close(sockfd);
        exit(EXIT_FAILURE);
    }

    //create threads
    //Thread 1: takes in user input and sends out messages
    //Thread 2: listens for messages that are comming in from the server and prints them to screen
    // Set up threads
    pthread_t threads[2];
    pthread_attr_t attr;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    // Run the sender and receiver threads
    pthread_create(&threads[0], &attr, sender, NULL);
    pthread_create(&threads[1], &attr, receiver, NULL);

    // Wait until done is TRUE then exit program
    while(!done);

    close(sockfd);
    return OK;
}
コード例 #17
0
ファイル: taskLib.c プロジェクト: openrobots/pocolibs
/*
 * Create and initialize a new TCB,
 * and prepare pthread attributes for the new task
 */
static OS_TCB *
newTcb(const char *name, int priority, int options, int stackSize,
       pthread_attr_t *attr)
{
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
    int pagesize = getpagesize();
#endif
    int status;
    OS_TCB *tcb;
    char bufName[12];

    /*
     * Allocate a TCB
     */
    tcb = (OS_TCB *)malloc(sizeof(OS_TCB));
    if (tcb == NULL) {
	errnoSet(S_portLib_NO_MEMORY);
	return NULL;
    }
    tcb->starter = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
    if (tcb->starter == NULL) {
	    errnoSet(S_portLib_NO_MEMORY);
	    free(tcb);
	    return NULL;
    }
    /* Startup mutex - guaranties the the tcb isn't destroyed early */
    pthread_mutex_init(tcb->starter, NULL);
    pthread_mutex_lock(tcb->starter);
    tcb->magic = TASK_MAGIC;
    if (name != NULL) {
	tcb->name = strdup(name);
    } else {
	snprintf(bufName, sizeof(bufName), "t%d", ++numTask);
	tcb->name = strdup(bufName);
    }
#ifdef DEBUG
    printf("taskSpawn \"%s\" %p\n", tcb->name, tcb);
#endif
    tcb->options = options;
    tcb->errorStatus = 0;
    tcb->pid = getpid();
    tcb->userData = 0; /* XXX */

    /*
     * Initialize thread attributes
     */
    status = pthread_attr_init(attr);
    if (status != 0) {
	errnoSet(status);
	return NULL;
    }
    status = pthread_attr_setdetachstate(attr,
					 PTHREAD_CREATE_DETACHED);
    if (status != 0) {
	errnoSet(status);
	pthread_mutex_unlock(tcb->starter);
	return NULL;
    }
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
    /*
     * If supported, determine the default stack size and report
     * it, and then select a stack size for the new thread.
     *
     * Note that the standard does not specify the default stack
     * size, and the default value in an attributes object need
     * not be the size that will actually be used.  Solaris 2.5
     * uses a value of 0 to indicate the default.
     */
    /* give at least 64kB */
    if (stackSize < 65536) stackSize = 65536;
#ifdef PTHREAD_STACK_MIN
    if (stackSize < PTHREAD_STACK_MIN)
	    stackSize = PTHREAD_STACK_MIN;
#endif
    /* round to page size */
    if (stackSize % pagesize != 0)
	stackSize = stackSize - (stackSize % pagesize) + pagesize;
    status = pthread_attr_setstacksize(attr, stackSize);
    if (status != 0) {
	errnoSet(status);
	pthread_mutex_unlock(tcb->starter);
	return NULL;
    }
#endif
#ifdef HAVE_PTHREAD_ATTR_SETSCHEDPOLICY
    if (rr_min_priority > 0 && rr_min_priority > 0) {
	    struct sched_param thread_param;

	    /* Set priority of new thread */
	    thread_param.sched_priority = priorityVxToPosix(priority);
	    status = pthread_attr_setschedpolicy(attr, SCHED_RR);
	    switch (status) {
	    case 0:
		    /* set policy is ok, set priority */
		    status = pthread_attr_setschedparam(attr,
							&thread_param);
		    if (status != 0) {
			    errnoSet(status);
			    pthread_mutex_unlock(tcb->starter);
			    return NULL;
		    }
		    break;

#ifdef ENOTSUP
# if ENOTSUP != EOPNOTSUPP
	    case ENOTSUP:
# endif
#endif /* ENOTSUP */
	    case EOPNOTSUPP:
		    /* fprintf(stderr,
		       "warning: cannot set RR scheduling policy\n"); */
		    break;

	    default :
		    errnoSet(status);
		    pthread_mutex_unlock(tcb->starter);
		    return NULL;
	    } /* switch */
    }
#endif /* HAVE_PTHREAD_ATTR_SETSCHEDPOLICY */
    return tcb;
}
コード例 #18
0
ファイル: loadbalancer.c プロジェクト: chintanvadgama/Twitter
int main (int argc, char **argv)
{
    int msock, ssock,clen;
    int portno;

    struct sockaddr_in server,cli;
    struct hostent *cli_details;
    char *caddr;
    pthread_t tid;
    pthread_attr_t tattr;

    //Test for correct number of arguments
    if (argc != 2)
    {
        fprintf(stderr, "Usage: <filename> <port>");
        exit(0);
    }



    primary_server = (server_dt *)malloc(sizeof(server_dt));
    secondary_server = (server_dt *)malloc(sizeof(server_dt));

    primary_server->server_id = 1;
    strcpy(primary_server->ipaddr , "127.0.0.1");
    primary_server->port = 6001;
    primary_server->count = 0;
    primary_server->is_alive = 0;

    secondary_server->server_id = 2;
    strcpy(secondary_server->ipaddr ,"127.0.0.1");
    secondary_server->port = 7001;
    secondary_server->count = 0;
    secondary_server->is_alive = 0;


    //server_dt *global_server;
    //global_server = (server_dt *)malloc(sizeof(server_dt));

    portno = atoi(argv[1]);//convert  port # to int
    server.sin_family = AF_INET;
    server.sin_port = htons(portno);
    server.sin_addr.s_addr = htonl(INADDR_ANY);//use client ip address

    msock = socket(PF_INET,SOCK_STREAM,0);
    if (msock<0) {
      printf("Error: Failed to create a socket.\n");
      exit(0);
    }

    if (bind(msock, (struct sockaddr *)&server, sizeof(server)) < 0) {
        printf("Error: Failed to bind the socket.\n");
        exit(0);
    }
    

    if (listen(msock,5) <0) {
        printf("Error: Failed to set socket in passive mode.\n");
        exit(0);
    }

    printf("\n\n****************LOAD BALANCER********************\n\n");

    while(1) {

      clen = sizeof(cli);
      if ((ssock = accept(msock, (struct sockaddr *)&cli,&clen)) < 0) {
        printf("Error: Failed to connect to remote host.\n");
        exit(0);
      }

      cli_details = gethostbyaddr((const char *)&cli.sin_addr.s_addr, sizeof(cli.sin_addr.s_addr), PF_INET);
      caddr = inet_ntoa(cli.sin_addr);

     // printf("Connection established to %s(%s) \n",(char *)cli_details->h_name,caddr);


      /*args_dt *args = (args_dt *)malloc(sizeof(args_dt));
      args->sock = ssock;
      args->server_list[0] = primary_server;
      args->server_list[1] = secondary_server;*/

      pthread_attr_init(&tattr);
      pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_DETACHED);
      pthread_mutex_init(&primary_server->th_mutex,0);
      pthread_mutex_init(&secondary_server->th_mutex,0);


      if (pthread_create(&tid,&tattr,(void*(*)(void *))session,(void *)ssock) < 0) {
        printf("Error: Failed to create a new thread.\n");
        exit(0);
      }
    }
    return 0;
}
コード例 #19
0
ファイル: socketserver.cpp プロジェクト: JavierQuerol/upnpx
int SocketServer::Start(){
    int ret = 0;
    u32 optval = 0;
    int cnt = 0;

    //Get the IP Address
    ret = getLocalIPAddress(ipAddress, 10);
    if(ret != 0){
        return -1;
    }

    mServerSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (mServerSocket < 0){
        return -1;
    }

    //Set nonblocking
    optval = fcntl( mServerSocket, F_GETFL, 0 );
    STATNVAL(optval, -1,  CLEAN_AND_EXIT);
    ret = fcntl(mServerSocket, F_SETFL, optval | O_NONBLOCK);
    STATNVAL(ret, -1,  CLEAN_AND_EXIT);

    memset((char*)&mServerAddr, 0, sizeof(struct sockaddr_in));

    //Bind to port (try 500 numbers if needed)
    cnt = 0;
    do{
        mServerAddr.sin_family = AF_INET;
        mServerAddr.sin_addr.s_addr = INADDR_ANY;
        mServerAddr.sin_port = htons(mPort);

        if (bind(mServerSocket, (struct sockaddr *) &mServerAddr, sizeof(struct sockaddr_in)) == 0){
            break;
        }
        mPort++;
        cnt++;
    }while(cnt < 500);

    if(cnt == 500){
        return -2;
    }

    //Start the read thread
    pthread_attr_t  attr;
    ret = pthread_attr_init(&attr);
    if(ret != 0){
        goto CLEAN_AND_EXIT;
    }
    ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    if(ret != 0){
        pthread_attr_destroy(&attr);
        goto CLEAN_AND_EXIT;
    }

    ret = pthread_create(&mReadThread, &attr, SocketServer::sReadLoop, (void*)this);

    pthread_attr_destroy(&attr);

    goto EXIT;

CLEAN_AND_EXIT:
    close(mServerSocket);
    mServerSocket = INVALID_SOCKET;

EXIT:
    return ret;
}
コード例 #20
0
ファイル: problem2.c プロジェクト: titusjung/ee451
int main(int argc, char** argv){
   int i,j,k;
    FILE *fp;

    //int p =4; 

	pthread_attr_t attr;

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

	if(argc<2)
	{
		printf("not enough arguemnets\n");
		return 1; 
	}
	int p = atoi(argv[1]);

	volatile struct thread_data thread_data_array[p]; 

	pthread_t threads[p]; 

  	a = (unsigned char*) malloc (sizeof(unsigned char)*h*w);
    
	// the matrix is stored in a linear array in row major fashion
	if (!(fp=fopen(input_file, "rb"))) {
		printf("can not opern file\n");
		return 1;
	}
	fread(a, sizeof(unsigned char), w*h, fp);
	fclose(fp);
    
	// measure the start time here
	//  Your code goes here
	struct timespec start, stop; 
	double time;		
	if( clock_gettime(CLOCK_REALTIME, &start) == -1) { perror("clock gettime");}


	uArray[0] =0; 
	uArray[1] =65; 
	uArray[2] =100; 
	uArray[3] =125; 
	uArray[4] = 190;
	uArray[5] =255; 

	volatile double sumArray[nClusters];
	volatile double noArray[nClusters];

	for(i=0; i<nClusters; i++)
	{
		sumArray[i]=0; 
		noArray[i]=0 ; 
		for(j=0; j<p; j++)
		{
			thread_data_array[j].sumArray[i]=0; 
			thread_data_array[j].noArray[i]=0; 
		}

	}


	int tt, rc; 

	for(i=0; i<nRuns; i++)
	{
		for(j=0; j<p; j++)
		{
			thread_data_array[j].thread_id = j;
			thread_data_array[j].startIndex = j*w*h/p; 
			thread_data_array[j].endIndex = (j+1)*w*h/p-1; 	

			rc = pthread_create(&threads[j],&attr,processArray,(void *) &thread_data_array[j]);
			if(rc)
			{
				printf("ERROR; return code from pthread_create() is%d\n", rc);
				exit(-1); 
			}
		}
		for(tt = 0; tt< p; tt++)
		{
			rc = pthread_join(threads[tt],NULL); 
			if(rc)
			{
				printf("ERROR; joining thread error return code from pthread_create() is%d\n", rc);
				exit(-1); 
			}			 
		}
		for(tt = 0; tt< p; tt++)
		{
			for(j=0; j<nClusters; j++)
			{
				//uArray[j] = (double)thread_data_array[tt].sumArray[j]/(double) thread_data_array[tt].noArray[j]; 
				sumArray[j]+=thread_data_array[tt].sumArray[j]; 
				noArray[j]+=thread_data_array[tt].noArray[j]; 
				thread_data_array[tt].sumArray[j]=0; 
				thread_data_array[tt].noArray[j]=0; 
			}
		}

		for(j=0; j<nClusters; j++)
		{
			uArray[j] = sumArray[j]/noArray[j]; 
			sumArray[j]=0; 
			noArray[j]=0 ; 
		}

	}
	pthread_attr_destroy(&attr);

	//
	for(j=0; j<h*w; j++)
	{

		int element = a[j];
		int clusterIndex = indexToClosestu(element);
		a[j]=uArray[clusterIndex]; 
		
	}	
	// measure the end time here
	
	// print out the execution time here
	if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) { perror("clock gettime");}		
	time = (stop.tv_sec - start.tv_sec)+ (double)(stop.tv_nsec - start.tv_nsec)/1e9;
	
	printf(" Execution time = %f sec", time);			
	
	if (!(fp=fopen(output_file,"wb"))) {
		printf("can not opern file\n");
		return 1;
	}	
	fwrite(a, sizeof(unsigned char),w*h, fp);
    fclose(fp);
    
    return 0;
}
コード例 #21
0
ファイル: verse.c プロジェクト: laishi/verse
int32_t vrs_send_connect_request(const char *hostname,
		const char *service,
		const uint16_t flags,
		uint8_t *session_id)
{
	struct VSession *vsession = NULL;
	uint16 _flags = flags;
	int already_connected = 0, i, ret;

	/* Check if CTX was initialized (initialization is done) */
	if(vc_ctx == NULL) {
		v_print_log(VRS_PRINT_ERROR,
				"Basic callback functions were not set.\n");
		return VRS_NO_CB_FUNC;
	} else {
		/* Check if all needed callback functions was set up */
		if(vc_ctx->vfs.receive_connect_accept == NULL) {
			v_print_log(VRS_PRINT_ERROR,
					"receive_connect_accept() callback functions was not set.\n");
			return VRS_NO_CB_CONN_FUNC;
		}
		if(vc_ctx->vfs.receive_connect_terminate == NULL) {
			v_print_log(VRS_PRINT_ERROR,
					"receive_connect_terminate() callback functions was not set.\n");
			return VRS_NO_CB_TERM_FUNC;
		}
		if(vc_ctx->vfs.receive_user_authenticate == NULL) {
			v_print_log(VRS_PRINT_ERROR,
					"receive_user_authenticat() callback functions was not set.\n");
			return VRS_NO_CB_USER_AUTH;
		}
	}

	/* Set security protocol */
#if OPENSSL_VERSION_NUMBER >= 0x10000000
	/* Check consistency of flags */
	if((_flags & VRS_SEC_DATA_NONE) && (_flags & VRS_SEC_DATA_TLS)) {
		if(is_log_level(VRS_PRINT_ERROR))
			v_print_log(VRS_PRINT_ERROR,
					"VRS_SEC_DATA_NONE or VRS_SEC_DATA_TLS could be set, not both.\n");
		return VRS_FAILURE;
	}
#else
	if (_flags & VRS_SEC_DATA_TLS) {
		v_print_log(VRS_PRINT_WARNING,
				"flag VRS_SEC_DATA_TLS could be set due to low version of OpenSSL (at least 1.0 is required).\n");
		_flags &= ~VRS_SEC_DATA_TLS;
		_flags |= VRS_SEC_DATA_NONE;
	}
#endif

	/* Set transport protocol */
	if((_flags & VRS_TP_UDP) && (_flags & VRS_TP_TCP)) {
		if(is_log_level(VRS_PRINT_ERROR))
			v_print_log(VRS_PRINT_ERROR,
					"VRS_TP_UDP or VRS_TP_TCP could be set, not both.\n");
		return VRS_FAILURE;
	} else if(!(_flags & VRS_TP_UDP) && !(_flags & VRS_TP_TCP)) {
		/* When no transport protocol is selected, then use UDP as default */
		_flags |= VRS_TP_UDP;
	}

	pthread_mutex_lock(&vc_ctx->mutex);

	/* Check if this client isn't already connected to this server or isn't
	 * trying to connect to the server with hostname:service */
	for(i=0; i<vc_ctx->max_sessions; i++) {
		if(vc_ctx->vsessions[i] != NULL) {
			if(strcmp(vc_ctx->vsessions[i]->peer_hostname, hostname) == 0 &&
					strcmp(vc_ctx->vsessions[i]->service, service) == 0) {
				v_print_log(VRS_PRINT_ERROR, "Client already connected to this server.\n");
				already_connected = 1;
				break;
			}
		}
	}

	if(already_connected == 0) {
		/* Try to find free verse session slot */
		for(i=0; i<vc_ctx->max_sessions; i++) {
			/* When free VSession slot is found, then create new session */
			if(vc_ctx->vsessions[i]==NULL) {
				vsession = (struct VSession*)malloc(sizeof(struct VSession));
				v_init_session(vsession);
				vsession->peer_hostname = strdup(hostname);
				vsession->service = strdup(service);
				/* Copy flags */
				vsession->flags = _flags;
				vsession->in_queue = (struct VInQueue*)calloc(1, sizeof(VInQueue));
				v_in_queue_init(vsession->in_queue, IN_QUEUE_DEFAULT_MAX_SIZE);
				vsession->out_queue = (struct VOutQueue*)calloc(1, sizeof(VOutQueue));
				v_out_queue_init(vsession->out_queue, OUT_QUEUE_DEFAULT_MAX_SIZE);

				vc_ctx->vsessions[i] = vsession;
				break;
			}
		}
	}

	pthread_mutex_unlock(&vc_ctx->mutex);

	if(already_connected == 1) {
		return VRS_FAILURE;
	}

	/* Check if we found free slot for new session */
	if(vsession == NULL) {
		v_print_log(VRS_PRINT_DEBUG_MSG,
				"Maximal count of sessions: %d reached.\n",
				vc_ctx->max_sessions);
		return VRS_FAILURE;
	}

	vsession->session_id = vc_ctx->session_counter++;
	*session_id = vsession->session_id;

	/* Try to initialize thread attributes */
	if( (ret = pthread_attr_init(&vsession->tcp_thread_attr)) != 0 ) {
		if(is_log_level(VRS_PRINT_ERROR)) v_print_log(VRS_PRINT_ERROR, "pthread_attr_init(): %s\n", strerror(errno));
		return VRS_FAILURE;
	}

	/* Try to set thread attributes as detached */
	if( (ret = pthread_attr_setdetachstate(&vsession->tcp_thread_attr, PTHREAD_CREATE_DETACHED)) != 0) {
		if(is_log_level(VRS_PRINT_ERROR)) v_print_log(VRS_PRINT_ERROR, "pthread_attr_setdetachstate(): %s\n", strerror(errno));
		return VRS_FAILURE;
	}

	/* Create thread for new client session */
	if(pthread_create(&vsession->tcp_thread, &vsession->tcp_thread_attr, vc_new_session_thread, (void*)vsession) != 0) {
		if(is_log_level(VRS_PRINT_ERROR)) v_print_log(VRS_PRINT_ERROR, "Thread creation failed.\n");
		pthread_mutex_lock(&vc_ctx->mutex);
		v_destroy_session(vsession);
		free(vsession);
		vc_ctx->vsessions[i] = NULL;
		pthread_mutex_unlock(&vc_ctx->mutex);
		return VRS_FAILURE;
	}

	/* Destroy thread attributes */
	pthread_attr_destroy(&vsession->tcp_thread_attr);

	return VRS_SUCCESS;
}
コード例 #22
0
        virtual void accepted(MessagingPort * p) {

            if ( ! connTicketHolder.tryAcquire() ) {
                log() << "connection refused because too many open connections: " << connTicketHolder.used() << endl;

                // TODO: would be nice if we notified them...
                p->shutdown();
                delete p;

                sleepmillis(2); // otherwise we'll hard loop
                return;
            }

            try {
#ifndef __linux__  // TODO: consider making this ifdef _WIN32
                boost::thread thr( boost::bind( &pms::threadRun , p ) );
#else
                pthread_attr_t attrs;
                pthread_attr_init(&attrs);
                pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);

                static const size_t STACK_SIZE = 1024*1024; // if we change this we need to update the warning

                struct rlimit limits;
                verify(15887, getrlimit(RLIMIT_STACK, &limits) == 0);
                if (limits.rlim_cur > STACK_SIZE) {
                    pthread_attr_setstacksize(&attrs, (DEBUG_BUILD
                                                        ? (STACK_SIZE / 2)
                                                        : STACK_SIZE));
                } else if (limits.rlim_cur < 1024*1024) {
                    warning() << "Stack size set to " << (limits.rlim_cur/1024) << "KB. We suggest 1MB" << endl;
                }


                pthread_t thread;
                int failed = pthread_create(&thread, &attrs, (void*(*)(void*)) &pms::threadRun, p);

                pthread_attr_destroy(&attrs);

                if (failed) {
                    log() << "pthread_create failed: " << errnoWithDescription(failed) << endl;
                    throw boost::thread_resource_error(); // for consistency with boost::thread
                }
#endif
            }
            catch ( boost::thread_resource_error& ) {
                connTicketHolder.release();
                log() << "can't create new thread, closing connection" << endl;

                p->shutdown();
                delete p;

                sleepmillis(2);
            }
            catch ( ... ) {
                connTicketHolder.release();
                log() << "unknown error accepting new socket" << endl;

                p->shutdown();
                delete p;

                sleepmillis(2);
            }

        }
コード例 #23
0
ファイル: gmetad.c プロジェクト: AsherBond/monitor-core
int
main ( int argc, char *argv[] )
{
   int rc;
   struct stat struct_stat;
   pthread_t pid;
   pthread_attr_t attr;
   int i, num_sources;
   uid_t gmetad_uid;
   mode_t rrd_umask;
   char * gmetad_username;
   struct passwd *pw;
   gmetad_config_t *c = &gmetad_config;
   apr_interval_time_t sleep_time;
   apr_time_t last_metadata;
   double random_sleep_factor;
   unsigned int rand_seed;

   rc = apr_initialize();
   if (rc != APR_SUCCESS) {
      return -1;
   }

   /* create a memory pool. */
   apr_pool_create(&global_context, NULL);

   /* Ignore SIGPIPE */
   signal( SIGPIPE, SIG_IGN );

   initialize_scoreboard();

   /* Mark the time this gmetad started */
   started = apr_time_now();

   if (cmdline_parser(argc, argv, &args_info) != 0)
      err_quit("command-line parser error");

   num_sources = number_of_datasources( args_info.conf_arg );
   if(!num_sources)
      {
         err_quit("%s doesn't have any data sources specified", args_info.conf_arg);
      }

   memset(&root, 0, sizeof(root));
   root.id = ROOT_NODE;

   /* Get the real number of data sources later */
   sources = hash_create( num_sources + 10 );
   if (! sources )
      {
         err_quit("Unable to create sources hash\n");
      }

   root.authority = hash_create( num_sources + 10 );
   if (!root.authority)
      {
         err_quit("Unable to create root authority (our grids and clusters) hash\n");
      }

   root.metric_summary = hash_create (DEFAULT_METRICSIZE);
   if (!root.metric_summary)
      {
         err_quit("Unable to create root summary hash");
      }

   parse_config_file ( args_info.conf_arg );
    /* If given, use command line directives over config file ones. */
   if (args_info.debug_given)
      {
         c->debug_level = args_info.debug_arg;
      }
   debug_level = c->debug_level;
   set_debug_msg_level(debug_level);

   /* Setup our default authority pointer if the conf file hasnt yet.
    * Done in the style of hash node strings. */
   if (!root.stringslen)
      {
         gethostname(hostname, HOSTNAMESZ);
         root.authority_ptr = 0;
         sprintf(root.strings, "http://%s/ganglia/", hostname);
         root.stringslen += strlen(root.strings) + 1;
      }

   rand_seed = apr_time_now() * (int)pthread_self();
   for(i = 0; i < root.stringslen; rand_seed = rand_seed * root.strings[i++]);

   /* Debug level 1 is error output only, and no daemonizing. */
   if (!debug_level)
      {
         rrd_umask = c->umask;
         daemon_init (argv[0], 0, rrd_umask);
      }

   if (args_info.pid_file_given)
     {
       update_pidfile (args_info.pid_file_arg);
     }

   /* The rrd_rootdir must be writable by the gmetad process */
   if( c->should_setuid )
      {
         if(! (pw = getpwnam(c->setuid_username)))
            {
               err_sys("Getpwnam error");
            }
         gmetad_uid = pw->pw_uid;
         gmetad_username = c->setuid_username;
      }
   else
      {
         gmetad_uid = getuid();
         if(! (pw = getpwuid(gmetad_uid)))
            {
               err_sys("Getpwnam error");
            } 
         gmetad_username = strdup(pw->pw_name);
      }

   debug_msg("Going to run as user %s", gmetad_username);
   if( c->should_setuid )
      {
         become_a_nobody(c->setuid_username);
      }

   if( c->write_rrds )
      {
         if( stat( c->rrd_rootdir, &struct_stat ) )
            {
                err_sys("Please make sure that %s exists", c->rrd_rootdir);
            }
         if ( struct_stat.st_uid != gmetad_uid )
            {
                err_quit("Please make sure that %s is owned by %s", c->rrd_rootdir, gmetad_username);
            }
         if (! (struct_stat.st_mode & S_IWUSR) )
            {
                err_quit("Please make sure %s has WRITE permission for %s", gmetad_username, c->rrd_rootdir);
            }
      }

   if(debug_level)
      {
         fprintf(stderr,"Sources are ...\n");
         hash_foreach( sources, print_sources, NULL);
      }

#ifdef WITH_MEMCACHED
   if (c->memcached_parameters != NULL)
      {
         memcached_connection_pool = memcached_pool(c->memcached_parameters, strlen(c->memcached_parameters));
      }
#endif /* WITH_MEMCACHED */

   server_socket = g_tcp_socket_server_new( c->xml_port );
   if (server_socket == NULL)
      {
         err_quit("tcp_listen() on xml_port failed");
      }
   debug_msg("xml listening on port %d", c->xml_port);
   
   interactive_socket = g_tcp_socket_server_new( c->interactive_port );
   if (interactive_socket == NULL)
      {
         err_quit("tcp_listen() on interactive_port failed");
      }
   debug_msg("interactive xml listening on port %d", c->interactive_port);

    /* Forward metrics to Graphite using carbon protocol */
    if (c->carbon_server != NULL)
      {
         if (!strcmp(c->carbon_protocol, "udp"))
            {
               carbon_udp_socket = init_carbon_udp_socket (c->carbon_server, c->carbon_port);

               if (carbon_udp_socket == NULL)
                  err_quit("carbon %s socket failed for %s:%d", c->carbon_protocol, c->carbon_server, c->carbon_port);
            }
         debug_msg("carbon forwarding ready to send via %s to %s:%d", c->carbon_protocol, c->carbon_server, c->carbon_port);
      }

#ifdef WITH_RIEMANN
    if (c->riemann_server !=NULL)
      {
         if (!strcmp(c->riemann_protocol, "udp"))
            {
               riemann_udp_socket = init_riemann_udp_socket (c->riemann_server, c->riemann_port);

               if (riemann_udp_socket == NULL)
                  err_quit("[riemann] %s socket failed for %s:%d", c->riemann_protocol, c->riemann_server, c->riemann_port);
            } else if (!strcmp(c->riemann_protocol, "tcp")) {
                riemann_tcp_socket = init_riemann_tcp_socket (c->riemann_server, c->riemann_port);
                if (riemann_tcp_socket == NULL) {
                   riemann_circuit_breaker = RIEMANN_CB_OPEN;
                   riemann_reset_timeout = apr_time_now () + RIEMANN_RETRY_TIMEOUT * APR_USEC_PER_SEC;
                } else {
                   riemann_circuit_breaker = RIEMANN_CB_CLOSED;
                   riemann_failures = 0;
                }
            } else {
                err_quit("ERROR: Riemann protocol must be 'udp' or 'tcp'");
            }
         debug_msg("[riemann] Ganglia configured to forward metrics via %s to %s:%d", c->riemann_protocol, c->riemann_server, c->riemann_port);
      }
#endif /* WITH_RIEMANN */

   /* initialize summary mutex */
   root.sum_finished = (pthread_mutex_t *) 
                          malloc(sizeof(pthread_mutex_t));
   pthread_mutex_init(root.sum_finished, NULL);

   pthread_attr_init( &attr );
   pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );

   /* Spin off the non-interactive server threads. (Half as many as interactive). */
   for (i=0; i < c->server_threads/2; i++)
      pthread_create(&pid, &attr, server_thread, (void*) 0);

   /* Spin off the interactive server threads. */
   for (i=0; i < c->server_threads; i++)
      pthread_create(&pid, &attr, server_thread, (void*) 1);

   hash_foreach( sources, spin_off_the_data_threads, NULL );

   /* A thread to cleanup old metrics and hosts */
   pthread_create(&pid, &attr, cleanup_thread, (void *) NULL);
   debug_msg("cleanup thread has been started");

#ifdef WITH_RIEMANN
   if (!strcmp(c->riemann_protocol, "tcp")) {
      /* A thread to re-poll riemann TCP port if circuit breaker tripped */
      pthread_create(&pid, &attr, circuit_breaker_thread, (void *) NULL);
      debug_msg ("[riemann] circuit breaker thread has been started");
   }
#endif /* WITH_RIEMANN */

    /* Meta data */
   last_metadata = apr_time_now();
   for(;;)
      {
         /* Do at a random interval, between 
                 (shortest_step/2) +/- METADATA_SLEEP_RANDOMIZE percent */
         random_sleep_factor = (1 + (METADATA_SLEEP_RANDOMIZE / 50.0) * ((rand_r(&rand_seed) - RAND_MAX/2)/(float)RAND_MAX));
         sleep_time = random_sleep_factor * apr_time_from_sec(c->shortest_step) / 2;
         /* Make sure the sleep time is at least 1 second */
         if(apr_time_sec(apr_time_now() + sleep_time) < (METADATA_MINIMUM_SLEEP + apr_time_sec(apr_time_now())))
            sleep_time += apr_time_from_sec(METADATA_MINIMUM_SLEEP);
         apr_sleep(sleep_time);

         /* Need to be sure root is locked while doing summary */
         pthread_mutex_lock(root.sum_finished);

         /* Flush the old values */
         hash_foreach(root.metric_summary, zero_out_summary, NULL);
         root.hosts_up = 0;
         root.hosts_down = 0;

         /* Sum the new values */
         hash_foreach(root.authority, do_root_summary, NULL );

         /* summary completed */
         pthread_mutex_unlock(root.sum_finished);

         /* Save them to RRD */
         hash_foreach(root.metric_summary, write_root_summary, NULL);

         /* Remember our last run */
         last_metadata = apr_time_now();
      }

   apr_pool_destroy(global_context);

   apr_terminate();
   return 0;
}
コード例 #24
0
static void *
handle_fildes_io (void *arg)
{
  pthread_t self = pthread_self ();
  struct sched_param param;
  struct requestlist *runp = (struct requestlist *) arg;
  aiocb_union *aiocbp;
  int policy;
  int fildes;

  pthread_getschedparam (self, &policy, &param);

  do
    {
      /* If runp is NULL, then we were created to service the work queue
	 in general, not to handle any particular request. In that case we
	 skip the "do work" stuff on the first pass, and go directly to the
	 "get work off the work queue" part of this loop, which is near the
	 end. */
      if (runp == NULL)
	pthread_mutex_lock (&__aio_requests_mutex);
      else
	{
	  /* Hopefully this request is marked as running.  */
	  assert (runp->running == allocated);

	  /* Update our variables.  */
	  aiocbp = runp->aiocbp;
	  fildes = aiocbp->aiocb.aio_fildes;

	  /* Change the priority to the requested value (if necessary).  */
	  if (aiocbp->aiocb.__abs_prio != param.sched_priority
	      || aiocbp->aiocb.__policy != policy)
	    {
	      param.sched_priority = aiocbp->aiocb.__abs_prio;
	      policy = aiocbp->aiocb.__policy;
	      pthread_setschedparam (self, policy, &param);
	    }

	  /* Process request pointed to by RUNP.  We must not be disturbed
	     by signals.  */
	  if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_READ)
	    {
	      if (sizeof (off_t) != sizeof (off64_t)
		  && aiocbp->aiocb.aio_lio_opcode & 128)
		aiocbp->aiocb.__return_value =
		  TEMP_FAILURE_RETRY (__pread64 (fildes, (void *)
						 aiocbp->aiocb64.aio_buf,
						 aiocbp->aiocb64.aio_nbytes,
						 aiocbp->aiocb64.aio_offset));
	      else
		aiocbp->aiocb.__return_value =
		  TEMP_FAILURE_RETRY (__libc_pread (fildes,
						    (void *)
						    aiocbp->aiocb.aio_buf,
						    aiocbp->aiocb.aio_nbytes,
						    aiocbp->aiocb.aio_offset));

	      if (aiocbp->aiocb.__return_value == -1 && errno == ESPIPE)
		/* The Linux kernel is different from others.  It returns
		   ESPIPE if using pread on a socket.  Other platforms
		   simply ignore the offset parameter and behave like
		   read.  */
		aiocbp->aiocb.__return_value =
		  TEMP_FAILURE_RETRY (read (fildes,
					    (void *) aiocbp->aiocb64.aio_buf,
					    aiocbp->aiocb64.aio_nbytes));
	    }
	  else if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_WRITE)
	    {
	      if (sizeof (off_t) != sizeof (off64_t)
		  && aiocbp->aiocb.aio_lio_opcode & 128)
		aiocbp->aiocb.__return_value =
		  TEMP_FAILURE_RETRY (__pwrite64 (fildes, (const void *)
						  aiocbp->aiocb64.aio_buf,
						  aiocbp->aiocb64.aio_nbytes,
						  aiocbp->aiocb64.aio_offset));
	      else
		aiocbp->aiocb.__return_value =
		  TEMP_FAILURE_RETRY (__libc_pwrite (fildes, (const void *)
					      aiocbp->aiocb.aio_buf,
					      aiocbp->aiocb.aio_nbytes,
					      aiocbp->aiocb.aio_offset));

	      if (aiocbp->aiocb.__return_value == -1 && errno == ESPIPE)
		/* The Linux kernel is different from others.  It returns
		   ESPIPE if using pwrite on a socket.  Other platforms
		   simply ignore the offset parameter and behave like
		   write.  */
		aiocbp->aiocb.__return_value =
		  TEMP_FAILURE_RETRY (write (fildes,
					     (void *) aiocbp->aiocb64.aio_buf,
					     aiocbp->aiocb64.aio_nbytes));
	    }
	  else if (aiocbp->aiocb.aio_lio_opcode == LIO_DSYNC)
	    aiocbp->aiocb.__return_value =
	      TEMP_FAILURE_RETRY (fdatasync (fildes));
	  else if (aiocbp->aiocb.aio_lio_opcode == LIO_SYNC)
	    aiocbp->aiocb.__return_value =
	      TEMP_FAILURE_RETRY (fsync (fildes));
	  else
	    {
	      /* This is an invalid opcode.  */
	      aiocbp->aiocb.__return_value = -1;
	      __set_errno (EINVAL);
	    }

	  /* Get the mutex.  */
	  pthread_mutex_lock (&__aio_requests_mutex);

	  if (aiocbp->aiocb.__return_value == -1)
	    aiocbp->aiocb.__error_code = errno;
	  else
	    aiocbp->aiocb.__error_code = 0;

	  /* Send the signal to notify about finished processing of the
	     request.  */
	  __aio_notify (runp);

	  /* For debugging purposes we reset the running flag of the
	     finished request.  */
	  assert (runp->running == allocated);
	  runp->running = done;

	  /* Now dequeue the current request.  */
	  __aio_remove_request (NULL, runp, 0);
	  if (runp->next_prio != NULL)
	    add_request_to_runlist (runp->next_prio);

	  /* Free the old element.  */
	  __aio_free_request (runp);
	}

      runp = runlist;

      /* If the runlist is empty, then we sleep for a while, waiting for
	 something to arrive in it. */
      if (runp == NULL && optim.aio_idle_time >= 0)
	{
	  struct timeval now;
	  struct timespec wakeup_time;

	  ++idle_thread_count;
	  __gettimeofday (&now, NULL);
	  wakeup_time.tv_sec = now.tv_sec + optim.aio_idle_time;
	  wakeup_time.tv_nsec = now.tv_usec * 1000;
	  if (wakeup_time.tv_nsec >= 1000000000)
	    {
	      wakeup_time.tv_nsec -= 1000000000;
	      ++wakeup_time.tv_sec;
	    }
	  pthread_cond_timedwait (&__aio_new_request_notification,
				  &__aio_requests_mutex,
				  &wakeup_time);
	  --idle_thread_count;
	  runp = runlist;
	}

      if (runp == NULL)
	--nthreads;
      else
	{
	  assert (runp->running == yes);
	  runp->running = allocated;
	  runlist = runp->next_run;

	  /* If we have a request to process, and there's still another in
	     the run list, then we need to either wake up or create a new
	     thread to service the request that is still in the run list. */
	  if (runlist != NULL)
	    {
	      /* There are at least two items in the work queue to work on.
		 If there are other idle threads, then we should wake them
		 up for these other work elements; otherwise, we should try
		 to create a new thread. */
	      if (idle_thread_count > 0)
		pthread_cond_signal (&__aio_new_request_notification);
	      else if (nthreads < optim.aio_threads)
		{
		  pthread_t thid;
		  pthread_attr_t attr;

		  /* Make sure the thread is created detached.  */
		  pthread_attr_init (&attr);
		  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);

		  /* Now try to start a thread. If we fail, no big deal,
		     because we know that there is at least one thread (us)
		     that is working on AIO operations. */
		  if (pthread_create (&thid, &attr, handle_fildes_io, NULL)
		      == 0)
		    ++nthreads;
		}
	    }
	}

      /* Release the mutex.  */
      pthread_mutex_unlock (&__aio_requests_mutex);
    }
  while (runp != NULL);

  return NULL;
}
コード例 #25
0
ファイル: test_weighted_rr.c プロジェクト: bbiiggppiigg/rts
int main(int argc, char *argv[])
{
	struct sched_param param;
	struct thread_args *targs;
	pthread_attr_t attr;
	int i;
	char cur;
	srand(time(NULL));	
	if( argc != 5 )
		fail("Invalid arguments count");
	
	//+ parse arguments
	if( strcmp(argv[1], "default") == 0 )
		sched_policy = SCHED_NORMAL;
	else if( strcmp(argv[1], "weighted_rr") == 0 )
		sched_policy = SCHED_WEIGHTED_RR;
	else
		fail("Invalid scheduling policy");
	
	quantum = atoi(argv[2]);
	num_threads = atoi(argv[3]);
	buffer_size = atoi(argv[4]);

	printf("sched_policy: %d, quantum: %d, num_threads: %d, buffer_size: %d\n", sched_policy, quantum, num_threads, buffer_size);
	fflush(stdout);
	
	//+ set weighted rr scheduling policy
	if (sched_policy == SCHED_WEIGHTED_RR)
	{
		param.sched_priority = 0;
		if ( sched_setscheduler(getpid(), sched_policy, &param) == -1)
		{
			perror("sched_setscheduler");
			fail("sched_setscheduler fail");
		};

		old_quantum = syscall (SYS_weighted_rr_getquantum);
		syscall (SYS_weighted_rr_setquantum, quantum);
   	}
	
	//+ create the buffer
	if ( (val_buf = (char *) malloc(buffer_size)) == NULL )
		fail("malloc(buffer_size) fail");
	total_num_chars  = (buffer_size / sizeof(char));

	//+ create and start each thread
	if ( (threads = malloc(num_threads*sizeof(pthread_t))) == NULL )
		fail("malloc(num_threads) fail");
		
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
	
	for (i = 0; i < num_threads; i++)
	{
		targs = malloc(sizeof(*targs));
		targs->tid    = i;
		targs->prio   = i;
		targs->mychar = (char) (i+START_CHAR);
		targs->nchars = (total_num_chars / num_threads);
		int execTime = (rand()%20)+quantum;
		printf("%c job exeTime=%d\n",targs->mychar,execTime);
		if(quantum <= i)
			printf("TIme quantum too small\n");
		else 
			syscall (SYS_weighted_rr_setquantum, execTime);
		pthread_create(&threads[i], &attr, run, (void *)targs);

		//if (sched_policy == SCHED_WEIGHTED_RR)quantum*=2;
	}
	
	//+ wait for all threads to complete
	for (i = 0; i < num_threads; i++) 
	{
		pthread_join(threads[i], NULL);
	}

	//+ print val_buf results
	for (i = 0; i < total_num_chars; i++) 
	{
		if (cur != val_buf[i]) 
		{
			cur = val_buf[i];
			printf("%c", cur);;
		}
	}
	printf("\n");
	
	//+ reset time quantum
	syscall (SYS_weighted_rr_setquantum, old_quantum);

	//+ clean up and exit
	pthread_attr_destroy(&attr);
	pthread_exit (NULL);
	
}
コード例 #26
0
int main(int argc, char **argv)
{
  int c;
  int num_threads = 4;
  int img = 1;
  bool * received_fragments = calloc(N, sizeof(bool));

  while ((c = getopt (argc, argv, "t:i:")) != -1) {
    switch (c) {
    case 't':
      num_threads = strtoul(optarg, NULL, 10);
      if (num_threads == 0) {
	printf("%s: option requires an argument > 0 -- 't'\n", argv[0]);
	return -1;
      }
      break;
    case 'i':
      img = strtoul(optarg, NULL, 10);
      if (img == 0) {
	printf("%s: option requires an argument > 0 -- 'i'\n", argv[0]);
	return -1;
      }
      break;
    default:
      return -1;
    }
  }
  //Lock for pthread
  pthread_mutex_init(&lock, NULL);

    CURLcode result = curl_global_init(CURL_GLOBAL_NOTHING); // 1
if(result != CURLE_OK) // 2
{
  #ifdef _DEBUG_1_
	printf("\nCURL Global Init Failed\n");
	fflush(stdout);
#endif
}

  
  png_byte * output_buffer = calloc(WIDTH*HEIGHT*4, sizeof(png_byte));



	char * url = malloc(sizeof(char)*strlen(BASE_URL)+4*5);
	char * url_2 = malloc(sizeof(char)*strlen(BASE_URL_2)+4*5);
	char * url_3 = malloc(sizeof(char)*strlen(BASE_URL_3)+4*5);

	// request appropriate URL
	sprintf(url, BASE_URL, img);
	sprintf(url_2, BASE_URL_2, img);
	sprintf(url_3, BASE_URL_3, img);
	
	printf("requesting URL %s\n", url);

	struct threaddata tempthreaddata = {0};
	tempthreaddata.url = url;
	tempthreaddata.output_buffer = output_buffer;
	tempthreaddata.received_fragments = received_fragments;
	
	struct threaddata tempthreaddata2 = {0};
	tempthreaddata2.url = url_2;
	tempthreaddata2.output_buffer = output_buffer;
	tempthreaddata2.received_fragments = received_fragments;
	
	struct threaddata tempthreaddata3 = {0};
	tempthreaddata3.url = url_3;
	tempthreaddata3.output_buffer = output_buffer;
	tempthreaddata3.received_fragments = received_fragments;
	
	//Thread management
	    /* the next three lines (and the use of attr) weren't in lecture;
     * they aren't mandatory, but show how you can use pthread_attr_t
     * to make sure that the thread is joinable (and not detacthed */
  	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
	
	  pthread_t tid[num_threads];
	  
	  for(int i = 0; i < num_threads; i++)
	  {		  
		  //Call refactored function (loop)
		  //requestFrag((void *) &tempthreaddata);
		#ifdef _DEBUG_1_
		printf("\n###Before create###\n");
		printf("%s\n", tempthreaddata.url);
		fflush(stdout);
		#endif
		if(i%3 == 0)
		{
		  pthread_create(&tid[i], NULL, requestFrag, &tempthreaddata);
		}
		else if (i%3 == 1)
		{
			pthread_create(&tid[i], NULL, requestFrag, &tempthreaddata2);
		}
		else
		{
			pthread_create(&tid[i], NULL, requestFrag, &tempthreaddata3);
		}
	  }
	  
	for(int i = 0; i < num_threads; i++){
		pthread_join(tid[i], NULL);
	}
	
  #ifdef _DEBUG_1_
	printf("\n###2###\n");
	fflush(stdout);
#endif


	free(url);
	free(url_2);
	free(url_3);

	//Cleanup global init
	curl_global_cleanup();


  // now, write the array back to disk using write_png_file
  png_bytep * output_row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * HEIGHT);

  for (int i = 0; i < HEIGHT; i++)
    output_row_pointers[i] = &output_buffer[i*WIDTH*4];

  write_png_file("output.png", output_row_pointers);
  free(output_row_pointers);
  free(output_buffer);
  free(received_fragments);
  //destory lock
  	pthread_mutex_destroy(&lock);
  return 0;
}
コード例 #27
0
ファイル: main.c プロジェクト: detournement/iProxy
int main_srelay(int ac, char **av)
{
  int     ch, i=0;
  pid_t   pid;
  FILE    *fp;
  uid_t   uid;
#ifdef USE_THREAD
  pthread_t tid;
  pthread_attr_t attr;
  struct rlimit rl;
  rlim_t max_fd = (rlim_t)MAX_FD;
  rlim_t save_fd = 0;
#endif

  /* try changing working directory */
  if ( chdir(WORKDIR0) != 0 )
    if ( chdir(WORKDIR1) != 0 )
      msg_out(norm, "giving up chdir to workdir");

#ifdef USE_THREAD
  threading = 1;
  max_thread = MAX_THREAD;
#endif

  max_child = MAX_CHILD;
  cur_child = 0;

  serv_init(NULL);

  proxy_tbl = NULL;
  proxy_tbl_ind = 0;

  method_num = 0;

  uid = getuid();

  openlog(ident, LOG_PID, LOG_DAEMON);

  while((ch = getopt(ac, av, "a:c:i:J:m:o:p:u:frstbwgvh?")) != -1)
    switch (ch) {
    case 'a':
      if (optarg != NULL) {
	for (i=0; i<sizeof method_tab; optarg++) {
	  if (*optarg == '\0')
	    break;
	  switch (*optarg) {
	  case 'p':
	    if ( uid != 0 ) {
	      /* process does not started by root */
	      msg_out(warn, "uid == %d (!=0),"
		      "user/pass auth will not work, ignored.\n",
		      uid);
	      break;
	    }
	    method_tab[i++] = S5AUSRPAS;
	    method_num++;
	    break;
	  case 'n':
	    method_tab[i++] = S5ANOAUTH;
	    method_num++;
	    break;
	  default:
	    break;
	  }
	}
      }
      break;

    case 'b':
      bind_restrict = 0;
      break;

    case 'c':
      if (optarg != NULL) {
        config = strdup(optarg);
      }
      break;

    case 'u':
      if (optarg != NULL) {
        pwdfile = strdup(optarg);
      }
      break;

    case 'i':
      if (optarg != NULL) {
	if (serv_init(optarg) < 0) {
	  msg_out(crit, "cannot init server socket(-i)\n");
	  exit(-1);
	}
      }
      break;

#ifdef SO_BINDTODEVICE
    case 'J':
      if (optarg != NULL) {
	bindtodevice = strdup(optarg);
      }
      break;
#endif

    case 'o':
      if (optarg != NULL) {
	idle_timeout = atol(optarg);
      }
      break;

    case 'p':
      if (optarg != NULL) {
	pidfile = strdup(optarg);
      }
      break;

    case 'm':
      if (optarg != NULL) {
#ifdef USE_THREAD
	max_thread = atoi(optarg);
#endif
	max_child = atoi(optarg);
      }
      break;

    case 't':
#ifdef USE_THREAD
      threading = 0;    /* threading disabled. */
#endif
      break;

    case 'g':
      same_interface = 1;
      break;

    case 'f':
      fg = 1;
      break;

    case 'r':
      resolv_client = 1;
      break;

    case 's':
      forcesyslog = 1;
      break;

    case 'w':
#ifdef HAVE_LIBWRAP
      use_tcpwrap = 1;
#endif /* HAVE_LIBWRAP */
      break;

    case 'v':
      show_version();
      exit(1);

    case 'h':
    case '?':
    default:
      usage();
    }

  ac -= optind;
  av += optind;

  fp = fopen(config, "r");
  if (readconf(fp) != 0) {
    /* readconf error */
    exit(1);
  }
  if (fp)
    fclose(fp);

  if (serv_sock_ind == 0) {   /* no valid ifs yet */
    if (serv_init(":") < 0) { /* use default */
      /* fatal */
      msg_out(crit, "cannot open server socket\n");
      exit(1);
    }
  }

#ifdef USE_THREAD
  if ( ! threading ) {
#endif
    if (queue_init() != 0) {
      msg_out(crit, "cannot init signal queue\n");
      exit(1);
    }
#ifdef USE_THREAD
  }
#endif

  if (!fg) {
    /* force stdin/out/err allocate to /dev/null */
    fclose(stdin);
    fp = fopen("/dev/null", "w+");
    if (fileno(fp) != STDIN_FILENO) {
      msg_out(crit, "fopen: %m");
      exit(1);
    }
    if (dup2(STDIN_FILENO, STDOUT_FILENO) == -1) {
      msg_out(crit, "dup2-1: %m");
      exit(1);
    }
    if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
      msg_out(crit, "dup2-2: %m");
      exit(1);
    }

    switch(fork()) {
    case -1:
      msg_out(crit, "fork: %m");
      exit(1);
    case 0:
      /* child */
      pid = setsid();
      if (pid == -1) {
	msg_out(crit, "setsid: %m");
	exit(1);
      }
      break;
    default:
      /* parent */
      exit(0);
    }
  }

  master_pid = getpid();
  umask(S_IWGRP|S_IWOTH);
  if ((fp = fopen(pidfile, "w")) != NULL) {
    fprintf(fp, "%u\n", (unsigned)master_pid);
    fchown(fileno(fp), PROCUID, PROCGID);
    fclose(fp);
  } else {
    msg_out(warn, "cannot open pidfile %s", pidfile);
  }

  setsignal(SIGHUP, reload);
  setsignal(SIGINT, SIG_IGN);
  setsignal(SIGQUIT, SIG_IGN);
  setsignal(SIGILL, SIG_IGN);
  setsignal(SIGTRAP, SIG_IGN);
  setsignal(SIGABRT, SIG_IGN);
#ifndef LINUX
  setsignal(SIGEMT, SIG_IGN);
#endif
  setsignal(SIGFPE, SIG_IGN);
  setsignal(SIGBUS, SIG_IGN);
  setsignal(SIGSEGV, SIG_IGN);
  setsignal(SIGSYS, SIG_IGN);
  setsignal(SIGPIPE, SIG_IGN);
  setsignal(SIGALRM, SIG_IGN);
  setsignal(SIGTERM, cleanup);
  setsignal(SIGUSR1, SIG_IGN);
  setsignal(SIGUSR2, SIG_IGN);
#if !defined(FREEBSD) && !defined(MACOSX)
  setsignal(SIGPOLL, SIG_IGN);
#endif
  setsignal(SIGVTALRM, SIG_IGN);
  setsignal(SIGPROF, SIG_IGN);
  setsignal(SIGXCPU, SIG_IGN);
  setsignal(SIGXFSZ, SIG_IGN);

#ifdef USE_THREAD
  if ( threading ) {
    if (max_thread <= 0 || max_thread > THREAD_LIMIT) {
      max_thread = THREAD_LIMIT;
    }
    /* resource limit is problem in threadig (e.g. Solaris:=64)*/
    memset((caddr_t)&rl, 0, sizeof rl);
    if (getrlimit(RLIMIT_NOFILE, &rl) != 0)
      msg_out(warn, "getrlimit: %m");
    else
      save_fd = rl.rlim_cur;
    if (rl.rlim_cur < (rlim_t)max_fd)
      rl.rlim_cur = max_fd;        /* willing to fix to max_fd */
    if ( rl.rlim_cur != save_fd )  /* if rlim_cur is changed   */
      if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
        msg_out(warn, "cannot set rlimit(max_fd)");

    setregid(0, PROCGID);
    setreuid(0, PROCUID);

    pthread_mutex_init(&mutex_select, NULL);
    /*    pthread_mutex_init(&mutex_gh0, NULL); */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    msg_out(norm, "Starting: MAX_TH(%d)", max_thread);
    for (i=0; i<max_thread; i++) {
      if (pthread_create(&tid, &attr,
			 (void *)&serv_loop, (void *)NULL) != 0)
        exit(1);
    }
    main_thread = pthread_self();   /* store main thread ID */
    for (;;) {
      pause();
    }
  } else {
#endif
    setsignal(SIGCHLD, reapchild);
    setregid(0, PROCGID);
    setreuid(0, PROCUID);
    msg_out(norm, "Starting: MAX_CH(%d)", max_child);
    serv_loop();
#ifdef USE_THREAD
  }
#endif
  return(0);
}
コード例 #28
0
ファイル: dserve_net.c プロジェクト: JackieXie168/whitedb
int run_server(int port, dserve_global_p globalptr) {
  struct sockaddr_in clientaddr;
  int rc, sd, connsd, next; 
  thread_data_p tdata; 
  struct common_data *common;
  long tid, maxtid, tcount, i;
  size_t clientlen;
  //struct timeval timeout;
#ifdef MULTI_THREAD
#if _MSC_VER
  HANDLE thandle;
  HANDLE thandlearray[MAX_THREADS];
  DWORD threads[MAX_THREADS];
#else
  pthread_t threads[MAX_THREADS];
  pthread_attr_t attr;
  struct timespec tim, tim2;
#endif
#ifdef USE_OPENSSL    
  SSL_CTX *ctx; 
  SSL *ssl;  
#endif 
#endif
#if _MSC_VER
  void* db=NULL; // actual database pointer
  WSADATA wsaData;
   
  if (WSAStartup(MAKEWORD(2, 0),&wsaData) != 0) {
    errprint(WSASTART_ERR,NULL);
    exit(ERR_EX_UNAVAILABLE);
  }
  db = wg_attach_existing_database("1000");
  //db = wg_attach_database(database,100000000);
  if (!db) {
    errprint(DB_ATTACH_ERR,NULL);
    exit(ERR_EX_UNAVAILABLE);
  }
#else 
  signal(SIGPIPE,SIG_IGN); // important for linux TCP/IP handling   
#endif  
  tdata=&(globalptr->threads_data[0]); 
#ifdef MULTI_THREAD    
#if _MSC_VER
#else
  if (THREADPOOL) {
  
    // ---------------- run as server with threadpool -----------
    
    infoprint(THREADPOOL_INFO,NULL);
    // setup nanosleep for 100 microsec
    tim.tv_sec = 0;
    tim.tv_nsec = 100000;   
#ifdef USE_OPENSSL    
    // prepare openssl    
    ctx=init_openssl(globalptr->conf);
#endif    
    // prepare threads
    common=(struct common_data *)malloc(sizeof(struct common_data));
    tid=0;
    tcount=0;
    maxtid=0;
    if (pthread_mutex_init(&(common->mutex),NULL) !=0 ||
        pthread_cond_init(&(common->cond),NULL) != 0 ||
        pthread_attr_init(&attr) !=0) {
      errprint(MUTEX_ERROR,NULL);    
      exit(ERR_EX_UNAVAILABLE);
    }        
    common->threads = threads;
    common->queue = (common_task_t *)malloc(sizeof(common_task_t) * QUEUE_SIZE);
    common->thread_count = 0;
    common->queue_size = QUEUE_SIZE;
    common->head = common->tail = common->count = 0;
    common->shutdown = common->started = 0;
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); //PTHREAD_CREATE_DETACHED);
    // create threads
    for(tid=0;tid<MAX_THREADS;tid++) {
      // init thread data block 
      tdata[tid].isserver=1;
      tdata[tid].thread_id=tid;
      tdata[tid].realthread=2;
      tdata[tid].common=common;
      tdata[tid].inuse=0;
      tdata[tid].conn=0;
      tdata[tid].ip=NULL;
      tdata[tid].port=0;
      tdata[tid].method=0;
      tdata[tid].res=0; 
      tdata[tid].global=globalptr;
      //fprintf(stderr,"creating thread %d tcount %d \n",(int)tid,(int)tcount); 
      rc=pthread_create(&threads[tid], &attr, handle_http, (void *) &tdata[tid]);
      if (rc) {
        errprint(THREAD_CREATE_ERR,strerror(errno));
        exit(ERR_EX_UNAVAILABLE);
      }      
      tcount++;
    }
    //
    sd=open_listener(port);
    if (sd<0) {
      errprint(PORT_LISTEN_ERR, strerror(errno));
      return -1;
    }
    clientlen = sizeof(clientaddr);
    // loop forever, servicing requests
    while (1) {
      connsd=accept(sd,(struct sockaddr *)&clientaddr, &clientlen);
      if (connsd<0) {
        warnprint(CONN_ACCEPT_WARN, strerror(errno));
        continue;
      }  
      if(pthread_mutex_lock(&(common->mutex)) != 0) {
        errprint(THREADPOOL_LOCK_ERR,NULL);
        exit(ERR_EX_UNAVAILABLE);
      }  
#ifdef USE_OPENSSL
      ssl = SSL_new(ctx);  // get new SSL state with context
      SSL_set_fd(ssl,connsd);	
#endif
      // now we have a connection: add to queue
      next=common->tail+1;
      next=(next==common->queue_size) ? 0 : next;
      do {
        if(common->count==common->queue_size) { // full?
          //fprintf(stderr, "queue full\n");
          nanosleep(&tim , &tim2);
          break; //continue;
        }
        if(common->shutdown) { 
          warnprint(SHUTDOWN_WARN,NULL);
          break;
        }
        // add to task queue
        common->queue[common->tail].conn=connsd;
#ifdef USE_OPENSSL
        common->queue[common->tail].ssl=ssl; 
#endif
        common->tail=next;
        common->count+=1;
        //printf("next %d\n",next);
        // broadcast
        if(pthread_cond_signal(&(common->cond)) != 0) {
          warnprint(COND_SIGNAL_FAIL_WARN,NULL);
          break;
        }
      } while(0);
      //fprintf(stderr,"starting to unlock \n");
      if(pthread_mutex_unlock(&(common->mutex)) != 0) {
        errprint(THREADPOOL_UNLOCK_ERR,NULL);
        exit(ERR_EX_UNAVAILABLE);
      }
    }
    return 0; // never come to this
    
  } else 
#endif // threadpool not implemented on windows version: using a non-threadpool version
         {
         
    // ------------- run as server without threadpool -------------
    
    infoprint(MULTITHREAD_INFO,NULL);
    // setup nanosleep for 100 microsec
#if _MSC_VER
#else    
    tim.tv_sec = 0;
    tim.tv_nsec = 100000;
#endif    
    // prepare common block
    common=(struct common_data *)malloc(sizeof(struct common_data));     
    common->shutdown=0;           
    // mark thread data blocks free
    for(i=0;i<MAX_THREADS;i++) {
      tdata[i].inuse=0;      
      tdata[i].common=common;
      tdata[i].global=globalptr;
    }
    // prepare threads
    tid=0;
    tcount=0;
    maxtid=0;    
#if _MSC_VER
#else
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); //PTHREAD_CREATE_JOINABLE); 
#endif
    sd=open_listener(port);
    if (sd<0) {
      errprint(PORT_LISTEN_ERR, strerror(errno));
      return -1;
    }
    clientlen = sizeof(clientaddr);
    while (1) {
      connsd=accept(sd,(struct sockaddr *)&clientaddr, &clientlen);
      if (common->shutdown==1) break; 
      if (connsd<0) {     
        warnprint(CONN_ACCEPT_WARN, strerror(errno));
        continue;
      }
      tid=-1;
      // find first free thread data block
      // loop until we get a free one
      while(tid<0) {
        for(i=0;i<MAX_THREADS;i++) {
          if (!tdata[i].inuse) {
            tid=i;
            break;
          }
        }
        if (tid>=0) break;
#if _MSC_VER
        usleep(1);
#else
        nanosleep(&tim , &tim2);
#endif
      }
      if (tid>maxtid) maxtid=tid;
      tcount++;
      // init thread data block
      tdata[tid].isserver=1;
      tdata[tid].thread_id=tid;
      tdata[tid].realthread=1;
      tdata[tid].inuse=1;
      tdata[tid].conn=connsd;
      tdata[tid].ip=NULL;
      tdata[tid].port=0;
      tdata[tid].method=0;
      tdata[tid].res=0;  
      tdata[tid].global=globalptr; 
#if _MSC_VER
      tdata[tid].db=db;
      thandle=CreateThread(NULL, 0, handle_http, (void *) &tdata[tid], 0, &threads[tid]);
      if (thandle==NULL) {
        win_err_handler(TEXT("CreateThread"));
        ExitProcess(3);
      } else {
        thandlearray[tid]=thandle;
      }       
#else
      rc=pthread_create(&threads[tid], &attr, handle_http, (void *) &tdata[tid]);
#endif
    }
    return 0; // never come to this
  } 
#else  

  // ------------ run as an iterative server -------------
  
  sd=open_listener(port);
  if (sd<0) {
    errprint(PORT_LISTEN_ERR, strerror(errno));
    return -1;
  }
  clientlen = sizeof(clientaddr);
  while (1) {      
    connsd=accept(sd,(struct sockaddr *)&clientaddr, &clientlen);
    if (connsd<0) {
      warnprint(CONN_ACCEPT_WARN, strerror(errno));
      continue;
    }           
    tid=0;  
    tdata[tid].isserver=1;
    tdata[tid].thread_id=tid;
    tdata[tid].realthread=0;
    tdata[tid].conn=connsd;
    tdata[tid].ip=NULL;
    tdata[tid].port=0;
    tdata[tid].method=0;
    tdata[tid].res=0;  
    tdata[tid].global=globalptr;  
#if _MSC_VER
    tdata[tid].db=db;
#endif
    handle_http((void *) &tdata[tid]);
  }
  return 0; // never come to this
#endif
}  
コード例 #29
0
int stpro::run()
{
	myLogger.lw(INFO,"RUN: Starting STPRO Run...");
	
	// Setup a thread attribute:
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
	int rc=0;

	// Setup and start the command thread:
	myLogger.lw(INFO,"RUN: Starting command thread");
	rc = pthread_create(&commandThread, &attr, stpro::command, (void *)this);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: Bad return code from pthread_create() is %d\n", rc);
		exit(-1);
	}
	
	// Setup and start the inData thread:
	myLogger.lw(INFO,"RUN: Starting inData thread");
	rc = pthread_create(&inDataThread, &attr, stpro::inData, (void *)this);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: Bad return code from pthread_create() is %d\n", rc);
		exit(-1);
	}
	
	// Let inData finish a loading a dataset before starting procData:
	sleep(2);
	
	// Setup and start the processData thread:
	myLogger.lw(INFO,"RUN: Starting processData thread");
	rc = pthread_create(&procDataThread, &attr, stpro::procData, (void *)this);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: Bad return code from pthread_create() is %d\n", rc);
		exit(-1);
	}
	
	// Let processData finish processing a dataset before starting outData:
	sleep(3);
	
	// Setup and start the outData thread:
	myLogger.lw(INFO,"RUN: Starting outData thread");
	rc = pthread_create(&outDataThread, &attr, stpro::outData, (void *)this);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: Bad return code from pthread_create() is %d\n", rc);
		exit(-1);
	}
	
	// Setup and start the monitor thread:
	myLogger.lw(INFO,"RUN: Starting monitor thread");
	rc = pthread_create(&monitorThread, &attr, stpro::monitor, (void *)this);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: Bad return code from pthread_create() is %d\n", rc);
		exit(-1);
	}
	
	pthread_attr_destroy(&attr);

	// Joining our threads until we are killed by a signal:
	rc = pthread_join(commandThread, NULL);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: return code from pthread_join() is %d\n", rc);
		exit(-1);
	}
	rc = pthread_join(inDataThread, NULL);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: return code from pthread_join() is %d\n", rc);
		exit(-1);
	}
	rc = pthread_join(procDataThread, NULL);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: return code from pthread_join() is %d\n", rc);
		exit(-1);
	}
	rc = pthread_join(outDataThread, NULL);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: return code from pthread_join() is %d\n", rc);
		exit(-1);
	}
	rc = pthread_join(monitorThread, NULL);
	if (rc)
	{
		myLogger.lw(ERROR,"RUN: return code from pthread_join() is %d\n", rc);
		exit(-1);
	}
	
	// Cleaning up:
	myLogger.lw(WARNING,"RUN: All threads have terminated. Dying gracefully.");
	pthread_exit(NULL);
	exit(0);
}
コード例 #30
0
ファイル: mex_pcdtv2.c プロジェクト: hanbingyan/cdtv
void pcdtv2(double lambda){
  int i=0,j=0,l,k,t,s,rc;
  double b,u[5],temp[5],temps;
  void *status;
  struct prob prob0;
  struct prob prob1;
  prob0.lambda=lambda;
  prob1.lambda=lambda;
  pthread_t threads[2];
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
  printf("creating thread 0\n");
  rc=pthread_create(&threads[0],&attr,threadcd0,(void *)&prob0);
  if(rc){
    printf("ERROR: return code from pthread_create() is %d\n",rc);
    exit(-1);
  }
  printf("creating thread 1\n");
  rc=pthread_create(&threads[1],&attr,threadcd1,(void *)&prob1);
  if(rc){
    printf("ERROR: return code from pthread_create() is %d\n",rc);
    exit(-1);
  }

  pthread_attr_destroy(&attr);

  rc=pthread_join(threads[0],&status);
  if(rc){
    printf("ERROR; return code from pthread_join() is %d\n", rc);
    exit(-1);
  }
  printf("Main: completed join with thread %d having a status of %ld\n",0,(long)status);

  rc=pthread_join(threads[1],&status);
  if(rc){
    printf("ERROR; return code from pthread_join() is %d\n", rc);
    exit(-1);
  }
  printf("Main: completed join with thread %d having a status of %ld\n",1,(long)status);

  for(i=0;i<512;i++)
    for(j=0;j<256;j++)
      out[i][j]=prob0.U[i][j];
  for(i=0;i<512;i++)
    for(j=256;j<512;j++)
      out[i][j]=prob1.U[i][j-256];
  
  for(l=1;l<31;l++){
    for(k=1;k<201;k++){
     /* srand((unsigned)time(NULL));*/
      t=rand()%(512-2)+1;
      s=rand()%2+255;
      b=B[t][s];
      u[0]=b;
      u[1]=out[t+1][s];
      u[2]=out[t-1][s];
      u[3]=out[t][s+1];
      u[4]=out[t][s-1];
      temp[0]=lambda*(fabs(b-u[1])+fabs(b-u[2])+fabs(b-u[3])+fabs(b-u[4]));
      temp[1]=(u[1]!=b)+lambda*(fabs(u[1]-u[2])+fabs(u[1]-u[3])+fabs(u[1]-u[4]));
      temp[2]=(u[2]!=b)+lambda*(fabs(u[1]-u[2])+fabs(u[2]-u[3])+fabs(u[2]-u[4]));
      temp[3]=(u[3]!=b)+lambda*(fabs(u[3]-u[1])+fabs(u[3]-u[2])+fabs(u[3]-u[4]));
      temp[4]=(u[4]!=b)+lambda*(fabs(u[4]-u[1])+fabs(u[4]-u[2])+fabs(u[4]-u[3]));
      temps=temp[0];
      j=0;
      for(i=1;i<5;i++){
        if(temp[i]<temps){
          temps=temp[i];
          j=i;
        }
      }
      out[t][s]=u[j];
    }
    /* minimize four corner */
    if(k%50==0){
      b=B[0][s];
      u[0]=b;
      u[1]=out[1][s];
      u[2]=out[0][s-1];
      u[3]=out[0][s+1];
      temp[0]=lambda*(fabs(b-u[1])+fabs(b-u[2])+fabs(b-u[3])+fabs(b-u[4]));
      temp[1]=(u[1]!=b)+lambda*(fabs(u[1]-u[2])+fabs(u[1]-u[3])+fabs(u[1]-u[4]));
      temp[2]=(u[2]!=b)+lambda*(fabs(u[1]-u[2])+fabs(u[2]-u[3])+fabs(u[2]-u[4]));
      temp[3]=(u[3]!=b)+lambda*(fabs(u[3]-u[1])+fabs(u[3]-u[2])+fabs(u[3]-u[4]));
      temps=temp[0];
      j=0;
      for(i=1;i<4;i++){
        if(temp[i]<temps){
          temps=temp[i];
          j=i;
        }
      }
      out[0][s]=u[j];

      b=B[511][s];
      u[0]=b;
      u[1]=out[510][s];
      u[2]=out[511][s-1];
      u[3]=out[511][s+1];
      temp[0]=lambda*(fabs(b-u[1])+fabs(b-u[2])+fabs(b-u[3])+fabs(b-u[4]));
      temp[1]=(u[1]!=b)+lambda*(fabs(u[1]-u[2])+fabs(u[1]-u[3])+fabs(u[1]-u[4]));
      temp[2]=(u[2]!=b)+lambda*(fabs(u[1]-u[2])+fabs(u[2]-u[3])+fabs(u[2]-u[4]));
      temp[3]=(u[3]!=b)+lambda*(fabs(u[3]-u[1])+fabs(u[3]-u[2])+fabs(u[3]-u[4]));
      temps=temp[0];
      j=0;
      for(i=1;i<4;i++){
        if(temp[i]<temps){
          temps=temp[i];
          j=i;
        }
      }
      out[511][s]=u[j];
    }
  }
}