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 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 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++; } }
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; }
/* * \internal Read data from usart interface * * \param[in] io_descr The pointer to an io descriptor * \param[in] buf A buffer to read data to * \param[in] length The size of a buffer * \param[in] timeout The millisecond of timeout * * \return The number of bytes user want to read. */ static int32_t usart_os_read(struct io_descriptor *const io_descr, uint8_t *const buf, const uint16_t length, uint32_t timeout) { uint16_t was_read = 0; struct usart_os_descriptor *descr = CONTAINER_OF(io_descr, struct usart_os_descriptor, io); ASSERT(buf); if (aos_mutex_lock(&descr->rx_mutex, AOS_WAIT_FOREVER) != 0) { return -1; } if (ringbuffer_num(&descr->rx) < length) { descr->rx_size = 0; descr->rx_length = length; descr->rx_buffer = buf; while (ringbuffer_num(&descr->rx) > 0) { ringbuffer_get(&descr->rx, &descr->rx_buffer[descr->rx_size++]); } if (sem_down(&descr->rx_sem, timeout) != 0) { aos_mutex_unlock(&descr->rx_mutex); return ERR_TIMEOUT; } } else { while (was_read < length) { ringbuffer_get(&descr->rx, &buf[was_read++]); } } aos_mutex_unlock(&descr->rx_mutex); return (int32_t)length; }
/* Attempts to get a lock on the page for IO operations. If it is already * locked, it will block the kthread until it is unlocked. Note that this is * really a "sleep on some event", not necessarily the IO, but it is "the page * is ready". */ void lock_page(struct page *page) { /* when this returns, we have are the ones to have locked the page */ sem_down(&page->pg_sem); assert(!(atomic_read(&page->pg_flags) & PG_LOCKED)); atomic_or(&page->pg_flags, PG_LOCKED); }
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); }
void uvolniZidli(int zidle) { sem_down(SEM_MUTEX); default_filozof(zidle); sem_up(SEM_MUTEX); }
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 usbSerListener(void) { /* Wait until USB is plugged in */ sem_down(&cdc_run); /* And wait until some character has been typed */ getch(&cdc_instr); cmdProcessor(&cdc_instr, &cdc_outstr); }
void sort(void* args) { int value = (int) args; sleep_proc(value*5); sem_down(insert_sem); sorted[size] = value; size++; sem_up(insert_sem); }
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 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); }
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 f_pong(void *args) { while(1) { sem_down(&gsem); printf("1") ; printf("2") ; printf("\n"); sem_up(&gsem); } }
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); } }
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) { mstime_t rv; if (!sem || !*sem) return SYS_ARCH_TIMEOUT; rv = sem_down(*sem, timeout); if (rv == (mstime_t)-1) return SYS_ARCH_TIMEOUT; else return rv; }
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 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 UGrad(int mutex, int usem){ while(1){ sem_down(mutex); if(fork() == 0){ UGrad1(usem); } else{ UGrad2(usem); } sem_up(mutex); } return; }
__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. */ }
int mutex_acquire(struct mutex_s * mutex) { /* Vérification des paramètres */ if (mutex == NULL) { return -1; } /* Processus : * - sem_down bloque le process courant si le mutex est déjà bloqué * - immédiatement après, on est sûr que le processus courant a * vérouillé le mutex */ /* FIXME fait planter le kernel */ sem_down(&mutex->sem); mutex->owner = get_current_process(); return 0; }
extern int logread_main(int argc, char **argv) { int i; /* no options, no getopt */ if (argc > 1) show_usage(); // handle intrrupt signal if (setjmp(jmp_env)) goto output_end; // attempt to redefine ^C signal signal(SIGINT, interrupted); if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1) error_exit("Can't find circular buffer"); // Attach shared memory to our char* if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL) error_exit("Can't get access to circular buffer from syslogd"); if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) error_exit("Can't get access to semaphone(s) for circular buffer from syslogd"); sem_down(log_semid); // Read Memory i=buf->head; //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size); if (buf->head == buf->tail) { printf("<empty syslog>\n"); } while ( i != buf->tail) { printf("%s", buf->data+i); i+= strlen(buf->data+i) + 1; if (i >= buf->size ) i=0; } sem_up(log_semid); output_end: if (log_shmid != -1) shmdt(buf); return EXIT_SUCCESS; }
/* * \internal Write the given data to usart interface * * \param[in] descr The pointer to an io descriptor * \param[in] buf Data to write to usart * \param[in] length The number of bytes to write * \param[in] timeout The millisecond of timeout * * \return The number of bytes written or <0 for timeout. */ static int32_t usart_os_write(struct io_descriptor *const io_descr, const uint8_t *const buf, const uint16_t length, uint32_t timeout) { int ret; struct usart_os_descriptor *descr = CONTAINER_OF(io_descr, struct usart_os_descriptor, io); if (aos_mutex_lock(&descr->tx_mutex, AOS_WAIT_FOREVER) != 0) { return -1; } descr->tx_buffer = (uint8_t *)buf; descr->tx_buffer_length = length; descr->tx_por = 0; _usart_async_enable_byte_sent_irq(&descr->device); ret = sem_down(&descr->tx_sem, timeout); aos_mutex_unlock(&descr->tx_mutex); return (ret == 0) ? length : ERR_TIMEOUT; }
int dejZidli() // -1 obsazeno, 0 .. N-1 misto u stolu { sem_down(SEM_MUTEX); int zidle = -1; for (int i = 0; i < N; i++) { if ((zidle < 0) && (global_data->zidle[i] == VOLNA)) { global_data->zidle[i] = OBSAZENA; zidle = i; break; } } sem_up(SEM_MUTEX); return zidle; }