#include <sys/types.h>
#include <unistd.h>
{
int var = 1;
pid_t pid;
printf("Before execute fork system call, g_val = %d,var = %d.\n",g_var,var);
{
printf("Creat a new process failed.\n");
return 1;
}
else if(pid==0)
{
g_var++;
var++;
printf("Process ID: %ld,g_var = %d,var =%d.\n",(long)getpid(),g_var,var);
_exit(0);
}
printf("Process ID: %ld,g_var = %d,var =%d.\n",(long)getpid(),g_var,var);
return 0;
}
修改以上代码,分别使用fork和vfork,执行结果如下:
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ ./syst
Process ID: 6006
Before execute fork system call, g_val = 0,var = 1.
Process ID: 6007,g_var = 1,var =2.
Process ID: 6006,g_var = 0,var =1.
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ gcc -o syst systemcall.c
gaolu@gaolu-desktop:~$ ./syst
Process ID: 5990
Before execute vfork system call, g_val = 0,var = 1.
Process ID: 5991,g_var = 1,var =2.
Process ID: 5990,g_var = 1,var =2.
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
#include <unistd.h>
{
if(argc <2)
{
printf("Usage: %s path.\n",argv[0]);
return 1;
}
execlp("/bin/ls","ls",argv[1],(char*)NULL); //支持变长参数,以NULL结尾
return 0;
}
gaolu@gaolu-desktop:~$ gcc -o syst2 systemcall2.c
gaolu@gaolu-desktop:~$ ./syst2
Usage: ./syst2 path.
gaolu@gaolu-desktop:~$ ./syst2 /home/gaolu
commen files music
data.dat~ picture
desktop process.c~
document program~
etcnetworkinterface syst
Examples syst2
fcitx-3.4.2 systemcall2.c
fcitx-install systemcall2.c~
file.c~ systemcall.c~
fileopera~ test.c~
file_operate.c ~ test.dat~
fork_file.c~ test_folder
gao.lu.c~ test_program
ls~ test_program (copy)
LumaQQ uid.c~
lumaqq_2005-linux_gtk2_x86_with_jre.tar.gz uidprint.c~
mnt video
module
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
--->execvp()
#include <unistd.h>
{
if(argc <2)
{
printf("Usage: %s arg list 1 2 3...\n",argv[0]);
return 1;
}
execvp(argv[1],&argv[1]);
return 0;
}
gaolu@gaolu-desktop:~$ ./syst3
Usage: ./syst3 arg list 1 2 3...
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ ./syst3 ls /home/gaolu
commen files music
data.dat~ picture
desktop process.c~
document program~
etcnetworkinterface syst
Examples syst2
fcitx-3.4.2 syst3
fcitx-install systemcall2.c
file.c~ systemcall2.c~
fileopera~ systemcall.c~
file_operate.c ~ test.c~
fork_file.c~ test.dat~
gao.lu.c~ test_folder
ls~ test_program
LumaQQ test_program (copy)
lumaqq_2005-linux_gtk2_x86_with_jre.tar.gz uid.c~
mnt uidprint.c~
module video
gaolu@gaolu-desktop:~$
#include <unistd.h>
#include <stdlib.h>
{
printf("You can see the output when the program teminates.\n");
}
{
int flag = 0;
flag = atexit(do_at_exit);
if(flag != 0)
{
printf("Can't set exit function.\n");
return EXIT_FAILURE;
}
exit(EXIT_SUCCESS);
执行结果:
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ gcc -o syst4 systemcall2.c
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ ./syst4
You can see the output when the program teminates. //执行了注册的回调函数
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
#include <unistd.h>
#include <stdlib.h>
{
printf("You can see the output when the program teminates.\n");
}
{
int flag = 0;
flag = atexit(do_at_exit);
if(flag != 0)
{
printf("Can't set exit function.\n");
return EXIT_FAILURE;
}
_exit(EXIT_SUCCESS);
gaolu@gaolu-desktop:~$ ./syst5 //返回shell之前不会执行do_at_exit()
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$