《exit函数实现》
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main (void )
{
pid_t ret;
printf("hello!\n");
printf("In parent process, pid =%d", getpid());
exit(0);
return 0;
}
结果:
hello!
In parent process, pid =3342
********************************************************
《_exit函数实现》
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main (void )
{
pid_t ret;
printf("hello!\n");
printf("22hello!\n");
printf("two hello!");
printf("In parent process, pid =%d", getpid());
_exit(0);
printf(" e next hello!\n");
printf(" e frok hello!");
return 0;
}
结果:
hello!
22hello!
********************************************************
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void exit_func1(void)
{
printf("exit_func1\n");
}
void exit_func2(void)
{
printf("exit_func2\n");
}
int main (void )
{
// pid_t ret;
printf("hello!\n");
atexit(exit_func1);
atexit(exit_func2);
printf("In parent process, pid =%d", getpid());
exit(0);
printf("newver be dispalyed\n");
return 0;
}
结果:
hello!
In parent process, pid =3554exit_func2
exit_func1
-----------
main主函数执行完毕后,是否可能会再执行一段代码?
如果需的话,可以用ateixt()函数注册一个函数:
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 void fumc1()
5 {printf("next\n");}
6
7 void fumc2()
8 {printf("executed");}
9 void fumc3()
10 {printf("is");}
11 void fumc4()
12 {printf("this");}
13
14 void main()
15 {
16 char str[]="0123456789";
17 atexit(fumc1);
18 atexit(fumc2);
19 atexit(fumc3);
20 atexit(fumc4);
21 printf("This is execued frist. \n");
22 printf("%d\n",sizeof(str));
23}
结果:
This is execued frist.
11
thisisexecutednext
本来到该结束程序printf("%d\n",sizeof(str));,
但是还打印thisisexecutednext
说明:该程序结束后,还运行atexit函数
而且到过来运行
****************************************************
《exec函数实现》
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main (void )
{ pid_t ret;
printf("hello!\n");
ret = fork();
printf("Have forked!\n");
if (ret < 0 ) {
perror("fork");
exit(1); }
if (ret == 0 ) { //子进程
//printf("In child process, pid=%d, ppid=%d\n", getpid(), getppid());
if (execl("/bin/ls", "ls", "-al", NULL) <0 ) {
perror("execl error!");
exit(1);
}
printf("Can't run to here in normal!\n");
// execlp("ls", "ls", "-al", NULL);
exit(0); }
sleep(5);
printf("In parent process, pid =%d, child pid=%d\n", getpid(), ret);
return 0;
}
结果:
hello!
Have forked!
Have forked!
total 108
drwxrwxrwx 2 nobody nogroup 4096 2011-12-07 18:06 .
drwxrwxrwx 9 lsb lsb 4096 2011-12-07 18:00 ..
-rwxr--r-- 1 nobody nogroup 380 2011-07-20 23:58 atexit.c
-rwxr-xr-x 1 lsb lsb 7861 2011-12-07 02:33 c
-rwxr--r-- 1 nobody nogroup 3527 2011-12-07 02:33 c.c
-rwxr-xr-x 1 lsb lsb 7350 2011-12-07 18:06 exec
-rwxr--r-- 1 nobody nogroup 601 2011-07-20 23:47 exec.c
-rwxr-xr-x 1 lsb lsb 7202 2011-12-07 18:04 exit
-rwxr--r-- 1 nobody nogroup 193 2011-07-20 23:51 _exit.c
-rwxr--r-- 1 lsb lsb 194 2011-12-07 18:04 exit.c
-rwxrwxrwx 1 root root 7350 2011-12-07 02:20 fork
-rwxrwxrwx 1 nobody nogroup 502 2011-07-20 23:40 fork.c
prw-r--r-- 1 lsb lsb 0 2011-12-07 02:30 in1
prw-r--r-- 1 lsb lsb 0 2011-12-07 02:30 in2
-rwxr-xr-x 1 lsb lsb 7508 2011-12-07 02:26 pipe
-rwxrwxrwx 1 nobody nogroup 1378 2008-12-19 03:17 pipe.c
-rwxr-xr-x 1 lsb lsb 7528 2011-12-07 02:30 pipe_select
-rwxr--r-- 1 nobody nogroup 3029 2008-12-19 03:17 pipe_select.c
-rwxr-xr-x 1 lsb lsb 12180 2011-12-07 02:35 s
-rwxr--r-- 1 nobody nogroup 4865 2011-12-07 02:32 s.c
In parent process, pid =3157, child pid=3158
************************************************************************