进程的调度与使用

来源:互联网 发布:除了淘宝联盟哪个最好 编辑:程序博客网 时间:2024/06/06 17:36

本程序通过创建新进程,使用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;}

这里写图片描述

原创粉丝点击