博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

愿逝者安息 让生者前行 深切哀悼5.12遇难同胞

愿逝者安息 让生者前行 深切哀悼5.12遇难同胞
creatory.cublog.cn


Member
使用fork创建进程
<sys/types.h>
<unistd.h>
pid_t fork(void);
例:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==(pid_t)(-1))
{
fprintf(stderr,"Fork:%s\n",strerror(errno));
exit(13);
}else if(pid==0)
{
printf("PID:%ld:Child started ,parent is :%ld\n",(long)getpid(),(long)getppid()));
}else
{
printf("PID:%ld:Started child PID:%ld\n",(long)getpid(),(long)pid);
}
sleep(1);
return 0;
}
僵进程(zombie)
#include XXXX
int main()
{
pid_t pid;
pid=fork();
if(pid==-1)
{
printf("error:%s\n",strerror(errno));
exit(1);
}else if(pid==0)
{
printf("PID:%ld:Child started,parent is %ld\n",(long)getpid(),(long)getppid());
return 0;  //child process just exit
}
//parent process
printf("PID:%ld started child pid %ld\n",(long)getpid(),(long)pid);
sleep(1);  //wait one second
//by this time,child process should have terminated
system("ps -l";
return 0;
}
wait函数将挂起调用进程,直到子进程已经终止或接收到了信号
<sys/types.h>
<sys/wait.h>
pid_t wait(int *status);
status接收子进程的终止状态信息
例:调用wait回收僵进程
#include "head.h"
int main()
{
pid_t pid,wpid;
int status;
pid=fork();
if(pid==-1)
exit(13);
else if(pid==0)
{
printf("PID %ld Child started,parent is %ld\n",(long)getpid(),(long)getppid());
return 0;
}
else
{
printf("PID %ld started child pid %ld\n",(long)getpid(),(long)pid);
wpid=wait(&status);
if(wpid==-1)
perror("wait");
system("ps -l");
return 0;
}
}
测试退出状态
WIFEXITED(status)进程正常退出,返回真,用WEXITSTATUS(status)得到退出码
WIFSIGNALED(status)进程接收一个并未捕获信号导致它终止,返回直,用WTERMSIG(status)提取信号序号,WCOREDUMP(status)测试是否建立内核文件,如果是返回真
WIFSTOPPED(status)进程停止返回真,用WSTOPSIG(status)提取信号的序号
例:进程正常退出,并显示它的错误代码
int status;
if(WIFEXITED(status))
{
printf("Exited with return code:%d\n",(int)WEXITSTATUS(status));
}
例:测试进程是异常终止还是等待信号
int status;
if(WIFSIGNALED(status))
printf("Terminated with signal:%d\n",(int)WTERMSIG(status));
if(WCOREDUMP(status))
printf("A core file was written\n");
等待特定的进程
pid_t waitpid(pid_t wpid,int *status,int options);
执行新程序
exec系列函数
extern char **environ; //当前环境信息
例:
使用exec启动ps命令的示例
#include XXXX
#define PS_PATH "/bin/ps"
extern char **environ; //外部环境指针
static void exec_ps_cmd(void)
{
static char *argv[]={"ps","-l",NULL};
execve(PS_PATH,argv,environ);
fprintf(stderr,"error:%s\n",strerror(errno)); //if display this line,then exec call has failed
}
int main()
{
pid_t pid,wpid;
int status;
pid=fork();
if(pid==-1)
exit(13);
else if(pid==0)
exec_ps_cmd(); //start the ps command
else
{
printf("PID :%ld,started child pid %ld\n",(long)getpid(),(long)pid);
wpid=wait(&status);
if(wpid==-1)
return 1;
esle if(wpid!=pid)
abort();
if(WIFEXITED(status))
printf("Exited:%d\n",WEXITSTATUS(status));
else if(WIFSIGNALED(status))
printf("Signal:%d %s\n",WTERMSIG(status),WCOREDUMP(status)?"with core file","");
else
puts("stopped child process");
}
return 0;
}
exec系列其他函数
#include <unistd.h>
int execl(const char *path,const char *arg,...);
int execlp(const char *file,const char *arg,...);
int execle(const char *path,const char *arg,...);
int exect(const char *path,char *const argv[],char *const envp[]);
int execv(const char *path,char *const argv[]);
int execvp(const char *file,char *const argv[]);
int execve(const char *path,char *const argv[],char *const envp[]);
说明:
path指定要执行的文件
file以shell搜索命令的方式搜索文件的PATH变量
argv用于指定参数,参数列表中的最后一个参数一定要用NULL
execlp("ps","ps","-l",NULL);
环境变量数组:
extern char **environ;
 

发表于: 2008-03-31 ,修改于: 2008-03-31 20:27,已浏览131次,有评论0条 推荐 投诉


网友评论

发表评论