Пример #1
0
static int		player_turn(t_lemipc *lemipc)
{
  if (set_sem(lemipc, LOCK) == EXIT_FAILURE)
    return (EXIT_FAILURE);
  if ((int)lemipc->pos == -1 && put_player_on_map(lemipc) == EXIT_FAILURE)
    return (set_sem(lemipc, UNLOCK));
  if (lemipc->is_alone == TRUE)
    lemipc->is_alone = player_is_alone(lemipc, FALSE);
  else if (player_is_alone(lemipc, FALSE) == TRUE)
    {
      lemipc->is_dead = TRUE;
      lemipc->is_alone = TRUE;
    }
  if (lemipc->is_alone == FALSE)
    {
      if (lemipc->is_dead == FALSE)
	lemipc->is_dead = player_is_surrounded(lemipc);
      if (lemipc->is_dead == FALSE)
	move_algorithm(lemipc);
    }
  usleep(SLEEP);
  if (lemipc->killed_by != 0 && send_msg(lemipc) == EXIT_FAILURE)
    return (EXIT_FAILURE);
  recieve_msg(lemipc);
  return (set_sem(lemipc, UNLOCK));
}
Пример #2
0
int main(int argc, char **argv) {
	shmid = shmget( IPC_PRIVATE, sizeof( varijable ) * 100, 0600 );
	if( shmid == -1 ){
		printf( "Greska prilikom stvaranja zajednicke memorije\n" );
		exit(1);	
	}

	zajednicke = ( varijable * ) shmat( shmid, NULL, 0 );
	
	get_sem( 5, & (zajednicke->semid) );
	// postavljanje semafora
	set_sem( 5, semPUN, zajednicke->semid );
	set_sem( 0, semPRAZAN, zajednicke->semid );
	set_sem( 1, semPISI, zajednicke->semid );
	set_sem( 0, semGO1, zajednicke->semid );
	set_sem( 0, semGO2, zajednicke->semid );

	sigset( SIGINT, clean_exit );
	
	switch( fork() ){
		case -1:
			printf( "Greska prilikom stvaranja procesa!\n" );
			break;
		case 0:
			proizvodac( 0 );
			exit( 0 );
			break;
	}

	switch( fork() ){
		case -1:
			printf( "Greska prilikom stvaranja procesa!\n" );
			break;
		case 0:
			proizvodac( 1 );
			exit( 0 );
			break;
	}

	switch( fork() ){
		case -1:
			printf( "Greska prilikom stvaranja procesa!\n" );
			break;
		case 0:
			potrosac();
			exit( 0 );
			break;
	}
	

	wait( NULL );	wait( NULL );	wait( NULL );

	clean_exit( 0 );
	return 0;
}
Пример #3
0
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;
}
Пример #4
0
void vater()
{
    while (1)
    {
        sleep(20);

        if (deadlock_observer())
        {
            /* Zufaellige Freigabe eines der Autos */
            int freigabe = rand()%ANZAHL_AUTOS;

            printf("DEADLOCK - Freigabe fuer Auto %d\n", freigabe);
            set_sem(semid, ANZAHL_AUTOS+1, freigabe); 
        }
    }
}
Пример #5
0
void kind(int pos)
{
    enum STATUS state = HERANFAHREN;

    while (1)
    {
        /* Wagen steht an der Kreuzung */
        state = WARTEN;
        p(semid, pos);
        printf("Auto %d steht an der Kreuzung.\n", pos);

        /* Vergewissern, ob Strasse frei */
        do
        {
            sleep(3);
            printf("Auto %d wartet auf freie Fahrt.\n", pos);

            /* evtl Deadlock Entsperrung */
            if (get_sem(semid, ANZAHL_AUTOS+1) == pos)
                break;
        }
        while (get_sem(semid, (pos+1)%4) == 0 || get_sem(semid, ANZAHL_AUTOS) == 0);

        /* claimen der entsprechenden Strassenabschnitte */
        p(semid, ANZAHL_AUTOS);

        /* Ueberqueren der Strasse */
        state = FAHREN;
        printf("Auto %d ueberquert die Strasse.\n", pos);
        sleep(1);

        /* Zuruecksetzen der Deadlock Sperre, falls noetig */
        if (get_sem(semid, ANZAHL_AUTOS+1) == pos)
            set_sem(semid, ANZAHL_AUTOS+1, ANZAHL_AUTOS);

        /* Freigabe der entsprechenden Strassenabschnitte */
        v(semid, pos);
        v(semid, ANZAHL_AUTOS);
        printf("Auto %d hat die Strasse ueberquert.\n", pos);

        /* Zurueckkehren zur Kreuzung */
        state = HERANFAHREN;
        printf("Auto %d kehrt zur Kreuzung zurueck.\n", pos);
        sleep(3);
    }
}
Пример #6
0
static void rtl8139_tx_timeout(struct dev *dev) {
  struct nic *tp = (struct nic *) dev->privdata;
  long ioaddr = tp->iobase;
  int status = inpw(ioaddr + IntrStatus);
  int mii_reg;
  unsigned int i;

  kprintf("%s: Transmit timeout, status %2.2x %4.4x media %2.2x\n",
    dev->name, inp(ioaddr + ChipCmd), status, inp(ioaddr + GPPinData));

  if (status & (TxOK | RxOK)) {
    kprintf("%s: RTL8139 Interrupt line blocked, status %x\n", dev->name, status);
  }

  // Disable interrupts by clearing the interrupt mask
  outpw(ioaddr + IntrMask, 0x0000);

  // Emit info to figure out what went wrong
  kprintf("%s: Tx queue start entry %d  dirty entry %d\n", dev->name, tp->cur_tx, tp->dirty_tx);
  
  for (i = 0; i < NUM_TX_DESC; i++) {
    kprintf("%s:  Tx descriptor %d is %8.8x.%s\n",
      dev->name, i, inpd(ioaddr + TxStatus0 + i*4),
      i == tp->dirty_tx % NUM_TX_DESC ? " (queue head)" : "");
  }
  kprintf("%s: MII #%d registers are:", dev->name, tp->phys[0]);
  for (mii_reg = 0; mii_reg < 8; mii_reg++) kprintf(" %4.4x", mdio_read(dev, tp->phys[0], mii_reg));
  kprintf("\n");

  // Dump the unsent Tx packets
  for (i = 0; i < NUM_TX_DESC; i++) {
    if (tp->tx_pbuf[i]) {
      pbuf_free(tp->tx_pbuf[i]);
      tp->tx_pbuf[i] = NULL;
      tp->stats.tx_dropped++;
    }
  }

  // Reset chip
  rtl_hw_start(dev);

  // Clear tx ring
  tp->dirty_tx = tp->cur_tx = 0;
  set_sem(&tp->tx_sem, NUM_TX_DESC);
}
Пример #7
0
int main(int argc,char *argv[])
{
	int rate;
	if(argv[1] != NULL) rate = atoi(argv[1]);
	else rate = 3; 

	buff_key = 101;
	buff_num = 3; 
	cget_key = 103; 
	cget_num = 1; 
	shm_flg = IPC_CREAT | 0644; 

	buff_ptr = (char *)set_shm(buff_key,buff_num,shm_flg);

	cget_ptr = (int *)set_shm(cget_key,cget_num,shm_flg);

	prod_key = 201; 
	cmtx_key = 203; 
	c_PG_key = 301;
	c_TP_key = 302;
	c_TG_key = 303;
	sem_flg = IPC_CREAT | 0644; 

	sem_val = 1;

	prod_sem = set_sem(prod_key,sem_val,sem_flg);

	sem_val = 0;

	c_PG_sem = set_sem(c_PG_key,sem_val,sem_flg);
	c_TP_sem = set_sem(c_TP_key,sem_val,sem_flg);
	c_TG_sem = set_sem(c_TG_key,sem_val,sem_flg);

	sem_val = 1;

	cmtx_sem = set_sem(cmtx_key,sem_val,sem_flg);
	int pid1, pid2;
	pid1 = fork();
	if(pid1 == 0) {
	    while(1){
	        down(c_PG_sem);
	        down(cmtx_sem);

	        sleep(rate);
	        printf("%dsmoker has tobacco T, receive paper %c and glue %c, smoking……\n",
	            getpid(),buff_ptr[*cget_ptr + 1],buff_ptr[*cget_ptr + 2]);

	        up(cmtx_sem);
	        up(prod_sem);
	    }
	} else {
	    pid2 = fork();
	    if(pid2 == 0) {
	        while(1){
	            down(c_TP_sem);
	            down(cmtx_sem);

	            sleep(rate);
	            printf("%d smoker has glue G,receive paper %c and tobacco %c, smoking……\n",
	                getpid(),buff_ptr[*cget_ptr + 1],buff_ptr[*cget_ptr]);

	            up(cmtx_sem);
	            up(prod_sem);
	        }
	    } else {
	        while(1){
	            down(c_TG_sem);
	            down(cmtx_sem);
	            sleep(rate);
	            printf("%d smoker has paper P, receive glue %c and tobacco %c, smoking…\n",
	                getpid(),buff_ptr[*cget_ptr + 2],buff_ptr[*cget_ptr]);
	            up(cmtx_sem);
	            up(prod_sem);
	        }
	        }
	}
	return EXIT_SUCCESS;
}
Пример #8
0
static int serial_ioctl(struct dev *dev, int cmd, void *args, size_t size) {
  struct serial_port *sp = (struct serial_port *) dev->privdata;
  struct serial_status  *ss;

  switch (cmd) {
    case IOCTL_GETDEVSIZE:
      return 0;

    case IOCTL_GETBLKSIZE:
      return 1;

    case IOCTL_SERIAL_SETCONFIG:
      if (!args || size != sizeof(struct serial_config)) return -EINVAL;
      memcpy(&sp->cfg, args, sizeof(struct serial_config));
      serial_config(sp);
      return 0;

    case IOCTL_SERIAL_GETCONFIG:
      if (!args || size != sizeof(struct serial_config)) return -EINVAL;
      memcpy(args, &sp->cfg, sizeof(struct serial_config));
      return 0;

    case IOCTL_SERIAL_WAITEVENT:
      if (!args && size == 0) {
        return wait_for_object(&sp->event, INFINITE);
      } else if (args && size == 4) {
        return wait_for_object(&sp->event, *(unsigned int *) args);
      } else {
        return -EINVAL;
      }

    case IOCTL_SERIAL_STAT:
      if (!args || size != sizeof(struct serial_status)) return -EINVAL;
      ss = (struct serial_status *) args;
      ss->linestatus = sp->linestatus;
      sp->linestatus = 0;
      ss->modemstatus = inp((unsigned short) (sp->iobase + UART_MSR)) & 0xFF;
      ss->rx_queue_size = sp->rxq.count;
      ss->tx_queue_size = sp->txq.count;
      return 0;

    case IOCTL_SERIAL_DTR:
      if (!args || size != 4) return -EINVAL;
      
      if (*(int *) args) {
        sp->mcr |= MCR_DTR;
      } else {
        sp->mcr &= ~MCR_DTR;
      }

      outp(sp->iobase + UART_MCR, sp->mcr);
      return 0;

    case IOCTL_SERIAL_RTS:
      if (!args || size != 4) return -EINVAL;

      if (*(int *) args) {
        sp->mcr |= MCR_RTS;
      } else {
        sp->mcr &= ~MCR_RTS;
      }

      outp(sp->iobase + UART_MCR, sp->mcr);
      return 0;

    case IOCTL_SERIAL_FLUSH_TX_BUFFER:
      cli();
      fifo_clear(&sp->txq);
      set_sem(&sp->tx_sem, QUEUE_SIZE);
      sp->tx_queue_rel = 0;
      if (sp->type == UART_16550A) outp(sp->iobase + UART_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_TRIGGER_14);
      sti();
      return 0;

    case IOCTL_SERIAL_FLUSH_RX_BUFFER:
      cli();
      fifo_clear(&sp->rxq);
      set_sem(&sp->rx_sem, 0);
      sp->rx_queue_rel = 0;
      if (sp->type == UART_16550A) outp(sp->iobase + UART_FCR, FCR_ENABLE | FCR_RCV_RST | FCR_TRIGGER_14);
      sti();
      return 0;
  }
  
  return -ENOSYS;
}