明天上机的程序

对forkwaitexecexit的使用

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>

int main()
{
        pid_t   pid;
        pid=fork();
        if(pid>0){
                printf("My Pid is :%d,Pid of my son is: %dn",getpid(),pid);
        }
        else    if(pid==0){
                        printf("My Pid is :%d,Pid of my father is: %dn",getpid(),getppid());
                        sleep(5);
                }
                else{
                        perror("Failed to fork!n");
                        exit(0);
                }
        if(pid){
                printf("Waiting my good son.....n0t");
                fflush(stdout);
                sleep(1);
                printf("1t");
                fflush(stdout);
                sleep(1);
                printf("2t");
                fflush(stdout);
                sleep(1);
                printf("3t");
                fflush(stdout);
                sleep(1);
                printf("4n");
        }
        fflush(stdout);
        wait(NULL);
        execl("/bin/echo","echo","Get the end of the process!",NULL);
        perror("Failed to execln");
}

2 thoughts on “明天上机的程序

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.