Beispiel #1
0
int main(int argc, char *argv[])
{
	struct sigaction act;
	int fd, ret;
	char buf[30];

	memset(&act, 0, sizeof(act));

	act.sa_flags = SA_RESTART;
	act.sa_handler = handler;
	if (sigaction(SIGINT, &act, NULL) < 0)	/* for CTRL-C */
		DEATH("sigaction");

	printf("Successfully installed signal handler for SIGINT\n");

	fd = open("./myfifo", O_RDWR);
	if (fd < 0)
		DEATH("OPEN:");

	ret = read(fd, buf, 30);
	if (ret < 0)
		perror("read:");

	printf("no of bytes read %d : data read %s \n", ret, buf);

	exit(EXIT_SUCCESS);
}
Beispiel #2
0
Datei: shm.c Projekt: coder03/ldd
void remove_it (void)
{
    int shmid;

    if ((shmid = shmget (KEY, SIZE, 0)) == -1)
        DEATH ("shmget");

    if (shmctl (shmid, IPC_RMID, 0))
        DEATH ("shmctl");

    printf ("Marked shared memory segment for deletion\n");

    exit (EXIT_SUCCESS);
}
Beispiel #3
0
int main()
{
    int semid, shmid, xfrs, bytes;
    struct shmseg *shmp;

    /* Get IDs for semaphore set and shared memory created by writer */

    semid = semget(SEM_KEY, 0, 0);
    if (semid == -1)
        DEATH ("semget");

    shmid  = shmget(SHM_KEY, 0, 0);
    if (shmid == -1)
        DEATH("shmget");

    /* Attach shared memory read-only, as we will only be reading */

    shmp = shmat(shmid, NULL, SHM_RDONLY);
    if (shmp == (void *) -1)
        DEATH ("shmat");

    /* Transfer blocks of data from shared memory to stdout */

    for (xfrs = 0, bytes = 0; ; xfrs++) {
        if (reserveSem(semid, READ_SEM) == -1)          /* Wait for our turn */
            DEATH ("reserveSem");

        if (shmp->cnt == 0)                     /* Writer encountered EOF */
            break;
        bytes += shmp->cnt;

        if (write(1, shmp->buf, shmp->cnt) != shmp->cnt)
            perror("partial/failed write");

        if (releaseSem(semid, WRITE_SEM) == -1)         /* Give writer a turn */
            DEATH("releaseSem");
    }

    if (shmdt(shmp) == -1)
        perror("shmdt");

    /* Give writer one more turn, so it can clean up */

    if (releaseSem(semid, WRITE_SEM) == -1)
        perror("releaseSem");

    fprintf(stderr, "Received %d bytes (%d xfrs)\n", bytes, xfrs);
    exit(0);
}
Beispiel #4
0
int main (int argc, char *argv[])
{
    int savesigs = 1;
    struct sigaction act;
    memset (&act, 0, sizeof (act));
    act.sa_handler = sig_int;
    if (sigaction (SIGINT, &act, NULL) < 0) /* for CTRL-C */
        DEATH ("sigaction");
    printf ("Successfully installed signal handler for SIGINT\n");
    printf ("returning from setjmp/longjmp, rc=%d\n",
            sigsetjmp (senv, savesigs));
    /* hit Ctl-C while the sleep is proceeding */
    sleep (10);
    DEATH ("returned to the wrong place!\n");
}
Beispiel #5
0
int main (int argc, char *argv[])
{
    struct sigaction act;
    struct my_s s;
    int j, sig = SIGALRM;
    union sigval sv;

    if (argc > 1)
        sig = atoi (argv[1]);

    memset (&act, 0, sizeof (act));
    act.sa_sigaction = sig_act;
    act.sa_flags = SA_SIGINFO;
    if (sigaction (sig, &act, NULL) < 0)  
        DEATH ("sigaction");

    printf ("pid=%d Successfully installed signal handler for signal=%d\n",
            getpid (), sig);

    for (j = 0; j < 3; j++) {
        printf ("This is a pointless message\n");
        s.x = j * 100;
        strcpy (s.s, "hello buddy");
        sv.sival_ptr = &s;
        printf ("sigqueue returns %d\n",sigqueue (getpid (), sig, sv));
        sleep (1);
    }
    exit (0);
}
int main(int argc, char *argv[])
{
	int fd, j;
	char *nodename = "/dev/mycdrv";
	int *interrupts = malloc(NB);
	if (argc > 1)
		nodename = argv[1];

	if ((fd = open(nodename, O_RDONLY)) < 0)
		DEATH("opening device node");
	if (read(fd, interrupts, NB) != NB)
		DEATH("reading interrupts");

	for (j = 0; j < MAXIRQS; j++)
		if (interrupts[j] > 0)
			printf(" %4d %10d\n", j, interrupts[j]);
	exit(0);
}
Beispiel #7
0
int main(int argc, char **argv)
{
	int fd, size, rc, j;
	char *area, *tmp, *nodename = "/dev/mycdrv";
	char c[2] = "CX";

	if (argc > 1)
		nodename = argv[1];

	size = getpagesize();	/* use one page by default */
	if (argc > 2)
		size = atoi(argv[2]);

	printf(" Memory Mapping Node: %s, of size %d bytes\n", nodename, size);

	if ((fd = open(nodename, O_RDWR)) < 0)
		DEATH("problems opening the node ");

	area = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	if (area == MAP_FAILED)
		DEATH("error mmaping");

	/* can close the file now */

	close(fd);

	/* put the string repeatedly in the file */

	tmp = area;
	for (j = 0; j < size - 1; j += 2, tmp += 2)
		memcpy(tmp, &c, 2);

	/* just cat out the file to see if it worked */

	rc = write(STDOUT_FILENO, area, size);

	if (rc != size)
		DEATH("problems writing");

	exit(EXIT_SUCCESS);
}
Beispiel #8
0
Datei: shm.c Projekt: coder03/ldd
void create_it (void)
{
    int shmid;
    void *shm_area;

    if ((shmid = shmget (KEY, SIZE, IPC_CREAT | 0666)) == -1)
        DEATH ("shmget");

    if ((shm_area = shmat (shmid, (void *)0, 0)) == (void *)-1)
        DEATH ("shmat");

    printf ("CREATE: Memory attached at %lX\n", (unsigned long)shm_area);

    if (shmdt (shm_area) == -1)
        DEATH ("shmdt");

    printf ("Shared Memory Region successfully created\n");

    exit (EXIT_SUCCESS);
}
Beispiel #9
0
Datei: shm.c Projekt: coder03/ldd
void send_it (void)
{
    int shmid, iflag = 1;
    void *shm_area;

    if ((shmid = shmget (KEY, SIZE, 0)) == -1)
        DEATH ("shmget");

    if ((shm_area = shmat (shmid, (void *)0, 0)) == (void *)-1)
        DEATH ("shmat");

    printf ("SEND: Memory attached at %lX\n", (unsigned long)shm_area);
    memcpy (shm_area, &iflag, 4);

    if (shmdt (shm_area) == -1)
        DEATH ("shmdt");

    printf ("SENDND has successfully completed\n");

    exit (EXIT_SUCCESS);
}
Beispiel #10
0
int main (int argc, char *argv[])
{
    int fd, bytes_read, i, start_addr = 0;
    unsigned char line[BYTES_PER_LINE];
    char *filename = FILE_NAME_DEFAULT;

    if (argc > 1)
        filename = argv[1];

    if (argc > 2)
        start_addr = atoi (argv[2]);

    printf
        ("\n **** DISPLAYING (IN HEX) FILE: %s at offset = %d (=0x%x) ****\n\n",
         filename, start_addr, start_addr);

    if ((fd = open (filename, O_RDONLY)) < 0)
        DEATH (filename);

    if (-1 == lseek (fd, start_addr, SEEK_SET))
        DEATH (filename);

    printf ("starting at 0x%x:\n", start_addr);

    do {
        if ((bytes_read = read (fd, line, BYTES_PER_LINE)) < 0)
            DEATH (filename);

        for (i = 0; i < bytes_read; i++)
            printf ("%2.2x ", line[i]);

        printf ("\n");
    }
    while (bytes_read > 0);

    close (fd);
    exit (0);
}
Beispiel #11
0
int main (int argc, char *argv[])
{
    char infile[128] = "./infile", outpipe[128] = "./outpipe";
    int rc, fd_in, fd_out, nbuf = INT_MAX;

    /* 1st arg = infilename        (./infile by default)
       2nd arg = outpipename       (./outpipe by default)
       3rd arg = size of chunks    ( infinite by default)
     */

    if (argc > 1)
        strcpy (infile, argv[1]);
    if (argc > 2)
        strcpy (outpipe, argv[2]);
    if (argc > 3)
        nbuf = atoi (argv[3]);

    printf ("Input file: %s,  Output pipe: %s\n", infile, outpipe);
    fflush (NULL);

    if ((fd_in = open (infile, O_RDONLY)) < 0)
        DEATH ("open fd_in");

    if ((fd_out = open (outpipe, O_WRONLY)) < 0)
        DEATH ("open fd_out");

    do {
        if ((rc = splice (fd_in, NULL, fd_out, NULL, nbuf, 0)) < 0)
            DEATH ("splice");
        printf ("Transferred %8d bytes\n", rc);
    } while (rc > 0);

    close (fd_in);
    close (fd_out);

    printf ("done\n");
    exit (EXIT_SUCCESS);
}
Beispiel #12
0
int main (int argc, char *argv[])
{
    struct rlimit rlim;
    pid_t pid = 0;
    int status = 0, nchildren = 3, i;

    /* Print out all limits */

    printf ("Printing out all limits for pid=%d:\n", getpid ());
    print_limits ();

    /* change and printout the limit for core file size */

    printf ("\nBefore Modification, this is RLIMIT_CORE:\n");
    do_limit (RLIMIT_CORE, "RLIMIT_CORE", &rlim);
    rlim.rlim_cur = 8 * 1024 * 1024;
    printf ("I forked off a child with pid = %d\n", (int)pid);

    setrlimit (RLIMIT_CORE, &rlim);
    printf ("\nAfter  Modification, this is RLIMIT_CORE:\n");
    do_limit (RLIMIT_CORE, "RLIMIT_CORE", &rlim);

    /* fork off the nchildren */

    fflush (stdout);
    for (i = 0; i < nchildren; i++) {
        pid = fork ();
        if (pid < 0)
            DEATH ("Failed in fork");
        if (pid == 0) {         /* any child */
            printf ("\nIn child pid= %d this is RLIMIT_CORE:\n",
                    (int)getpid ());
            do_limit (RLIMIT_CORE, "RLIMIT_CORE", &rlim);
            fflush (stdout);
            sleep (3);
            exit (0);
        }
    }

    while (pid > 0) {           /* parent */
        pid = wait (&status);
        printf ("Parent got return on pid=%dn\n", (int)pid);
    }

    printf (" **************************************************** \n");
    print_rusage (RUSAGE_SELF);
    print_rusage (RUSAGE_CHILDREN);

    exit (0);
}
Beispiel #13
0
Datei: shm.c Projekt: coder03/ldd
void receive_it (void)
{
    int shmid, iflag = 8;
    void *shm_area;

    if ((shmid = shmget (KEY, SIZE, 0)) == -1)
        DEATH ("shmget");

    if ((shm_area = shmat (shmid, (void *)0, 0)) == (void *)-1)
        DEATH ("shmat");

    printf ("RCV: Memory attached at %lX\n", (unsigned long)shm_area);

    printf ("iflag is now = %d\n", iflag);
    memcpy (&iflag, shm_area, 4);
    
    printf ("RCV has successfully completed\n");
    printf ("iflag is now = %d\n", iflag);

    if (shmdt (shm_area) == -1)
        DEATH ("shmdt");

    exit (EXIT_SUCCESS);
}
Beispiel #14
0
void					Health::impactDamage(Entity *e)
{
  if (al_get_time() - this->delayCounter_ < this->delay_)
    {
      this->delayCounter_ = al_get_time();
      return;
    }
  this->delayCounter_ = al_get_time();

  this->health_ -= DAMAGE(e)->getMagnitude();
  if (this->health_ <= 0 && this->entity->hasComponent(T_DEATH))
    {
      DEATH(this->entity)->kill();
    }
}
Beispiel #15
0
int main(int argc, char *argv[])
{
    pthread_t t1;
    void *res;
    int ret;

    ret = pthread_create(&t1, NULL, threadFunc, "Hello world\n");
    if (ret != 0)
        DEATH ("pthread_create");

    printf("Message from main()\n");

    ret = pthread_join(t1, &res);
    if (ret != 0)
        perror( "pthread_join");

    printf("Thread returned %ld\n", (long) res);

    exit(EXIT_SUCCESS);
}
Beispiel #16
0
int main (int argc, char *argv[])
{
    pid_t pid;

    printf ("\nThis is the parent, about to fork. pid=%d\n", getpid ());
    fflush (stdout);
    pid = fork ();

    if (pid > 0) {
        printf ("\nThis is the parent, after the fork. Child pid=%d\n", pid);
        sleep(10);
        printf ("\nThis is the parent waking up and exiting\n");
        exit (EXIT_SUCCESS);
    }
    if (pid == 0) {
        printf ("\nThis is the child, after the fork. pid=%d\n", getpid ());
        exit (EXIT_SUCCESS);
    }
    DEATH ("fork");
}
Beispiel #17
0
/**
 * Tiene la siguiente forma:
 * 1.Calcula aletoriamente un numero random que indica los nacimientos que se van a dar
 * 	en esta generacion, no es maximo a la mitdad de la poblacion, porque se requieren dos entities para reproducir
 * 2.selecciona aleatoriamente(con ventaja para los de fitness mas alto) a los padres del nuevo hijo, las veces que indique
 * 	el paso anterior
 * 3.Lo inserta en la poblacion
 * 4.Aumenta la edad de todos los individuos en 1.
 * 5.LLama a una verificacion de edad, donde los mayores a cierta edad tienen probabilidades cada vez mas altas de morir.
 */
