コード例 #1
0
ファイル: granularity.cpp プロジェクト: deepsea-inria/predict
void forkjoin(loop_by_eager_binary_splitting<Granularity_control_policy>& lpalgo,
             const Skel_cutoff_fct& skel_cutoff_fct,
             const Skel_complexity_measure_fct& skel_compl_fct,
             Input& input,
             Output& output,
             const Input_empty_fct& input_empty_fct,
             const Fork_input_fct& fork_input_fct,
             const Join_output_fct& join_output_fct,
             const Body& body) {
  auto seq_fct = [&] {
    body(input, output);
  };
  if (input_empty_fct(input)) {
    seq_fct();
  } else {
    auto cutoff_fct = [&] {
      return skel_cutoff_fct(input);
    };
    auto compl_fct = [&] {
      return skel_compl_fct(input);
    };
    Input input2;
    Output output2;
    fork_input_fct(input, input2);
    cstmt(lpalgo.gcpolicy, cutoff_fct, compl_fct, [&] {
    fork2([&] { forkjoin(lpalgo, skel_cutoff_fct, skel_compl_fct, input, output,
                        input_empty_fct, fork_input_fct, join_output_fct, body); },
          [&] { forkjoin(lpalgo, skel_cutoff_fct, skel_compl_fct, input2, output2,
                        input_empty_fct, fork_input_fct, join_output_fct, body); }); },
    seq_fct);
    join_output_fct(output, output2);
  }
}
コード例 #2
0
ファイル: vfork.and.run.c プロジェクト: sheumann/telnetd
pid_t vfork_and_run(void (*fn)(void*) /*NORETURN*/, void *arg) {
	/* GNO's fork2 call will return immediately and allow the parent and 
	 * child processes to execute concurrently using the same memory
	 * space.  To prevent them stomping on each other, we want to get
	 * behavior like a traditional vfork() implementation, where the
	 * parent blocks until the child terminates or execs.
	 *
	 * Our approach is to check the process tables to make sure the
	 * child has actually finished or exec'd.  If not, we loop and try again.
	 * We can't just rely on the fact that the child signaled us, because
	 * it may still be running in libc's implementation of exec*.
	 */
	
	long oldmask;
	pid_t pid;
	kvmt *kvm_context;
	struct pentry *proc_entry;
	int done = 0;
	
	/* Isolate child process's environment from parent */
	if (environPush() != 0)
		return  -1;
	
	/* Block all signals for now */
	oldmask = sigblock(-1);
	
	pid = fork2(fork_thunk, CHILD_STACKSIZE, 0, forked_child_name, 
				(sizeof(fn) + sizeof(arg) + sizeof(oldmask) + 1) / 2, 
				fn, arg, oldmask);
	if (pid < 0) 
		goto ret;
	
	while (!done) {
		/* Wait for ~100 ms.  If procsend worked, the child could send a 
		 * message with it to end the waiting earlier, but this isn't 
		 * possible in GNO 2.0.6 because procsend is broken.  This isn't
		 * too big an issue, since 100ms isn't very long to wait anyhow. */
		procrecvtim(1);
		
		/* Check if the child is really dead or forked by inspecting
		 * the kernel's process entry for it. */
		kvm_context = kvm_open();
		if (kvm_context == NULL)
			break;
		proc_entry = kvmgetproc(kvm_context, pid);
		if (proc_entry == NULL 
			|| (proc_entry->args != NULL 
				&& strcmp(forked_child_name, proc_entry->args + 8) != 0))
			done = 1;
		kvm_close(kvm_context);
	}
	
ret:
	sigsetmask(oldmask);
	environPop();
	return pid;
}
コード例 #3
0
ファイル: granularity.cpp プロジェクト: deepsea-inria/predict
// parallel fib with complexity function
static
long pfib1(long n) {
  if (n < 2)
    return n;
  long a,b;
  cstmt(cfib, [&] { return phi_to_pow(n); }, [&] {
  fork2([&] { a = pfib1(n-1); },
        [&] { b = pfib1(n-2); }); });
  return a + b;
}
コード例 #4
0
ファイル: granularity.cpp プロジェクト: deepsea-inria/predict
// parallel fib with manual cutoff and sequential body alternative
static
long pfib3(long n) {
  if (n < 2)
    return n;
  long a,b;
  cstmt(cfib2, [&] { return n <= fib_cutoff; }, [&] {
  fork2([&] { a = pfib3(n-1); },
        [&] { b = pfib3(n-2); }); },
  [&] { a = fib(n-1);
        b = fib(n-2); });
  return a + b;
}
コード例 #5
0
ファイル: native.hpp プロジェクト: grtkachenko/pasl
void parallel_while_cilk_rec(Input& input, const Size_input& size_input, const Fork_input& fork_input,
                             const Set_in_env& set_in_env, const Body& body) {
  set_in_env(input);
  size_t sz = size_input(input);
  while (sz > 0) {
    if (sz > 1 && my_deque_size() == 0) {
      Input input2;
      fork_input(input, input2);
      fork2([&] { parallel_while_cilk_rec(input, size_input, fork_input, set_in_env, body); },
            [&] { parallel_while_cilk_rec(input2, size_input, fork_input, set_in_env, body); });
      return;
    }
    body(input);
    sz = size_input(input);
  }
}
コード例 #6
0
ファイル: csapp_forks.c プロジェクト: Flamacue/classes
int main(int argc, char *argv[])
{
    int option = 0;
    if (argc > 1)
         option = atoi(argv[1]);
    switch(option) {
        case 1: fork1();
        break;
        case 2: fork2();
        break;
        case 3: fork3();
        break;
        case 4: fork4();
        break;
        case 5: fork5();
        break;
        case 6: fork6();
        break;
        case 7: fork7();
        break;
        case 8: fork8();
        break;
        case 9: fork9();
        break;
        case 10: fork10();
        break;
        case 11: fork11();
        break;
        case 12: fork12();
        break;
        case 13: fork13();
        break;
        case 14: fork14();
        break;
        case 15: fork15();
        break;
        case 16: fork16();
        break;
        case 17: fork17();
        break;
        default:
        printf("Unknown option %d\n", option);
        break;
    }
    return 0;
}
コード例 #7
0
ファイル: native.hpp プロジェクト: grtkachenko/pasl
void forkjoin(Input& in, Output& out,
            const Cutoff& cutoff, const Fork_input& fork, const Join_output& join,
            const Set_in_env& set_in_env, const Set_out_env& set_out_env,
            const Body& body) {
  if (cutoff(in)) {
    body(in, out);
  } else {
    Input in2;
    Output out2;
    set_in_env(in2);
    set_out_env(out2);
    fork(in, in2);
    fork2([&] { forkjoin(in,  out,  cutoff, fork, join, set_in_env, set_out_env, body); },
          [&] { forkjoin(in2, out2, cutoff, fork, join, set_in_env, set_out_env, body); });
    join(out, out2);
  }
}
コード例 #8
0
ファイル: sh.c プロジェクト: fenster/xv6-staus-treffert
void lottery_test(int tickets){

		int start_ticks = tick();
		if(fork2(tickets) == 0)
	  {
		
		int temp = 0;
		int dummy;
		while(temp < 10000000)
		{
			temp = temp + 1;
			if(temp == 10000000)
					printf(2, "%d tickets, %d ticks\n", tickets, tick() - start_ticks);
		}

 		exit();
    }

}
コード例 #9
0
ファイル: granularity.cpp プロジェクト: deepsea-inria/predict
void parallel_for(loop_by_eager_binary_splitting<Granularity_control_policy>& lpalgo,
                  const Loop_cutoff_fct& loop_cutoff_fct,
                  const Loop_complexity_measure_fct& loop_compl_fct,
                  Number lo, Number hi, const Body& body) {
  auto seq_fct = [&] {
    for (Number i = lo; i < hi; i++)
      body(i);
  };
  if (hi - lo < 2) {
    seq_fct();
  } else {
    auto cutoff_fct = [&] {
      return loop_cutoff_fct(lo, hi);
    };
    auto compl_fct = [&] {
      return loop_compl_fct(lo, hi);
    };
    Number mid = (lo + hi) / 2;
    cstmt(lpalgo.gcpolicy, cutoff_fct, compl_fct, [&] {
    fork2([&] { parallel_for(lpalgo, loop_cutoff_fct, loop_compl_fct, lo, mid, body); },
          [&] { parallel_for(lpalgo, loop_cutoff_fct, loop_compl_fct, mid, hi, body); }); },
    seq_fct);
  }
}
コード例 #10
0
ファイル: exec.c プロジェクト: GnoConsortium/gno
int
exec (const char *filename, const char *cmdline) {
	return fork2 (_exec_child, 1024, 0, "forked child of exec(2)", 4,
                filename, cmdline);
}
コード例 #11
0
int main(int argc, char **argv)
	{
	struct bpf_program fcode;
	u_char *pcap_userdata = 0;
#ifdef HAVE_PCAP_FINDALLDEVS
	pcap_if_t *Devices;
#endif
	char Error[PCAP_ERRBUF_SIZE];
	struct stat StatBuf;
	int i;
	int ForkBackground = TRUE;
	int ListDevices = FALSE;
	int Counter;
	char *bd_conf = NULL;
	struct in_addr addr, addr2;
	signal(SIGHUP, SIG_IGN);
	signal(SIGTERM, SIG_IGN);
	ProgramStart = time(NULL);
	// Init worker child pids to zero so we don't kill random things when we shut down
	for (Counter = 0; Counter < NR_WORKER_CHILDS; Counter++)
		workerchildpids[Counter] = 0;
	for(Counter = 1; Counter < argc; Counter++)
		{
		if (argv[Counter][0] == '-')
			{
			switch(argv[Counter][1])
				{
				case 'D':
					ForkBackground = FALSE;
					break;
				case 'l':
					ListDevices = TRUE; 
					break;
				case 'c':
					if (argv[Counter+1])
						{
						bd_conf = argv[Counter+1];
						Counter++;
						}
					else
						PrintHelp();
					break;
				default:
					printf("Improper argument: %s\n", argv[Counter]);
				case '-':
					PrintHelp();
				}
			}
		}
	config.dev = NULL;
	config.description = "No Description";
	config.management_url = "https://127.0.0.1";
	config.filter = "ip or ether proto 1537";
	//config.filter = "ip";
	config.skip_intervals = CONFIG_GRAPHINTERVALS;
	config.graph_cutoff = CONFIG_GRAPHCUTOFF;
	config.promisc = TRUE;
	config.extensions = FALSE;
	config.graph = TRUE;
	config.output_cdf = FALSE;
	config.recover_cdf = FALSE;
	config.meta_refresh = CONFIG_METAREFRESH;
	config.output_database = FALSE;
	config.db_connect_string = NULL;
	config.sensor_name = "unset";  
	config.log_dir = LOG_DIR;
	config.htdocs_dir = HTDOCS_DIR;
	openlog("bandwidthd", LOG_CONS, LOG_DAEMON);
	// Locate configuration file
	if (!(bd_conf && !stat(bd_conf, &StatBuf)))
		{
		if (bd_conf)
			{
			printf("Could not find %s\n", bd_conf);
			exit(1);
			}
		else
			if (!stat("bandwidthd.conf", &StatBuf))
				bd_conf = "bandwidthd.conf";
			else
				if (!stat("./etc/bandwidthd.conf", &StatBuf))
					bd_conf = "./etc/bandwidthd.conf";
				else
					if (!stat(CONFIG_FILE, &StatBuf))
						bd_conf = CONFIG_FILE;
					else
						{
						printf("Cannot find bandwidthd.conf, ./etc/bandwidthd.conf or %s\n", CONFIG_FILE);
						syslog(LOG_ERR, "Cannot find bandwidthd.conf, ./etc/bandwidthd.conf or %s", CONFIG_FILE);
						exit(1);
						}
		}
	bdconfig_in = fopen(bd_conf, "rt");
	if (!bdconfig_in)
		{
		syslog(LOG_ERR, "Cannot open bandwidthd.conf");
		printf("Cannot open ./etc/bandwidthd.conf\n");
		exit(1);
		}
	bdconfig_parse();
	// Log list of monitored subnets
	for (Counter = 0; Counter < SubnetCount; Counter++)
		{
		addr.s_addr = ntohl(SubnetTable[Counter].ip);
		addr2.s_addr = ntohl(SubnetTable[Counter].mask);
		syslog(LOG_INFO, "Monitoring subnet %s with netmask %s", inet_ntoa(addr), inet_ntoa(addr2));
		}
  for (Counter = 0; Counter < NotSubnetCount; Counter++)
    {
    addr.s_addr = ntohl(NotSubnetTable[Counter].ip);
    addr2.s_addr = ntohl(NotSubnetTable[Counter].mask);
    syslog(LOG_INFO, "Ignoring subnet %s with netmask %s", inet_ntoa(addr), inet_ntoa(addr2));
    }
#ifdef HAVE_PCAP_FINDALLDEVS
	pcap_findalldevs(&Devices, Error);
	if (config.dev == NULL && Devices && Devices->name)
		config.dev = strdup(Devices->name);
	if (ListDevices)
		{	
		while(Devices)
			{
			printf("Description: %s\nName: \"%s\"\n\n", Devices->description, Devices->name);
			Devices = Devices->next;
			}
		exit(0);
		}
#else
	if (ListDevices)
		{
		printf("List devices is not supported by you version of libpcap\n");
		exit(0);
		}
#endif
	if (config.graph)
		bd_CollectingData();
	/* detach from console. */
	if (ForkBackground)
		if (fork2())
			exit(0);
	makepidfile(getpid());
	setchildconfig(0); /* initialize first (day graphing) process config */
	if (config.graph || config.output_cdf)
		{
		/* fork processes for week, month and year graphing. */
		for (i=0; i<NR_WORKER_CHILDS; i++) 
			{
			workerchildpids[i] = fork();
			/* initialize children and let them start doing work,
			 * while parent continues to fork children.
			 */
			if (workerchildpids[i] == 0) 
				{ /* child */
				setchildconfig(i+1);
				break;
				}
			if (workerchildpids[i] == -1) 
				{ /* fork failed */
				syslog(LOG_ERR, "Failed to fork graphing child (%d)", i);
				/* i--; ..to retry? -> possible infinite loop */
				continue;
				}
			}
		}	
	signal(SIGHUP, signal_handler);
	signal(SIGTERM, signal_handler);
	if(config.recover_cdf)
		RecoverDataFromCDF();
	IntervalStart = time(NULL);
	syslog(LOG_INFO, "Opening %s", config.dev);
	pd = pcap_open_live(config.dev, SNAPLEN, config.promisc, 1000, Error);
	if (pd == NULL) 
		{
		syslog(LOG_ERR, "%s", Error);
		exit(0);
		}
	if (pcap_compile(pd, &fcode, config.filter, 1, 0) < 0)
		{
		pcap_perror(pd, "Error");
		printf("Malformed libpcap filter string in bandwidthd.conf\n");
		syslog(LOG_ERR, "Malformed libpcap filter string in bandwidthd.conf");
		exit(1);
		}
	if (pcap_setfilter(pd, &fcode) < 0)
		pcap_perror(pd, "Error");
	switch (DataLink = pcap_datalink(pd))
		{
		default:
			if (config.dev)
				printf("Unknown Datalink Type %d, defaulting to ethernet\nPlease forward this error message and a packet sample (captured with \"tcpdump -i %s -s 2000 -n -w capture.cap\") to [email protected]\n", DataLink, config.dev);
			else
				printf("Unknown Datalink Type %d, defaulting to ethernet\nPlease forward this error message and a packet sample (captured with \"tcpdump -s 2000 -n -w capture.cap\") to [email protected]\n", DataLink);
			syslog(LOG_INFO, "Unkown datalink type, defaulting to ethernet");
		case DLT_EN10MB:
			syslog(LOG_INFO, "Packet Encoding: Ethernet");
			IP_Offset = 14; //IP_Offset = sizeof(struct ether_header);
			break;
#ifdef DLT_LINUX_SLL 
		case DLT_LINUX_SLL:
			syslog(LOG_INFO, "Packet Encoding: Linux Cooked Socket");
			IP_Offset = 16;
			break;
#endif
#ifdef DLT_RAW
		case DLT_RAW:
			printf("Untested Datalink Type %d\nPlease report to [email protected] if bandwidthd works for you\non this interface\n", DataLink);
			printf("Packet Encoding:\n\tRaw\n");
			syslog(LOG_INFO, "Untested packet encoding: Raw");
			IP_Offset = 0;
			break;
#endif
		case DLT_IEEE802:
			printf("Untested Datalink Type %d\nPlease report to [email protected] if bandwidthd works for you\non this interface\n", DataLink);
			printf("Packet Encoding:\nToken Ring\n");
			syslog(LOG_INFO, "Untested packet encoding: Token Ring");
			IP_Offset = 22;
			break;
		}
	if (ForkBackground)
		{
		fclose(stdin);
		fclose(stdout);
		fclose(stderr);
		}
	if (IPDataStore)  // If there is data in the datastore draw some initial graphs
		{
		syslog(LOG_INFO, "Drawing initial graphs");
		pidGraphingChild = WriteOutWebpages(IntervalStart+config.interval);
		}
#ifdef HAVE_PYTHON
	Py_Initialize();
	IpTableDict=PyDict_New();
	Py_Finalize();
#endif
	ResetTrafficCounters();
	// This is also set in CloseInterval because it gets overwritten in some commit modules
	signal(SIGALRM, handle_interval);
	alarm(config.interval);
	nice(1);
	while (1)
		{
		// Bookeeping
		if (IntervalFinished){  // Then write out this intervals data and possibly kick off the grapher
#ifdef HAVE_PYTHON
			PyDict_Clear(IpTableDict);
			Py_Finalize();
#endif
			CloseInterval();
		}
		// Process 1 buffer full of data
		if (pcap_dispatch(pd, -1, PacketCallback, pcap_userdata) == -1) 
			{
			syslog(LOG_ERR, "Bandwidthd: pcap_dispatch: %s",  pcap_geterr(pd));
			exit(1);
			}
		}
	pcap_close(pd);
	exit(0);
	}
