本程序通过创建新进程,使用fork()函数调用,然后用exec调用让进程去加载,子进程中通过execl函数输入命令(date)的路径 (path),输出当前日期时间。
/* Program that shows how fork system call works */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
pid_t pid;
printf("There is only one process now.\n");
if ((pid = fork()) < 0) /*子进程创建失败时退出*/
{
perror("fork error");
exit(1);
}
if (0 == pid)
{
/* Now we are in the child process */
printf("This message is printed by the child process.\n");
execl("/bin/date", "date", (char *) 0);
perror("execl() failure!\n\n");
}
else
{
/* Now we are in the parent process*/
printf("PARENT PROCESS.\n");
}
printf("=== The End ===\n");
return 0;
}