Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7565245
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2010-12-08 18:57:10

#include

#include

#include

#include

#include

 

void  create_file(char *filename)

{

    /*创建的文件具有可读可写的属性*/

    if(creat(filename,0666)<0)

      {

        printf("create file %s failure!\n",filename);

        exit(EXIT_FAILURE);

    }

      else

      {

        printf("create file %s success!\n",filename);

    }

}

 

int main(int argc,char *argv[])

{

    /*判断入参有没有传入文件名 */

         if(argc<2)

      {

        printf("you haven't input the filename,please try again!\n");

        exit(EXIT_FAILURE);

    }

      create_file(argv[1]);   

      exit(EXIT_SUCCESS);

}

 

 

#include

#include

#include

#include

#include

#include

#include

 

int main(void)

{

      pid_t child;

 

      /* 创建子进程 */

      if((child=vfork())==-1)

      {

           printf("Fork Error : %s\n", strerror(errno));

           exit(1);

      }

      else

           if(child==0) // 子进程

           {

                 sleep(1); //子进程睡眠一秒

                 printf("I am the child: %d\n", getpid());

                 exit(0);

           }

           else        //父进程

           {

                 printf("I am the father:%d\n",getpid());

                 exit(0);

           }

}   

 

 

#include

#include

#include

 

int main(int argc,char *argv[])

{

    /*判断入参有没有传入文件名*/

      if(argc<2)

      {

          perror("you haven,t input the filename,please try again!\n");

           exit(EXIT_FAILURE);

     

      }

      /*调用execl函数,用可执行程序file_creat替换本进程*/

      if(execl("./file_creat","file_creat",argv[1],NULL)<0)

           perror("execl error!");

 

}

 

 

#include

#include

#include

#include

#include

#include

#include

 

int main(void)

{

      pid_t child;

 

      /* 创建子进程 */

      if((child=fork())==-1)

      {

           printf("Fork Error : %s\n", strerror(errno));

           exit(1);

      }

      else

           if(child==0) // 子进程

           {

                 printf("the child process is run\n");

                 sleep(1);  //子进程睡眠一秒,但并没有去运行父进程

                 printf("I am the child: %d\n", getpid());

                 exit(0);

           }

           else        //父进程

           {

                 wait(NULL); //等到子进程退出,父进程才会运行

                 printf("the father process is run\n");

                 printf("I am the father:%d\n",getpid());

                 return 0;

           }

}   

 

 

阅读(1528) | 评论(0) | 转发(3) |
0

上一篇:时间编程

下一篇:无名管道

给主人留下些什么吧!~~