コード例 #12
0
int sc_main (int argc , char *argv[])
{
	/*
	int	quantization[64] = { 8,  6,  6,  7,  6,  5,  8,  7,
							 7,  7,  9,  9,  8, 10, 12, 20,
							13, 12, 11, 11, 12, 25, 18, 19,
							15, 20, 29, 26, 31, 30, 29, 26,
							28, 28, 32, 36, 46, 39, 32, 34,
							44, 35, 28, 28, 40, 55, 41, 44,
							48, 49, 52, 52, 52, 31, 39, 57,
							61, 56, 50, 60, 46, 51, 52, 50 };
	 */

	int	quantization[64] = {16, 11, 10, 16, 24, 40, 51, 61,
							12, 12, 14, 19, 26, 58, 60, 55,
							14, 13, 16, 24, 40, 57, 69, 56,
							14, 17, 22, 29, 51, 87, 80, 62,
							18, 22, 37, 56, 68,109,103, 77,
							24, 35, 55, 64, 81,104,113, 92,
							49, 64, 78, 87,103,121,120,101,
							72, 92, 95, 98,112,100,103, 99};


	sc_ufixed<14,-2,SC_TRN,SC_WRAP> quantization_i[64] = {
			0.0625000000000000, 0.0909090909090909, 0.100000000000000, 0.0625000000000000, 0.0416666666666667, 0.0250000000000000, 0.0196078431372549, 0.0163934426229508,
			0.0833333333333333, 0.0833333333333333, 0.0714285714285714, 0.0526315789473684, 0.0384615384615385, 0.0172413793103448, 0.0166666666666667, 0.0181818181818182,
			0.0714285714285710, 0.0769230769230769, 0.0625000000000000, 0.0416666666666667, 0.0250000000000000, 0.0175438596491228, 0.0144927536231884, 0.0178571428571429,
			0.0714285714285714, 0.0588235294117647, 0.0454545454545455, 0.0344827586206897, 0.0196078431372549, 0.0114942528735632, 0.0125000000000000, 0.0161290322580645,
			0.0555555555555560, 0.0454545454545455, 0.0270270270270270, 0.0178571428571429, 0.0147058823529412, 0.00917431192660551, 0.00970873786407767, 0.0129870129870130,
			0.0416666666666667, 0.0285714285714286, 0.0181818181818182, 0.0156250000000000, 0.0123456790123457, 0.00961538461538462, 0.00884955752212389, 0.0108695652173913,
			0.0204081632653060, 0.0156250000000000, 0.0128205128205128, 0.0114942528735632, 0.00970873786407767, 0.00826446280991736, 0.00833333333333333, 0.00990099009900990,
			0.0138888888888889, 0.0108695652173913, 0.0105263157894737, 0.0102040816326531, 0.00892857142857143, 0.0100000000000000, 0.00970873786407767, 0.0101010101010101
	};

	// definition of default files
	const char*	inputfile		= "datain.pgm";
	const char*	outputfile		= "dataout.pgm";
	const char* typefile 		= "types.txt";

	// definition of FIFO queues
	fifo_stat<int>	stimulus("stimulus",1);
	fifo_stat<int>	parameters("parameters",3);
	fifo_stat<int>	stimulus_dup1("stimulus_dup1",1);
	fifo_stat<int>	stimulus_dup2("stimulus_dup2",MAXWIDTH8*8+64+64+64+64+64);
	fifo_stat<int>	parameters_dup1("parameters_dup1",3);
	fifo_stat<int>	parameters_dup2("parameters_dup2",3);
	fifo_stat<int>	parameters_dup3("parameters_dup3",3);
	fifo_stat<int>	jpeg_dec_in("jpeg_dec_in",1);
	fifo_stat<int>	result("result",1);
	fifo_stat<int>	result_dup1("result_dup1",1);
	fifo_stat<int>	result_dup2("result_dup2",1);

	// definition of signals
	sc_clock 				clk("clock", 12, SC_NS);
	sc_signal<bool> 		ask;
	sc_signal<bool>			ready;
	sc_signal< sc_uint<8> > data;

	// processing of command-line arguments
	bool	detected;
	for(int i=3; i<=argc; i+=2) {
		cout << argv[i-2] << " " << argv[i-1] << endl;
		detected = 0;
		if (strcmp(argv[i-2],"-i")==0) {
			inputfile = argv[i-1];
			detected = 1;
		}
		if (strcmp(argv[i-2],"-o")==0) {
			outputfile = argv[i-1];
			detected = 1;
		}
		if (strcmp(argv[i-2],"-t")==0) {
			typefile = argv[i-1];
			detected = 1;
		}
		if (detected == 0) {
			cout << "option " << argv[i-2] << " not known " << endl;
		}
	}

	//	definition of modules
	src src1("src1", inputfile, MAXWIDTH);
	src1.output(stimulus);
	src1.parameters(parameters);

	df_fork<int,2> fork1("fork1");
	fork1.in(stimulus);
	fork1.out[0](stimulus_dup1);
	fork1.out[1](stimulus_dup2);

	df_fork<int,3> fork_param("fork_param");
	fork_param.in(parameters);
	fork_param.out[0](parameters_dup1);
	fork_param.out[1](parameters_dup2);
	fork_param.out[2](parameters_dup3);

//	jpeg_enc jpeg_enc_1("jpeg_enc_1", quantization, MAXWIDTH);
//	jpeg_enc_1.input(stimulus_dup1);
//	jpeg_enc_1.clk(clk);
//	jpeg_enc_1.parameters(parameters_dup1);
//	jpeg_enc_1.output(data);
//	jpeg_enc_1.ready(ready);
//	jpeg_enc_1.ask(ask);

	jpeg_enc_float jpeg_enc_1("jpeg_enc_1", quantization_i, MAXWIDTH);
	jpeg_enc_1.input(stimulus_dup1);
	jpeg_enc_1.clk(clk);
	jpeg_enc_1.parameters(parameters_dup1);
	jpeg_enc_1.output(data);
	jpeg_enc_1.ready(ready);
	jpeg_enc_1.ask(ask);

	jpeg_dec jpeg_dec_1("jpeg_dec_1", quantization, MAXWIDTH, typefile);
	jpeg_dec_1.input(data);
	jpeg_dec_1.clk(clk);
	jpeg_dec_1.ready(ready);
	jpeg_dec_1.ask(ask);
	jpeg_dec_1.parameters(parameters_dup2);
	jpeg_dec_1.output(result);

	df_fork<int,2> fork2("fork2");
	fork2.in(result);
	fork2.out[0](result_dup1);
	fork2.out[1](result_dup2);

	snk snk1("snk1", outputfile);
	snk1.input(result_dup1);
	snk1.parameters(parameters_dup3);

	test test1("test1");
	test1.reference(stimulus_dup2);
	test1.data(result_dup2);

	sc_start();

	return 0;
}
コード例 #13
0
ファイル: ttymsg.c プロジェクト: antoine1fr/inetutils
/*
 * Display the contents of a uio structure on a terminal.  Used by wall(1),
 * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
 * waiting up to tmout seconds.  Returns pointer to error string on unexpected
 * error; string is not newline-terminated.  Various "normal" errors are
 * ignored (exclusive-use, lack of permission, etc.).
 */
