Esempio n. 1
0
void ctrrobot()
{
    if(readposition()==1)//co du lieu moi
    { 
		if(cmdCtrlRobot ==8)
		{
			speed(100,100);
			LED_L_ON;
		}
		else
		{
			speed(0,0);
			LED_L_OFF;
		}
	}
}
Esempio n. 2
0
int main(int argc, char **argv)
{
	/* pipe to parent, pipe to child */
	int p_prnt[2], p_chld[2];
	/* default filename to read, source code of this program */
	char *filename = "FILE1";
	/* filehandle of file we are reading */
	FILE *file;
	/* process 'id', just a string of parent or child */
	char *id;
	/* pid used when seeing the fork result */
	int pid;
	/* pipe used in this process, assigned after fork */
	int mypipe[2];
	/* position of file, read from other partner */
	off_t pos;
	/* stat info, so we don't fseek past the end */
	struct stat finfo;
 
	/* all using a separate file if a name is given */
	if(argc > 1)
	{
		filename = argv[1];
	}
 
	/* init the pipes */
	if(pipe(p_prnt) || pipe(p_chld))
	{
		perror("pipe");
		exit(1);
	}
 
	/* open the file */
	if((file = fopen(filename, "r")) == NULL)
	{
		fprintf(stderr, "Failed to open '%s': %s\n",
				filename, strerror(errno));
		exit(1);
	}
 
	/* get the stat/size of the file */
	if(fstat(fileno(file), &finfo))
	{
		perror("fstat");
		exit(1);
	}
 
	/* fork and assign 'id' and 'mypipe' */
	if((pid = fork()) < 0)
	{
		perror("fork");
		exit(1);
	}
	else if(pid == 0)
	{
		id = "CHLD";
		mypipe[0] = p_chld[0];
		mypipe[1] = p_prnt[1];
		/*
		 * the child should wait until the parent reads the first line
		 * and then position itself for the loop to begin with the second line
		 */
		pos = readposition(mypipe[0]);
		fseek(file, pos, SEEK_SET);
	}
	else
	{
		id = "PRNT";
		mypipe[0] = p_prnt[0];
		mypipe[1] = p_chld[1];
	}
 
	/*
	 * main loop to
	 * 	read the line
	 * 	write the position to the other partner
	 * 	wait for the position from the other partner
	 * 		(it will read the next line in the mean-time)
	 * 	seek to the new position
	 */
	while(pos < finfo.st_size)
	{
		/* read */
		readline(getpid(), file);
		/* send pos */
		writeposition(ftell(file), mypipe[1]);
		/* get next */
		pos = readposition(mypipe[0]);
		/* seek */
		fseek(file, pos, SEEK_SET);
	}
 
	/* get other side to close down */
	writeposition(ftell(file), mypipe[1]);
 
	/* clean up and exit */
	close(mypipe[0]);
	close(mypipe[1]);
	fclose(file);
	return(0);
}