Ejemplo n.º 1
0
/* 
 * A simple test for the binary embedding
 */
int test_embed_bin()
{
    int i, j, n, err = 0;
    string_t strs[10];

    input_config("lines");
    char *test_file = getenv("TEST_FILE");
    n = input_open(test_file);
    input_read(strs, n);

    test_printf("Testing binary embedding");
    config_set_string(&cfg, "features.vect_embed", "bin");
    config_set_string(&cfg, "features.vect_norm", "none");

    for (i = 0, err = 0; i < n; i++) {
        fvec_t *fv = fvec_extract(strs[i].str, strs[i].len);
        double n = 0;
        for (j = 0; j < fv->len; j++)
            n += fv->val[j];
        err += fabs(n - fv->len) > 1e-6;
        fvec_destroy(fv);
    }
    test_return(err, n);

    input_free(strs, n);
    input_close();
    return err;
}
Ejemplo n.º 2
0
/* 
 * A simple test for the l2 norm
 */
int test_norm_l2()
{
    int i, j, n, err = 0;
    string_t strs[10];

    input_config("lines");
    char *test_file = getenv("TEST_FILE");
    n = input_open(test_file);
    input_read(strs, n);

    test_printf("Testing L2 normalization");
    config_set_string(&cfg, "features.vect_norm", "l2");
    for (i = 0, err = 0; i < n; i++) {
        fvec_t *fv = fvec_extract(strs[i].str, strs[i].len);
        double n = 0;
        for (j = 0; j < fv->len; j++)
            n += fv->val[j] * fv->val[j];
        err += fabs(sqrt(n) - 1.0) > 1e-6;
        fvec_destroy(fv);
    }
    test_return(err, n);

    input_free(strs, n);
    input_close();
    return err;
}
Ejemplo n.º 3
0
/* 
 * A simple test for the binary embedding
 */
int test_embed_tfidf()
{
    int i, j, n, err = 0;
    string_t strs[10];

    config_set_string(&cfg, "features.vect_norm", "none");
    config_set_string(&cfg, "features.tfidf_file", TEST_TFIDF);

    unlink(TEST_TFIDF);
    char *test_file = getenv("TEST_FILE");
    idf_create(test_file);
    test_printf("Testing TFIDF embedding");

    input_config("lines");
    n = input_open(test_file);
    input_read(strs, n);

    /* Compute IDF manually */
    config_set_string(&cfg, "features.vect_embed", "bin");
    fvec_t *w = fvec_zero();
    for (i = 0, err = 0; i < n; i++) {
        fvec_t *fv = fvec_extract(strs[i].str, strs[i].len);
        fvec_add(w, fv);
        fvec_destroy(fv);
    }
    fvec_invert(w);
    fvec_mul(w, n);
    fvec_log2(w);

    if (!idf_check(w)) {
        err++;
        test_error("(%d) internal idf values seem to be wrong", i);
    }

    /* Invert w for multiplying out IDFs */
    fvec_invert(w);

    config_set_string(&cfg, "features.vect_embed", "tfidf");
    for (i = 0, err = 0; i < n; i++) {
        fvec_t *fv = fvec_extract(strs[i].str, strs[i].len);
        fvec_times(fv, w);

        /* Check if rest tf */
        double d = 0;
        for (j = 0; j < fv->len; j++)
            d += fv->val[j];
        err += fabs(d - 1.0) > 1e-6;
        fvec_destroy(fv);
    }
    test_return(err, n);

    fvec_destroy(w);
    input_free(strs, n);
    input_close();

    idf_destroy();
    unlink(TEST_TFIDF);

    return err;
}
Ejemplo n.º 4
0
void read_coordinates(char *argv[])
{
  static FILE *fp;
  static char Species[20];
  static double tmp0,tmp1;
  static int i,j;

  Gxyz = (double**)malloc(sizeof(double*)*(atomnum+1)); 
  for (i=0; i<=atomnum; i++){
    Gxyz[i] = (double*)malloc(sizeof(double)*60); 
  }

  if ((fp = fopen(argv[2],"r")) != NULL){
    printf("Read the coordinates file (%s)\n",argv[2]);

    /****************************************************
                         open the file
    ****************************************************/

    input_open(argv[2]);

    /****************************************************
                         read data
    ****************************************************/

    if (fp=input_find("<Atoms.SpeciesAndCoordinates") ) {
      for (i=1; i<=atomnum; i++){
        fscanf(fp,"%i %s %lf %lf %lf %lf %lf",&j,&Species,
	       &Gxyz[i][1],&Gxyz[i][2],&Gxyz[i][3],&tmp0,&tmp1);

        if (i!=j){
          printf("Format error of the sequential number %i in <Atoms.SpeciesAndCoordinates\n",j);
          exit(0);
        }
      }

      if (!input_last("Atoms.SpeciesAndCoordinates>")) {
        /* format error */
        printf("Format error for Atoms.SpeciesAndCoordinates\n");
        exit(0);
      }
    }

    /****************************************************
                       input_close
    ****************************************************/

    input_close();
  }
  else {
    printf("Failure of reading the coordinates file (%s).\n",argv[2]);
  }

  for (i=1; i<=atomnum; i++){
    printf("i=%i  coodinates=%15.12f %15.12f %15.12f\n",
	   i,Gxyz[i][1],Gxyz[i][2],Gxyz[i][3]);
  }

}
Ejemplo n.º 5
0
int cm36651_light_init(struct smdk4x12_sensors_handlers *handlers,
	struct smdk4x12_sensors_device *device)
{
	struct cm36651_light_data *data = NULL;
	char path[PATH_MAX] = { 0 };
	int input_fd = -1;
	int rc;

	ALOGD("%s(%p, %p)", __func__, handlers, device);

	if (handlers == NULL)
		return -EINVAL;

	data = (struct cm36651_light_data *) calloc(1, sizeof(struct cm36651_light_data));

	input_fd = input_open("light_sensor");
	if (input_fd < 0) {
		ALOGE("%s: Unable to open input", __func__);
		goto error;
	}

	rc = sysfs_path_prefix("light_sensor", (char *) &path);
	if (rc < 0 || path[0] == '\0') {
		ALOGE("%s: Unable to open sysfs", __func__);
		goto error;
	}

	snprintf(data->path_enable, PATH_MAX, "%s/enable", path);
	snprintf(data->path_delay, PATH_MAX, "%s/poll_delay", path);

	handlers->poll_fd = input_fd;
	handlers->data = (void *) data;

	return 0;

error:
	if (data != NULL)
		free(data);

	if (input_fd >= 0)
		close(input_fd);

	handlers->poll_fd = -1;
	handlers->data = NULL;

	return -1;
}
Ejemplo n.º 6
0
apr_status_t h2_stream_add_header(h2_stream *stream,
                                  const char *name, size_t nlen,
                                  const char *value, size_t vlen)
{
    AP_DEBUG_ASSERT(stream);
    if (h2_stream_is_scheduled(stream)) {
        return h2_request_add_trailer(stream->request, stream->pool,
                                      name, nlen, value, vlen);
    }
    else {
        if (!input_open(stream)) {
            return APR_ECONNRESET;
        }
        return h2_request_add_header(stream->request, stream->pool,
                                     name, nlen, value, vlen);
    }
}
Ejemplo n.º 7
0
Archivo: sally.c Proyecto: yangke/sally
/**
 * Init the Sally tool
 * @param argc number of arguments
 * @param argv arguments
 */
