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

📄 filedemo.c

📁 nucleus file NUCLEUS的文件系统源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/*  FUNCTION                           "task_3"                         */
/*                                                                      */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*                                                                      */
/*      This task creates it's own working directory and then creates   */
/*      5 files, writing 512 bytes to each file.  Then it renames       */
/*      all of the files using a wildcard.  It then sets an Event,      */
/*      to signal Task4 that it has completed this section.             */
/*      Now it waits for an Event from Task4 so it can continue.        */
/*      When it receives the Event, it changes it's working directory   */
/*      to Task4's working directory.  Then it reads a file that was    */
/*      created by Task4.  Finally, it deletes all the files created    */
/*      by Task4, and changes directory to it's own working             */
/*      directory.  All of these actions repeat in an endless loop.     */
/*                                                                      */
/*  AUTHOR                                                              */
/*                      Accelerated Technology                          */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      kernel                              Started by task_0.          */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*      bail_out                            Error reporting routine.    */
/*      NU_Become_File_User                 Registers this task as a    */
/*                                          user of the file system.    */
/*      NU_Close                            Close a file.               */
/*      NU_Delete                           Delete a file(s).           */
/*      NU_Make_Dir                         Create a new directory.     */
/*      NU_Open                             Open a file for access.     */
/*      NU_Open_Disk                        Open a disk for access.     */
/*      NU_Rename                           Rename a file               */
/*      NU_Set_Current_Dir                  Set default directory.      */
/*      NU_Set_Default_Drive                Select default drive.       */
/*      NU_Write                            Write data to a file.       */
/*                                          on the disk.                */
/*                                                                      */
/*  INPUTS                                                              */
/*       NONE.                                                          */
/*                                                                      */
/*  OUTPUTS                                                             */
/*       NONE                                                           */
/*                                                                      */
/************************************************************************/
void   task_3(UNSIGNED argc, VOID *argv)
{
int fd;                         /* File descriptor for file access */                   
int ii;                         /* Local loop variable */
STATUS status;                  /* Return value from funtion calls */
int task_id = TASK3;            /* Task number for this task */
CHAR fname[] = "T3FILE0.TXT";   /* Base file name */
UNSIGNED      event_group;      /* Local event group variable */


    /* Access argc and argv just to avoid compilation warnings.  */
    status = (STATUS)argc + (STATUS)argv;

    /* Each task must register as a Nucleus user */
    if (!NU_Become_File_User())
    {
        PRINTF("NU_Become_File_User error!");
        bail_out();
    }

    /* Open the ramdisk for access */
    status = NU_Open_Disk(DEMO_DISK); 

	/* Set default drive for future access */
    status = NU_Set_Default_Drive(DEMO_DRIVE);

    /* Create a working directory */
	status = NU_Make_Dir ("TASK3DIR");

	/* Set default to working directory */
	status = NU_Set_Current_Dir ("TASK3DIR");

    /* do forever loop */
    while (1)
	{
		/* Create 5 files in working directory */
		for ( ii = 0; ii < 5; ii++)
		{
			/* Make unique file name for each */
            fname[6] = 0x30 + ii;  /* ascii numbers 0-4 */

			/* Create File */
			fd = NU_Open(fname,(PO_TEXT|PO_RDWR|PO_CREAT|PO_TRUNC),
				               (PS_IWRITE | PS_IREAD));

            /* Write 512 bytes to the new file */
            status = NU_Write(fd, outbuf, 512);

			/* Check status for number of bytes written */
			if (status == 512)
			{
                Files_Created[task_id]++;  /* Increment Task3 Files Created */
			}
			else
			{
			    Errors[task_id]++;         /* Log error */
			}

			/* Close file */
 	        status = NU_Close(fd);

		} /* End For ( ii ) */


		/* Rename all files using wildcard */
		status = NU_Rename("*.TXT", "*.TK3");

		/* Check status for success */
		if( status == NU_SUCCESS)
		{
            Files_Renamed[task_id] += 5;  /* Increment Task3 Files Renamed */
		}
		else
		{
		    Errors[task_id]++;            /* Log error */
		}


        /* Set event that task_4 is waiting to receive */
        status =  NU_Set_Events(&Event_Group_0, TASK3_EVENT, NU_OR); 

		/* Wait for event from task_4 and consume it.  */
        status =  NU_Retrieve_Events(&Event_Group_0, TASK4_EVENT, NU_OR_CONSUME,
                                     &event_group, NU_SUSPEND);

        /* Set directory to Task4 directory */
	    status = NU_Set_Current_Dir ("..");
	    status = NU_Set_Current_Dir ("TASK4DIR");

        /* Open a file created by Task4 */
        fd = NU_Open("T4FILE0.TK4",(PO_TEXT|PO_RDWR),
                             (PS_IWRITE | PS_IREAD));

        /* Read the file */
        status = NU_Read(fd, inbuf, 512 );

		/* Check status for number of bytes read */
		if (status == 512)
		{
            Files_Read[task_id]++;  /* Increment Task3 Files Read */
		}
		else
		{
		    Errors[task_id]++;      /* Log error */
		}

        /* Close file */	 
        status = NU_Close(fd); 

		/* Delete all files */
		status = NU_Delete("*.TK4");
		if( status == NU_SUCCESS)
		{
            Files_Deleted[task_id] += 2;  /* Increment Task3 Files Deleted */
		}
		else
		{
			Errors[task_id]++;            /* Log error */
		}

	
        /* Set directory back to Task3 directory */
	    status = NU_Set_Current_Dir ("..");
	    status = NU_Set_Current_Dir ("TASK3DIR");

	
	} /* End While (1) */
	
	
}



