Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7723242
  • 博文数量: 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-10-27 21:17:57

#include

#include

#include

 

int main()

{

      pid_t pid;

      pid = fork();

      if(pid<0)

      {

           printf("fork error\n");

           exit(1);

      }

      if(pid==0)

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

      else

           printf("This is the parent process\n");     

          

      return 0;

}

 

 

#include

#include

#include

#include

 

int main()

{

      pid_t pid;

      pid = fork();

      int status,i=-1;

      if(pid<0)

      {

           printf("fork error\n");

           exit(1);

      }

      if(pid==0)

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

      else

      {

           sleep(2);

           printf("This is the parent process\n");     

           printf("Waiting for child process...\n");

           if(pid!=wait(&status))

           {

                 printf("wait error\n");

                 exit(1);

           }

           if(~WIFEXITED(status))

           {

                 i=WIFEXITED(status);         

           }

           printf("child is pid=%d\n",pid);

           printf("exit status=%d\n",i);

      }   

      return 0;

}

 

 

/*******************

进程创建过程中的写时拷贝

********************/

#include

#include

#include

#include

int i=0;

void child_process()

{

      printf("Child process %d,x=%d\n",getpid(),i);

      i++;

      printf("Child process %d,x=%d\n",getpid(),i);

}

int main()

{

      pid_t pid;

      pid = fork();

      int status;

      if(pid<0)

      {

           printf("fork error\n");

           exit(1);

      }

      if(pid==0)

           child_process();

      else

      {

           sleep(1);

           printf("Parent process %d,x=%d\n",getpid(),i); 

           printf("Waiting for child process...\n");

           if(pid!=wait(&status))

           {

                 printf("wait error\n");

                 exit(1);

           }

      }   

      return 0;

}

 

 

/*******************

exec函数执行Linux命令

********************/

#include

#include

#include

#include

 

int main()

{

      pid_t pid;

      pid = fork();

      int status;

      if(pid<0)

      {

           printf("fork error\n");

           exit(1);

      }

      if(pid==0)

           execlp("ps","ps","-o","pid,ppid,comm",NULL);

      else

      {

           sleep(1);

           printf("Parent process %d\n",getpid());  

           printf("Waiting for child process...\n");

           if(pid!=wait(&status))

           {

                 printf("wait error\n");

                 exit(1);

           }

      }   

      printf("Done!\n");

      return 0;

}

 

 

/***************************

*exec函数调用自定义的可执行文件*

****************************/

#include

#include

#include

 

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

{   

      int i=0;

      for(i=0;i

      {

           printf("arg%d: %s\n",i,argv[i]);

      }

      char **penv=__environ;

      while(penv && *penv)

      {

           printf("%s\n",*penv++);

      }   

      printf("Done!\n");

      return 0;

}

 

 

/***************************

*exec函数调用自定义的可执行文件*

****************************/

#include

#include

#include

#include

 

int main()

{

      pid_t pid;

      pid = vfork();

      int status;

      char *arrArg[]={"test","A","B","C",NULL};

      char *arrEnv[]={"env1=100","env2=yanyb",NULL};

      if(pid<0)

      {

           printf("fork error\n");

           exit(1);

      }

      if(pid==0)

           execve("./test",arrArg,arrEnv);

      else

      {

           sleep(1);

           printf("Parent process %d\n",getpid());  

           printf("Waiting for child process...\n");

           if(pid!=wait(&status))

           {

                 printf("wait error\n");

                 exit(1);

           }

      }   

      printf("Done!\n");

      return 0;

}

 

 

/*********************

*system创建一个新进程*

**********************/

#include

#include

 

int main()

{

      system("ps -o pid,ppid,comm");

      printf("Done!\n");

      return 0;

}

 

 

/*****************

*POPEN执行外部命令*

******************/

#include

#include

#define Size 1024

int main()

{

      char buf[Size];

      char *cmd="ps -f";

      FILE *fP;

      if((fp=popen(cmd,"r"))==NULL);

      {

           printf("popen error!\n");

           exit(1);

      }

      while(fgets(buf,sizeof(buf),fp))

      {

           printf("%s",buf);

      }

      pclose(fp);

      printf("Done!\n");

      return 0;

}

 

 

/*****************

*进程终止*

******************/

#include

#include

 

int main()

{

      printf("Done!\n");

      printf("ABC");

      _exit(0);

}

 

 

/*****************

*进程终止*

******************/

#include

#include

 

int main()

{

      printf("Done!\n");

      printf("ABC");

      exit(0);

}

 

 

/**********

获取进程信息

***********/

#include

#include

#include

#include

 

int main()

{

      pid_t pid;

      pid=fork();

      int status;

      if(pid<0)

      {

           printf("fork error\n");

           exit(1);

      }

      if(pid==0)

      {

           printf("Chile process \n");

           printf("pid=%d\n",getpid());

           printf("ppid=%d\n",getppid());

           printf("gid=%d\n",getpgrp());

           exit(1);

      }   

      else

      {

           printf("Parent process \n");   

           printf("pid=%d\n",getpid());

           printf("ppid=%d\n",getppid());

           printf("gid=%d\n",getpgrp());

           if(pid!=wait(&status))

           {

                 printf("wait error\n");

                 exit(1);

           }

      }   

      printf("Done!\n");

      return 0;

}

 

 

/**********

获取进程信息

***********/

#include

#include

#include

#include

#define maxfile 1024

int main()

{

      pid_t pid;

      pid=fork();

      int i;

      FILE *fp;

      char *buf="This is a Dameon\n";

     

      if(pid<0)

      {

           printf("fork error\n");

           exit(1);

      }

      if(pid>0)              

           exit(1);     

      else

      {

           setsid();

           for(i=0;i

                 close(i);

           chdir("/");

           umask(0);

           signal(SIGCHLD,SIG_IGN);

           while(1)

           {

                 fp=fopen("test","a");

                 if(fp==NULL)

                 {

                      perror("Open test failed");

                      exit(1);

                 }

                 fputs(buf,fp);

                 fclose(fp);

                 sleep(1);

           }        

      }   

      printf("Done!\n");

      return 0;

}

 

 

 

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

上一篇:Linux C编程一站式学习

下一篇:C 管道编程

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