static void sally_init()
{
    int ehash;
    const char *cfg_str;

    if (verbose > 1)
        config_print(&cfg);

    /* Set delimiters */
    config_lookup_string(&cfg, "features.ngram_delim", &cfg_str);
    if (strlen(cfg_str) > 0) 
        fvec_delim_set(cfg_str);

    /* Check for TFIDF weighting */
    config_lookup_string(&cfg, "features.vect_embed", &cfg_str);
    if (!strcasecmp(cfg_str, "tfidf"))
        idf_create(input);

    /* Load stop words */
    config_lookup_string(&cfg, "input.stopword_file", &cfg_str);
    if (strlen(cfg_str) > 0)
        stopwords_load(cfg_str);

    /* Check for feature hash table */
    config_lookup_int(&cfg, "features.explicit_hash", &ehash);
    config_lookup_string(&cfg, "features.hash_file", &cfg_str);
    if (ehash || strlen(cfg_str) > 0) {
        info_msg(1, "Enabling feature hash table.");
        fhash_init();
    }

    /* Open input */
    config_lookup_string(&cfg, "input.input_format", &cfg_str);
    input_config(cfg_str);
    info_msg(1, "Opening '%0.40s' with input module '%s'.", input, cfg_str);
    entries = input_open(input);
    if (entries < 0)
        fatal("Could not open input source");

    /* Open output */
    config_lookup_string(&cfg, "output.output_format", &cfg_str);
    output_config(cfg_str);
    info_msg(1, "Opening '%0.40s' with output module '%s'.", output, cfg_str);
    if (!output_open(output))
        fatal("Could not open output destination");
}
Ejemplo n.º 8
0
Archivo: nal.c Proyecto: walafc0/soclib
int32_t _test_nal(int32_t argc, int8_t *argv[])
{
  nal_unit unit;
  int32_t count;

  if (!input_open("../streams/in.264"))
    return 1;

  for (count = 1; get_next_nal_unit(&unit); ++count) {
    printf("%d: count=%d zero=%d ref_idc=%d type: %s\n", count,
	   unit.NumBytesInNALunit, unit.forbidden_zero_bit,
	   unit.nal_ref_idc, _str_nal_unit_type(unit.nal_unit_type));
  }

  input_close();
  return 0;
}
Ejemplo n.º 9
0
int
main (int ac, char *av[])
{
    char        string [BUFFER];
    char        aryidx [BUFFER];
    FILE       *in;
    int         rtn, n;

    options (ac, av);

    create_rex (configure ());

    in = input_open ();

    if (opt_webpage)
        initialize_htm ();
    else
        initialize_con ();

    inp_lineno = 0;
    while (fgets (string, sizeof (string), in)) {
        string [n=strlen (string)-1] = 0;
        inp_lineno++;
        rtn = colour_sca (string, aryidx, n);
        if (opt_webpage) {
            if (!opt_matching || rtn)
                printf_htm (string, aryidx, n);
        } else {
            if (!opt_matching || rtn)
                printf_con (string, aryidx, n);
        }
    }

    if (opt_webpage)
        finalize_htm ();
    else
        finalize_con ();

    return (0);
}
Ejemplo n.º 10
0
gint bossao_play (gchar *filename)
{
   input_plugin_s *plugin = input_plugin_find (filename);
   input_plugin_set (plugin);
   input_open (plugin, filename);
   //bossao_pause ();
   //produce_eof_sem->count = 0;
   //consume_eof_sem->count = 0;
   bossao_pause ();
   //semaphore_v (produce_eof_sem);
   static_semaphore_v (&produce_eof_sem);
   //semaphore_v (consume_eof_sem);
   static_semaphore_v (&consume_eof_sem);
   //g_mutex_unlock (produce_mutex);
   // give the produce buffer a little time to fill
   g_usleep (50000);
   //paused = 0;
   //g_mutex_unlock (pause_mutex);
   bossao_unpause ();
   
   return 0;
}
Ejemplo n.º 11
0
static int runtest(char *filename, char *ironout)
{
	struct input *input = input_open(filename);
	char current_line[MAXLINELEN];
	char cmd[128];
	char *line;
	if (!input)
		return 1;
	while ((line = input_line(input))) {
		int result = -1;
		strcpy(current_line, line);
		nthtoken(cmd, line, " \n", 2);
		if (!strcmp(cmd, "comment") || !strcmp(cmd, "#") || !*cmd)
			result = read_comment(input);
		if (!strcmp(cmd, "write") || !strcmp(cmd, ">"))
			result = write_file(input);
		if (!strcmp(cmd, "read") || !strcmp(cmd, "<"))
			result = read_file(input);
		if (!strcmp(cmd, "ironout"))
			result = exec_ironout(input, ironout);
		if (result == -1) {
			printf("unknown cmd: %s\n", cmd);
			return 1;
		}
		if (result > 0) {
			char *testname = filename;
			if (strchr(testname, '/'))
				testname = strrchr(testname, '/') + 1;
			printf("%s:%d %s",
			       testname, input->lineno, current_line);
			return 1;
		}
	}
	input_free(input);
	return 0;
}
Ejemplo n.º 12
0
int 
main(int argc, char** argv) {


  int c, i, rd, j;
  long bytes_read = 0;
  int samples_per_tick;
  div_t rest;
  rdfun input_read;
  char* input = "alsa:default";

  while ( ( c = getopt(argc, argv, "vphN:o:g:k:r:c:lS:i:t:s") ) != -1) 
	switch (c) {
	case 'v': debug++; break;
	case 'p': print_freqs = 1; break;
	case 'h': use_harm_comp = 1; break;
	case 'N': N = atoi(optarg); break;
	case 'g': gain = atof(optarg); break;
	case 'o': 
	  if (strcmp(optarg, "seq") == 0 ) use_sequencer = 1;
	  else midi_filename =strdup(optarg); 
	  break;
	case 'k': hysteresis = atof(optarg); break;
	case 'r': ringsize = atoi(optarg); break;
	case 'c': midi_channel = atoi(optarg); break;
	case 'i': input = strdup(optarg); break;
	case 'S': SR = atoi(optarg); break;
	case 's': print_statistics = 1; break;
	case 't': threshold = atof(optarg); break;
	default: usage();
	}

  if ( debug > 0 )
	fprintf(stderr, 
			"ringbuffersize: %d ringbuffermask:0x%08x\n"
			"gain: %f\n",
			RINGBUFFERSIZE, RINGBUFFERMASK, gain);



  print_prologoue(N, SR);

  input_read = input_open(input);

  // allocate buffers

  buffer_f = (real_t**)malloc ( RINGBUFFERSIZE * sizeof(real_t*) );
  for ( j = 0; j < RINGBUFFERSIZE; j++ ) {
	buffer_f[j] = malloc ( N * sizeof(real_t) );
	for ( i = 0; i < N ; i ++ )
	  buffer_f[j][i] = 0.0;
  }


  // prepare midi file
  if (midi_filename != NULL ) {
	midi_file = midi_write_open(midi_filename);
	midi_write_header(midi_timeDivision, 1, midi_file);
	midi_write_track_header(midi_file);
  }

  if ( use_sequencer )
	midi_connect_sequencer();


  // prebuffer everthing
  precalculate();

  samples_per_tick = samples_per_midi_tick(SR, midi_bpm, midi_timeDivision);
  rest.rem = 0;

  // process data
  while ( (rd = input_read(buffer_f[current_buffer], N)) ) { 

	bytes_read += rd;

	rest = div(rest.rem + N, samples_per_tick);

	if (midi_file != NULL )
	  midi_write_increase_difftime(rest.quot, midi_file);

	absolute_time += N;

 	scan_freqs(); 

	// advance buffer:
	current_buffer = (current_buffer+1) & RINGBUFFERMASK;
  }



  for ( j = lo_note; j < hi_note; j++ ) {
	if ( act_freq[j] )
	  note_off(j, 0);
  }

  // free stuff
  for ( j = 0; j < RINGBUFFERSIZE; j++ ) free(buffer_f[j]);
  free(buffer_f);
  for (i = 0; i< NTONES; i++ ) free(cos_precalc[i]);

  // fixup midi
  if (midi_file != NULL ) {
	midi_write_track_end(midi_file);
	midi_write_close(midi_file);
  }


  if ( print_statistics ) {

	// octave style output of powers over frequency
	// first row: frequencies
	// second row: power for that freq
	fprintf(stderr, "freqs = [");
	for (j=lo_note;j<hi_note-1;j++) fprintf(stderr, "%.2f,", midi_note_to_hertz(j));
	fprintf(stderr, "%.2f", midi_note_to_hertz(j));
	fprintf(stderr, ";");
	for (j=lo_note;j<hi_note-1;j++) fprintf(stderr, "%.2f,", max_powers[j]);
	fprintf(stderr, "%.2f", max_powers[j]);
	fprintf(stderr, "];\n");
	
	fprintf(stderr, 
			"\n\n note_ons:%ld bytes_read:%ld playtime:%ld:%ld\n", 
			stats_note_ons, bytes_read, bytes_read/(SR*60), (bytes_read / SR)%60 );
  }


  input_close();

  print_epilogue();

  return 0;
}
Ejemplo n.º 13
0
void Show_DFT_DATA(char *argv[]) 
{
  FILE *fp,*fp0,*fp1,*fp2,*fp3;
  int Num_DatFiles,i,j,k,fp_OK;
  int Num_Atoms;
  int NGrid1_1,NGrid1_2,NGrid1_3;
  int NGrid2_1,NGrid2_2,NGrid2_3;
  double Utot1,Utot2,dU,dF;
  double gx,gy,gz,fx,fy,fz;
  double sum1,sum2;
  double time1,TotalTime;
  char SN[30][YOUSO10];
  char SB[30][YOUSO10];
  char SV[30][YOUSO10];
  char fname[YOUSO10];
  char fname0[YOUSO10];
  char fname1[YOUSO10];
  char fname2[YOUSO10];
  char fname_dat[YOUSO10];
  char fname_dat2[YOUSO10];
  char fname_out1[YOUSO10];
  char fname_out2[YOUSO10];
  char ftmp[YOUSO10];
  fname_type *fndat;
  int numprocs,myid;

  char *dir;
  char input_dir[300];
  char *output_file;
  DIR *dp;
  struct dirent *entry;

  MPI_Request request;
  MPI_Status  status;

  /* set up MPI */

  MPI_Comm_size(mpi_comm_level1,&numprocs);
  MPI_Comm_rank(mpi_comm_level1,&myid);

  sprintf(input_dir,"%s",argv[2]);

  printf("%s\n",input_dir);
  
  /* print the header */

  if (myid==Host_ID){

    /* set dir */

    dir = input_dir;

    /* count the number of dat files */

    if(( dp = opendir(dir) ) == NULL ){
      printf("could not find the directry '%s'\n",input_dir);
      MPI_Finalize();
      exit(0);
    }

    Num_DatFiles = 0;
    while((entry = readdir(dp)) != NULL){

      if ( strstr(entry->d_name,".dat")!=NULL ){ 
          
        Num_DatFiles++;
      }
    }
    closedir(dp);

    fndat = (fname_type*)malloc(sizeof(fname_type)*Num_DatFiles);

    /* store the name of dat files */

    if(( dp = opendir(dir) ) == NULL ){
      printf("could not find the directry '%s'\n",input_dir);
      MPI_Finalize();
      exit(0);
    }

    Num_DatFiles = 0;
    while((entry = readdir(dp)) != NULL){
 
      if ( strstr(entry->d_name,".dat")!=NULL ){ 

        sprintf(fndat[Num_DatFiles].fn,"%s/%s",input_dir,entry->d_name);  
        Num_DatFiles++;
      }
    }
    closedir(dp);

  } /* if (myid==Host_ID) */

  if (myid==Host_ID){
    printf(" %2d dat files are found in the directory '%s'.\n\n\n",Num_DatFiles,input_dir);
  }

  /***********************************************************
         start calculations
  ***********************************************************/

  for (i=0; i<Num_DatFiles; i++){

    sprintf(fname_dat,"%s",fndat[i].fn);

    input_open(fname_dat);
    input_int("Species.Number",&SpeciesNum,0);

    /* read Definition.of.Atomic.Species */

    if (fp=input_find("<Definition.of.Atomic.Species")) {

      for (k=0; k<SpeciesNum; k++){
        fscanf(fp,"%s %s %s",SN[k],SB[k],SV[k]);

        printf("i=%2d k=%2d %s %s\n",i,k,SB[k],SV[k]);

	/*
        SpeciesString2int(i);
	*/
      }

      if (! input_last("Definition.of.Atomic.Species>")) {
	MPI_Finalize();
	exit(0);
      }
    }

    input_close();
  }

  if (myid==Host_ID){
    free(fndat);
  }

  MPI_Barrier(mpi_comm_level1);
  MPI_Finalize();
  exit(0);
}
Ejemplo n.º 14
0
Archivo: esp.c Proyecto: certik/openmx
void read_input(char *file)
{
  static int i,j,spe,po;
  static int coordinates_unit;
  static int ct_AN;
  double r_vec[20],r_vec2[20];
  int i_vec[20],i_vec2[20];
  char *s_vec[20],Species[20];
  char buf[MAXBUF];
  FILE *fp;


  if (input_open(file)){

    /****************************************************
                 find dipole moment
    ****************************************************/

    if (fp=input_find("Dipole") ) {
      fscanf(fp,"%s",buf);
      fscanf(fp,"%s",buf);
      fscanf(fp,"%s %lf %lf %lf",buf,&Ref_DipMx,&Ref_DipMy,&Ref_DipMz);
    }

    input_close();
  }
  else {
    exit(0);
  }

  /****************************************************
              Definition of Atomic Species
  ****************************************************/

  /*
  input_int("Species.Number",&SpeciesNum,0);
  if (SpeciesNum<=0){
    printf("Species.Number may be wrong.\n");
    po++;
  }

  SpeName = (char**)malloc(sizeof(char*)*SpeciesNum);
  for (spe=0; spe<SpeciesNum; spe++){
    SpeName[spe] = (char*)malloc(sizeof(char)*YOUSO10);
  }  

  SpeBasis = (char**)malloc(sizeof(char*)*SpeciesNum);
  for (spe=0; spe<SpeciesNum; spe++){
    SpeBasis[spe] = (char*)malloc(sizeof(char)*YOUSO10);
  }  

  SpeVPS = (char**)malloc(sizeof(char*)*SpeciesNum);
  for (spe=0; spe<SpeciesNum; spe++){
    SpeVPS[spe] = (char*)malloc(sizeof(char)*YOUSO10);
  }  

  if (fp=input_find("<Definition.of.Atomic.Species")) {
    for (i=0; i<SpeciesNum; i++){
      fscanf(fp,"%s %s %s",SpeName[i],SpeBasis[i],SpeVPS[i]);
    }
    if (! input_last("Definition.of.Atomic.Species>")) {

      po++;

      printf("Format error for Definition.of.Atomic.Species\n");
      exit(0);
    }
  }
  */

  /****************************************************
                        atoms
  ****************************************************/

  /*
  input_int("Atoms.Number",&atomnum,0);
  s_vec[0]="Ang";  s_vec[1]="AU";
  i_vec[0]= 0;     i_vec[1]= 1;
  input_string2int("Atoms.SpeciesAndCoordinates.Unit",
                   &coordinates_unit,2,s_vec,i_vec);

  if (fp=input_find("<Atoms.SpeciesAndCoordinates") ) {

    for (i=1; i<=atomnum; i++){
      fgets(buf,MAXBUF,fp);

      sscanf(buf,"%i %s %lf %lf %lf",
             &j, &Species, &Gxyz[i][1],&Gxyz[i][2],&Gxyz[i][3]);

      WhatSpecies[i] = Species2int(Species);

      printf("i=%3d spe=%2d %15.12f %15.12f %15.12f\n",
              i,WhatSpecies[i],Gxyz[i][1],Gxyz[i][2],Gxyz[i][3]);

      if (i!=j){
	printf("Format error of the sequential number %i in <Atoms.SpeciesAndCoordinates\n",j);
	po++;
      }
    }

    ungetc('\n',fp);
    if (!input_last("Atoms.SpeciesAndCoordinates>")) {

      printf("Format error for Atoms.SpeciesAndCoordinates\n");
      po++;
    }

    if (coordinates_unit==0){
      for (i=1; i<=atomnum; i++){
	Gxyz[i][1] = Gxyz[i][1]/BohrR;
	Gxyz[i][2] = Gxyz[i][2]/BohrR;
	Gxyz[i][3] = Gxyz[i][3]/BohrR;
      }
    }
  }
  */


}
int lsm330dlc_acceleration_init(struct smdk4x12_sensors_handlers *handlers,
	struct smdk4x12_sensors_device *device)
{
	struct lsm330dlc_acceleration_data *data = NULL;
	pthread_attr_t thread_attr;
	int device_fd = -1;
	int uinput_fd = -1;
	int input_fd = -1;
	int rc;
	int i;

