#include
#include
#include
int main()
{
pid_t pid;
if((pid=fork())==-1)
perror("fork");
else if(pid==0)
{
printf("pid=%d,ppid=%d\n",getpid(),getppid());
sleep(2);
printf("pid=%d,ppid=%d\n",getpid(),getppid());
}
else
exit(0);
}
#include
#include
#include
int main()
{
pid_t pid;
if((pid=fork())==-1)
perror("fork");
else if(pid==0)
{
printf("child_pid pid=%d\n",getpid());
exit(0);
}
sleep(3);
system("ps");
exit(0);
}
进程的模式
在Linux系统中,进程的执行模式划分为用户模式和内核模式
进程的类型
按照进程的功能和运行的程序来分,进程划分为两大类:一类是系统进程,另一类是用户进程 。
#include
#include
int main(int argc,char *argv[])
{
printf("the current program's pid is %d\n",getpid());
return 0;
}
#include
#include
int main(int argc,char *argv[])
{
printf("the current program's ppid is %d\n",getppid());
return 0;
}
会话:一个或多个进程组的集合
函数:
控制终端
函数:
会话与进程组的特点
进程用户属性
pid_t new_pid;
new_pid = fork();
switch(new_pid)
{
case -1:break;
case 0:break;
default:break;
}
#include
#include
#include
int main(int argc,char *argv[])
{
pid_t pid;
if((pid=fork())==-1)
printf("fork error");
printf("bye!\n");
return 0;
}
#include
#include
#include
int main(void)
{
pid_t pid;
if((pid=fork())==-1)
printf("fork error");
else if(pid==0)
{
printf("in the child process\n");
}
else
{
printf("in the parent process\n");
}
return 0;
}
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
pid_t pid;
int fd;
int i=1;
int status;
char *ch1="hello";
char *ch2="world";
char *ch3="IN";
if((fd=open("test.txt",O_RDWR|O_CREAT,0644))==-1)
{
perror("parent open");
exit(EXIT_FAILURE);
}
if(write(fd,ch1,strlen(ch1))==-1)
{
perror("parent write");
exit(EXIT_FAILURE);
}
if((pid=fork())==-1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if(pid==0)
{
i=2;
printf("in child\n");
printf("i=%d\n",i);
if(write(fd,ch2,strlen(ch2))==-1)
perror("child write");
return 0;
}
else
{
sleep(1);
printf("in parent\n");
printf("i=%d\n",i);
if(write(fd,ch3,strlen(ch3))==-1)
perror("parent,write");
wait(&status);
return 0;
}
}
fork_example.c
#include
#include
#include
#include
#include
int glob=6;
int main()
{
int var;
pid_t pid;
var=88;
printf("in beginning:\tglob=%d\tvar=%d\n",glob,var);
if((pid=fork())<0)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if(pid==0)
{
printf("in child,modify the var:glob++,var++\n");
glob++;
var++;
printf("in child:\tglob=%d\tvar=%d\n",glob,var);
_exit(0);
}
else
{
printf("in parent:\tglob=%d\tvar=%d\n",glob,var);
return 0;
}
}
vfork_example.c
#include
#include
#include
#include
#include
int glob=6;
int main()
{
int var;
pid_t pid;
var=88;
printf("in beginning:\tglob=%d\tvar=%d\n",glob,var);
if((pid=vfork())<0)
{
perror("vfork");
exit(EXIT_FAILURE);
}
else if(pid==0)
{
printf("in child,modify the var:glob++,var++\n");
glob++;
var++;
printf("in child:\tglob=%d\tvar=%d\n",glob,var);
_exit(0);
}
else
{
printf("in parent:\tglob=%d\tvar=%d\n",glob,var);
return 0;
}
}
#include
#include
#include
void test()
{
pid_t pid;
pid=vfork();
if(pid==-1)
{
perror("vfork");
exit(EXIT_FAILURE);
}
else if(pid==0)
{
printf("1:child pid=%d,ppid=%d\n",getpid(),getppid());
return;
}
else
printf("2:parent pid=%d,ppid=%d\n",getpid(),getppid());
}
void fun()
{
int i;
int buf[100];
for(i=0;i<100;i++)
buf[i]=0;
printf("3:child pid=%d,ppid=%d\n",getpid(),getppid());
}
int main()
{
pid_t pid;
test();
fun();
}
#include
#include
#include
#include
main()
{
pid_t pid;
int status,i;
if(fork()==0)
{
printf("This is the child process .pid =%d\n",getpid());
exit(5);
}
else{
sleep(1);
printf("This is the parent process ,wait for child...\n");
pid=wait(&status);
i=WEXITSTATUS(status);
printf("child’s pid =%d .exit status=%d\n",pid,i);
}
}
on_exit
exit
举例:比较exit和_exit的区别
#include
int main(int argc,char **argv)
{
printf(“output\n”);
printf(“content in buffer”);
_exit(0);
//exit(0);
}
execl
#include
main()
{
execl(“/bin/ls”,”ls”,”-l”,”/etc/passwd”(char * )0);
}
execlp
#include
main()
{
execlp(“ls”,”ls”,”-l”,”/etc/passwd”,
(char * )0);
}
execv
#include
main(){
char * argv[ ]={“ls”,”l”,
”/etc/passwd”,(char*)0 };
execv(“/bin/ls”,argv);
}
execvp
#include
main(){
char * argv[ ] ={ “ls”,”l”,
”/etc/passwd”,0};
execvp(“ls”,argv);
}
execve
#include
main()
{
char * argv[ ]={“ls”,”l”,
”/etc/passwd”,(char *)0};
char * envp[ ]={“PATH=/bin”,0}
execve(“/bin/ls”,argv,envp);
}
fatherFork.c
#include
#include
int main()
{
int i;
for( i= 0; i< 3; i++)
{
int pid= fork();
if(pid== 0)
{
printf("son\n");
}
else
{
printf("father\n");
}
}
return 0;
}
waitpid_example.c
#include
#include
#include
#include
#include
#include
extern int errno;
int main(int argc,char *argv[])
{
pid_t pid_one,pid_wait;
int status;
if((pid_one=fork())==-1)
perror("fork");
if(pid_one==0)
{
printf("my pid is %d\n",getpid());
sleep(1);
exit(EXIT_SUCCESS);
}
pid_wait=waitpid(pid_one,&status,0);
if(WIFEXITED(status))
printf("wait on pid:%d,return value is:%4x\n",pid_wait,WEXITSTATUS(status));
else if(WIFSIGNALED(status))
printf("wait on pid:%d,return value is:%4x\n",pid_wait,WIFSIGNALED(status));
return 0;
}