现在的位置: 首页 > 综合 > 正文

Ubuntu下C语言实现以子进程号命名的文件及文件夹的新建、删除等操作。

2018年01月31日 ⁄ 综合 ⁄ 共 777字 ⁄ 字号 评论关闭
#include <sys/stat.h>
#include<stdio.h>
#include<sys/types.h>

void main()
{
	pid_t pid; 
	//创建新的进程 
    	pid=fork();
	int processID = getpid();
	char strID[10],filename[100],cmd[200];
	//如果返回0,则表示为子进程
	if(0 == pid)
        {
		sprintf(filename, "tmp/%d", processID);
		sprintf(cmd,"rm -r %s",filename);
		if(access(filename,0)==0)//使用access函数查看文件夹是否存在,存在则删除原文件夹
		{
			//printf("file exists!!!");			
			system(cmd);
		}
		if (mkdir(filename,0777))//用mkdir函数来创建新文件夹
		{
			printf("creat file bag failed!");
		}

		FILE *fp;
		
		sprintf(filename, "tmp/%d/hello.txt", processID);
		if((fp=fopen(filename,"w"))==NULL)//创建文件hello.txt
		{
			printf("creat file failed!\n");
		}

		fprintf(fp,"hello,world!");
		fclose(fp);
	}
}

程序说明:

使用fork()新建进程,并在子进程中作如下操作:

         1、查询当前目录下是否有以子进程ID命名的文件夹,如有则删除;

         2、新建以子进程ID为文件名的文件;

         3、在2中文件夹中新建hello.txt文件,并在其中写入一句话。

本程序为测试程序,仅为完成linux下文件夹的删除、新建,以及fork()的基本使用。


抱歉!评论已关闭.