int main(int argc, char *argv[]) { endpoint_t who; if (argc != 3) { fprintf(stderr, "Usage: %s <name> <endpoint>\n", argv[0]); return -1; } who = atoi(argv[2]); while (1) { /* take a fork */ sem_down(who); /* take another fork */ sem_down(who); printf("[%s] is eating...\n", argv[1]); sleep(5); /* put down two forks */ sem_up(who); sem_up(who); /* the philosopher often thinks too hard * and gets hungry easily, so he needs to * eat again in a short time. :-) */ printf("[%s] is thinking...\n", argv[1]); sleep(15); } return 0; }
void producteur (void * args) { int i = 100; while (i) { sem_down(&vide); /* dec. nb places libres */ sem_down(&mutex); /* entree en section critique */ // mettre_objet(); /* mettre l'objet dans le tampon */ produire_objet(); /* produire l'objet suivant */ sem_up(&mutex); /* sortie de section critique */ sem_up(&plein); /* inc. nb place occupees */ } }
void consommateur (void * args) { int i = 100; while (1) { sem_down(&plein); /* dec. nb emplacements occupes */ sem_down(&mutex); /* entree section critique */ // retirer_objet (); /* retire un objet du tampon */ utiliser_objet(); /* utiliser l'objet */ sem_up(&mutex); /* sortie de la section critique */ sem_up(&vide); /* inc. nb emplacements libres */ } }
void consommateur(void *args) { int i = 1; while (i<=4) { sem_down(fullSem); sem_down(mutex); printf("Le produit numéro %i a été pris\n", i); yield(); sem_up(mutex); sem_up(emptySem); printf("Le produit numéro %i a été utilisé\n", i); yield(); i++; } }
void producteur(void *args) { int i = 1; while (i<=4) { printf("Le produit numéro %i a été confectionné\n", i); sem_down(emptySem); sem_down(mutex); yield(); printf("Le produit numéro %i est disponible\n", i); sem_up(mutex); sem_up(fullSem); yield(); i++; } }
void int_handler_ata() { struct t_processor_reg processor_reg; t_io_request* io_request; struct t_process_context* process_context; SAVE_PROCESSOR_REG disable_irq_line(14); DISABLE_PREEMPTION EOI_TO_SLAVE_PIC EOI_TO_MASTER_PIC STI // static unsigned int* xx; // xx=0xBFFFEC18; // if ((*xx)==0xcc27a000) // { // printk("got!! \n"); // } io_request=system.device_desc->serving_request; process_context=io_request->process_context; if (system.device_desc->status!=POOLING_MODE) { sem_up(&io_request->device_desc->sem); } system.device_desc->status=DEVICE_IDLE; enable_irq_line(14); ENABLE_PREEMPTION EXIT_INT_HANDLER(0,processor_reg) }
void mtx_unlock(struct mtx_s* mutex){ DISABLE_IRQ(); if (current_process == mutex->owner) { sem_up(mutex->sem_mtx); } ENABLE_IRQ(); }
void semafor_down() { int* shmem = get_mem(); fore(getpid()); print("Proceso 1 intenta bajar semaforo\n"); sem_down(); fore(getpid()); print("Proceso 1 baja semaforo:\n"); print(" Variable en shared memory tiene valor:\n"); print(" "); printint(*shmem); *shmem = *shmem + 2; print("\n Se le suma 2 y ahora tiene valor:\n"); print(" "); printint(*shmem); print("\n"); sleep(5); sem_up(); fore(getpid()); print("Proceso 1 levanta semaforo\n"); fore(getppid()); while(1); }
int process3() { sem_down(&sem); // blocked while (sem.counter != 0) ; sem_up(&sem); return 0; }
int process1() { sem_down(&sem); while (sem.counter != -1) ; sem_up(&sem); return 0; }
void uvolniZidli(int zidle) { sem_down(SEM_MUTEX); default_filozof(zidle); sem_up(SEM_MUTEX); }
/* Event handler for the library USB Configuration Changed event. */ void EVENT_USB_Device_ConfigurationChanged(void) { if ((CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface))) { led_usb_on(); sem_up(&cdc_run); } }
void polozVidlicky(int zidle) { sem_down(SEM_MUTEX); global_data->filozof[zidle] = PREMYSLI; // filozof premysli testVidlicek(VLEVO); // muze jist levy soused ? testVidlicek(VPRAVO); // muze jist pravy soused ? sem_up(SEM_MUTEX); }
void sort(void* args) { int value = (int) args; sleep_proc(value*5); sem_down(insert_sem); sorted[size] = value; size++; sem_up(insert_sem); }
void vezmiVidlicky(int zidle) { sem_down(SEM_MUTEX); global_data->filozof[zidle] = HLADOVI; // filozof zjistil, ze je hladovy testVidlicek(zidle); // pokus o ziskani dvou vidlicek sem_up(SEM_MUTEX); sem_down(SEM_FILOZOF + zidle); // zablokovani pokud nejsou vidlicky tiskVidlicek(zidle); }
void fbq_put(FBQ* q, FBUF b) { sem_down(&q->capacity); register uint16_t i = q->index + q->length.cnt; if (i >= q->size) i -= q->size; q->buf[(uint8_t) i] = b; sem_up(&q->length); }
FBUF fbq_get(FBQ* q) { sem_down(&q->length); register uint8_t i = q->index; if (++q->index >= q->size) q->index = 0; sem_up(&q->capacity); return q->buf[i]; }
void consommateur (void* arg) { if(arg){ fprintf(stderr, "ERROR: fonction consommateur sans arguments"); return; } objet_t objet ; while (1) { sem_down(&plein); /* dec. nb emplacements occupes */ sem_down(&mutex); /* entree section critique */ retirer_objet (&objet); /* retire un objet du tampon */ sem_up(&mutex); /* sortie de la section critique */ sem_up(&vide); /* inc. nb emplacements libres */ utiliser_objet(objet); /* utiliser l'objet */ } }
void producteur (void* arg) { if(arg){ fprintf(stderr, "ERROR: fonction producteur sans arguments"); return; } objet_t objet ; while (1) { produire_objet(&objet); /* produire l'objet suivant */ sem_down(&vide); /* dec. nb places libres */ sem_down(&mutex); /* entree en section critique */ mettre_objet(objet); /* mettre l'objet dans le tampon */ sem_up(&mutex); /* sortie de section critique */ sem_up(&plein); /* inc. nb place occupees */ } }
void testVidlicek(int zidle) { if ((global_data->filozof[zidle] == HLADOVI) && (global_data->filozof[VLEVO] != POJIDA) && (global_data->filozof[VPRAVO] != POJIDA)) { global_data->filozof[zidle] = POJIDA; sem_up(SEM_FILOZOF + zidle); } }
void f_pong(void *args) { while(1) { sem_down(&gsem); printf("1") ; printf("2") ; printf("\n"); sem_up(&gsem); } }
int mutex_release(struct mutex_s * mutex) { /* Vérification des paramètres */ if (mutex == NULL || mutex->owner != get_current_process()) { return -1; } mutex->owner = NULL; sem_up(&mutex->sem); return 0; }
/* * This is used to insert a block which is not previously on the * free list. Only the a.size field of the arena header is assumed * to be valid. */ void __inject_free_block(struct free_arena_header *ah) { struct free_arena_header *head = &__core_malloc_head[ARENA_HEAP_GET(ah->a.attrs)]; struct free_arena_header *nah; size_t a_end = (size_t) ah + ARENA_SIZE_GET(ah->a.attrs); size_t n_end; dprintf("inject: %#zx bytes @ %p, heap %u (%p)\n", ARENA_SIZE_GET(ah->a.attrs), ah, ARENA_HEAP_GET(ah->a.attrs), head); sem_down(&__malloc_semaphore, 0); for (nah = head->a.next ; nah != head ; nah = nah->a.next) { n_end = (size_t) nah + ARENA_SIZE_GET(nah->a.attrs); /* Is nah entirely beyond this block? */ if ((size_t) nah >= a_end) break; /* Is this block entirely beyond nah? */ if ((size_t) ah >= n_end) continue; printf("conflict:ah: %p, a_end: %p, nah: %p, n_end: %p\n", ah, a_end, nah, n_end); /* Otherwise we have some sort of overlap - reject this block */ sem_up(&__malloc_semaphore); return; } /* Now, nah should point to the successor block */ ah->a.next = nah; ah->a.prev = nah->a.prev; nah->a.prev = ah; ah->a.prev->a.next = ah; __free_block(ah); sem_up(&__malloc_semaphore); }
void funcOne(void* args) { while (1==1){ int i; sem_down(sem_test); for (i=0; i<10000000;i++){ cpt++; } //sleep_proc(4); sem_up(sem_test); } }
void TaskA (void * arg) { int i ; for (i=0; i<10; i++) { sem_down (&s1) ; printf ("%s zig\n", (char *) arg) ; task_sleep (1) ; sem_up (&s2) ; } task_exit (0) ; }
void Grad(int mutex, int gsem){ while(1){ sem_down(mutex); int k; for(k=0; k<=5; k++){ fork(); GradEat(gsem, k); } sem_up(mutex); } return; }
void UGrad(int mutex, int usem){ while(1){ sem_down(mutex); if(fork() == 0){ UGrad1(usem); } else{ UGrad2(usem); } sem_up(mutex); } return; }
int main(int argc, char *argv[]){ if(argc != 3){ printf("enter the semaphore number as an argument\n"); exit(0); } //sem will be the semaphore number we are using for grad students int sem = strtol(argv[1],NULL,10); // Uses another semaphore to signal to the dispatcher that it's ready int start_sem = strtol(argv[2],NULL,10); sem_up(start_sem); int i; for(i=0;i<6;i++){ // we fork six children to represent the 6 grad students pid_t child_p = fork(); if(child_p < 0){ printf("ERROR in child process"); exit(1); } else if (child_p==0){ //in child process, run the loop while(1){ sem_down(sem); printf("grad #%d started eating\n",i); sleep(2); printf("grad #%d finished eating\n",i); sem_up(sem); } } } return 0; }
__export void free(void *ptr) { #ifdef DEBUG_MALLOC dprintf("free(%p) @ %p\n", ptr, __builtin_return_address(0)); #endif if ( !ptr ) return; sem_down(&__malloc_semaphore, 0); firmware->mem->free(ptr); sem_up(&__malloc_semaphore); /* Here we could insert code to return memory to the system. */ }
/** * \brief Process byte reception * * \param[in] device The pointer to device structure * \param[in] data Data read */ static void usart_os_fill_rx_buffer(struct _usart_async_device *device, uint8_t data) { struct usart_os_descriptor *descr = CONTAINER_OF(device, struct usart_os_descriptor, device); if (descr->rx_buffer == NULL) { ringbuffer_put(&descr->rx, data); } else { descr->rx_buffer[descr->rx_size++] = data; if (descr->rx_size >= descr->rx_length) { descr->rx_buffer = NULL; sem_up(&descr->rx_sem); } } }