void Population::DoGeneration(){
	//pthread_mutex_lock(&mutex);
	int newBorns = _Random->getRandomNumber(1+(Constants::REPRODUCTION_PER_GENERATION*_individuos->getLength()*0.5));

	for(int k=0; k < newBorns;k++){
		//La seleccion natural ocurre en las dos siguiente lineas.
		Entity* NewFather = randomSelectTheFittestFather();
		Entity* NewMother = randomSelectTheFittestMother();
		Entity* NewSon = _reproduction->reproducir(NewFather, NewMother);
		_individuos->insertTail(NewSon);
	}

	//cout << "GENERATION: " << CurrentGeneration <<"Population " << _individuos->getLength() <<endl;
	CurrentGeneration++;
	EverybodyBirthday();
	DEATH();

	//mejores(_individuos); //PRUEBA DE MEJORES INDIVIDUOS

	//_Fitness->setBase(_individuos);	//de nuevo fitness

	return;
}
Beispiel #18
0
int main()
{
	struct sigaction act;

	memset(&act, 0, sizeof(act));

	/* Install signal handlers */
	act.sa_handler = handler;
	if (sigaction(SIGALRM, &act, NULL) < 0)
		DEATH("sigaction");

	printf("Successfully installed signal handler\n");

	/* schedule alarm to be signalled after 5 seconds */
	alarm(5);

	/* Do something pointless, forever */
	for (;;) {
		printf("This is a pointless message.\n");
		sleep(1);
	}

	return 0;
}
Beispiel #19
0
void print_rusage (int who)
{
    struct rusage usage;
    if (getrusage (who, &usage))
        DEATH ("getrusage failed");

    if (who == RUSAGE_SELF)
        printf ("For RUSAGE_SELF\n");
    if (who == RUSAGE_CHILDREN)
        printf ("\nFor RUSAGE_CHILDREN\n");

    printf
        ("ru_utime.tv_sec, ru_utime.tv_usec = %4d  %4d (user time used)\n",
         (int)usage.ru_utime.tv_sec, (int)usage.ru_utime.tv_usec);
    printf
        ("ru_stime.tv_sec, ru_stime.tv_usec = %4d  %4d (system time used)\n",
         (int)usage.ru_stime.tv_sec, (int)usage.ru_stime.tv_usec);
    printf ("ru_maxrss =  %4ld (max resident set size)\n", usage.ru_maxrss);
    printf ("ru_ixrss =   %4ld (integral shared memory size)\n",
            usage.ru_ixrss);
    printf ("ru_idrss =   %4ld (integral unshared data size)\n",
            usage.ru_idrss);
    printf ("ru_isrss =   %4ld (integral unshared stack size)\n",
            usage.ru_isrss);
    printf ("ru_minflt =  %4ld (page reclaims)\n", usage.ru_minflt);
    printf ("ru_majflt =  %4ld (page faults)\n", usage.ru_majflt);
    printf ("ru_nswap =   %4ld (swaps)\n", usage.ru_nswap);
    printf ("ru_inblock = %4ld (block input operations)\n", usage.ru_inblock);
    printf ("ru_oublock = %4ld (block output operations)\n", usage.ru_oublock);
    printf ("ru_msgsnd =  %4ld (messages sent)\n", usage.ru_msgsnd);
    printf ("ru_msgrcv =  %4ld (messages received)\n", usage.ru_msgrcv);
    printf ("ru_nsignals= %4ld (signals received)\n", usage.ru_nsignals);
    printf ("ru_nvcsw=    %4ld (voluntary context switches)\n", usage.ru_nvcsw);
    printf ("ru_nivcsw=   %4ld (involuntary context switches)\n",
            usage.ru_nivcsw);
}
Beispiel #20
0
//////////////////////////////////////////////////////////////////////////***START OF PROGRAM***//////////////////////////////////////////////////////////////////////////////////////////////
int main(void){
	
//Initialization
	PLL_Init();  // Set the clocking to run at 80MHz from the PLL.
  LCD_Init();  // Initialize LCD
  LCD_Goto(10,0);
  LCD_SetTextColor(255,255,0); // yellow= red+green, no blue
  printf("Lab 10");
  LCD_DrawLine(10,16,310,16,BURNTORANGE);
	ADC_Initialize();
	Timer2_Init(7256);
	Timer2A_Stop();
	SysTick_Init();





	
///////////////////////////////////////////////////////////////////////////Set Up Waves///////////////////////////////////////////////////////////////////////////////////////////////
	while(1){
     	//set up for the wave 1 (11 means wave needs to be setup, 10 means wave is set up)
			if (wave==11) {  
				wavesize=5;
				enemycount=0;
				LCD_SetCursor(140,120);
				LCD_SetTextColor(255,0,0);
        LCD_PrintString("WAVE ONE");	
				LCD_SetTextColor(0,0,0);
				LCD_PrintString("WAVE ONE");
				 
				enemy[0].x=50;
				enemy[1].x=90;
				enemy[2].x=130;
				enemy[3].x=170;
				enemy[4].x=210;
				for (k=0;k<5;k++) {
					enemy[k].y=40;
					enemy[k].s=1;
					enemy[k].e=0;
					enemy[k].c=0;
					enemy[k].b=0;
					wave=10;
				}
			}
     //set up for wave 2 (22 means wave needs to be setup, 20 means wave is set up)
    	//e: so we don't print eraser more than once
			//s: so we know the state of the sprite
			//c; so we don't count the death of a sprite more than once
			
	if (wave==21) {  
        wavesize=15;				
				enemycount=0;
				LCD_SetCursor(140,120);
				LCD_SetTextColor(255,0,0);
        LCD_PrintString("WAVE TWO");	
				LCD_SetTextColor(0,0,0);
				LCD_PrintString("WAVE TWO"); 
				enemy[0].x=50;
				enemy[1].x=90;
				enemy[2].x=130;
				enemy[3].x=170;
				enemy[4].x=210;
				for (k=0;k<5;k++) {
					enemy[k].y=40;
					enemy[k].s=1;
					enemy[k].e=0;
					enemy[k].c=0;
					enemy[k].b=0;
	       
				}
				for (k=5;k<10;k++) {
					enemy[k].s=4;       //set newer coming enemies to 4, so they don't get counted as dead when they don't arrive yet (count happens after printing)
					enemy[k].e=0;
					enemy[k].y=40;
					enemy[k].x=50;
					enemy[k].c=0;
					enemy[k].b=0;
				}
				for (k=10;k<15;k++) {
					enemy[k].s=4;
					enemy[k].s=4;       //set newer coming enemies to 4, so they don't get counted as dead when they don't arrive yet (count happens after printing)
					enemy[k].e=0;
					enemy[k].y=64;
					enemy[k].x=296;
					enemy[k].c=0;
					enemy[k].b=0;
				}
				wave=20;
			}
		//set up for wave 3
		//set up for wave 3
			if(wave==31) {
				wavesize=1;
			  enemycount=0;
				LCD_SetCursor(140,30);
				LCD_SetTextColor(255,0,0);
        LCD_PrintString("Time For Ziping");	
				for(k=0;k<10;k++) {enemy[k].x=0;enemy[k].y=0;enemy[k].s=0;;enemy[k].e=0;}
				enemy[0].s=1;
				enemy[0].x=100;
				enemy[0].y=40;
				enemy[0].b=0;
				enemy[0].e=0;
				wave=30;
			}
						
						
						
 ////////////////////////////////////////////////////////////////////////////Update and Print User Ship/////////////////////////////////////////////////////////////////////////////////////////          
		if(Semaphore){																//wait for SysTick
			Semaphore = 0; 																	//"acknowledge" SysTick
			ADCData = ADC_In();
			Position=(ADCData*.4053751277)+157.0113976;			//position 0-2000
			xShip = ((Position)*.14)+20;									//x pixel coordinate of center of ship (.14 = 280/2000)
			LCD_DrawBMP(UshipErase,xShipTrail,200);					//erase previous image
			LCD_DrawBMP(userShip,xShip,200);
			xShipTrail = xShip;
			
			
			
			
			
////////////////////////////////////////////////////////////////////////////////////Update Enemies/////////////////////////////////////////////////////////////////////////////////////////////

			//enemy ship update coordinates for wave 1	 (s=1 means alive, s=0 means just dead, s=2 means the moment of death)
		  if (wave==10) {
			for (k=0;k<5;k++){
						if (enemy[k].s == 1) {
							enemy[k].y = enemy[k].y +1;
											trainWreckL = xShip-enemy[k].x;												//enemy within left boundary of ship
											trainWreckR = enemy[k].x - xShip;											//enemy within right boundary of ship
											
											if( (enemy[k].y >= 176) &&
													(((trainWreckL <24)&&(trainWreckL >0) )||((trainWreckR < 30)&&(trainWreckR > 0))) ){
														enemy[k].s = 2;
														DEATH();								
														userDeath++;
													}	
					  if (enemy[k].y>=210) {enemy[k].s=0;}
						} }
					}
			//enemy ship update coordinates for wave 2 
						//enemy ship update coordinates for wave 2 
			else if (wave==20) {
				for (k=0;k<5;k++) { if(enemy[k].s==1) {       //updates only if object is alive (s==1)
					if (countx<=92 && wave2==0) {
						enemy[k].x = enemy[k].x +1;
					}
					countx++;
					if (countx >92 && wave2==0) {wave2=1; countx=0;}
					if (wave2==1) {
						for (j=0;j<5;j++) {
							enemy[j].y=enemy[j].y+1;
							
						}
						wave2=2;  
						countx=0;
						county++;
					} 
					if (countx <=92 && wave2==2) {
						enemy[k].x= enemy[k].x-1;
					}
					countx++;
					if (countx >= 92 && wave2==2 ) {wave2=3; countx=0;}
					if (wave2==3) {
						for (j=0;j<5;j++) {
							enemy[j].y=enemy[j].y+1;
							
						}
						wave2=0; county++;
					}
					trainWreckL = xShip-enemy[j].x;												//enemy within left boundary of ship
					trainWreckR = enemy[j].x - xShip;											//enemy within right boundary of ship
					
					if( (enemy[j].y >= 176) &&
													(((trainWreckL <24)&&(trainWreckL >0) )||((trainWreckR < 30)&&(trainWreckR > 0))) ){
						enemy[j].s = 2;
						DEATH();								
						userDeath++;
					}	
					if (enemy[k].y>=210) {enemy[k].s=0;}
				}
			}
				//fleet 2
			if (county==24 || enemycount==10) {enemy[5].s=1; wave3=1;} //activate the bigger ships once there is room vertically, e.i. check if y coordinate has been met
			if (countx2==24) {enemy[6].s=1;}
			if (countx2==48 ) {enemy[7].s=1;}
			if (countx2==72) {enemy[8].s=1;}
			if (countx2==96) {enemy[9].s=1;}
	
			//if (enemy[5].x>=196) {wave2big=1;} //once the enemies get to the edge of the screen, set a flag (wave big) to one
			if (countx2>=146) {wave2big=1;}
	    for (k=5;k<10;k++) {
				if(wave3==1 && k==5) {countx2++;}	
				if(enemy[k].s==1 && wave2big==0) {  //update coordinates for new ships for wave 2 only if they become active (due to above code) and if max x coordinate hasn't been set
				enemy[k].x=enemy[k].x+1;	}
        	
			 if(enemy[k].y>=210) {enemy[k].s=0;} //enemy dead if off the screen vertically, we don't care if horizontally
			 if(enemy[k].s==1 && wave2big==1) {
				 if(k==5 || k==6) {enemy[k].x=enemy[k].x-1; enemy[k].y=enemy[k].y+1;}
				 if(k==8 || k==9) {enemy[k].x=enemy[k].x+1; enemy[k].y=enemy[k].y+1;}
				 if(k==7 && enemy[k].y<=100) {enemy[k].y=enemy[k].y+1;} 
			 }
			 
		 }
			 			 //fleet 3
			if (county==48 || enemycount==10) {enemy[10].s=1; wave4=1;} //activate the bigger ships once there is room vertically, e.i. check if y coordinate has been met
			if (countx3==24) {enemy[11].s=1;}
			if (countx3==48 ) {enemy[12].s=1;}
			if (countx3==72) {enemy[13].s=1;}
			if (countx3==96) {enemy[14].s=1;}
			 
				if (countx3>=146) {wave3big=1;}
	    for (k=10;k<15;k++) {
				if(wave4==1 && k==10) {countx3++;}	
				if(enemy[k].s==1 && wave3big==0) {  //update coordinates for new ships for wave 2 only if they become active (due to above code) and if max x coordinate hasn't been set
				enemy[k].x=enemy[k].x-1;	}
        	
			 if(enemy[k].y>=210) {enemy[k].s=0;} //enemy dead if off the screen vertically, we don't care if horizontally
			 if(enemy[k].s==1 && wave3big==1) {
         if(k==10) {if(enemy[k].y<120 && enemy[k].x==50) {enemy[k].y=enemy[k].y+1;}
				          if(enemy[k].y==120 && enemy[k].x<130) {enemy[k].x=enemy[k].x+1;}
									if(enemy[k].y>64 && enemy[k].x==130) {enemy[k].y=enemy[k].y-1;}
									if(enemy[k].y==64 && enemy[k].x>50) {enemy[k].x=enemy[k].x-1;}}
				
			   if(k==14) {if(enemy[k].y<120 && enemy[k].x==246) {enemy[k].y=enemy[k].y+1;}
				          if(enemy[k].y==120 && enemy[k].x>166) {enemy[k].x=enemy[k].x-1;}
									if(enemy[k].y>64 && enemy[k].x==166) {enemy[k].y=enemy[k].y-1;}
									if(enemy[k].y==64 && enemy[k].x<246) {enemy[k].x=enemy[k].x+1;}}
				 if(k==11 || k==13 || k==12) {enemy[k].y=enemy[k].y+1;}
			 }
									
			 } 
			 
			 
			 
			 
			
			
			
		}
		
	
										
										
	///////////////////////////////////////////////////////////////////////Update User missiles////////////////////////////////////////////////////////////////////////////////////////////////
			for(i=0;i<100;i++){														
					if(missile[i].s){
						  missile[i].y = missile[i].y - 2;							//if missile is active, move up the screen
							if(missile[i].y < 18 ){
									missile[i].s = 0;																				//if the missile has moved off the screen, deactivate it
									LCD_DrawBMP(UmissileErase,missile[i].x,missile[i].y);		//cover up with black
							      }
							
										
												
							
//////////////////////////////////////////////////////////////////Test for Enemy-Missile Collisions (small aliens)///////////////////////////////////////////////////////////////////////////////////////
										for(k=0;k<wavesize;k++){																																						      //(missile:11x15, enemy: 24x24)
										crashTestY = ((enemy[k].y + 24) - missile[i].y);											//difference in y-coordinates
										crashTestXR = (missile[i].x - enemy[k].x);													//distance of missile from right border of alien
										crashTestXL = (enemy[k].x - missile[i].x);													//distance of missile from left border of alien
										
											if   ( ( (crashTestY <5) && (crashTestY > 0) ) &&
														( (	(crashTestXR < 24) && (crashTestXR > 0 ) ) || ( (crashTestXL < 11)&&(crashTestXL > 0) ) ) ){
															enemy[k].s = 2;
														}
										
									 
					          }
					 }
			}
			
			
			
/////////////////////////////////////////////////////////////////////Test for Enemy-User Collisions///////////////////////////////////////////////////////////////////////////////////////////////////
									/*	for(k=0;k<5;k++){
											trainWreckL = xShip-enemy[k].x;												//enemy within left boundary of ship
											trainWreckR = enemy[k].x - xShip;											//enemy within right boundary of ship
											
											if( (enemy[k].y >= 176) &&
													(((trainWreckL <24)&&(trainWreckL >0) )||((trainWreckR < 30)&&(trainWreckR > 0))) ){
														enemy[k].s = 2;
														DEATH();								
														userDeath++;
													}		*/		
//////////////////////////////////////////////////////////////////////////Activate New Missile/////////////////////////////////////////////////////////////////////////////////////////////
					if(newMissile==3){
							while(missile[notActiveSlot].s == 1){												//find a non-active missile in the array
									if(notActiveSlot == 99){notActiveSlot = 0;}										//wrap around if at end of array
									notActiveSlot++;																							//check next slot
								}
							missile[notActiveSlot].x = xShip + 10;												//missile(11x15) aligned to center of ship
							missile[notActiveSlot].y = 185;																//missile firing out 200 - 15
							missile[notActiveSlot].s = 1;																//activate
							Timer2A_Start();																								//arm sound
							newMissile=0;                                                 //reset button count
								tester++;
							
					}
					
						 
						 
						
	///////////////////////////////////////////////////////Update Enemy Missiles/////////////////////////////////////////////////////////////////////////////////////////////////////////
					for(i=0;i<100;i++){
								if(emissile[i].s==1){
										emissile[i].y = emissile[i].y + 2;
										if(emissile[i].y > 210){
												emissile[i].s = 0;
												LCD_DrawBMP(UmissileErase,emissile[i].x,emissile[i].y);
										}
									}
								}
									

///////////////////////////////////////////////////////////////////////Test for User Missile Collisions////////////////////////////////////////////////////////////////////////////////////
					for(i=0;i<100;i++){
								if((emissile[i].s ==1)&&(emissile[i].y >= 185)&&
										((((emissile[i].x - xShip)<30)&&((emissile[i].x - xShip)>0))|| 
													(((xShip- emissile[i].x)<11)&&((xShip-emissile[i].x)>0)))){
										emissile[i].s = 0;
										DEATH();
										userDeath++;
							 }
					}


////////////////////////////////////////////////////////////////////////New Enemy Missiles////////////////////////////////////////////////////////////////////////////////////////////////////
//activate new enemy missiles
					for(k=0;k<wavesize;k++){
									if((enemy[k].s)&&(enemy[k].wait == 1)){																	//alive and finished (.wait == 1 means countdown done)
															while(emissile[notActiveSlotE].s == 1){												//find a non-active enemy missile slot in array
																	if(notActiveSlotE == 99){notActiveSlotE = 0;}							//wrap around if at end of array
																	else notActiveSlotE++;																			//check next slot
															 }
												emissile[notActiveSlotE].x = enemy[k].x + (enemy[k].w / 2);    //center horizontal positioning of missile on enemy
												emissile[notActiveSlotE].y = enemy[k].y + enemy[k].h;						//get correct vertical positioning for missile
												emissile[notActiveSlotE].s = 1;																	//activate missile
												enemy[k].wait = 0;																							//signifying missile fired, reaady for new wait time
									}
					}


//loading enemies that just fired with new wait times
					for(k=0;k<wavesize;k++){
									if((enemy[k].s)&&(enemy[k].wait == 0)){					//alive and not currently waiting to fire (.wait == 0 means need new time)
												j = (Random()%60);											//get random number from 0-30
												if(j < 20){j=20;}												//minimum # 20 (3 shots per second max firing)
												enemy[k].wait = j;
									}
					}

	

	/////////////////////////////////////////////////////////////////////enemy missile printing//////////////////////////////////////////////////////////////////////////////////////////////
			for(i=0;i<100;i++){
					if(emissile[i].s == 1){
						LCD_DrawBMP(enemyMissile,emissile[i].x,emissile[i].y);}}
					/*else if ((emissile[i].s == 0)&&(emissile[i].b==0)){
							LCD_DrawBMP(UmissileErase,emissile[i].x,emissile[i].y);}
							emissile[i].b = 1;
					}*/
				
	

						 
//////////////////////////////////////////////////////////////////////////////missile printing///////////////////////////////////////////////////////////////////////////////////////////
			for(i=0;i<100;i++){
				if(missile[i].s){
					LCD_DrawBMP(userMissile,missile[i].x,missile[i].y);							//print if missile is active
			   	}
				}


				
					

            
    
             
						
								
								
								

///////////////////////////////////////////////////////////////////////////enemy ship printing///////////////////////////////////////////////////////////////////////////////////////
/////////////ship printing for wave 1
				if(wave==10) {
			for (k=0;k<5;k++) {
				if (enemy[k].s ==1) {
			LCD_DrawBMP(AlienEnemyBig, enemy[k].x,enemy[k].y); }
				if (enemy[k].s ==2 &&enemy[k].b ==0) {
			LCD_DrawBMP(Explosion1, enemy[k].x,(enemy[k].y)); LCD_DrawBMP (Explosion1, enemy[k].x, enemy[k].y);
			LCD_DrawBMP(Explosion1, enemy[k].x,(enemy[k].y)); LCD_DrawBMP (Explosion1,enemy[k].x, enemy[k].y);
      enemy[k].s=0;		enemy[k].b=1;
				} 
				if (enemy[k].s ==0 && enemy[k].e ==0) {
			LCD_DrawBMP(ExplosionBlack, enemy[k].x,(enemy[k].y));
				enemy[k].e=1;}
		}


			 for (k=0;k<5;k++) {
				if(enemy[k].s==0 && enemy[k].c==0) {enemycount++; enemy[k].c=1;} //count enemies if s=0 and c=0
			}
			if (enemycount==5) {wave=21;} //new wave is all five enemies are "dead"
		} 
	
/////////////Ship printing for wave 2
				if(wave==20) {
			for (k=0;k<5;k++) {
				if (enemy[k].s ==1) {
			LCD_DrawBMP(AlienEnemySmall, enemy[k].x,enemy[k].y); }
				if (enemy[k].s ==2 && enemy[k].b ==0) {
			LCD_DrawBMP(Explosion1, enemy[k].x,(enemy[k].y)); LCD_DrawBMP (Explosion1,enemy[k].x, enemy[k].y);
			LCD_DrawBMP(Explosion1, enemy[k].x,(enemy[k].y)); LCD_DrawBMP (Explosion1,enemy[k].x, enemy[k].y);
      enemy[k].s=0;		enemy[k].b=1;
				} 
				if (enemy[k].s ==0 && enemy[k].e==0) {                               //once explosion eraser is printed, set the s to zero and e to one
			LCD_DrawBMP(ExplosionBlack, enemy[k].x,(enemy[k].y));                     
				enemy[k].e=1;}
		}
			for(k=5;k<10;k++) {
				if(enemy[k].s==1) {
			 LCD_DrawBMP(AlienEnemyBig, enemy[k].x,enemy[k].y); }
	      if (enemy[k].s ==2 && enemy[k].b ==0) {
			LCD_DrawBMP(Explosion1, enemy[k].x,(enemy[k].y)); LCD_DrawBMP (Explosion1,enemy[k].x, enemy[k].y);
			LCD_DrawBMP(Explosion1, enemy[k].x,(enemy[k].y)); LCD_DrawBMP (Explosion1,enemy[k].x, enemy[k].y);
      enemy[k].s=0;		enemy[k].b=1;
				} 
				if (enemy[k].s ==0 && enemy[k].e==0) {                               //once explosion eraser is printed, set the s to zero and e to one
			LCD_DrawBMP(ExplosionBlack, enemy[k].x, enemy[k].y);                     
				enemy[k].e=1;}}
				
	

			for (k=0;k<10;k++) { 
				if(enemy[k].s==0 && enemy[k].c==0) {enemycount++; enemy[k].c=1;} //count enemy if s=0 and c=0
			}
			
			
			if (enemycount==20) {wave=31;}  //new wave if all ten enemies are "dead"
		
				
		
					for(k=10;k<15;k++) {
				if(enemy[k].s==1) {
			 LCD_DrawBMP(AlienEnemyBig, enemy[k].x,enemy[k].y); }
	      if (enemy[k].s ==2 && enemy[k].b ==0) {
			LCD_DrawBMP(Explosion1, enemy[k].x,(enemy[k].y)); LCD_DrawBMP (Explosion1,enemy[k].x, enemy[k].y);
			LCD_DrawBMP(Explosion1, enemy[k].x,(enemy[k].y)); LCD_DrawBMP (Explosion1,enemy[k].x, enemy[k].y);
      enemy[k].s=0;		enemy[k].b=1;
				} 
				if (enemy[k].s ==0 && enemy[k].e==0) {                               //once explosion eraser is printed, set the s to zero and e to one
			LCD_DrawBMP(ExplosionBlack, enemy[k].x, enemy[k].y);                     
				enemy[k].e=1;}} }
				
			}					//close semaphore
	}								//close while loop
} 							//close main
Beispiel #21
0
int main (int argc, char *argv[])
{
    int sd, ci, n, backlog = MAX_BACKLOG;
    short port = PORTNUM;

    if (argc > 1)
        ncons = atoi (argv[1]);
    if (ncons > FD_SETSIZE)
        DEATH ("Can't have more than 1024 for select(), and ulimit -s"
               "  must be a bit larger")

            cd = malloc (ncons * sizeof (int));
    isopen = malloc (ncons * sizeof (int));
    gethostname (hostname, 128);

    /* open an internet tcp stream socket */
    /* socket, setsockopt for reuse, bind, listen; */

    sd = get_socket (port, backlog);

    for (ci = 0; ci < ncons; ci++)
        isopen[ci] = 0;

    FD_ZERO (&fdset);
    FD_SET (sd, &fdset);

    for (;;) {

        /* wait for something to happen on one of the descriptors */

        testset = fdset;
        if (sd > fdmax)
            fdmax = sd;         /* to begin with */
        n = select (fdmax + 1, &testset, NULL, NULL, NULL);

        if (n < 0)
            DEATH ("select");

        /* if you do a time out you have to deal with n = 0 */

        /* accept new connection only if less than the maximum is there */

        if (FD_ISSET (sd, &testset) && (nopen < ncons)) {

            /*  find the first open one */

            ci = accept_one (sd, isopen, cd, ncons);
            isopen[ci] = 1;
            FD_SET (cd[ci], &fdset);
            nopen++;
            if (cd[ci] > fdmax)
                fdmax = cd[ci];
            printf
                ("connection accepted (cd[%2d] = %2d), nopen = %2d\n",
                 ci, cd[ci], nopen);

        }
        /* service existing connections */

        for (ci = 0; ci < ncons; ci++) {
            if (isopen[ci] && FD_ISSET (cd[ci], &testset))
                if (handle_client (cd[ci]))
                    terminate_client (ci);
        }
    }

    close (sd);
    free (cd);
    free (isopen);
    exit (EXIT_SUCCESS);
}
Beispiel #22
0
int main (int argc, char *argv[])
{
    int sd, ci, n, backlog = MAX_BACKLOG, timeout = -1, nfds, nconsp1;
    short port = PORTNUM;
    struct pollfd *pfd, *pfd0;

    if (argc > 1)
        ncons = atoi (argv[1]);
    nconsp1 = ncons + 1;

    cd = malloc ((nconsp1 + 8) * sizeof (int));
    isopen = malloc ((nconsp1 + 8) * sizeof (int));
    ufds = malloc ((nconsp1 + 8) * sizeof (struct pollfd));
    memset (ufds, 0, (nconsp1 + 8) * sizeof (struct pollfd));

    /* increase maximum number of file descriptors, must be root! */

    /* a few extra, for 0, 1, 2 etc. */
    check_and_set_max_fd (nconsp1 + 8);

    gethostname (hostname, 128);

    /* open an internet tcp stream socket */
    /* socket, setsockopt for reuse, bind, listen; */
    sd = get_socket (port, backlog);

    /* for the listening socket */

    pfd0 = &ufds[0];
    pfd0->fd = sd;
    pfd0->events = POLLIN;

    isopen[0] = 0;

    for (ci = 1; ci < nconsp1 + 8; ci++) {
        pfd = &ufds[ci];
        pfd->fd = -1;
        pfd->events = 0;
        isopen[ci] = 0;
    }

    for (;;) {

        /* wait for something to happen on one of the descriptors */

        nfds = 1 + nopen;
        if (sd > fdmax)
            fdmax = sd;         /* to begin with */

        /*        n = poll (ufds, nconsp1, timeout);  */
        n = poll (&ufds[0], fdmax + 1, timeout);

        if (n < 0)
            DEATH ("poll");

        /* if you do a time out you have to deal with n = 0 */

        /* accept new connection only if less than the maximum is there */

        if ((pfd0->revents && POLLIN) && (nopen < ncons)) {
            isopen[0] = 1;

            /*  find the first open one */

            ci = accept_one (sd, isopen, cd, ncons);
            isopen[ci] = 1;
            pfd = &ufds[ci];
            pfd->fd = cd[ci];
            pfd->events = POLLIN;
            nopen++;
            if (cd[ci] > fdmax)
                fdmax = cd[ci];
            printf
            ("connection accepted (cd[%2d] = %2d), nopen = %2d\n",
             ci, cd[ci], nopen);

        }
        /* service existing connections */

        for (ci = 1; ci < nconsp1; ci++) {
            pfd = &ufds[ci];
            if (isopen[ci] && (pfd->revents && POLLIN)) {
                if (handle_client (cd[ci]))
                    terminate_client (ci);
            }
            fflush (stdout);
        }
    }
    close (sd);
    free (cd);
    free (isopen);
    exit (EXIT_SUCCESS);
}
Beispiel #23
0
int main (int argc, char *argv[])
{
    int sd, ci, backlog = MAX_BACKLOG;
    short port = PORTNUM;

    if (argc > 1)
        ncons = atoi (argv[1]);

    /* decrease stack size, non-privilged operation */
    /*    shrink_stack (STACK_SIZE); not needed for parent */

    /* increase maximum number of file descriptors, must be root! */
    /* a few extra, for 0, 1, 2 etc. */
    check_and_set_max_fd (ncons + 8);

    cd = malloc (ncons * sizeof (int));
    isopen = malloc (ncons * sizeof (int));
    pid = malloc (ncons * sizeof (pid_t));
    gethostname (hostname, 128);

    signal (SIGCHLD, cleanupkids);

    /* open an internet tcp stream socket */
    /* socket, setsockopt for reuse, bind, listen; */

    sd = get_socket (port, backlog);

    for (ci = 0; ci < ncons; ci++)
        isopen[ci] = 0;

    for (;;) {

        /* accept new connection only if less than
           the maximum is there */
        while (nopen == ncons) {
            /* reap any children; there may be a
               race condition or signal pileup! */
            cleanupkids (SIGCHLD);
        }

        /*  find the first open one */

        ci = accept_one (sd, isopen, cd, ncons);
        isopen[ci] = 1;
        nopen++;
        printf ("connection accepted (cd[%2d] = %2d), nopen = %2d\n",
                ci, cd[ci], nopen);
        fflush (stdout);

        /* fork off a child to handle the connection */

        pid[ci] = fork ();
        if (pid[ci] < 0)
            DEATH ("Forking");

        if (pid[ci] == 0) {     /* child */

            /* decrease stack size, non-privilged operation */
            shrink_stack (STACK_SIZE);
            /* can set back for kids */
            set_max_fd (16);

            while (!handle_client (cd[ci])) ;
            terminate_client (ci);
        } else {                /* parent */
            printf (" I forked for ci=%d, pid=%d\n", ci, pid[ci]);
            fflush (stdout);
        }
    }

    close (sd);
    free (isopen);
    free (cd);
    exit (EXIT_SUCCESS);
}