char *
ttymsg (struct iovec *iov, int iovcnt, char *line, int tmout)
{
  static char errbuf[MAX_ERRBUF];
  char *device;
  register int cnt, fd, left, wret;
  struct iovec localiov[6];
  int forked = 0;

  if (iovcnt > (int) (sizeof (localiov) / sizeof (localiov[0])))
    return (char *) ("too many iov's (change code in wall/ttymsg.c)");

  device = malloc (sizeof PATH_TTY_PFX - 1 + strlen (line) + 1);
  if (!device)
    {
      snprintf (errbuf, sizeof errbuf,
		"Not enough memory for tty device name");
      return errbuf;
    }

  strcpy (device, PATH_TTY_PFX);
  strcat (device, line);
  normalize_path (device, "/");
  if (strncmp (device, PATH_TTY_PFX, strlen (PATH_TTY_PFX)))
    {
      /* An attempt to break security... */
      snprintf (errbuf, sizeof (errbuf), "bad line name: %s", line);
      return (errbuf);
    }

  /*
   * open will fail on slip lines or exclusive-use lines
   * if not running as root; not an error.
   */
  fd = open (device, O_WRONLY | O_NONBLOCK, 0);
  if (fd < 0)
    {
      if (errno == EBUSY || errno == EACCES)
	return (NULL);
      snprintf (errbuf, sizeof (errbuf), "%s: %s", device, strerror (errno));
      free (device);
      return errbuf;
    }

  for (cnt = left = 0; cnt < iovcnt; ++cnt)
    left += iov[cnt].iov_len;

  for (;;)
    {
      wret = writev (fd, iov, iovcnt);
      if (wret >= left)
	break;
      if (wret >= 0)
	{
	  left -= wret;
	  if (iov != localiov)
	    {
	      memcpy (localiov, iov, iovcnt * sizeof (struct iovec));
	      iov = localiov;
	    }
	  for (cnt = 0; wret >= (int) iov->iov_len; ++cnt)
	    {
	      wret -= iov->iov_len;
	      ++iov;
	      --iovcnt;
	    }
	  if (wret)
	    {
	      iov->iov_base = (char *) iov->iov_base + wret;
	      iov->iov_len -= wret;
	    }
	  continue;
	}
      if (errno == EWOULDBLOCK)
	{
	  int cpid, off = 0;

	  if (forked)
	    {
	      close (fd);
	      _exit (EXIT_FAILURE);
	    }
	  cpid = fork2 ();
	  if (cpid < 0)
	    {
	      snprintf (errbuf, sizeof (errbuf),
			"fork: %s", strerror (errno));
	      close (fd);
	      free (device);
	      return (errbuf);
	    }
	  if (cpid)		/* Parent.  */
	    {
	      close (fd);
	      free (device);
	      return (NULL);
	    }
	  forked++;
	  /* wait at most tmout seconds */
	  signal (SIGALRM, SIG_DFL);
	  signal (SIGTERM, SIG_DFL);	/* XXX */
#ifdef HAVE_SIGACTION
	  {
	    sigset_t empty;
	    sigemptyset (&empty);
	    sigprocmask (SIG_SETMASK, &empty, 0);
	  }
#else
	  sigsetmask (0);
#endif
	  alarm ((u_int) tmout);
	  fcntl (fd, O_NONBLOCK, &off);
	  continue;
	}
      /*
       * We get ENODEV on a slip line if we're running as root,
       * and EIO if the line just went away.
       */
      if (errno == ENODEV || errno == EIO)
	break;
      close (fd);
      if (forked)
	_exit (EXIT_FAILURE);
      snprintf (errbuf, sizeof (errbuf), "%s: %s", device, strerror (errno));
      free (device);
      return (errbuf);
    }

  free (device);
  close (fd);
  if (forked)
    _exit (EXIT_SUCCESS);
  return (NULL);
}
コード例 #14
0
int sc_main (int argc , char *argv[]) {

/*
	int	quantization[64] = { 8,  6,  6,  7,  6,  5,  8,  7,
								 7,  7,  9,  9,  8, 10, 12, 20,
								13, 12, 11, 11, 12, 25, 18, 19,
								15, 20, 29, 26, 31, 30, 29, 26,
								28, 28, 32, 36, 46, 39, 32, 34,
								44, 35, 28, 28, 40, 55, 41, 44,
								48, 49, 52, 52, 52, 31, 39, 57,
								61, 56, 50, 60, 46, 51, 52, 50 };
*/

	int	quantization[64] = { 16, 11, 10, 16, 24, 40, 51, 61,
								 12, 12, 14, 19, 26, 58, 60, 55,
								 14, 13, 16, 24, 40, 57, 69, 56,
								 14, 17, 22, 29, 51, 87, 80, 62,
								 18, 22, 37, 56, 68,109,103, 77,
								 24, 35, 55, 64, 81,104,113, 92,
								 49, 64, 78, 87,103,121,120,101,
								 72, 92, 95, 98,112,100,103, 99};

// definition of default files
	const char*	inputfile		= "datain.pgm";
	const char*	outputfile		= "dataout.pgm";
	const char* typefile 		= "types.txt";

//	definition of FIFO queues
	fifo_stat<int>	stimulus("stimulus",1);
	fifo_stat<int>	parameters("parameters",3);
	fifo_stat<int>	stimulus_dup1("stimulus_dup1",1);
	fifo_stat<int>	stimulus_dup2("stimulus_dup2",MAXWIDTH8*8+64+64+64+64+64);
	fifo_stat<int>	parameters_dup1("parameters_dup1",3);
	fifo_stat<int>	parameters_dup2("parameters_dup2",3);
	fifo_stat<int>	parameters_dup3("parameters_dup3",3);
	// fifo_stat<int>	jpeg_enc_out("jpeg_enc_out",1);
	fifo_stat<int>	jpeg_dec_in("jpeg_dec_in",1);
	fifo_stat<int>	result("result",1);
	fifo_stat<int>	result_dup1("result_dup1",1);
	fifo_stat<int>	result_dup2("result_dup2",1);

  sc_signal<bool> ask,ready;
  sc_signal<sc_int<9> > data;

// processing of command-line arguments
	bool	detected;
	for(int i=3; i<=argc; i+=2) {
		cout << argv[i-2] << " " << argv[i-1] << endl;
		detected = 0;
		if (strcmp(argv[i-2],"-i")==0) {
			inputfile = argv[i-1];
			detected = 1;
		}
		if (strcmp(argv[i-2],"-o")==0) {
			outputfile = argv[i-1];
			detected = 1;
		}
		if (strcmp(argv[i-2],"-t")==0) {
			typefile = argv[i-1];
			detected = 1;
		}
		if (detected == 0) {
			cout << "option " << argv[i-2] << " not known " << endl;
		}
	}

//	definition of modules

  sc_clock clk ( "clock",10, SC_NS ) ;

	src src1("src1", inputfile, MAXWIDTH);
	src1.output(stimulus);
	src1.parameters(parameters);

	df_fork<int,2> fork1("fork1");
	fork1.in(stimulus);
	fork1.out[0](stimulus_dup1);
	fork1.out[1](stimulus_dup2);

	df_fork<int,3> fork_param("fork_param");
	fork_param.in(parameters);
	fork_param.out[0](parameters_dup1);
	fork_param.out[1](parameters_dup2);
	fork_param.out[2](parameters_dup3);

	jpeg_enc jpeg_enc_1("jpeg_enc_1", quantization, MAXWIDTH);
	jpeg_enc_1.input(stimulus_dup1);
	jpeg_enc_1.clk(clk);
	jpeg_enc_1.parameters(parameters_dup1);
	jpeg_enc_1.output(data);
	jpeg_enc_1.ready(ready);
	jpeg_enc_1.ask(ask);
  
  // FF2P<int> FF2P_1("FF2P_1");
  // FF2P_1.input(jpeg_enc_out);
  // FF2P_1.clk(clk);
  // FF2P_1.ask(ask);
  // FF2P_1.ready(ready);
  // FF2P_1.output(data);
  
  // P2FF<int> P2FF_1("P2FF_1");
  // P2FF_1.input(data);
  // P2FF_1.clk(clk);
  // P2FF_1.ask(ask);
  // P2FF_1.ready(ready);
  // P2FF_1.output(jpeg_dec_in);

	jpeg_dec jpeg_dec_1("jpeg_dec_1", quantization, MAXWIDTH, typefile);
	jpeg_dec_1.input(data);
	jpeg_dec_1.clk(clk);
	jpeg_dec_1.ready(ready);
	jpeg_dec_1.ask(ask);
	jpeg_dec_1.parameters(parameters_dup2);
	jpeg_dec_1.output(result);

	df_fork<int,2> fork2("fork2");
	fork2.in(result);
	fork2.out[0](result_dup1);
	fork2.out[1](result_dup2);

	snk snk1("snk1", outputfile);
	snk1.input(result_dup1);
	snk1.parameters(parameters_dup3);

	test test1("test1");
	test1.reference(stimulus_dup2);
	test1.data(result_dup2);

	// sc_start();
	sc_start(200000,SC_NS);

	return 0;
}
コード例 #15
0
ファイル: main.cpp プロジェクト: moiri-usi/USI-specification
int sc_main (int argc , char *argv[]) {

/*
	int	quantization[64] = { 8,  6,  6,  7,  6,  5,  8,  7,
								 7,  7,  9,  9,  8, 10, 12, 20,
								13, 12, 11, 11, 12, 25, 18, 19,
								15, 20, 29, 26, 31, 30, 29, 26,
								28, 28, 32, 36, 46, 39, 32, 34,
								44, 35, 28, 28, 40, 55, 41, 44,
								48, 49, 52, 52, 52, 31, 39, 57,
								61, 56, 50, 60, 46, 51, 52, 50 };
*/

	int	quantization[64] = { 16, 11, 10, 16, 24, 40, 51, 61,
								 12, 12, 14, 19, 26, 58, 60, 55,
								 14, 13, 16, 24, 40, 57, 69, 56,
								 14, 17, 22, 29, 51, 87, 80, 62,
								 18, 22, 37, 56, 68,109,103, 77,
								 24, 35, 55, 64, 81,104,113, 92,
								 49, 64, 78, 87,103,121,120,101,
								 72, 92, 95, 98,112,100,103, 99};

// definition of default files
	const char*	inputfile		= "datain.pgm";
	const char*	outputfile		= "dataout.pgm";
	const char* typefile 		= "types.txt";
	const char* costfilename;
	bool outputcost = false;

//	definition of FIFO queues
	fifo_stat<int>	stimulus("stimulus",1);
	fifo_stat<int>	parameters("parameters",3);
	fifo_stat<int>	stimulus_dup1("stimulus_dup1",1);
	fifo_stat<int>	stimulus_dup2("stimulus_dup2",MAXWIDTH8*8+64+64+64+64+64);
	fifo_stat<int>	parameters_dup1("parameters_dup1",3);
	fifo_stat<int>	parameters_dup2("parameters_dup2",3);
	fifo_stat<int>	parameters_dup3("parameters_dup3",3);
	fifo_stat<int>	jpeg_enc_out("jpeg_enc_out",1);
	fifo_stat<int>	result("result",1);
	fifo_stat<int>	result_dup1("result_dup1",1);
	fifo_stat<int>	result_dup2("result_dup2",1);


// processing of command-line arguments
	bool	detected;
	for(int i=3; i<=argc; i+=2) {
		cout << argv[i-2] << " " << argv[i-1] << endl;
		detected = 0;
		if (strcmp(argv[i-2],"-i")==0) {
			inputfile = argv[i-1];
			detected = 1;
		}
		if (strcmp(argv[i-2],"-o")==0) {
			outputfile = argv[i-1];
			detected = 1;
		}
		if (strcmp(argv[i-2],"-t")==0) {
			typefile = argv[i-1];
			detected = 1;
		}
		if (strcmp(argv[i-2],"-c")==0) {
			costfilename = argv[i-1];
			outputcost = true;
			detected = 1;
		}
		if (detected == 0) {
			cout << "option " << argv[i-2] << " not known " << endl;
		}
	}

//	definition of modules

	src src1("src1", inputfile, MAXWIDTH);
	src1.output(stimulus);
//	src1.output(stimulus_dup1);
//	src1.output(stimulus_dup2);
	src1.parameters(parameters);

	df_fork<int,2> fork1("fork1");
	fork1.in(stimulus);
	fork1.out[0](stimulus_dup1);
	fork1.out[1](stimulus_dup2);

	df_fork<int,3> fork_param("fork_param");
	fork_param.in(parameters);
	fork_param.out[0](parameters_dup1);
	fork_param.out[1](parameters_dup2);
	fork_param.out[2](parameters_dup3);

	jpeg_enc jpeg_enc_1("jpeg_enc_1", quantization, MAXWIDTH);
	jpeg_enc_1.input(stimulus_dup1);
	jpeg_enc_1.parameters(parameters_dup1);
	jpeg_enc_1.output(jpeg_enc_out);

	jpeg_dec jpeg_dec_1("jpeg_dec_1", quantization, MAXWIDTH, typefile);
	jpeg_dec_1.input(jpeg_enc_out);
	jpeg_dec_1.parameters(parameters_dup2);
	jpeg_dec_1.output(result);

	df_fork<int,2> fork2("fork2");
	fork2.in(result);
	fork2.out[0](result_dup1);
	fork2.out[1](result_dup2);

	snk snk1("snk1", outputfile);
	snk1.input(result_dup1);
	snk1.parameters(parameters_dup3);

	test test1("test1");
	test1.reference(stimulus_dup2);
	test1.data(result_dup2);

	sc_start();

	if (outputcost == true) {
		int cost = jpeg_dec_1.idct_1->hardware_cost();
		float snr = test1.snr();
		float psnr = test1.psnr();
		cout << "hardware cost IDCT 2nd " << cost << endl;
		ofstream *costfile;
		costfile = new ofstream(costfilename);
		if (!costfile->is_open()) cout << "Error opening file " << costfilename << endl;
		*costfile << cost << " " << snr << " " << psnr << endl;
		costfile->close();
	}
	return 0;
}