/************************************************************************/
/*                                                                      */
/*  FUNCTION                           "task_4"                         */
/*                                                                      */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*                                                                      */
/*      This task creates it's own working directory and then           */
/*      it waits for an Event from Task3 so it can continue.            */
/*      When it receives the Event, it changes it's working directory   */
/*      to Task3's working directory.  Then it reads a file that was    */
/*      created by Task3 and then it deletes all the files created      */
/*      by Task3.  Now it changes directory to it's own working         */
/*      directory and creates 2 files, writing 512 bytes to each file.  */
/*      Then it renames all of the files using a wildcard.  It then     */
/*      sets an Event, to signal Task3 that it has completed this       */
/*      section. All of these actions repeat in an endless loop.        */
/*                                                                      */
/*  AUTHOR                                                              */
/*                      Accelerated Technology                          */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      kernel                              Started by task_0.          */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*      bail_out                            Error reporting routine.    */
/*      NU_Become_File_User                 Registers this task as a    */
/*                                          user of the file system.    */
/*      NU_Close                            Close a file.               */
/*      NU_Delete                           Delete a file(s).           */
/*      NU_Make_Dir                         Create a new directory.     */
/*      NU_Open                             Open a file for access.     */
/*      NU_Open_Disk                        Open a disk for access.     */
/*      NU_Rename                           Rename a file               */
/*      NU_Set_Current_Dir                  Set default directory.      */
/*      NU_Set_Default_Drive                Select default drive.       */
/*      NU_Write                            Write data to a file.       */
/*                                          on the disk.                */
/*                                                                      */
/*  INPUTS                                                              */
/*       NONE.                                                          */
/*                                                                      */
/*  OUTPUTS                                                             */
/*       NONE                                                           */
/*                                                                      */
/************************************************************************/
void   task_4(UNSIGNED argc, VOID *argv)
{
int fd;                         /* File descriptor for file access */                   
int ii;                         /* Local loop variable */
STATUS status;                  /* Return value from funtion calls */
int task_id = TASK4;            /* Task number for this task */
CHAR fname[] = "T4FILE0.TXT";   /* Base file name */
UNSIGNED      event_group;      /* Local event group variable */


    /* Access argc and argv just to avoid compilation warnings.  */
    status = (STATUS)argc + (STATUS)argv;

	/* Each task must register as a Nucleus user */
    if (!NU_Become_File_User())
    {
        PRINTF("NU_Become_File_User error!");
        bail_out();
    }

    /* Open the ramdisk for access */
    status = NU_Open_Disk(DEMO_DISK); 

    /* Open the ramdisk for access */
    status = NU_Set_Default_Drive(DEMO_DRIVE);

    /* Create a working directory */
	status = NU_Make_Dir ("TASK4DIR");

	/* Set default to working directory */
	status = NU_Set_Current_Dir ("TASK4DIR");

    /* do forever loop */
    while (1)
	{

		/* Wait for event from task_3 and consume it.  */
        status =  NU_Retrieve_Events(&Event_Group_0, TASK3_EVENT, NU_OR_CONSUME,
                                     &event_group, NU_SUSPEND);


        /* Set directory to Task3 directory */
	    status = NU_Set_Current_Dir ("..");
	    status = NU_Set_Current_Dir ("TASK3DIR");

        /* Open a file created by Task3 */
		fd = NU_Open("T3FILE0.TK3",(PO_TEXT|PO_RDWR),
                                   (PS_IWRITE | PS_IREAD));

        /* Read the file */
        status = NU_Read(fd, inbuf, 512 );

		/* Check status for number of bytes read */
		if (status == 512)
		{
            Files_Read[task_id]++;  /* Increment Task4 Files Read */
		}
		else
		{
		    Errors[task_id]++;      /* Log error */
		}

	    /* Close file */
        status = NU_Close(fd); 

        /* Delete all files */
		status = NU_Delete("*.TK3");

		/* Check status for success */
		if( status == NU_SUCCESS)
		{

⌨️ 快捷键说明

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