Ejemplo n.º 1
0
void *thread_work(void *ptr){
 //printf("entered worker thread \n");
 mapreduce_t* mr = (mapreduce_t*)ptr;
  int counter = num_process; 
  int i;
   char s[size_buf][BUFFER_SIZE + 1];
    for(i=0;i<size_buf;i++)
          s[i][0]='\0';
    while (counter) {
       
            
         struct epoll_event ev;
           epoll_wait(epoll_fd,&ev,1,-1);
         //  printf("read data from pipe%d\n",ev.data.fd);
         
	int ret=read_from_fd(ev.data.fd, s[ev.data.fd-1], mr);
          
          if(ret==0){    --counter;
           // printf("read failed counter =%d\n",counter+1);
              //if finished read one pipe
                    epoll_ctl(epoll_fd,EPOLL_CTL_DEL,ev.data.fd,NULL);
        }
    }
    
    /* close unused pipes */
    /* add your code here */
     /*for(i=0;i<num_process;i++)
              //close all read side of pipe
               close(fd[i][0]); 
*/
//printf("exit");

return NULL;
}
Ejemplo n.º 2
0
static int
calc_chunk_metrics( f_c * ebuf, f_c * rbuf, f_c * tbuf ) {
  char buf[CHUNKBYTES];
  double *chunk_window;
  int chunkcount = 0;
  f_c *in, *out;
  f_p p;
  in = fftw_malloc( sizeof( f_c ) * CHUNKSAMPS );
  assert( in != NULL );
  out = fftw_malloc( sizeof( f_c ) * CHUNKSAMPS );
  assert( out != NULL );
  p = fftw_plan_dft_1d( CHUNKSAMPS, in, out, FFTW_FORWARD, FFTW_ESTIMATE );
  chunk_window = setup_window( CHUNKSAMPS );
  while ( CHUNKBYTES == read_from_fd( 0, buf, CHUNKBYTES ) ) {
    /* Forget about the last fraction of a second of audio data. */
    /* Process exactly one second of audio data per iteration. */
    double be[NUM_BANDS];       /* band energies */
    audio_to_fftw( buf, in );
    window( in, chunk_window, CHUNKSAMPS );
    fftw_execute( p );          /* post: in[] -> FFT -> out[] is done */
    calc_band_energies( out, be );      /* post: be[] is valid */
    chunk_metrics( be, ebuf, rbuf, tbuf, chunkcount );
    chunkcount++;
    /* post: ebuf[], rbuf[], and tbuf[] have chunkcount valid elements */
  }                             /* post: we got to the end of the input data */
  fftw_free( in );
  fftw_free( out );
  fftw_destroy_plan( p );
  free( chunk_window );
  return chunkcount;
}
Ejemplo n.º 3
0
void * worker(void * mr0){
	char ** buffers= malloc(sizeof(char *) * numValues);
	mapreduce_t * mr= (mapreduce_t *)mr0;
	int i= 0;
	for( ; i < numValues; i++){
		buffers[i]= malloc(BUFFER_SIZE+1);
		buffers[i][0]= '\0';
	}
	int n= numValues;
	while(n){
		i=0;
		struct epoll_event ev;
		epoll_wait(mr->epoll_fd, &ev, 1, -1);
		int index= getIndex(mr, ev.data.fd);
		if(index == -1)
			printf("error at getting index\n");
		int bytes= read_from_fd(ev.data.fd, buffers[index], mr);
        
		if(bytes == 0){
			n--;
			epoll_ctl(mr->epoll_fd,EPOLL_CTL_DEL,ev.data.fd,NULL);
		}
		else if(bytes == -1){
			printf("error\n");
		}
	}
	for(i=0; i < numValues; i++)
		free(buffers[i]);
	free(buffers);
	return NULL;
}
Ejemplo n.º 4
0
static void
input_from_file(const char *fname, size_t offset, size_t fp_len, int action,
                int options) {
  size_t len, buf_len;
  int fd = open(fname, O_RDONLY, 0);

  if (fd == -1) DIE("cannot open '%s'", fname);

  if (action == BMZ_A_LIST) {
    do_list(fd);
  }
  else {
    void *data = read_from_fd(fd, &len, &buf_len);
    do_block(data, len, buf_len, offset, fp_len, action, options);
  }
  /* close and free etc. are omitted intentionally */
}
Ejemplo n.º 5
0
void * worker_function (void * in){
	mapreduce_t * mr = (mapreduce_t *) in; 
	fd_set master_fdset, active_fdset; 

    FD_ZERO(&master_fdset);
    FD_ZERO(&active_fdset);

    int i, j; 
    int alive_num = mr -> size; 

    char ** buffer_arr = malloc(sizeof(char *) * mr->size);
    mr -> buffers = buffer_arr;

    for (i = 0; i < (mr-> size); i++){
    	FD_SET(mr->pipes[i][0], &master_fdset);
    	buffer_arr[i] = malloc(BUFFER_SIZE + 1);
    	buffer_arr[i][0] = '\0';
    }

    while(alive_num> 0){
        active_fdset = master_fdset; 
        select(FD_SETSIZE, &active_fdset, NULL, NULL, NULL);
        for (j = 0; j < mr -> size; j++){
        	if (FD_ISSET(mr->pipes[j][0], &active_fdset)){
        		if (!read_from_fd(mr->pipes[j][0], buffer_arr[j], mr)){
        			close(mr->pipes[j][0]);
        			FD_CLR(mr->pipes[j][0], &master_fdset);
                	alive_num --;  
        		}
        	}
        }
    }

    
    //for (i = 0; i < mr -> size; i++){
    //	free(buffer_arr[i]);
    //	free(mr -> pipes[i]);
    //}

    //free(buffer_arr);
    //free(mr -> pipes);
	
    mr -> done = 1; 

    return 0; 
}
Ejemplo n.º 6
0
void define_test(void(*callback)(ZRuntimeContext *), struct zco_context_t *ctx, const char *title)
{
	trace("======================\n");
	trace("%s:\n", title);

        /* redirect stdout to a pipe */
        int the_pipe[2];
        pipe(the_pipe);
        int original_stdout = write_stdout_to_pipe(the_pipe[1]);

        /* run test in native mode */
        ZRuntimeContext *runtime_context = z_runtime_context_new(ctx, NULL);
        z_runtime_context_set_target(runtime_context, 0);
        callback(runtime_context);

        /* restore stdout as we don't need any more data that is sent to it */
        restore_stdout(original_stdout);

        /* read data that is sent to the pipe and close the pipe */
        ZString *out = read_from_fd(ctx, NULL, the_pipe[0]);
        close(the_pipe[0]);
        z_object_unref(Z_OBJECT(runtime_context));

        /* run test in JS mode and verify its output */
        runtime_context = z_runtime_context_new(ctx, NULL);
        z_runtime_context_set_target(runtime_context, 1);
        callback(runtime_context);
        verify_output(runtime_context, out);
        z_object_unref(Z_OBJECT(runtime_context));

        /* print out the expected string */
        char *print_out = z_string_get_cstring(out, Z_STRING_ENCODING_UTF8);
        fputs(print_out, stdout);
        free(print_out);

        z_object_unref(Z_OBJECT(out));
        trace("\n");
}
Ejemplo n.º 7
0
void verify_output(ZRuntimeContext *runtime_context, ZString *expected_value)
{
        pid_t pid;

        /* create a full-duplex pipe */
        int pipe_to_child[2];
        int pipe_to_parent[2];

        pipe(pipe_to_child);
        pipe(pipe_to_parent);

        switch (pid = fork())
        {
        case -1:
                perror("fork");
                abort();
                break;

        case 0:  /* child */
                close(pipe_to_child[1]);
                close(pipe_to_parent[0]);
                dup2(pipe_to_child[0], 0);
                dup2(pipe_to_parent[1], 1);
                execlp("node", "node", NULL);
                break;
        }

        close(pipe_to_child[0]);
        close(pipe_to_parent[1]);

        if (is_verbose) {
                /* print out the script if we are in verbose mode */
                z_runtime_context_run(runtime_context);
        }

        /* backup the original stdout and redirect stdout to 'pipe_to_child' */
        int original_stdout = write_stdout_to_pipe(pipe_to_child[1]);

        /* push the script to stdout, which is the pipe to the child */
        z_runtime_context_run(runtime_context);
        z_runtime_context_clear(runtime_context);

        /* restore the original stdout */
        restore_stdout(original_stdout);

        /* wait for child to exit */
        siginfo_t info;
        memset (&info, 0, sizeof(info));
        waitid (P_PID, pid, &info, WEXITED | WUNTRACED | WNOHANG | WNOWAIT);

        /* read all data from pipe_to_parent[0] and store it in a string */
        ZString *out = read_from_fd(CTX_FROM_OBJECT(runtime_context), ALLOCATOR_FROM_OBJECT(runtime_context), pipe_to_parent[0]);
        close(pipe_to_parent[0]);

        /* compare the output matches the expected value */
        if (z_string_compare(out, NULL, expected_value, NULL, 0, -1)) {
                char *s;

                s = z_string_get_cstring(expected_value, Z_STRING_ENCODING_UTF8);
                printf("Expected value: \n'%s'\n", s);
                free(s);

                s = z_string_get_cstring(out, Z_STRING_ENCODING_UTF8);
                printf("\nActual value: \n'%s'\n", s);
                free(s);

                abort();
        }

        z_object_unref(Z_OBJECT(out));
}
Ejemplo n.º 8
0
int trace_main(char *hostIp, int portNum, char *imsi, int outflag, char *path)
{
	int				fd_stdin, peersock, maxfd, status, readsize;
	int				selStatus;
	fd_set			fdset_Read;
	FILE			*fp = NULL;
	char			buff[128];
	struct timeval	timeout;

	fd_stdin = fileno(stdin);

	peersock = connect_to_peer(hostIp, portNum);
	if(peersock < 0)
		return 1;

	if(outflag){
		fp = open_trace_file(path, imsi);
		if(!fp)
			exit(1);
	}

	timeout.tv_sec = 0;
	timeout.tv_usec = 100;

	while(1){
		FD_ZERO(&fdset_Read);
		FD_SET(fd_stdin, &fdset_Read);
		FD_SET(peersock, &fdset_Read);
		maxfd = (fd_stdin >= peersock)?fd_stdin:peersock;
		maxfd += 1;

		selStatus = select(maxfd, &fdset_Read, NULL, NULL, &timeout);
		if(selStatus < 0){
			fprintf(stderr, "[ERROR] select - %s\n", strerror(errno));
		}else if(selStatus > 0){
			if(FD_ISSET(fd_stdin, &fdset_Read)){
				memset(buff, 0x00, sizeof(buff));
				read_from_fd(fd_stdin, buff, 1);
				if(buff[0] == 'q' || buff[0] == 'Q'){
					close(peersock);
					fclose(fp);
					return 1;
				}
			}
			if(FD_ISSET(peersock, &fdset_Read)){
				status = read_trace(peersock, fp, imsi);
				if(status < 0){
					// try to reconnect to peer.
					peersock = connect_to_peer(hostIp, portNum);
					if(peersock < 0)
						return 1;
				}
			}
		}
#ifdef DEBUG_1
		else{
			printf(".");
		}
#endif
	}

	return 0;

}
Ejemplo n.º 9
0
int read_trace(int sock, FILE *fp, char *imsi)
{
	char	buff[1024], *start = NULL, tmpB[128];
	int		readsize, traceFlag, sizeToRead, matchFlag;
	int		readBytes, restBytes;

	readsize = sizeof(SockLibHeadType);
	if(read_from_fd(sock, buff, readsize) < 0)
		return -1;

	parse_sock_header((SockLibHeadType *)buff, &sizeToRead, &traceFlag);

	if(readsize == 0)
		return 0;

	if(!traceFlag){
		while(read_from_fd(sock, buff, sizeof(buff)) > 0);
		return 0;
	}

	readsize = (sizeToRead > (sizeof(buff)-1))?(sizeof(buff)-1):sizeToRead;

	readBytes = 0;
	memset(buff, 0x00, sizeof(buff));
	if((readBytes = read_from_fd(sock, buff, readsize)) < 0)
		return -1;

	if(!readBytes)
		return 0;


	if(if_imsi_match(buff, imsi, &start)){
		restBytes = sizeToRead - readBytes;
		sprintf(tmpB, "\n\n[ ************************ TRACE RECEIVERD TIME : "
					  "%s ************************ ]\n", current_time());
		if(fp){
			if(fputs(tmpB, fp) == EOF){
				fprintf(stderr, "[ERROR] fputs error - %s\n", strerror(errno));
				fclose(fp);
				exit(0);
			}
			if(!fwrite(start, 1, strlen(start), fp)){
				fprintf(stderr, "[ERROR] fwrite error - %s\n", strerror(errno));
				fclose(fp);
				exit(0);
			}
		}
		
		// print trace out to STDOUT
		puts(tmpB);
		fwrite(start, 1, strlen(start), stdout);

		while(restBytes > 0){
			readsize = (restBytes > sizeof(buff))?sizeof(buff):restBytes;
			if((readBytes = read_from_fd(sock, buff, readsize)) < 0)
			        return -1;

			if(fp){
				if(!fwrite(buff, 1, readBytes, fp)){
					fprintf(stderr, "[ERROR] fwrite error - %s\n", strerror(errno));
					fclose(fp);
					exit(0);
				}
			}
			fwrite(buff, 1, readBytes, stdout);

			restBytes -= readBytes;
		}

		puts("\n\n");
		if(fp){
			fputs("\n", fp);
			fflush(fp);
		}
		fflush(stdout);
		
	}else{
		while(read_from_fd(sock, buff, sizeof(buff)) > 0);
#ifdef DEBUG_1
		puts(buff);
#endif
		return 0;
	}

	return 1;

}