void topological_sort(int **graph, int *indegree, int nodes) { int i, j, cnode; int *ts; ts = malloc(nodes * sizeof(int)); for (i = 0; i < nodes; i++) { if (indegree[i] == 0) { insert_queue(i); indegree[i] = -1; } } j = 0; while ((cnode = remove_queue()) != -1) { for (i = 0; i < nodes; i++) { indegree[i] -= graph[cnode][i]; if (indegree[i] == 0) { insert_queue(i); indegree[i] = -1; } } ts[j++] = cnode; printf("%d\n", cnode); } }
void BFS(int v) { int i; insert_queue(v); state[v] = waiting; while(!isEmpty_queue()) { v = delete_queue(); printf("Vertex %d visited\n",v); state[v] = visited; for(i=0; i<n; i++) { /* Check for adjacent unvisited vertices */ if(adj[v][i] == 1 && state[i] == initial) { insert_queue(i); state[i] = waiting; printf("-----Tree edge - (%d,%d)\n", v, i); } } } printf("\n"); }/*End of BFS()*/
static void retry_erase(erase_busy_t *busy, u_int cause) { eraseq_entry_t *erase = busy->erase; mtd_request_t req; client_t *mtd; socket_info_t *s; int ret; DEBUG(2, "cs: trying erase request 0x%p...\n", busy); if (busy->next) remove_queue(busy); req.Function = MTD_REQ_ERASE | cause; req.TransferLength = erase->Size; req.DestCardOffset = erase->Offset + erase->Handle->info.CardOffset; req.MediaID = erase->Handle->MediaID; mtd = erase->Handle->mtd; s = SOCKET(mtd); mtd->event_callback_args.mtdrequest = &req; wacquire(&mtd->mtd_req); ret = EVENT(mtd, CS_EVENT_MTD_REQUEST, CS_EVENT_PRI_LOW); wrelease(&mtd->mtd_req); if (ret == CS_BUSY) { DEBUG(2, " Status = %d, requeueing.\n", req.Status); switch (req.Status) { case MTD_WAITREQ: case MTD_WAITPOWER: insert_queue(&mtd->erase_busy, busy); break; case MTD_WAITTIMER: case MTD_WAITRDY: if (req.Status == MTD_WAITRDY) insert_queue(&s->erase_busy, busy); mod_timer(&busy->timeout, jiffies + req.Timeout*HZ/1000); break; } } else { /* update erase queue status */ DEBUG(2, " Ret = %d\n", ret); switch (ret) { case CS_SUCCESS: erase->State = ERASE_PASSED; break; case CS_WRITE_PROTECTED: erase->State = ERASE_MEDIA_WRPROT; break; case CS_BAD_OFFSET: erase->State = ERASE_BAD_OFFSET; break; case CS_BAD_SIZE: erase->State = ERASE_BAD_SIZE; break; case CS_NO_CARD: erase->State = ERASE_BAD_SOCKET; break; default: erase->State = ERASE_FAILED; break; } busy->client->event_callback_args.info = erase; EVENT(busy->client, CS_EVENT_ERASE_COMPLETE, CS_EVENT_PRI_LOW); kfree(busy); /* Resubmit anything waiting for a request to finish */ wakeup(&mtd->mtd_req); retry_erase_list(&mtd->erase_busy, 0); } } /* retry_erase */
int main(int argc, char** argv) { Car car; Queue check_car; int i; init_queue(&check_car); for(i=0;i<7;i++) { printf("Enter a car:\n"); printf("%17s", "mark: "); scanf("%s", car.mark); printf("%17s"," number: "); scanf("%s", car.number); //snprintf(car.number,9,"%d",i+1); printf("%17s", "production year: "); scanf("%hu", &car.year); if(insert_queue(&check_car, car) == QUEUE_FULL) { fprintf(stderr, "Queue overflow\n"); exit(1); } printf("\n"); } printf("\n"); while(remove_queue(&check_car, &car) != QUEUE_EMPTY) printf("%10s %7s %4hu\n", car.mark, car.number, car.year); queue_free(&check_car); return 0; }
int insert_into_queue_recycler( pbs_queue *pq) { int rc; pthread_mutex_lock(q_recycler.mutex); memset(pq->qu_qs.qu_name, 0, sizeof(pq->qu_qs.qu_name)); sprintf(pq->qu_qs.qu_name,"%d",q_recycler.next_id); pq->q_being_recycled = TRUE; q_recycler.queues.lock(); if (q_recycler.queues.count() >= MAX_RECYCLE_QUEUES) { enqueue_threadpool_request(remove_some_recycle_queues, NULL, task_pool); } q_recycler.queues.unlock(); rc = insert_queue(&q_recycler.queues,pq); update_queue_recycler_next_id(); pthread_mutex_unlock(q_recycler.mutex); return(rc); } /* END insert_into_queue_recycler() */
int netaware_delay_meta_fwrite() { meta = malloc(sizeof(struct meta_struct)); meta->offset = handle->offset; meta->length = handle->length; insert_queue(handle->meta_queue, meta); return 0; }
pbs_queue *que_alloc( char *name, int sv_qs_mutex_held) { static char *mem_err = "no memory"; int i; pbs_queue *pq; pq = (pbs_queue *)calloc(1, sizeof(pbs_queue)); if (pq == NULL) { log_err(errno, __func__, mem_err); return(NULL); } pq->qu_qs.qu_type = QTYPE_Unset; pq->qu_mutex = calloc(1, sizeof(pthread_mutex_t)); pq->qu_jobs = calloc(1, sizeof(struct all_jobs)); pq->qu_jobs_array_sum = calloc(1, sizeof(struct all_jobs)); if ((pq->qu_mutex == NULL) || (pq->qu_jobs == NULL) || (pq->qu_jobs_array_sum == NULL)) { log_err(ENOMEM, __func__, mem_err); return(NULL); } initialize_all_jobs_array(pq->qu_jobs); initialize_all_jobs_array(pq->qu_jobs_array_sum); pthread_mutex_init(pq->qu_mutex,NULL); lock_queue(pq, "que_alloc", NULL, LOGLEVEL); strncpy(pq->qu_qs.qu_name, name, PBS_MAXQUEUENAME); insert_queue(&svr_queues,pq); if (sv_qs_mutex_held == FALSE) lock_sv_qs_mutex(server.sv_qs_mutex, __func__); server.sv_qs.sv_numque++; if (sv_qs_mutex_held == FALSE) unlock_sv_qs_mutex(server.sv_qs_mutex, __func__); /* set the working attributes to "unspecified" */ for (i = 0; i < QA_ATR_LAST; i++) { clear_attr(&pq->qu_attr[i], &que_attr_def[i]); } return(pq); } /* END que_alloc() */
void bfs(int index,int *dist,struct vertex vert[SIZE]) { struct q *queue=NULL; struct edge *link; vert[index].visit=T; queue=insert_queue(index,queue); while(queue) { queue=delet_queue(&index,queue); for(link=vert[index].edge_ptr;link;link=link->next) if(vert[link->terminal].visit==F) { vert[link->terminal].visit=T; vert[link->terminal].path_lenth=vert[index].path_lenth+1; queue=insert_queue(link->terminal,queue); } } }
main() { int i, j = 0, k; int topsort[ MAX ], indeg[ MAX ]; create_graph(); printf( "The adjacency matrix is :\n" ); display(); /*Find the indegree of each node*/ for ( i = 1;i <= n;i++ ) { indeg[ i ] = indegree( i ); if ( indeg[ i ] == 0 ) insert_queue( i ); } while ( front <= rear ) /*Loop till queue is not empty */ { k = delete_queue(); topsort[ j++ ] = k; /*Add node k to topsort array*/ /*Delete all edges going fron node k */ for ( i = 1;i <= n;i++ ) { if ( adj[ k ][ i ] == 1 ) { adj[ k ][ i ] = 0; indeg[ i ] = indeg[ i ] – 1; if ( indeg[ i ] == 0 ) insert_queue( i ); } } /*End of for*/ } /*End of while*/ printf( "Nodes after topological sorting are :\n" ); for ( i = 0;i < j;i++ ) printf( "%d ", topsort[ i ] ); printf( "\n" ); } /*End of main()*/
main() { int i,v,count,topo_order[MAX],indeg[MAX]; create_graph(); /*Find the indegree of each vertex*/ for(i=0;i<n;i++) { indeg[i] = indegree(i); if( indeg[i] == 0 ) insert_queue(i); } count = 0; while( !isEmpty_queue( ) && count < n ) { v = delete_queue(); topo_order[++count] = v; /*Add vertex v to topo_order array*/ /*Delete all edges going fron vertex v */ for(i=0; i<n; i++) { if(adj[v][i] == 1) { adj[v][i] = 0; indeg[i] = indeg[i]-1; if(indeg[i] == 0) insert_queue(i); } } } if( count < n ) { printf("No topological ordering possible, graph contains cycle\n"); exit(1); } printf("Vertices in topological order are :\n"); for(i=1; i<=count; i++) printf( "%d ",topo_order[i] ); printf("\n"); }/*End of main()*/
int snd_seq_oss_write(seq_oss_devinfo_t *dp, const char __user *buf, int count, struct file *opt) { int rc, c, p, ev_size; evrec_t rec; if (! is_write_mode(dp->file_mode) || dp->writeq == NULL) return -EIO; c = count; p = 0; while (c >= SHORT_EVENT_SIZE) { if (copy_from_user(rec.c, buf + p, SHORT_EVENT_SIZE)) break; p += SHORT_EVENT_SIZE; if (rec.s.code == SEQ_FULLSIZE) { /* load patch */ int fmt = (*(unsigned short *)rec.c) & 0xffff; return snd_seq_oss_synth_load_patch(dp, rec.s.dev, fmt, buf, p, c); } if (ev_is_long(&rec)) { /* extended code */ if (rec.s.code == SEQ_EXTENDED && dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) return -EINVAL; ev_size = LONG_EVENT_SIZE; if (c < ev_size) break; /* copy the reset 4 bytes */ if (copy_from_user(rec.c + SHORT_EVENT_SIZE, buf + p, LONG_EVENT_SIZE - SHORT_EVENT_SIZE)) break; p += LONG_EVENT_SIZE - SHORT_EVENT_SIZE; } else { /* old-type code */ if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) return -EINVAL; ev_size = SHORT_EVENT_SIZE; } /* insert queue */ if ((rc = insert_queue(dp, &rec, opt)) < 0) break; c -= ev_size; } if (count == c && is_nonblock_mode(dp->file_mode)) return -EAGAIN; return count - c; }
void ir_default_stmt(nodeType* n) { nodeType* stmt = get_operand(n,0); char mylabel[16]; memset(mylabel,0,16); strcat(mylabel,newlabel()); debugger("%s: ",mylabel); fprintf(output,"%s: ",mylabel); generate(stmt); nodeType* const_exp = empty(EMPTY); insert_queue(const_exp,mylabel); }
void BFS(int v) { int i; insert_queue(v); state[v]=waiting; while(!isempty_queue()) { v=delete_queue(); printf("%d ",v); state[v]=visited; for(i=0;i<n;i++) { if(adj[v][i]==1&&state[i]==initial) { insert_queue(i); state[i]=waiting; } } } printf("\n"); }
int main() { struct priority_queue *queue = create_queue(10); if (queue == NULL) printf("create_queue error\n"); insert_queue(queue, 10, (struct huff_table*)10); insert_queue(queue, 11, (struct huff_table*)11); insert_queue(queue, 8, (struct huff_table*)8); insert_queue(queue, 3, (struct huff_table*)3); insert_queue(queue, 7, (struct huff_table*)7); insert_queue(queue, 15, (struct huff_table*)15); uint32_t priority = 0; struct huff_table *table = NULL; while (best_queue(queue, &priority, &table)) { printf("Table = %ld\n", (uint64_t)table); delete_queue(queue); } free_queue(queue); return EXIT_SUCCESS; }
void email_asciifile_tail( FILE* output, const char* file, int lines ) { FILE *input; int ch, last_ch; long loc; int first_line = TRUE; TAIL_QUEUE queue, *q = &queue; if( !file ) { return; } if( (input=safe_fopen_wrapper_follow(file,"r",0644)) == NULL ) { // try the .old file in the off shoot case we hit this during the transition. std::string szTmp = file; szTmp += ".old"; if( (input=safe_fopen_wrapper_follow(szTmp.c_str(),"r",0644)) == NULL ) { dprintf( D_FULLDEBUG, "Failed to email %s: cannot open file\n", file ); return; } } init_queue( q, lines ); last_ch = '\n'; while( (ch=getc(input)) != EOF ) { if( last_ch == '\n' && ch != '\n' ) { insert_queue( q, ftell(input) - 1 ); } last_ch = ch; } while( !empty_queue( q ) ) { loc = delete_queue( q ); /* If this is the first line, print header */ if ( first_line ) { first_line = FALSE; fprintf(output,"\n*** Last %d line(s) of file %s:\n", lines, file); } /* Now print the line */ display_line( loc, input, output ); } (void)fclose( input ); /* if we printed any of the file, print a footer */ if ( first_line == FALSE ) { fprintf(output,"*** End of file %s\n\n", condor_basename(file)); } }
void putinqueue (queue_ref queue, FILE *input, char *filename) { char buffer[1024]; for (int linenr = 1; ; ++linenr) { char *linepos = fgets (buffer, sizeof buffer, input); if (linepos == NULL) break; linepos = strchr (buffer, '\n'); if (linepos == NULL) { fflush (NULL); fprintf (stderr, "%s: %s[%d]: unterminated line\n", execname, filename, linenr); fflush (NULL); exit_status = EXIT_FAILURE; }else { *linepos = '\0'; } linepos = strdup (buffer); assert (linepos != NULL); insert_queue (queue, linepos); } }
int snd_seq_oss_write(struct seq_oss_devinfo *dp, const char __user *buf, int count, struct file *opt) { int result = 0, err = 0; int ev_size, fmt; union evrec rec; if (! is_write_mode(dp->file_mode) || dp->writeq == NULL) return -ENXIO; while (count >= SHORT_EVENT_SIZE) { if (copy_from_user(&rec, buf, SHORT_EVENT_SIZE)) { err = -EFAULT; break; } if (rec.s.code == SEQ_FULLSIZE) { /* load patch */ if (result > 0) { err = -EINVAL; break; } fmt = (*(unsigned short *)rec.c) & 0xffff; /* FIXME the return value isn't correct */ return snd_seq_oss_synth_load_patch(dp, rec.s.dev, fmt, buf, 0, count); } if (ev_is_long(&rec)) { /* extended code */ if (rec.s.code == SEQ_EXTENDED && dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) { err = -EINVAL; break; } ev_size = LONG_EVENT_SIZE; if (count < ev_size) break; /* copy the reset 4 bytes */ if (copy_from_user(rec.c + SHORT_EVENT_SIZE, buf + SHORT_EVENT_SIZE, LONG_EVENT_SIZE - SHORT_EVENT_SIZE)) { err = -EFAULT; break; } } else { /* old-type code */ if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) { err = -EINVAL; break; } ev_size = SHORT_EVENT_SIZE; } /* insert queue */ if ((err = insert_queue(dp, &rec, opt)) < 0) break; result += ev_size; buf += ev_size; count -= ev_size; } return result > 0 ? result : err; }
int find_files(char* dirname, int socket, int *number, pthread_mutex_t *mtx) { int return_code, i, err; DIR *dir_ptr; struct dirent entry; struct dirent *result; struct stat mybuf; char buf[256]; if ((dir_ptr = opendir (dirname)) == NULL) { fprintf(stderr,"find files: couldn't open folder"); return -1; } else { while( ( return_code = readdir_r(dir_ptr, &entry, &result) ) == 0 && result!=NULL) { if (entry.d_ino==0) continue; if ( strcmp(entry.d_name,".")==0 || strcmp(entry.d_name,"..")==0) continue; strcpy(buf,dirname); strcat(buf,"/"); strcat(buf, entry.d_name); if ( stat (buf, & mybuf ) < 0) { perror ( buf ) ; continue ; } if (( mybuf.st_mode & S_IFMT ) == S_IFDIR ) // directory encountered { // printf("\ndirectory: %s\n", buf); if ( find_files(buf, socket, number, mtx) == -1) { fprintf(stderr,"find files recursion error"); return -1; } } else // file encountered { QueueNode *node; int filesize = (strlen(dirname)+strlen(entry.d_name)+2); node = create_node(); node->number = number; node->mutex = mtx; node->clientSock = socket; node->filesize = mybuf.st_size; node->mode = mybuf.st_mode; node->filename = malloc( filesize * sizeof(char) ); for (i=0; i<filesize; i++) node->filename[i] = '\0'; for (i=0; i<strlen(dirname); i++) node->filename[i] = dirname[i]; strcat(node->filename, "/"); strcat(node->filename, entry.d_name); node->filename_size = filesize-1; if (err = pthread_mutex_lock (& queue_lock )) { perror2 ("insert queue pthread_mutex_lock ", err ); exit (1); } while(size_queue(queue) == queue_size) { pthread_cond_wait ( &insert , &queue_lock ); } insert_queue(queue, node); pthread_cond_signal ( &worker_cond ); color_text(client_color); printf("[Client Thread: %ld]: Just added to queue: %s\n", pthread_self(), node->filename); if (err = pthread_mutex_unlock (& queue_lock )) { perror2 ("insernt queue pthread_mutex_unlock ", err ); exit (1) ; } // printf("file: %d, %s/%s\n", mybuf.st_size ,dirname, entry.d_name); } } closedir(dir_ptr); } return 0; }
main() { int i,v; int topo_order[MAX],indeg[MAX]; int count; int j; create_graph(); /*Find the indegree of each vertex*/ for(i=0; i<n; i++) { indeg[i] = indegree(i); if( indeg[i] == 0 ) insert_queue(i); } for(j=0; j<n; j++) printf("In(%d) = %d\t",j, indeg[j]); printf("\n"); display_queue(); count = 0; while( !isEmpty_queue() && count<n ) { v = delete_queue(); printf("Delete %d and edges going from it\n",v); topo_order[++count] = v; /*Add vertex v to topo_order array*/ /*Delete all edges going fron vertex v */ for(i=0; i<n; i++) { if(adj[v][i]==1) { adj[v][i] = 0; indeg[i] = indeg[i]-1; if(indeg[i] == 0) { printf("Now vertex %d has zero indegree so insert it in the queue\n",i); insert_queue(i); } } } for(j=0; j<n; j++) { if(indeg[j]!=0) printf("In(%d) = %d\t",j, indeg[j]); } printf("\n"); display_queue(); STOP; } printf("count = %d\n",count); if( count < n ) { printf("No topological ordering possible, graph contains cycle\n"); exit(1); } printf("Vertices in topological order are :\n"); for(i=1; i<=count; i++) printf( "%d ",topo_order[i] ); printf("\n"); }/*End of main()*/
pbs_queue *que_alloc( char *name, int sv_qs_mutex_held) { static char *mem_err = (char *)"no memory"; int i; pbs_queue *pq; pq = (pbs_queue *)calloc(1, sizeof(pbs_queue)); if (pq == NULL) { log_err(errno, __func__, mem_err); return(NULL); } pq->qu_qs.qu_type = QTYPE_Unset; pq->qu_mutex = (pthread_mutex_t *)calloc(1, sizeof(pthread_mutex_t)); pq->qu_jobs = (struct all_jobs *)calloc(1, sizeof(struct all_jobs)); pq->qu_jobs_array_sum = (struct all_jobs *)calloc(1, sizeof(struct all_jobs)); if ((pq->qu_mutex == NULL) || (pq->qu_jobs == NULL) || (pq->qu_jobs_array_sum == NULL)) { log_err(ENOMEM, __func__, mem_err); free(pq); return(NULL); } initialize_all_jobs_array(pq->qu_jobs); initialize_all_jobs_array(pq->qu_jobs_array_sum); pthread_mutex_init(pq->qu_mutex,NULL); lock_queue(pq, __func__, NULL, LOGLEVEL); snprintf(pq->qu_qs.qu_name, sizeof(pq->qu_qs.qu_name), "%s", name); insert_queue(&svr_queues,pq); if (sv_qs_mutex_held == FALSE) lock_sv_qs_mutex(server.sv_qs_mutex, __func__); server.sv_qs.sv_numque++; if (sv_qs_mutex_held == FALSE) unlock_sv_qs_mutex(server.sv_qs_mutex, __func__); /* set up the user info struct */ pq->qu_uih = (user_info_holder *)calloc(1, sizeof(user_info_holder)); initialize_user_info_holder(pq->qu_uih); /* set the working attributes to "unspecified" */ for (i = 0; i < QA_ATR_LAST; i++) { clear_attr(&pq->qu_attr[i], &que_attr_def[i]); } /* Set route_retry_thread_id in case this is a routing queue */ pq->route_retry_thread_id = -1; return(pq); } /* END que_alloc() */