file operations

Lees, Christian Christian.Lees at dsto.defence.gov.au
Mon Aug 14 01:35:35 CEST 2006


I'm having trouble writing to a file from within a soft realtime task.
When I open the file it reports a success, but returns a file descriptor
of 0.  When I try to write to the file, it fails with a bad file
descriptor error.  Has anyone had this sort of problem before, or am I
just doing something wrong.  I know that you cannot write files from
within a hard realtime task.  Following is the routine that writes the
file, it is sent messages through a mailbox from a hard reatime task
that handles the timing, separate messages for exit, open, close and
write.

TIA
  Christian

static void *shutter(void *arg) {

	char msg;
	struct gpsInfoT gpsInfo;
	char fileBuffer[sizeof(struct gpsInfoT) + sizeof(short)];
	struct stillInfo_t *stillInfo = (struct stillInfo_t *)arg;
	int res, photoFd = -1;
	
	printf("Shutter thread started\n");
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	if(!(stillInfo->shutterTask =
rt_task_init_schmod(nam2num("SHUTT"),3,0,0,SCHED_FIFO,1))) {
		printf("cannot init shutter task\n");
		exit(1);
	}

	for(;;) {
		rt_mbx_receive(stillInfo->mbxShutter, &msg, 1);
		if(msg == 0) {
			rt_mbx_send(stillInfo->mbxFile, &msg, 1);
			break;
		}
		if(msg == 1) {
			send_shutter();
			gpsGet(stillInfo->gpsSock, &gpsInfo);
			stillInfo->fileNumber++;
			memcpy(fileBuffer, &stillInfo->fileNumber, 2);
			memcpy(&fileBuffer[2], &gpsInfo,
sizeof(gpsInfo));

			res = write(photoFd, fileBuffer,
sizeof(fileBuffer));
			perror("file write");
			printf("W: %d:%d bytes, fd = %d\n",
sizeof(fileBuffer), res, photoFd);
		}
		if(msg == 2) {
			// open a new file
#define filename "test.dat"						
			photoFd = open(filename, O_CREAT | O_RDWR,
S_IRWXU);
			perror("open file");
			printf("open file : %s fd = %d\n", filename,
photoFd);
		}
		if(msg == 3) {
			// close the file
			close(photoFd);
			photoFd = -1;
		}
			
	}
	printf("Shutter task exiting\n");
	rt_make_soft_real_time();	
	rt_task_delete(stillInfo->shutterTask);
	return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.rtai.org/pipermail/rtai/attachments/20060814/4438f2a9/attachment.htm 


More information about the Rtai mailing list