void server_run() {
    int filedes,filed,size,numbofargs,i,jsize=0,jobID=0,digit_count = 0,num,status,N,id;
    pid_t cpid,commpid,stopid;
    char *buffer;
    char *temp;
    char *command;
    char *jobname;
    char *str;
    char **args;

    static struct sigaction act;
    act.sa_handler = handler;
    sigfillset(&(act.sa_mask));
    sigaction(SIGRTMIN, &act, NULL);

    static struct sigaction chd;
    chd.sa_handler = child_handler;
    sigfillset(&(chd.sa_mask));
    sigaction(SIGCHLD, &chd, NULL);

    if (( filedes = open("server", O_CREAT | O_RDWR , 0666))== -1) {
        perror (" creating ");
        exit (3) ;
    }
    cpid = getpid();

    num = cpid;
    digit_count += no_of_digits(num);   
    str = malloc(sizeof(char)*(digit_count+1));//to str einai ena string pou exei mesa to pid tou server
    str[digit_count] = '\0';
    while(num > 0){
        str[digit_count-1] = num%10 + '0';
        num = num/10;
        digit_count--;
    }

    if ((write(filedes,str,sizeof(strlen(str)+1))) == -1) {
        perror ("Error in Writing ");
        exit (4) ; 
    }
    free(str);
    if ((filed = open("myfifo",O_RDWR)) <0 ){
        perror (" fifo open problem ");
        exit(8);
    }
    list_create();
    kill(getppid(),SIGRTMIN);
    while (1) {
        flag = 0;
        while (flag == 0)
            sleep(1);
        jsize = 0;
        read(filed,&commpid,sizeof(pid_t));

        read(filed,&numbofargs,sizeof(int));
        args = (char**)malloc(numbofargs*sizeof(char*));

        read(filed,&size,sizeof(int));
        buffer = (char *)malloc(size);

        read(filed,buffer,size);
        temp = (char *)malloc(size);
        strcpy(temp,buffer);
        command = strtok(temp," ");//pairnw thn entolh(issuejob,poll klp)
        for (i=0;i<numbofargs;i++) {
            args[i] = strtok(NULL," ");
            jsize += strlen(args[i])+1;//kai ta argument auths
        }
        jsize += 1;
        jobname = (char *)malloc(jsize);
        jobname[0] = '\0';
        for (i=0;i<numbofargs;i++) {
            strcat(jobname,args[i]);
            strcat(jobname," ");
        }        
        if (strcmp(command,"issuejob") == 0) {
            if (args[0] == NULL) {//se periptwsh pou den dwthei argument meta to issuejob
                free(args);
                free(temp);
                free(buffer);
                free(jobname);
                continue;
            }
            jobID++;
            issuejob(args,numbofargs,jobname,jobID,&status);
            
            write(filed,&jobID,sizeof(int));
            write(filed,&status,sizeof(int));
            write(filed,&jsize,sizeof(int));
            write(filed,jobname,jsize);
            kill(commpid,SIGRTMIN);
        }
        else if (strcmp(command,"setConcurrency") == 0) {
            if (args[0] == NULL) {
                free(args);
                free(temp);
                free(buffer);
                free(jobname);
                continue;
            }
            N = atoi(args[0]);
            setConcurrency(N);
        }
        else if (strcmp(command,"stop") == 0) {
            if (args[0] == NULL) {
                free(args);
                free(temp);
                free(buffer);
                free(jobname);
                continue;
            }
            id = atoi(args[0]);
            search_and_delete(id,&stopid);
            if (stopid != 0)
                kill(stopid,SIGKILL);
        }
        else if (strcmp(command,"poll") == 0) {
            if (args[0] == NULL) {
                free(args);
                free(temp);
                free(buffer);
                free(jobname);
                continue;
            }
            if (strcmp(args[0],"running") == 0)
                poll(1,filed);  
            else if (strcmp(args[0],"queued") == 0)
                poll(0,filed); 
            else {
                free(args);
                free(temp);
                free(buffer);
                free(jobname);
                continue;
            }             
            kill(commpid,SIGRTMIN);
        }
        else if (strcmp(command,"exit") == 0) {
            queued_delete_all();//sbinw olh th lista anamonhs
            free(args);
            free(temp);
            free(buffer);
            free(jobname);
            while (get_running_jobs() != 0)//kai prin kleisei o server perimenw na teleiwsoun oles oi running jobs
                sleep(1);
            break;
        }
        free(args);
        free(temp);
        free(buffer);
        free(jobname);
        
    }
    list_destroy();
    remove("server");
    remove("myfifo");
}
示例#2
0
loci_stats *bam_access_get_position_base_counts(char *chr, int posn){
	char *region = NULL;
	hts_itr_t *iter = NULL;
	bam1_t* b = NULL;
	bam_plp_t buf;
	loci_stats *stats = malloc(sizeof(loci_stats *));
	check_mem(stats);
	stats->base_counts = malloc(sizeof(int) * 4);
	check_mem(stats->base_counts);
	stats->base_counts[0] = 0;
	stats->base_counts[1] = 0;
	stats->base_counts[2] = 0;
	stats->base_counts[3] = 0;
	fholder->stats = stats;

	region = malloc((sizeof(char *) * (strlen(chr)+1))+sizeof(":")+sizeof("-")+(sizeof(char)*((no_of_digits(posn)*2)+1)));
	sprintf(region,"%s:%d-%d",chr,posn,posn);
	fholder->beg = posn;
	fholder->end = posn;

  // initialize pileup
	buf = bam_plp_init(pileup_func, (void *)fholder);
	bam_plp_set_maxcnt(buf,maxitercnt);

  /*
  sam_fetch(fholder->in, fholder->idx, ref, fholder->beg, fholder->end, buf, fetch_algo_func);
  */
  //Replace fetch with iterator for htslib compatibility.
  b = bam_init1();
  iter = sam_itr_querys(fholder->idx, fholder->head, region);
  int result;
  int count = 0;
  while ((result = sam_itr_next(fholder->in, iter, b)) >= 0) {
    if(b->core.qual < min_map_qual || (b->core.flag & BAM_FUNMAP)
			|| !(b->core.flag & BAM_FPROPER_PAIR) || (b->core.flag & BAM_FMUNMAP)//Proper pair and mate unmapped
			|| (b->core.flag & BAM_FDUP)//1024 is PCR/optical duplicate
			|| (b->core.flag & BAM_FSECONDARY) || (b->core.flag & BAM_FQCFAIL)//Secondary alignment, quality fail
			|| (b->core.flag & BAM_FSUPPLEMENTARY) ) continue;
    count++;
    bam_plp_push(buf, b);
  }
  sam_itr_destroy(iter);
  bam_plp_push(buf, 0);
  int tid, pos, n_plp = -1;
  const bam_pileup1_t *pil;
  while ( (pil=bam_plp_next(buf, &tid, &pos, &n_plp)) > 0) {
    if((pos+1) != posn) continue;
    int i=0;
   	for(i=0;i<n_plp;i++){
      const bam_pileup1_t *p = pil + i;
      int qual = bam_get_qual(p->b)[p->qpos];
      uint8_t c = bam_seqi(bam_get_seq(p->b), p->qpos);
      if(!(p->is_del) &&  qual >= min_base_qual
            &&  p->b->core.qual >= min_map_qual){
           //&& (c == 1 /*A*/|| c == 2 /*C*/|| c == 4 /*G*/|| c == 8 /*T*/)){
        //Now we add a new read pos struct to the list since the read is valid.
        //char cbase = toupper(bam_nt16_rev_table[c]);
        switch(c){
          case 1:
            fholder->stats->base_counts[0]++;
            break;

          case 2:
            fholder->stats->base_counts[1]++;
            break;

          case 4:
            fholder->stats->base_counts[2]++;
            break;

          case 8:
            fholder->stats->base_counts[3]++;
            break;

          default:
            break;

          }; // End of args switch statement */
   	    }
   	  }
  } //End of iteration through pileup
	//bam_plp_push(buf, 0); // finalize pileup
  bam_plp_destroy(buf);
	free(region);
	return fholder->stats;

error:
	//if(region) free(region);
	if(fholder->stats){
		if(fholder->stats->base_counts) free(fholder->stats->base_counts);
		free(fholder->stats);
	}
	if(iter) sam_itr_destroy(iter);
	if(b) bam_destroy1(b);
	if(region) free(region);
	return NULL;
}