C 进程操作编程
时间:2010-10-27 来源:luozhiyong131
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv[])
{
int i=0;
for(i=0;i<argc;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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
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 <stdio.h>
#include <stdlib.h>
int main()
{
system("ps -o pid,ppid,comm");
printf("Done!\n");
return 0;
}
/*****************
*POPEN执行外部命令*
******************/
#include <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
int main()
{
printf("Done!\n");
printf("ABC");
_exit(0);
}
/*****************
*进程终止*
******************/
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Done!\n");
printf("ABC");
exit(0);
}
/**********
获取进程信息
***********/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#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<maxfile;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;
}