全部博文(489)
分类:
2011-12-22 20:51:41
原文地址:进程编程 作者:luozhiyong131
#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;
}
}