static void init_serial_port(char *devname, int iobase, int irq, struct unit *unit) { struct serial_port *sp; dev_t devno; sp = (struct serial_port *) kmalloc(sizeof(struct serial_port)); memset(sp, 0, sizeof(struct serial_port)); sp->iobase = iobase; sp->irq = irq; sp->cfg.speed = 115200; sp->cfg.databits = 8; sp->cfg.parity = PARITY_NONE; sp->cfg.stopbits = 1; sp->cfg.rx_timeout = INFINITE; sp->cfg.tx_timeout = INFINITE; init_dpc(&sp->dpc); sp->dpc.flags |= DPC_NORAND; init_event(&sp->event, 0, 0); init_sem(&sp->tx_sem, QUEUE_SIZE); init_mutex(&sp->tx_lock, 0); init_sem(&sp->rx_sem, 0); init_mutex(&sp->rx_lock, 0); // Disable interrupts outp(sp->iobase + UART_IER, 0); // Determine UART type check_uart_type(sp); // Set baudrate, parity, databits and stopbits serial_config(sp); // Enable FIFO if (sp->type == UART_16550A) { outp(sp->iobase + UART_FCR, FCR_ENABLE | FCR_RCV_RST | FCR_XMT_RST | FCR_TRIGGER_14); } // Turn on DTR, RTS and OUT2 sp->mcr = MCR_DTR | MCR_RTS | MCR_IENABLE; outp(sp->iobase + UART_MCR, sp->mcr); // Create device devno = dev_make(devname, &serial_driver, unit, sp); // Enable interrupts register_interrupt(&sp->intr, IRQ2INTR(sp->irq), serial_handler, sp); enable_irq(sp->irq); outp((unsigned short) (sp->iobase + UART_IER), IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC); kprintf(KERN_INFO "%s: %s iobase 0x%x irq %d\n", device(devno)->name, uart_name[sp->type], sp->iobase, sp->irq); }
void init_mem(){ id = shmget(CLE, 7*sizeof(int), IPC_CREAT | 0777); sem_nvide = init_sem(tabsize); sem_nplein = init_sem(0); muttex = init_sem(1); if(id == -1){ return; } }
void test_setup(void) { init_sem(&full,0); init_sem(&empty,NBUF); init_sem(&mutex_r,1); init_sem(&mutex_w,1); int i; for(i=0;i<NR_PROD;i++) { wakeup(create_kthread(test_producer)); } for(i=0;i<NR_CONS;i++) { wakeup(create_kthread(test_consumer)); } }
int main() { pid = getpid(); int key = ftok(".",'a'); shm_id = init_shm(key,sizeof(pid_t),IPC_CREAT | 0666); shmptr = (pid_t*)shmat(shm_id,NULL,0); sem_id= init_sem(key,1,IPC_CREAT|0666); semctl(sem_id,0,SETVAL,1); create_shared_objects(); signal(SIGUSR1, sig_handler); printf("Enter Number to send from c1 to c2 : "); scanf("%d",shm_c1); P(sem_id); kill(*shmptr,SIGUSR1); *shmptr = pid; V(sem_id); while(1) sleep(1); }
int main(int argc, char** argv) { pthread_t threads[NUM_CONTROLLERS]; if (argc != 3) { fprintf(stderr, "Usage: parque <N_LUGARES> <T_ABERTURA>\n"); exit(1); } if (convert_str_to_int(argv[1], &numLugares) != 0) { fprintf(stderr, "Invalid entry for N_LUGARES\n"); exit(2); } if (convert_str_to_int(argv[2], &tAbertura) != 0) { fprintf(stderr, "Invalid entry for T_ABERTURA\n"); exit(3); } if ( (fp_logger = init_logger(LOGGER_NAME)) == NULL ) { fprintf(stderr, "Error opening %s\n", LOGGER_NAME); exit(4); } if ( (sem = init_sem(SEM_NAME)) == SEM_FAILED ) { exit(5); } start = clock(); int i; for (i = 0; i < NUM_CONTROLLERS; i++) { pthread_create(&threads[i], NULL, controlador, controller_name[i]); } sleep(tAbertura); /* Zona Critica */ sem_wait(sem); notify_controllers(FINISH_STR); for (i = 0; i < NUM_CONTROLLERS; i++) pthread_join(threads[i], NULL); sem_post(sem); /***************/ destroy_sem(sem, SEM_NAME); fclose(fp_logger); pthread_exit(0); }
int threadtest10(int nargs, char **args) { counter = 0; int loopBound; if (nargs != 3 && nargs != 2) { kprintf("Usage: tt10 [int] [optional int]"); return 0; } if (nargs == 3) { loopBound = atoi(args[2]); } else { loopBound = 10000; } int nThreads = atoi(args[1]); init_sem(); init_lock(); kprintf("Starting thread test...\n"); threadfun10(nThreads, loopBound); kprintf("\nThread test done.\n"); kprintf("\nThe value of the counter should be %d. \n", loopBound*nThreads); kprintf("\nThe value of the counter is %d. \n", counter); return 0; }
static void speedo_init_rx_ring(struct dev *dev) { struct nic *sp = (struct nic *) dev->privdata; struct RxFD *rxf, *last_rxf = NULL; int i; init_sem(&sp->tx_sem, TX_QUEUE_LIMIT); sp->cur_rx = 0; for (i = 0; i < RX_RING_SIZE; i++) { struct pbuf *p; p = pbuf_alloc(PBUF_RAW, PKT_BUF_SZ + sizeof(struct RxFD), PBUF_RW); sp->rx_pbuf[i] = p; if (p == NULL) break; // OK. Just initially short of Rx bufs rxf = (struct RxFD *) p->payload; sp->rx_ringp[i] = rxf; pbuf_header(p, - (int) sizeof(struct RxFD)); if (last_rxf) last_rxf->link = virt2phys(rxf); last_rxf = rxf; rxf->status = 0x00000001; // '1' is flag value only rxf->link = 0; // None yet rxf->rx_buf_addr = 0xffffffff; rxf->count = PKT_BUF_SZ << 16; } sp->dirty_rx = (unsigned int)(i - RX_RING_SIZE); // Mark the last entry as end-of-list. last_rxf->status = 0xC0000002; // '2' is flag value only sp->last_rxf = last_rxf; }
int main() { init_sem(); down(); sleep(10); up(); return 0; }
static int init(MQ *self) { // 初始化信号量 if (0 != init_sem(&self->p_sem, 0, 0)) { perror("init named sem faild"); return -1; } return 0; };
struct queue *alloc_queue(int size) { struct queue *q = (struct queue *) kmalloc(sizeof(struct queue)); if (!q) return q; q->elems = (void **) kmalloc(size * sizeof(void *)); if (!q->elems) { kfree(q); return NULL; } init_sem(&q->notfull, size); init_sem(&q->notempty, 0); q->size = size; q->in = 0; q->out = 0; return q; }
// 创建一个信号量集 int creat_sem(key_t key) { int sem_id; if((sem_id = semget(key, 1, IPC_CREAT|0666)) == -1) { perror("semget error"); exit(-1); } init_sem(sem_id, 1); /*初值设为1资源未占用*/ return sem_id; }
static void setup(void) { tst_require_root(NULL); #if !defined(__NR_process_vm_readv) tst_brkm(TCONF, NULL, "process_vm_readv does not exist " "on your system"); #endif semid = init_sem(1); TEST_PAUSE; }
int main(int argc,char* argv[]) { std::string name = "fifo0001.12"; semaphore sem; mkfifo(name.c_str(),0777); int fifo = open(name.c_str(),O_RDWR); int val; init_sem(sem); Server(sem,fifo); return 0; }
int sthread_cond_init(sthread_cond_t *cond, sthread_condattr_t *cond_attr) { cond->cond = (struct cond_struct *)mvshared_malloc(sizeof(struct cond_struct)); if(cond->cond) { int i; cond->cond->locks = new_sem(MAXTHREADS); for(i=0;i<MAXTHREADS;i++) init_sem(cond->cond->locks, i, 0); return 0; } return -1; }
int threadtest2(int nargs, char **args) { (void)nargs; (void)args; init_sem(); kprintf("Starting thread test 2...\n"); runthreads(0); kprintf("\nThread test 2 done.\n"); return 0; }
void init_res(t_data *data) { printf("Init res !\n"); data->map->width = MAP_WIDTH; data->map->height = MAP_HEIGHT; data->map->nb_gamer = 1; bzero(data->map->tab, (MAP_WIDTH * MAP_HEIGHT) * sizeof(char)); data->id = 0; data->team_id = 1; data->pos = 0; data->map->tab[data->pos] = data->team_id; init_sem(data); }
int threadtest_fun(int nargs, char **args) { (void)nargs; (void)args; init_sem(); kprintf("Starting the fun thread test...\n"); _runthreads(); kprintf("\nCustom thread test done.\n"); return 0; }
int sthread_barrier_init(sthread_barrier_t *barrier, const sthread_barrierattr_t *attr, unsigned int count) { barrier->barrier = (struct barrier_struct *)mvshared_malloc(sizeof(struct barrier_struct)); if(barrier->barrier) { barrier->barrier->total = count; barrier->barrier->num = 0; barrier->barrier->sema = new_sem(1); init_sem(barrier->barrier->sema, 0, 0); barrier->barrier->inited = 1; return 0; } return -1; }
int main() { int i = 0; /*Signalhandler registrieren*/ struct sigaction aktion; aktion.sa_handler = &programmabbruch; sigemptyset(&aktion.sa_mask); if (sigaction(SIGINT,&aktion,NULL) == -1) { perror("set actionhandler"); exit(EXIT_FAILURE); } /* Zufallsgenerator initialisieren */ srand(time(NULL)); /*Die Kindprozesse erben den Signalhandler, da wir diesen bereits vor dem fork registrieren. Damit der Vater (Kreuzung) weiss, dass er der Vater ist, speichern wir an dieser Stelle die PID.*/ vaterpid = getpid(); /* Semaphoren erzeugen */ semid = erzeuge_sem(ANZAHL_AUTOS+2, 0xaffe); if (semid == -1) { fprintf(stderr, "Semaphore noch in Benutzung.\n"); exit(-1); } /* Semaphoren initialisieren */ init_sem(semid, ANZAHL_AUTOS+1, 1); /* Semaphore fuer Deadlock Entsperrung initialisieren */ set_sem(semid, ANZAHL_AUTOS+1, ANZAHL_AUTOS); /* Autos forken */ for (i = 0; i < ANZAHL_AUTOS; i++) { autopids[i] = erzeugeauto(i); sleep(1); } /* Ueberwachungs-Prozess (Vater) */ vater(); return 0; }
int threadtest5(int nargs, char **args ) { (void)nargs; int VNAME = atoi(args[1]); int upperBound = atoi(args[2]); unsafethreadcounter = 0; init_sem(); kprintf("starting thread test...\n"); threadfun2(VNAME, upperBound); kprintf("\nThread test done.\n"); V(tsem); kprintf("\nThe value of the unsafethreadcounter is %d. \n", unsafethreadcounter); kprintf("\nThe value of the unsafethreadcounter SHOULD be %d. \n", VNAME * upperBound ); return 0; }
int threadtest8(int nargs, char **args ) { (void)nargs; int VNAME = atoi(args[1]); int upperBound = atoi(args[2]); lockcounter = 0; init_sem(); init_lock(); kprintf("starting thread test...\n"); threadfun5(VNAME, upperBound); kprintf("\nThread test done.\n"); kprintf("\nThe value of the spinlockthreadcounter is %d. \n", lockcounter); kprintf("\nThe value of the spinlockthreadcounter SHOULD be %d. \n", VNAME * upperBound ); return 0; }
int main() { void *shared_memory = NULL; struct shm_buff *shm_buff_inst; char buffer[BUFSIZ]; int shmid,semid; ignore_signal(); semid = semget(ftok(".",'a'),1,0666|IPC_CREAT); init_sem(semid,1); shmid = shmget(ftok(".",'b'),sizeof(struct shm_buff),0666|IPC_CREAT); if(shmid == -1) { perror("shmget failed"); del_sem(semid); exit(1); } shared_memory=shmat(shmid,(void*)0,0); if(shared_memory==(void*)-1) { perror("shmat"); del_sem(semid); exit(1); } printf("memory attached at %X\n",(int)shared_memory); shm_buff_inst = (struct shm_buff*)shared_memory; do { sem_p(semid); printf("enter some text to the shared memory(enter'quit'to exit):"); if(fgets(shm_buff_inst->buffer,SHM_BUFF_SZ,stdin)==NULL) { perror("fgets"); sem_v(semid); break; } shm_buff_inst->pid=getpid(); sem_v(semid); } while(strncmp(shm_buff_inst->buffer,"quit",4)!=0); del_sem(semid); if(shmdt(shared_memory)==1) { perror("shmdt"); exit(1); } exit(0); }
static void setup(void) { tst_require_root(); nr_iovecs = nflag ? SAFE_STRTOL(NULL, nr_opt, 1, IOV_MAX) : 10; bufsz = sflag ? SAFE_STRTOL(NULL, sz_opt, NUM_LOCAL_VECS, LONG_MAX) : 100000; #if !defined(__NR_process_vm_readv) tst_brkm(TCONF, NULL, "process_vm_readv does not exist " "on your system"); #endif semid = init_sem(1); srand(time(NULL)); TEST_PAUSE; }
void init() { int i, j, k, pid, aux_semid; int string_length; int to_exec_length; char * to_exec; int ct_length; char * ct_path = get_ipc_path(); ct_length = strlen(ct_path); ipc_create(server_params); ipc_open(server_params, O_RDONLY); aux_semid = init_sem(); for(i = 0; i < CANT_INSTRUCTIONS; i++){ switch(pid = fork()){ case -1: perror("Fork exception"); exit(1); break; case 0: /* Hijo */ (*(process_list[i]))->pid = getpid(); to_exec_length = strlen(process_name_list[i]) + ct_length + 1; to_exec = calloc(1, to_exec_length); strcpy(to_exec, ct_path); strcat(to_exec, process_name_list[i]); to_exec[to_exec_length - 1] = 0; execvp(to_exec, NULL); /* No deberia llegar aca */ perror("Process error"); exit(1); break; } (*(process_list[i]))->params->unique_id = pid; (*process_list[i])->params->socklistener = TRUE; ipc_create((*process_list[i])->params); ipc_open((*(process_list[i]))->params, O_WRONLY); } free(ct_path); }
int main() { int sem_id = create_sem(1); init_sem(sem_id, 0, 1); pid_t id = fork(); if(id < 0) { perror("fork"); return -1; } else if(id == 0) {//child while(1) { sem_P(sem_id); printf("A"); fflush(stdout); usleep(20000); usleep(23456); printf("A"); sem_V(sem_id); } } else {//father while(1) { sem_P(sem_id); printf("B"); fflush(stdout); usleep(29633); usleep(23456); printf("B"); sem_V(sem_id); } } destroy_sem(sem_id); return 0; }
int main() { int i; pthread_t sm[3],prov; init_sem(); pthread_create(&prov,NULL,proveedor,NULL); for (i=0; i<3; i++) { pthread_create(&sm[i],NULL,smoke,(void*)(&i)); usleep(100);//just in case; } //recycling.... for (i=0; i<3; i++) { pthread_join(sm[i],NULL); } pthread_join(prov,NULL); return 0; }
static int rtl8139_open(struct dev *dev) { struct nic *tp = (struct nic *) dev->privdata; long ioaddr = tp->iobase; int rx_buf_len_idx; enable_irq(tp->irq); init_sem(&tp->tx_sem, NUM_TX_DESC); // The Rx ring allocation size is 2^N + delta, which is worst-case for // the kernel binary-buddy allocation. We allocate the Tx bounce buffers // at the same time to use some of the otherwise wasted space. // The delta of +16 is required for dribble-over because the receiver does // not wrap when the packet terminates just beyond the end of the ring rx_buf_len_idx = RX_BUF_LEN_IDX; do { tp->rx_buf_len = 8192 << rx_buf_len_idx; tp->rx_ring = alloc_pages_linear(PAGES(tp->rx_buf_len + 16 + (TX_BUF_SIZE * NUM_TX_DESC)), 'NIC'); } while (tp->rx_ring == NULL && --rx_buf_len_idx >= 0); if (tp->rx_ring == NULL) return -ENOMEM; tp->tx_bufs = tp->rx_ring + tp->rx_buf_len + 16; rtl8139_init_ring(dev); tp->full_duplex = tp->duplex_lock; tp->tx_flag = (TX_FIFO_THRESH << 11) & 0x003f0000; tp->rx_config = (RX_FIFO_THRESH << 13) | (rx_buf_len_idx << 11) | (RX_DMA_BURST << 8); rtl_hw_start(dev); //netif_start_tx_queue(dev); //kprintf("%s: rtl8139_open() ioaddr %#lx IRQ %d GP Pins %2.2x %s-duplex\n", // dev->name, ioaddr, tp->irq, inp(ioaddr + GPPinData), // tp->full_duplex ? "full" : "half"); // Set the timer to switch to check for link beat and perhaps switch // to an alternate media type init_timer(&tp->timer, rtl8139_timer, dev); mod_timer(&tp->timer, get_ticks() + 3*HZ); return 0; }
int main(void) { int semid = -1; int pid = -1; /*1.申请信号量 */ semid = semget(ftok(".", 'b'), 1, IPC_CREAT|0666); if(semid < 0) { perror("semget error"); exit(1); } /* 2.初始化信号量 */ init_sem(semid, 0, 1); //资源初始化成可用 /*创建子进程 */ if( (pid = fork()) <0) { perror("fork"); exit(1); } if(pid >0) { //父进程 sem_p(semid, 0, 1); //P操作 //操作资源 //... sem_v(semid, 0, 1); //V操作 } else { //子进程 sem_p(semid, 0, 1); //P操作 //操作资源 //... sem_v(semid, 0, 1); //V操作 //删除信号量 del_sem(semid); } return 0; }
int unsafethreadcounter(int nargs, char **args) { unsigned long num_threads = DEFAULT_THREADS; unsigned long num_inc = DEFAULT_INC; if (nargs > 1) num_threads = atoi(args[1]); if (nargs > 2) num_inc = atoi(args[2]); init_sem(); kprintf("Starting an unsafe thread counter (close your eyes kids!) with %lu threads...\n", num_threads); rununsafethreadcounter(num_threads, num_inc); kprintf("\nThread test done.\n"); kprintf("Counter: %lu (should be %lu)\n", counter, num_threads * num_inc); destroy_sem(); return 0; }
int main(){ init_sem(); int semval=0; semval=semctl(semid,0,GETVAL); printf("1、parent semval===%d\n",semval); if(fork()==0) { down(); semval=semctl(semid,0,GETVAL); printf("child semval===%d\n",semval); exit(0); } sleep(1); semval=semctl(semid,0,GETVAL); printf("2、parent semval===%d\n",semval); return 0; }