⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 树大根深三棱尽所标杆ssfsfsa fdsafs
💻 C
📖 第 1 页 / 共 2 页
字号:
		readers = orig_readers;	tv.tv_sec = 1;		/* check once a second if we want to quit */	tv.tv_usec = 0;	selres = select(maxfd, &readers, NULL, NULL, &tv);	if (selres < 0) {	    perror("select");	    return;	}	if (selres > 0) {	    /* There's something ready to go. */	    int res;	    if (gdb_towrite >= 0 && FD_ISSET(gdb_towrite, &readers)) {		/* The CPU wants to write to the gdb port */		res = read(gdb_towrite, buf, FIFO_SIZE);		if (res > 0) {		    if (correct_write(gdb_fd, buf, res) <= 0) {			if (!strcmp(pilot->Debugging, "gdb")) {			    close(gdb_fd);			    attach_gdb(pilot->DebugArgs, &gdb_fd);			}		    }		}	    }	    if (gdb_fd >= 0 && FD_ISSET(gdb_fd, &readers)) {		/* There is data available on the gdb port */		int curhead = shptr->gdb.head;		int curtail = shptr->gdb.tail;		if (curtail >= curhead && 		    (curtail != FIFO_SIZE-1 || curhead != 0)) {		    res = read(gdb_fd,				shptr->gdb.fifo+curtail, FIFO_SIZE-curtail);		    if (res > 0) {			curtail += res;			if (curtail == FIFO_SIZE) curtail = 0;			shptr->gdb.tail = curtail;		    } else if (!strcmp(pilot->Debugging, "gdb")) {			close(gdb_fd);			attach_gdb(pilot->DebugArgs, &gdb_fd);		    }		} else if (curtail+1 < curhead) {		    res = read(gdb_fd,				shptr->gdb.fifo+curtail, curhead-curtail-1);		    if (res > 0) {			curtail += res;			if (curtail == FIFO_SIZE) curtail = 0;			shptr->gdb.tail = curtail;		    } else if (!strcmp(pilot->Debugging, "gdb")) {			close(gdb_fd);			attach_gdb(pilot->DebugArgs, &gdb_fd);		    }		}	    }	}    }}/********************************* * This is the serial I/O thread * *********************************/static struct termios saved_termio;static int saved_termio_fd = -1;void fix_termio(void) { tcsetattr(saved_termio_fd, TCSANOW, &saved_termio); }static void serial_proc(shared_img *shptr, Pilot *pilot){    fd_set	orig_readers;    fd_set	readers;    int		maxfd;    int		serial_towrite = pilot->serial_in;    int		serial_read = 0;    int		serial_write = 1;    int		serial_baud = 0;    int		serial_flags = 0;#ifndef DISABLE_AUTORUN    /* Start the CPU */    CPU_start(shptr);#endif    attach_serial(pilot->Serial, pilot->SerialPort, &serial_read,		                 &serial_write);    FD_ZERO(&orig_readers);    FD_SET(serial_towrite, &orig_readers);    FD_SET(serial_read, &orig_readers);    maxfd = 0;    if (serial_towrite > maxfd) maxfd = serial_towrite;    if (serial_read > maxfd) maxfd = serial_read;    maxfd++;    /*     * WARNING: This uses a shared memory data structure to store the FIFO. The     * consumer is getting things from this _at the same time_ as this is     * producing.  Examine custom.c and take a course in concurrent programming     * before modifying this.  :-) - Ian     */    while(shptr->CpuReq != cpuExit) {	struct timeval tv;	int selres;	unsigned char buf[FIFO_SIZE];		readers = orig_readers;	tv.tv_sec = 1;		/* check once a second if we want to quit */	tv.tv_usec = 0;	selres = select(maxfd, &readers, NULL, NULL, &tv);	if (selres < 0) {	    perror("select");	    return;	}	if (selres > 0) {	    /* There's something ready to go. */	    int res;	    if (serial_towrite >= 0 && FD_ISSET(serial_towrite, &readers)) {		/* The CPU wants to write to the serial port */                res = read(serial_towrite, buf, FIFO_SIZE);		if (res > 0) {			if (saved_termio_fd == -1) {				saved_termio_fd = serial_write;				tcgetattr(serial_write, &saved_termio);				atexit(fix_termio);			}		    /* Check the baud rate */		    if (serial_baud != shptr->serial_baud) {			set_baud(serial_write, shptr->serial_baud);			serial_baud = shptr->serial_baud;		    }		    if (serial_flags != shptr->serial_flags) {			set_flags(serial_write, shptr->serial_flags);			serial_flags = shptr->serial_flags;		    }		    if (correct_write(serial_write, buf, res) <= 0) {			attach_serial(pilot->Serial, pilot->SerialPort,			              &serial_read, &serial_write);		    }		}	    }	    if (serial_read >= 0 && FD_ISSET(serial_read, &readers)) {		/* There is data available on the serial port */		int curhead = shptr->serial.head;		int curtail = shptr->serial.tail;                 		if (curtail >= curhead &&		    (curtail != FIFO_SIZE-1 || curhead != 0)) {		    res = read(serial_read,				shptr->serial.fifo+curtail, FIFO_SIZE-curtail);		    if (res > 0) {			curtail += res;			if (curtail == FIFO_SIZE) curtail = 0;			shptr->serial.tail = curtail;		    } else {			attach_serial(pilot->Serial, pilot->SerialPort,			              &serial_read, &serial_write);		    }		} else if (curtail+1 < curhead) {		    res = read(serial_read,				shptr->serial.fifo+curtail, curhead-curtail-1);		    if (res > 0) {			curtail += res;			if (curtail == FIFO_SIZE) curtail = 0;			shptr->serial.tail = curtail;		    } else {			attach_serial(pilot->Serial, pilot->SerialPort,			              &serial_read, &serial_write);		    }		}	    }	}    }}/***************************************************************************** *			Pilot Thread stuff *****************************************************************************/typedef void PT_proc(shared_img *shptr, Pilot *pilot);static voidPT_create(Pilot *pilot, PT_proc *proc){	pid_t	pid;	/*	 * fork off a new thread	 */	if ((pid = fork()) < 0) {		perror("fork");		exit(1);	} else if (pid == 0) {		shared_img *shptr;		shptr = attach_shm(pilot);		proc(shptr, pilot);  		shmdt((char *)shptr);		exit(0);	} else {		pilot->threads++;	}}static voidPT_wait(Pilot *pilot){	/*	 * catch thread as they exit	 */	while (pilot->threads > 0) {		int	status;		if (wait(&status) < 0) {			if (errno == EINTR) {			} else if (errno == ECHILD) {				exit(-1); /* Hey, where did everyone go? */			} else {				exit(-1); /* not unexpected, what the hell? */			}		} else {			pilot->threads--;		}	}}static void expand_tilde(char **string){  if (*string[0] == '~') {    struct passwd *user_info;    char *new_dir;    char *end_of_name = strchr(*string, '/');    if (end_of_name == *string + 1 )      user_info = getpwuid(getuid());     else {      char *name;      int name_len;      name_len = end_of_name - &(*string)[0] - 1;      name = malloc(name_len + 1);      strncpy(name, &(*string)[1], name_len);      name[name_len] = '\0';      user_info = getpwnam(name);       free(name);    }    new_dir = malloc(strlen(user_info->pw_dir) + strlen(end_of_name + 1) + 2);    strcpy(new_dir, user_info->pw_dir);    strcat(new_dir, "/");    strcat(new_dir, end_of_name + 1);    *string = new_dir;  }}static void add_slash(char **string){  if ((*string)[strlen(*string) - 1] != '/') {    char *new_string = malloc(strlen(*string) + 2);    strcpy(new_string, *string);    strcat(new_string, "/");    *string = new_string;  }}/***************************************************************************** *                                                                           * *		     Main - Execution starts here                            * *                                                                           * *****************************************************************************/int init(Pilot *pilot){  struct stat buf;        //initially set pilot->threads to 0;  pilot->threads=0;                                                               expand_tilde(&pilot->DataDir);  add_slash(&pilot->DataDir);	printf("pilot %s\n", pilot->DataDir);  if (stat(pilot->DataDir, &buf) == -1) {    if (errno == ENOENT) {      if (mkdir(pilot->DataDir, S_IRWXU)) {        perror("Could not create rc directory");        exit(1);      }    }    else {        perror("Could not access rc directory");        exit(1);    }  }  /* Set the memory version */  pdebug_memversion = pilot->MemVersion;  /* Set up the pipes */  if (pipe(pilot->serial_pipefd) < 0) {    perror("pipe");    exit(1);  }  if (!strcmp(pilot->Debugging, "gdb")) {    if (pipe(pilot->gdb_pipefd) < 0) {      perror("pipe");      exit(1);    }  } else {    pilot->gdb_pipefd[0] = -1;	/* gdb_in */    pilot->gdb_pipefd[1] = -1;	/* gdb_out */  }  /*   * get a shared memory segment   */  pilot->shmid = shmget((key_t) 0, sizeof(shared_img), IPC_CREAT | 0777);  if (pilot->shmid < 0) {    perror("shmget");    exit(1);  } else {    shared_img *shptr = attach_shm(pilot);      /* Init the shm segment */    shptr->CpuReq = cpuNone;    shptr->run_updateisr = 0;    shptr->allowromwrites = 0;    shptr->pen = 0;    shptr->pendown = 0;    shptr->kbin = 0;    shptr->kbout = 0;    shptr->LcdReq = lcdNone;    shptr->gdb_writefd = pilot->gdb_out;    shptr->gdb.head = 0;    shptr->gdb.tail = 0;    shptr->serial_writefd = pilot->serial_out;    shptr->serial.head = 0;    shptr->serial.tail = 0;    shptr->serial_baud = 0;    shptr->serial_flags = 0;      /*     * Detach the segment before doing fork().  Some OS's have the     * child inherit the attached segments.     */    shmdt((char *)shptr);  }  /*   * Start display thread   * Start cpu thread   * Start debug thread   * Start serial thread   */  PT_create(pilot, lcd_proc);  PT_create(pilot, cpu_proc);  PT_create(pilot, debug_proc);  PT_create(pilot, gdb_proc);  PT_create(pilot, serial_proc);  PT_wait(pilot);	/* wait until all threads have exited */  /* Delete the shared mem */  shmctl(pilot->shmid, IPC_RMID, 0);  exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -