Linux_fork 进程操作demo

main.7

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    system("ls -l");

    printf("end!\n");

    return 0;
}

main8.c

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char * env[] = {"PATH=/bin", 0};
    char * argv[] = {"ls", "-l", 0};

    printf("ready...\n");

    //printf("****************execl**************\n");
    //execl("ls", "ls", "-l", 0);

    printf("****************execlp**************\n");
    execlp("ls", "ls", "-l", 0);

    //printf("****************execle**************\n");
    //execle("/bin/ls", "ls", "-l", 0, env);

    //printf("****************execv**************\n");
    //execv("/bin/ls", argv);

    //printf("****************execvp**************\n");
    //execvp("ls", argv);

    //printf("****************execve**************\n");
    //execve("/bin/ls", argv, env);

    printf("end!\n");
    return 0;
}

main9.c

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int pid;
    char *msg;
    int n;

    printf("father process begin...\n");

    pid = fork();

    if (pid == -1) {
        printf("fork error!\n");
        exit(1);
    } else if (pid == 0) {
        printf("In child process!\n");
        msg = "child process ";
        n = 2;
    }  else {
        printf("In father process!\n");
        msg = "father process ";
        n = 1;
    }

    while(n--) {
        printf("%s\n", msg);
        sleep(1);
    }

    printf("%s end!\n", msg);

    return 0;
}

main10.c

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

int main(void)
{
    int pid;
    char *msg;
    int status;
    int child_pid;
    int n;

    printf("father process begin...\n");

    pid = fork();

    if (pid == -1) {
        printf("fork error!\n");
        exit(1);
    } else if (pid == 0) {
        printf("In child process!\n");
        msg = "child process ";
        n = 3;
    }  else {
        printf("In father process!\n");
        msg = "father process ";
        n = 1;
    }

    while(n--) {
        printf("%s\n", msg);
        sleep(1);
    }

    if (pid) {
        printf("father process wait...\n");
        child_pid = wait(&status);
        if (WIFEXITED(status)) {
            printf("Wait finished and child process terminated normally, child pid = %d\n", child_pid);
        } else {
            printf("Wait finished and child process terminated unnormally, child pid = %d\n", child_pid);
        }
    }

    printf("%s end!\n", msg);

    return 0;
}

main11.c

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

int main(void)
{
    int pid;
    char *msg;
    int status;
    int child_pid;
    int n;

    printf("father process begin...\n");

    pid = fork();

    if (pid == -1) {
        printf("fork error!\n");
        exit(1);
    } else if (pid == 0) {
        printf("In child process!\n");
        msg = "child process ";
        n = 1;
    }  else {
        printf("In father process!\n");
        msg = "father process ";
        n = 10;
    }

    while(n--) {
        printf("%s\n", msg);
        sleep(5);
    }

    if (pid) {
        printf("father process wait...\n");
        child_pid = wait(&status);
        if (WIFEXITED(status)) {
            printf("Wait finished and child process terminated normally, child pid = %d\n", child_pid);
        } else {
            printf("Wait finished and child process terminated unnormally, child pid = %d\n", child_pid);
        }
    }

    printf("%s end!\n", msg);

    return 0;
}

main12.c

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int pid;
    int i;

    for (i=0; i<10; i++) {
        pid = fork();
        if (pid == -1) {

            perror("fork error!\n");
            exit(1);
        } else if (pid == 0) {
            //printf("Start run a new child pid, pid=%d\n", getpid());          
        } else {
            //printf("Add a new child, pid = %d\n", pid);
        }
    }

    printf("My pid = %d\n", getpid());

    return 0;
}
posted @ 2016-03-31 13:10  夜色下的港湾  Views(372)  Comments(0Edit  收藏  举报