	ALOGD("%s(%p, %p)", __func__, handlers, device);

	if (handlers == NULL || device == NULL)
		return -EINVAL;

	data = (struct lsm330dlc_acceleration_data *) calloc(1, sizeof(struct lsm330dlc_acceleration_data));

	device_fd = open("/dev/accelerometer", O_RDONLY);
	if (device_fd < 0) {
		ALOGE("%s: Unable to open device", __func__);
		goto error;
	}

	uinput_fd = uinput_rel_create("acceleration");
	if (uinput_fd < 0) {
		ALOGD("%s: Unable to create uinput", __func__);
		goto error;
	}

	input_fd = input_open("acceleration");
	if (input_fd < 0) {
		ALOGE("%s: Unable to open acceleration input", __func__);
		goto error;
	}

	data->thread_continue = 1;

	pthread_mutex_init(&data->mutex, NULL);
	pthread_mutex_lock(&data->mutex);

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

	rc = pthread_create(&data->thread, &thread_attr, lsm330dlc_acceleration_thread, (void *) handlers);
	if (rc < 0) {
		ALOGE("%s: Unable to create lsm330dlc acceleration thread", __func__);
		pthread_mutex_destroy(&data->mutex);
		goto error;
	}

	data->device_fd = device_fd;
	data->uinput_fd = uinput_fd;
	handlers->poll_fd = input_fd;
	handlers->data = (void *) data;

