Chinaunix首页 | 论坛 | 博客
  • 博客访问: 844738
  • 博文数量: 489
  • 博客积分: 475
  • 博客等级: 下士
  • 技术积分: 3087
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 16:28
文章分类

全部博文(489)

文章存档

2013年(7)

2012年(301)

2011年(181)

分类:

2011-12-22 20:54:22

原文地址:C 进程操作编程 作者:luozhiyong131

#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;

}

 

 

 

阅读(418) | 评论(0) | 转发(0) |
0

上一篇:C 管道编程

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

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