#include
#include
#include
int main()
{
execve("/bin/ls",NULL,NULL);
printf("test!\n");
perror("execve failed!");
return 0
;
}
output:
# ./execve
execve execve.c
从上面的输出可以发现,执行execve语句之后,如果执行成功,则跳转到第三方程序,不再返回。如果我们将"/bin/ls"故意写作,重新编译执行,输出为:
# ./execve
test!
execve failed!: No such file or directory
失败原因放在errno中。
#include
#include
#include
int main()
{
pid_t child ;
int ans ;
printf("1 copy 2 no copy\n");
scanf("%d",&ans);
if(ans ==
1)
{
child = fork();
if(child == -1)
{
perror("fork
error");
exit(1);
}
}
if(child == 0)
{
puts("it is child.....");
printf("child pid = %d\n",getpid());
printf("parent pid = %d\n\n\n",getppid());
exit(0);
}
else
{
puts("it is parent.....");
printf("child pid = %d\n",getpid());
printf("parent pid = %d\n\n\n",getppid());
exit(0);
}
return 0 ;
}
#include
#include
int main()
{
int myret
;
printf("return value of system is %d\n",myret =
system("ls -l"));
printf("the main process is still alive!\n");
return 0
;
}
output:
# ./system
total 40
-rwxr-xr-x 1 root root 5040 2010-12-14 17:30 execve
-rw-r--r-- 1 root root 179 2010-12-14 17:30
execve.c
-rwxr-xr-x 1 root root 5084 2010-12-14 17:40 system
-rw-r--r-- 1 root root 208 2010-12-14 17:40
system.c
return value of system is 0
the main process is still alive!