	return 0;

error:
	if (data != NULL)
		free(data);

	if (uinput_fd >= 0)
		close(uinput_fd);

	if (input_fd >= 0)
		close(input_fd);

	if (device_fd >= 0)
		close(device_fd);

	handlers->poll_fd = -1;
	handlers->data = NULL;

	return -1;
}
Ejemplo n.º 16
0
int h2_stream_input_is_open(h2_stream *stream) 
{
    return input_open(stream);
}
Ejemplo n.º 17
0
static int parse_client_line(const int client_socket, char *msg)
{
  char *token;
  
  /* On récupère le premier mot, s'il est vide, on retourne direct  */
  if (!(token = strtok(msg, " ")))
    return MSG_OK;

  /*****************************************************************************
   *                              CMD_QUIT
   ****************************************************************************/
  if (!strcmp(CMD_QUIT, token))
    {
      send_ok(client_socket, DETAIL_RET_QUIT);
      return MSG_QUIT;
    }
  
  /*****************************************************************************  
   *                          CMD_CREATE_PROCESS 
   ****************************************************************************/
  else if (!strcmp(CMD_CREATE_PROCESS, token))
    {
      char *args[MAX_ARGS];
      char **pc = args;

      /* On récup le nom du prog */
      if (!(token = strtok(NULL, " ")))
	{
	  send_failure(client_socket, DETAIL_RET_CREATE_PROCESS_SYNTAX);
	  return MSG_ERR;
	}
      
      /* strtok renvoie un buffer static, on le copie */
      /* *pc = args[0] = nom du programme */
      if (!(*pc++ = strdup(token))) 
	{
	  perror("strdup");
	  return MSG_ERR;
	}
      
      /* La suite devient optionelle, c'est les arguments */
      while ((token = strtok(NULL, " ")))
	{
	  if ((*pc++ = strdup(token)) == NULL)
	    {
	      perror("strdup");
	      return MSG_ERR;
	    }
	}
      
      *pc = NULL;             /* Fin des arguments */
      
      /* On crée le processus */
      pid_t proc = create_process(args[0], args);

      /* Le processus n'a pas pu être créé */
      if (proc == -1) {
	send_failure(client_socket, DETAIL_RET_CREATE_PROCESS_ERROR);
	return MSG_ERR;
      }

      send_ok(client_socket, itoa(proc));
      return MSG_OK;
    }

  /*****************************************************************************  
   *                          CMD_DESTROY_PROCESS 
   ****************************************************************************/
  else if (!strcmp(CMD_DESTROY_PROCESS, token))
    {
      if ((token = strtok(NULL, " ")) == NULL)
	{
	  send_failure(client_socket, DETAIL_RET_DESTROY_PROCESS_SYNTAX);
	  return MSG_ERR;
	}
      
      pid_t process_to_kill = atoi(token);
      
      if (!process_exists(process_to_kill))
	{
	  send_failure(client_socket, DETAIL_RET_UNKNOWN_PROCESS);
	  return MSG_ERR;
	}
      
      destroy_process(process_to_kill);
      send_ok(client_socket, NULL);
      return MSG_OK;
    }

  /*****************************************************************************  
   *                          CMD_SEND_INPUT      
   ****************************************************************************/
  else if (!strcmp(CMD_SEND_INPUT, token))
    {
      char buffer[MESSAGE_BUFFER_SIZE];
      buffer[0] = '\0';
      
      /* On récup le PID */
      if ((token = strtok(NULL, " ")) == NULL)
	{
	  send_failure(client_socket, DETAIL_RET_SEND_INPUT_SYNTAX);
	  return MSG_ERR;
	}
      
      /* Il existe ? */
      pid_t send_to_process = atoi(token);
      if (!process_exists(send_to_process))
	{
	  send_failure(client_socket, DETAIL_RET_UNKNOWN_PROCESS);
	  return MSG_ERR;
	}
      
      /* Il est déjà terminé ? */
      if (get_return_code(send_to_process) != PROCESS_NOT_TERMINATED)
	{
	  send_failure(client_socket, DETAIL_RET_PROCESS_TERMINATED);
	  return MSG_ERR;
	}

      /* Son stdin est ouvert ? */
      if (!input_open(send_to_process))
	{
	  send_failure(client_socket, DETAIL_RET_INPUT_CLOSE);
	  return MSG_ERR;
	}

      /* On récup' le message à envoyer  */
      /* TODO: Prendre la chaîne telle qu'elle, sans splitter puis merger avec un espace */
      while ((token = strtok(NULL, " ")))
	{
	  strcat(buffer, token);
	  strcat(buffer, " ");
	}
      
      /* Si le message est vide, erreur ! */
      if (strlen(buffer) == 0)
	{
	  send_failure(client_socket, DETAIL_RET_SEND_INPUT_SYNTAX);
	  return MSG_ERR;
        }
      
      /* Sinon on envoie ! */
      send_input(send_to_process, buffer);
      send_ok(client_socket, NULL);
      return MSG_OK;
    }


  /*****************************************************************************  
   *                          CMD_CLOSE_INPUT     
   ****************************************************************************/
  else if (!strcmp(CMD_CLOSE_INPUT, token))
    {
      if ((token = strtok(NULL, " ")) == NULL)
	{
	  send_failure(client_socket, DETAIL_RET_CLOSE_INPUT_SYNTAX);
	  return MSG_ERR;
        }
      
      pid_t process_to_close_input = atoi(token);
      if (!process_exists(process_to_close_input))
	{
	  send_failure(client_socket, DETAIL_RET_UNKNOWN_PROCESS);
	  return MSG_ERR;
	}

      close_input(process_to_close_input);
      send_ok(client_socket, NULL);
      return MSG_OK;
    }
  
  /*****************************************************************************  
   *                          CMD_GET_OUTPUT
   ****************************************************************************/
  else if (!strcmp(CMD_GET_OUTPUT, token))
    {
      if ((token = strtok(NULL, " ")) == NULL)
	{
	  send_failure(client_socket, DETAIL_RET_GET_OUTPUT_SYNTAX);
	  return MSG_ERR;
	}
      
      pid_t process_to_get_output = atoi(token);
      if (!process_exists(process_to_get_output))
	{
	  send_failure(client_socket, DETAIL_RET_UNKNOWN_PROCESS);
	  return MSG_ERR;
        }
     
      get_output(client_socket, process_to_get_output);
      send_ok(client_socket, NULL);
      return MSG_OK;
    }


  /*****************************************************************************  
   *                          CMD_GET_ERROR
   ****************************************************************************/
  else if (!strcmp(CMD_GET_ERROR, token))
    {
      if ((token = strtok(NULL, " ")) == NULL)
	{
	  send_failure(client_socket, DETAIL_RET_GET_ERROR_SYNTAX);
	  return MSG_ERR;
        }
      
      pid_t process_to_get_error = atoi(token);
      if (!process_exists(process_to_get_error))
	{
	  send_failure(client_socket, DETAIL_RET_UNKNOWN_PROCESS);
	  return MSG_ERR;
	}
      
      get_error(client_socket, process_to_get_error);
      send_ok(client_socket, NULL);
      return MSG_OK;
    }


  /*****************************************************************************  
   *                          CMD_GET_RETURN_CODE
   ****************************************************************************/
  else if (!strcmp(CMD_GET_RETURN_CODE, token))
    {
      if ((token = strtok(NULL, " ")) == NULL)
	{
	  send_failure(client_socket, DETAIL_RET_GET_RETURN_CODE_SYNTAX);
	  return MSG_ERR;
        }
      
      pid_t process_to_get_ret = atoi(token);
      if (!process_exists(process_to_get_ret))
	{
	  send_failure(client_socket, DETAIL_RET_UNKNOWN_PROCESS);
	  return MSG_ERR;
	}
      
      int ret = get_return_code(process_to_get_ret);
      if (ret == PROCESS_NOT_TERMINATED)
	{
	  send_failure(client_socket, DETAIL_RET_GET_RETURN_CODE_ERROR);
	  return MSG_ERR;
	}
      
      send_ok(client_socket, itoa(ret));
      return MSG_OK;
    }

  /*****************************************************************************  
   *                          CMD_LIST_PROCESS   
   ****************************************************************************/
  else if (!strcmp(CMD_LIST_PROCESS, token))
    {
      list_process(client_socket);
      send_ok(client_socket, NULL);
      return MSG_OK;
    }

  /*****************************************************************************  
   *                          CMD_GET_HELP
   ****************************************************************************/
  else if (!strcmp(CMD_GET_HELP, token))
    {
      send_basic(client_socket, help, strlen(help));
      return MSG_OK;
    }

  /*****************************************************************************  
   *                        COMMANDE INCONNUE
   ****************************************************************************/
  else
    {
      send_failure(client_socket, DETAIL_RET_UNKNOWN_COMMAND);
      return MSG_UNKNOWN_COMMAND;
    }
}
Ejemplo n.º 18
0
void Runtest(char *mode, int argc, char *argv[]) 
{
  FILE *fp,*fp0,*fp1,*fp2,*fp3;
  int Num_DatFiles,i,j,k,fp_OK;
  int Num_Atoms;
  int NGrid1_1,NGrid1_2,NGrid1_3;
  int NGrid2_1,NGrid2_2,NGrid2_3;
  double Utot1,Utot2,dU,dF;
  double Spread1,Spread2;
  double Omega1,Omega2;
  double gx,gy,gz,fx,fy,fz;
  double sum1,sum2;
  double time1,TotalTime;
  char fname[YOUSO10];
  char fname0[YOUSO10];
  char fname1[YOUSO10];
  char fname2[YOUSO10];
  char fname_dat[YOUSO10];
  char fname_dat2[YOUSO10];
  char fname_out1[YOUSO10];
  char fname_out2[YOUSO10];
  char ftmp[YOUSO10];
  fname_type *fndat;
  char operate[800];
  int numprocs,myid;

  char *dir;
  char *input_dir;
  char *output_file;
  DIR *dp;
  struct dirent *entry;

  MPI_Request request;
  MPI_Status  status;

  /* set up MPI */

  MPI_Comm_size(MPI_COMM_WORLD1, &numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD1, &myid);

  if (strcasecmp(mode,"S")==0){  
    input_dir = "input_example";
    output_file = "runtest.result";
  }
  else if (strcasecmp(mode,"L")==0){  
    input_dir = "large_example";
    output_file = "runtestL.result";
  }
  else if (strcasecmp(mode,"L2")==0){  
    input_dir = "large2_example";
    output_file = "runtestL2.result";
  }
  else if (strcasecmp(mode,"G")==0){  
    input_dir = "geoopt_example";
    output_file = "runtestG.result";
  }
  else if (strcasecmp(mode,"WF")==0){  
    input_dir = "wf_example";
    output_file = "runtestWF.result";
  }
  else if (strcasecmp(mode,"NEGF")==0){  
    input_dir = "negf_example";
    output_file = "runtestNEGF.result";
  }

  /* set Runtest_flag */

  Runtest_flag = 1;

  /* initialize TotalTime */

  TotalTime = 0.0;

  /* print the header */

  if (myid==Host_ID){

    printf("\n*******************************************************\n");  fflush(stdout);
    printf("*******************************************************\n");    fflush(stdout);
    printf(" Welcome to OpenMX   Ver. %s                           \n",Version_OpenMX); fflush(stdout);
    printf(" Copyright (C), 2002-2013, T.Ozaki                     \n");    fflush(stdout);
    printf(" OpenMX comes with ABSOLUTELY NO WARRANTY.             \n");    fflush(stdout);
    printf(" This is free software, and you are welcome to         \n");    fflush(stdout);
    printf(" redistribute it under the constitution of the GNU-GPL.\n");    fflush(stdout);
    printf("*******************************************************\n");    fflush(stdout);
    printf("*******************************************************\n\n\n");fflush(stdout);  

    printf("\n");
    printf(" OpenMX is now in the mode to check whether OpenMX runs normally\n"); fflush(stdout);
    printf(" on your machine or not by comparing the stored *.out and\n");        fflush(stdout);
    printf(" generated *.out \n"); fflush(stdout);
    printf("\n");fflush(stdout);

    /* set dir */

    dir = input_dir;

    /* count the number of dat files */

    if(( dp = opendir(dir) ) == NULL ){
      printf("could not find the directry '%s'\n",input_dir);
      MPI_Finalize();
      exit(0);
    }

    Num_DatFiles = 0;
    while((entry = readdir(dp)) != NULL){

      if ( strstr(entry->d_name,".dat")!=NULL ){ 
          
        Num_DatFiles++;
      }
    }
    closedir(dp);

    fndat = (fname_type*)malloc(sizeof(fname_type)*Num_DatFiles);

    /* store the name of dat files */

    if(( dp = opendir(dir) ) == NULL ){
      printf("could not find the directry '%s'\n",input_dir);
      MPI_Finalize();
      exit(0);
    }

    Num_DatFiles = 0;
    while((entry = readdir(dp)) != NULL){
 
      if ( strstr(entry->d_name,".dat")!=NULL ){ 

        sprintf(fndat[Num_DatFiles].fn,"%s/%s",input_dir,entry->d_name);  
        Num_DatFiles++;
      }
    }
    closedir(dp);

    /* sorting fndat */

    qsort(fndat, Num_DatFiles, sizeof(fname_type), stringcomp);  

    /*
    for (i=0; i<Num_DatFiles; i++){
      printf("i=%2d %s\n",i,fndat[i].fn);
    } 
    */

  } /* if (myid==Host_ID) */

  sprintf(fname2,"%s",output_file);

  if (myid==Host_ID){
    fp = fopen(fname2, "r");   
    if (fp!=NULL){
      fclose(fp); 
      sprintf(operate,"%s",fname2);
      remove(operate);
    }
  }

  if (myid==Host_ID){
    printf(" %2d dat files are found in the directory '%s'.\n\n\n",Num_DatFiles,input_dir);
  }

  MPI_Bcast(&Num_DatFiles, 1, MPI_INT, Host_ID, MPI_COMM_WORLD1);

  /***********************************************************
                      start calculations
  ***********************************************************/

  for (i=0; i<Num_DatFiles; i++){

    if (myid==Host_ID){
      sprintf(fname_dat,"%s",fndat[i].fn);
    }  

    MPI_Bcast(&fname_dat, YOUSO10, MPI_CHAR, Host_ID, MPI_COMM_WORLD1);

    /* run openmx */

    argv[1] = fname_dat;
    run_main(argc, argv, numprocs, myid); 

    /***********************************************************
          comparison between two files and save the result               
    ***********************************************************/

    if (myid==Host_ID){

      input_open(fname_dat);
      input_string("System.Name",fname_dat2,"default");
      input_close();

      /* compare two out files */

      sprintf(fname_out1,"%s.out",fname_dat2);
      sprintf(fname_out2,"%s/%s.out",input_dir,fname_dat2);

      /* generated file */

      input_open(fname_out1);
      input_double("Utot.",&Utot1,(double)0.0);

      /* for Wannier functions */

      if (strcasecmp(mode,"WF")==0){  
        input_double("Sum.of.Spreads.",&Spread1,(double)0.0);
        input_double("Total.Omega.=",&Omega1,(double)0.0);
      }

      input_int("Num.Grid1.",&NGrid1_1,(int)0);
      input_int("Num.Grid2.",&NGrid1_2,(int)0);
      input_int("Num.Grid3.",&NGrid1_3,(int)0);

      input_double("Elapsed.Time.",&time1,(double)0.0);

      TotalTime += time1;

      if (fp3=input_find("<coordinates.forces")) {
          
	fscanf(fp3,"%d",&Num_Atoms);

	sum1 = 0.0;
	for (j=1; j<=Num_Atoms; j++){  
	  fscanf(fp3,"%d %s %lf %lf %lf %lf %lf %lf",
		 &k,ftmp,&gx,&gy,&gz,&fx,&fy,&fz);
	  sum1 += fx + fy + fz;
	}

	if ( ! input_last("coordinates.forces>") ) {
	  printf("Format error for coordinates.forces\n");
	}
      }
      else {
	sum1 = 1000.0;
      }

      input_close();

      /* stored file */

      input_open(fname_out2);

      /* Utot */

      input_double("Utot.",&Utot2,(double)0.0);

      /* for Wannier functions */

      if (strcasecmp(mode,"WF")==0){  
        input_double("Sum.of.Spreads.",&Spread2,(double)0.0);
        input_double("Total.Omega.=",&Omega2,(double)0.0);
      }

      /* grids */

      input_int("Num.Grid1.",&NGrid2_1,(int)0);
      input_int("Num.Grid2.",&NGrid2_2,(int)0);
      input_int("Num.Grid3.",&NGrid2_3,(int)0);

      /* coordinates and forces */

      if (fp3=input_find("<coordinates.forces")) {
          
	fscanf(fp3,"%d",&Num_Atoms);

	sum2 = 0.0;
	for (j=1; j<=Num_Atoms; j++){  
	  fscanf(fp3,"%d %s %lf %lf %lf %lf %lf %lf",
		 &k,ftmp,&gx,&gy,&gz,&fx,&fy,&fz);
	  sum2 += fx + fy + fz;
	}

	if ( ! input_last("coordinates.forces>") ) {
	  /* format error */
	  printf("Format error for coordinates.forces\n");
	}
      }
      else {
	sum2 = 100.0;
      }

      input_close();

      dU = fabs(Utot1 - Utot2);
      dF = fabs(sum1 - sum2);

      /* write the result to a file, runtest.result */

      if ( (fp2 = fopen(fname2,"a")) != NULL ){

	if (  (NGrid1_1!=NGrid2_1)
	      || (NGrid1_2!=NGrid2_2)
	      || (NGrid1_3!=NGrid2_3) )
	  {
	    fprintf(fp2,"  Invalid comparison due to the different number of grids.\n");
	    fprintf(fp2,"  You may use a different radix for FFT.\n");
	  }

        if (strcasecmp(mode,"WF")==0){  

	  fprintf(fp2,"%4d  %-32.30s Elapsed time(s)=%8.2f  diff spread=%15.12f  diff Omega=%15.12f\n",
		  i+1,fname_dat,time1,fabs(Spread1-Spread2),fabs(Omega1-Omega2));
	}
        else{

	  fprintf(fp2,"%4d  %-32.30s Elapsed time(s)=%8.2f  diff Utot=%15.12f  diff Force=%15.12f\n",
		i+1,fname_dat,time1,dU,dF);
	}

	if (i==(Num_DatFiles-1)){
	  fprintf(fp2,"\n\nTotal elapsed time (s) %11.2f\n",TotalTime);
	}

	fclose(fp2);
      }
    }

  }

  /* tell us the end of calculation */
  if (myid==Host_ID){
    printf("\n\n\n\n");
    printf("The comparison can be found in a file '%s'.\n\n\n",output_file);
  }

  if (myid==Host_ID){
    free(fndat);
  }


  MPI_Barrier(MPI_COMM_WORLD1);
  MPI_Finalize();
  exit(0);
}
Ejemplo n.º 19
0
int main(int argc, char *argv[])
{
	pthread_t parser_thread;

	if (argc < 3) {
		fprintf(stderr, "Usage: %s <input file> <m2m device>\n", argv[0]);
		return 1;
	}

	if (input_open(argv[1]) < 0)
		return -1;
	
	if (m2m_open(argv[2]) < 0)
		return -1;

	if (subscribe_source_change() < 0)
		return -1;

	/* Setup flow based on ELCE2014 slides and Nicolas Dufresne's knowledge:
	 * S_FMT(OUT)
	 * S_FMT(CAP) to suggest a fourcc for the raw format; may be changed later
	 * G_CTRL(MIN_BUF_FOR_OUTPUT)
	 * REQBUFS(OUT)
	 * QBUF (the header)
	 * STREAMON(OUT)
	 * QBUF/DQBUF frames on OUT
	 * source change event, DQEVENT
	 * G_FMT(CAP)
	 * ENUM_FMT(CAP)
	 * S_FMT(CAP) to set fourcc chosen from ENUM_FMT; also get resolution from returned values?
	 * G_SELECTION to get visible size
	 * G_CTRL(MIN_BUF_FOR_CAPTURE)
	 * REQBUFS(CAP)
	 * STREAMON(CAP)
	 */


	if (output_set_format() < 0)
		return -1;

	// FIXME capture S_FMT

	// FIXME G_CTRL(MIN_BUF_FOR_OUTPUT)

	if (output_request_buffers() < 0)
		return -1;

	map_output();

	//if (parse_and_queue_header() < 0)
	//	return -1;

	parse_one_nal();
	stream(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, VIDIOC_STREAMON);

	parser_init();
	if (pthread_create(&parser_thread, NULL, parser_thread_func, NULL)) {
		fprintf(stderr, "Failed to launch parser thread\n");
		return -1;
	}

	if (wait_for_source_change() < 0)
		return -1;

	if (setup_capture() < 0)
		return -1;

	map_capture();
	queue_capture();
	stream(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, VIDIOC_STREAMON);
	capture();

	pthread_join(parser_thread, 0);
	return 0;
}
Ejemplo n.º 20
0
int main(int argc, char** argv) 
{
	char *record;
	char *block;									//endiamesos xwros mnhmhs ston opoio ekxwroun ta block oi reducers//
	int i,j,fd;
	char buffer[56];								//pinakas ston opoion sxhmatizoume to katallhlo format gia ta pipenames//
	pid_t pid;
	int status;
	fd_set read_set, active_set;							//ta set me tous file descriptors pou xrhsimopoiei h select//
	BUFFERS *buffers;								//deikths sthn arxh twn buffers//
	
	struct sigaction sig_act;

	if (argc!=3)									//onoma ektelesimou, #Mappers, #Reducers//
	{
		printf("Usage:%s  #Mappers  #Reducers\n",argv[0]);
		return -1;
	}
	
	sig_act.sa_handler=handler1;     		    				//Signal Handler1 gia ton patera//
	sig_act.sa_flags=0;
											//prosthetw ta simata sto set tou patera.
	if ((sigemptyset(&sig_act.sa_mask ) == -1) || (sigaction(SIGINT,&sig_act,NULL) == -1 ) 
	|| (sigaction(SIGQUIT,&sig_act,NULL) == -1 ) || (sigaction(SIGTERM,&sig_act,NULL) == -1 ))
	{
	      perror("FAIL TO INSTAL  SIGNAL HANDLER FOR SIGUSR1");
	      return -1;
	}
	Mappers=atoi(argv[1]);
	Reducers=atoi(argv[2]);
	for(i=0;i<Mappers;i++)								//dhmiourgia twn pipes prin dhmiourgh8oun ta paidia//
	{
		for (j=0;j<Reducers;j++)
		{
			buffer[0]='\0';
			sprintf(buffer,".pipe.%d.%d",i+1,j+1);				
			if ( mkfifo(buffer,0666 )==-1)	
       			{
                        	perror("mkfifo");
				removepipes(Mappers,Reducers);
                        	return -1;
                	}
		}
	}
	mkdir("./output_folder",0700);					//dhmiourgoume ton output folder//
	for(i=0;i<Mappers+Reducers;i++)							//kanena paidi den exei dhmiourgh8ei akoma//							
		pids[i]=0;
	for(i=0;i<Mappers+Reducers;i++)
	{
 		pids[i]=pid=fork();
		if (pid<0)								//periptwsh apotyxias fork()//
        	{									//epeidh den 3eroume posa paidia exoun gennh8ei epityxws,ara posa prepei na//
        		perror("fork failed\n");					//skotwsoume,o pateras stelnei ena SIGINT ston eauto tou kai o handler tou patera//
               		kill(getpid(),SIGINT);						//eidopoiei ta gennhmena paidia na termatisoun//
			removepipes(Mappers,Reducers);
                        return -1;
        	}
		else if ( pid==0 )							//to paidi vgainei apo to loop ka8ws mono h Initial prepei na kanei fork()//
			break;
	}
	if (pid==0)									//an eimaste sto paidi//
	{
		sigset_t ign_signals;
		
		//prosthetw sta 3 simata sto set twn paidiwn kai meta ta mplokarw//
		if ( (sigemptyset(&ign_signals ) == -1) || (sigaddset(&ign_signals,SIGINT) == -1 ) || (sigaddset(&ign_signals,SIGQUIT) == -1 ) 
				|| (sigaddset(&ign_signals,SIGTERM) == -1 ))
		{
	      		kill(getppid(),SIGINT);
		}
		else
			sigprocmask(SIG_BLOCK,&ign_signals,NULL);

 		
		sig_act.sa_handler=handler2;     		    			 //Signal Handler2-o handler twn paidiwn//
		sig_act.sa_flags=0;
		//orizw ton handler na antistoixei sto SIGUSR1, pou stelnei o parent//
		if ((sigemptyset(&sig_act.sa_mask ) == -1) || (sigaction(SIGUSR1,&sig_act,NULL) == -1 ))
	      		kill(getppid(),SIGINT);
		for(i=0;i<Mappers+Reducers;i++)						//anazhtw ston pinaka me ta pids to 10 mhdeniko,pou ypodeiknyei th seira gennhshs//
			if (pids[i]==0)
				break;
		i++;									//auti einai i thesi mou, gia na 3exwrizw an eimai maper i reducer//
		if (i<=Mappers)								//kwdikas tou mapper//
		{
			MRIFILE* fp;
			buffers=calloc(Reducers,sizeof(struct BUFFERS));		//desmeuoume mnhmh gia tosous buffers osoi einai kai oi reducers//	
			for (j=0;j<Reducers && !error ;j++)
			{
				buffer[0]='\0';
				sprintf(buffer,".pipe.%d.%d",i,j+1);			//dinoume onoma sta pipe//
				if ( (fd=open(buffer,O_WRONLY))<0 ) 			//anoigei to pipe gia grapsimo//
       		                {
					if ( errno != EINTR )				//an yparxei la8os k den einai apo shma//
        	                        	kill(getppid(),SIGINT);			//steile to shma //
	       	                	perror("Mapper::open");				//alliws ginetai allo la8os sthn open//
					free(buffers);					//kai apodesmevoume tous porous tou mapper//
					return -1;
        	                }
				initialize(buffers, j, fd);				//arxikopoioume tous buffers//
			}
			DIR             *dip;
			struct dirent   *dit;
			if ( (dip = opendir("input_folder")) == NULL)			//anoigoume ton inputfolder//
       			{
				if ( errno != EINTR )                           	//an yparxei la8os k den einai apo shma//
                                	kill(getppid(),SIGINT);                 	//steile to shma //
                                perror("Mapper::opendir");                         	//alliws ginetai allo la8os sthn open//
        		}
			while ((dit = readdir(dip)) != NULL  &&  !error)		//oso exoume files gia diavasma//
        		{
                		if ( strncmp(dit->d_name,"file_",5) == 0)		//an oi prwtoi 5 xarakthres twn 2 autwn string einai idioi,dhladh an einai file_//
				{
					int filenumber;					//ari8mos arxeiou input//
					
					sscanf(dit->d_name,"file_%d",&filenumber);	//kanw retrieve ton ari8mo tou trexodos arxeiou//
					if ( filenumber%Mappers+1==i)			//apofasizoume me katakermatismo poios mapper 8a diavazei apo ka8e file//
					{	
						char filename[50];
						sprintf(filename,"./input_folder/%s",dit->d_name);
						fp=input_open(filename);			//anoigoume to file//
						while ( (record=input_next(fp))!= NULL )	//kai oso uparxoun eggrafes gia diavasma//
						{						//kaleitai h addrecord gia pros8hkh eggrafwn stous buffers//
							addrecord(buffers,record,partition(map(record).key,Reducers));	
						}
						input_close(fp);			//kleinoume to file//
					}
				}
			}
			closedir(dip);							//kleinoume kai ton katalogo//
			for (j=0; j<Reducers && error==0; j++)				
			{
				if ( buffers[j].curbyte != 4 ){
					if ( my_write(buffers[j].pipedesc,buffers[j].block)!= BLKSIZE )	//grafoume sto pipe oses eggrafes exoun 3emeinei//
        	                       		printf("error here\n");
				}
			}
			for (j=0;j<Reducers;j++)
                       	{
				close(buffers[j].pipedesc);				//kleinoume ta pipe afou exoume diavasei//
			}
			free(buffers);							//afou teleiwnoume me to diavasma apodesmevoume taous buffers//
		}
		else									//kwdikas tou reducer//
		{
			char **keys;
			char **values;
			char *tempkey;							//deikths sthn prwth 8esh tou pinaka twn keys//
			char **tempval;							//deikths sthn prwth 8esh tou pinaka twn values//
			int counter;
			int total_records=0;
			int memsize=100;
			int whoami;							//8esh mias diergasias ston pinaka twn paidiwn//
			whoami=i;
			char buffer[40];
			MROFILE *fp;

			sprintf(buffer,"./output_folder/file_%d",whoami-Mappers);	//ka8orismos twn files sto output folder gia ton ka8e reducer
                        fp=output_open(buffer);

			if (  (keys=calloc(memsize,sizeof(char*))) == NULL		//an exoume provlhma me th desmeush tou pinaka gia ta keys,h' ta values// 
			|| (values=calloc(memsize,sizeof(char*)))==NULL 		//h' to block ,tote//
			|| (block=calloc(BLKSIZE,sizeof(char)))==NULL )
			{
			 	kill(getppid(),SIGINT);					//stelnoume shma ston patera//
			}
			FD_ZERO(&read_set);						//mhdenizoume to set twn file descriptors twn pipe//
			for (j=0;j<Mappers;j++)
			{
				buffer[0]='\0';
				sprintf(buffer,".pipe.%d.%d",j+1,i-Mappers);	
				if ( (fd=open(buffer,O_RDONLY))<0 ) 			//anoigoume to pipe gia diavasma//
       		                {
					if ( errno != EINTR )				//an yparxei la8os kai den einai apo shma//
						kill(getppid(),SIGINT);			//tote stelnoume to shma//
        	                	perror("Reducer::open");
					break;
        	                }
				FD_SET(fd,&read_set);					//pros8etoume ton fd sto readset//
			}
			active_set=read_set;
			int closed=0;							//arxikopoioume to plh8os twn diavasmenwn fds//
			while ( error==0 && closed != Mappers)				//oso uparxoun fds sto active set kai den lavei shma//
			{
				read_set=active_set;
				int chosen;
				if ( (chosen=select(FD_SETSIZE,&read_set,NULL,NULL,NULL)) < 0) 		
				{
					if ( errno != EINTR ){
                                                kill(getppid(),SIGINT);
						error=1;
					}
					perror("select");				//alliws yparxei provlhma sth select//
					break;
				}
				else							//h select epistrefei ton katallhlo fd//
				{
					for (i = 0; i < FD_SETSIZE; i++)		//gia olous tous fds pou yparxoun mesa sto set//
					{
             					if (FD_ISSET (i, &read_set))		//an o i fd einai melos tou read set otan epistrepsei h select//
						{
							if ( (chosen=my_read(i,block))<0 )		//diavase apo ton i fd//
							{
								if ( errno != EINTR )
								{
                                         			       	kill(getppid(),SIGINT);
                                                			error=1;
                                        			}
								perror("read");
								break;
							}
							else if ( chosen==0 )			
							{
								FD_CLR (i, &active_set); 	//afairei ton i fd apo to active set//
								closed++;			//au3anetai kata ena o ari8mos twn fds pou diavasthkan//
								close(i);			//kleinei o i fd//
								if ( closed == Mappers )
									break;
							}
							else
								retrieve_records(block,&keys,&values,&total_records,&memsize);
						}
					}
				}
			}
			printf("%s\n",keys[10000]);
			printf("%d\n",total_records);
			if ( error==0 )								//an den exoume lavei shma//
			{
				sort(keys, values, total_records,compare);						
        	                tempkey=keys[0];						//o deikths tempkey deixnei sthn 1h 8esh tou pinaka twn keys//
                	        tempval=values;
                        	counter=1;
                	        for ( i=1; i<total_records; i++)
                        	{
                               		if ( compare(tempkey,keys[i]) == 0 )			//an ta kleidia se dyo diadoxikes 8eseis tou pinaka einai idia//
                                	        counter++;					//au3anetai kata 1 to plh8os twn eggrafwn me to idio kleidi//	
                       		        else							//an ta kleidia se dyo diadoxikes 8eseis tou pinaka einai diaforetika//
                                	{
						output_next(fp,reduce(tempkey,tempval,counter));//kaleitai h reduce kai grafoume to apotelesma sto output file.//
                         	                counter=1;					//epilegoume neo kleidi//
                                	        tempkey=keys[i];				//h kainouria 8esh tou tempkey//
                                        	tempval=values+i;				//h kainouria 8esh tou tempval//
                                	}
        	                }
			}
			output_close(fp);							//kleinoume to output file tou kathe reducer//
			if ( block != NULL )							//apodesmeush twn porwn tou reducer//
				free(block);
			if ( keys != NULL && values!= NULL )					//an den exei ginei la8os kata th desmeush//
			{									//apodesmeuoume olh th dunamika desmeumenh mnhmh//
				for(i=0;i<memsize;i++)						
				{
					if ( keys[i]!=NULL && values[i]!=NULL )
					{
						free(keys[i]);
						free(values[i]);
					}
				}
				free(keys);
				free(values);
			}
			for (i = 0; i < FD_SETSIZE; i++)					//kleinoume olous tous file descriptors pou vriskodai sto active set//
                              if (FD_ISSET (i, &active_set))   
					close(i);
		}	
	}
	else											//alliws an eisai ston patera,Initial process//
	{
		while ( (pid=r_wait(NULL))>0 )							//perimenei ta paidia tou na teleiwsoun// 
		{	
			fprintf(stderr,"Child %d finished\n",pid);
		}	
		removepipes(Mappers,Reducers);							//svhsimo twn pipes//
	}
	return 0;
	
}