void fbq_clear(FBQ* q) { register uint16_t i; for (i = q->index; i < q->index + q->length.cnt; i++) fbuf_release(&q->buf[(uint8_t) (i % q->size)]); sem_set(&q->length, 0); sem_set(&q->capacity, q->size); q->index = 0; }
int main(void) { int freie_ressourcen; /* Anlegen des Semaphors */ sem_anfordid = sem_create(SEM_READKEY); /* Setzen des Sempahors auf 1 */ sem_set(sem_anfordid, 1); /* Beginn Serverschleife */ while(1) { /* Abfragen wieviel Ressourcen frei sind, falls 0 Schleife verlassen */ freie_ressourcen = sem_get(sem_anfordid); if (freie_ressourcen == 0) break; /* eine Sekunde warten */ sleep(1); /* Ende Serverschleife */ } /* Semaphor entfernen */ sem_rm(sem_anfordid); printf("Server terminiert.\n"); exit(0); }
void sem_remove_r(char *data, char *buf) { char *sem; strtok(data, ","); sem = xstrcpy(strtok(NULL, ";")); if (sem_set(sem, FALSE)) snprintf(buf, 40, "100:0;"); else snprintf(buf, 40, "200:1,16;"); free(sem); return; }
void stat_set_open(int op) { if (op) { if (!s_bbsopen) { Syslog('!', "The bbs is open"); sem_set((char *)"scanout", TRUE); } } else { if (s_bbsopen) { Syslog('!', "The bbs is closed"); } } s_bbsopen = status.open = op; status_write(); }
int set_sem(struct proc *p, int sem) { int val; val = word2int(p->r); if (val < 0) { fprintf(stderr, "set_sem: Invalid semaphor value "); print_word(stderr, p->r); fprintf(stderr, " (pid %u)\n", p->pid); return 1; } if (sem < 0) { fprintf(stderr, "set_sem: Invalid semaphor (pid %u)\n", p->pid); return 1; } if (sem_set(sem, val) != 0) { fprintf(stderr, "set_sem: Error setting semaphor (pid %u)\n", p->pid); return 1; } return 0; }
int main(int argc, char *argv[]) { if (argc != 2 ) { printf("usage: ./server MSG_NUMBER\n"); return 1; } int msg_number = atoi(argv[1]); int semid,ret,status; semid = sem_init(); sem_set(semid, 0); int running = 1; void *shm = NULL; struct shared_use_st *shared = NULL; char buffer[BUFSIZ + 1];//用于保存输入的文本 int shmid; //创建共享内存 shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666|IPC_CREAT); if(shmid == -1) { fprintf(stderr, "shmget failed\n"); exit(EXIT_FAILURE); } //将共享内存连接到当前进程的地址空间 shm = shmat(shmid, (void*)0, 0); if(shm == (void*)-1) { fprintf(stderr, "shmat failed\n"); exit(EXIT_FAILURE); } //设置共享内存 shared = (struct shared_use_st*)shm; int i = 0; while(i < msg_number)//向共享内存中写数据 { //数据还没有被读取,则等待数据被读取,不能向共享内存中写入文本 if (shared->written == 1) { sem_p(semid); sprintf(buffer, "%s%d\n", SENDER_MSG, i); strncpy(shared->text, buffer, strlen(buffer)+1); //写完数据,设置written使共享内存段可读 shared->written = 1; i++; }else { //向共享内存中写入数据 //printf("Enter some text: "); //fgets(buffer, BUFSIZ, stdin); sprintf(buffer, "%s%d\n", SENDER_MSG, i); strncpy(shared->text, buffer, strlen(buffer)+1); //写完数据,设置written使共享内存段可读 shared->written = 1; i++; sem_v(semid); } //printf("%s\n", buffer); } sprintf(buffer, "end"); strncpy(shared->text, buffer, strlen(buffer)+1); //写完数据,设置written使共享内存段可读 shared->written = 1; //把共享内存从当前进程中分离 if(shmdt(shm) == -1) { fprintf(stderr, "shmdt failed\n"); exit(EXIT_FAILURE); } sem_del(semid); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int shmid; int i,j; char in_str[16]; process_arguments(argc, argv); /* open the camera device */ if( (camera_fd = open(psz_video_dev, O_RDWR)) < 0 ) { char error_buf[256]; sprintf(error_buf, "open() %s", psz_video_dev); perror(error_buf); exit(-1); } get_caps(); get_format(); if( b_verbose ) printf("Device opened.\n"); if( b_verbose ) { printf("Video device:\t\t%s\n", psz_video_dev); print_caps(); print_format(); printf("Ouput directory:\t%s\n", psz_output_dir); printf("Image format:\t\t%s\n",str_formats[e_outfmt]); printf("\n"); printf("Opening device %s\n", psz_video_dev); if( b_named_filename ) { printf("Ouput filename:\t%s\n", psz_output_filename); } else if( b_named_pipe ) { printf("Using named pipe %s\n", psz_named_pipe); } if( b_shared_mem ) printf("Using shared memory. key = %i\n", shared_mem_key); } if( b_printinfo ) { printf("Device info:\n"); print_caps(); print_format(); close(camera_fd); exit(EXIT_SUCCESS); } (void)signal(SIGINT, exit_program); if( b_shared_mem && b_named_pipe ) { printf("WARNING: shared memory and named pipe can't be used together. Use more instances of camshot. Defaulting to named pipe.\n"); b_shared_mem = 0; } if( b_named_pipe ) { int ret_val = mkfifo(psz_named_pipe, 0666); if ((ret_val == -1) && (errno != EEXIST)) { perror("Error creating the named pipe"); exit(EXIT_FAILURE); } } if( req_width && req_height ) { if( b_verbose ) printf("Trying to set resolution to %ux%u.\n", req_width, req_height); if( set_width_height(req_width,req_height) == -1 ) printf("Unable to set the desired resolution.\n"); else if( b_verbose ) printf("Resolution set to %ux%u\n", req_width, req_height); } else { get_format(); req_width = camera_format.fmt.pix.width; req_height = camera_format.fmt.pix.height; } if( b_shared_mem ) { if((shmid = shmget(shared_mem_key, req_width*req_height*3, IPC_CREAT | 0666)) < 0) { perror("Error getting shared memory id"); exit(EXIT_FAILURE); } if((p_shm = (uint8_t *)shmat(shmid, NULL, 0)) == (void *) -1) { perror("Error getting shared memory ptr"); exit(EXIT_FAILURE); } shm_sem = semget((key_t)shared_mem_key, 1, IPC_CREAT | 0666); sem_set(&shm_sem); if( b_verbose ) printf("Shared memory ID: %i\nSemaphore ID: %i\n", shmid, shm_sem); } total_buffers = req_mmap_buffers(2); /* start the capture */ streaming_on(); /* let the camera self adjust by 'ignoring' 200 complete buffer queues */ printf("Letting the camera automaticaly adjust the picture:"); for(i=0; i<AUTO_ADJUST_TURNS; i++) { for(j=0; j<total_buffers; j++) { int ready_buf = dequeue_buffer(); /* don't queue the last buffers */ if( i<AUTO_ADJUST_TURNS-1 ) queue_buffer(ready_buf); } printf("."); fflush(stdout); } printf("Done.\n"); if( b_shared_mem || b_named_pipe ) { pthread_create(&stream_thread, NULL, &stream_func, NULL); while(1) { pthread_join(stream_thread, NULL); } } else { pthread_create(&capture_thread, NULL, &capture_func, NULL); } if( b_named_filename ) { usleep(200000); pthread_mutex_lock(&cond_mutex); pthread_cond_signal(&condition); pthread_mutex_unlock(&cond_mutex); usleep(200000); exit_program(SIGINT); fflush(stdout); return 0; } while( in_str[0] != 'q' ) { printf("Command (h for help): "); fflush(stdout); if( fgets(in_str, 16, stdin) == NULL ) { printf("Got NULL! Try again.\n"); continue; } switch(in_str[0]) { case 'x': pthread_mutex_lock(&cond_mutex); pthread_cond_signal(&condition); pthread_mutex_unlock(&cond_mutex); break; case 'h': printf("\nCommands:\n"); printf("\tx\tCapture a picture from camera.\n"); printf("\th\tPrints this help.\n"); printf("\tq\tQuits the program.\n"); printf("\n"); break; case 'q': case '\n': break; default: fprintf(stderr, "Unknown command %c\n", in_str[0]); break; } } /* Clean up */ exit_program(SIGINT); return 0; }