一起来探讨Linux中fork与mutex的混合使用

一 预备知识

本篇代码要用到的Linux函数

1.  fork--Linux创建进程的函数

2.  fopen--打开文件的函数

3.  fgets--读取文件内容的函数

4.  fputs--写文件的函数

5  waitpid--等待子进程的函数

6. sleep--进程休眠的函数

7. pthread_mutex_t--线程互斥锁类型

8. pthread_mutex_init--线程互斥锁初始化

9. pthread_mutex_lock--加锁

10.  pthread_mutex_unlock--解锁

11. getpid--获取当前进程的id

12. getppid--获取当前进程的父进程的id

二 问题描述

开启子进程去并发读文件内容,然后写入到另一个文件中,父进程不参与文件的读、写。

三 代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
/*
 * A instance to use mutex lock 
 * */
const int MAXSIZE = 1024;
int main() {
    FILE *srcfp, *destfp;
    int num = 0;     // the number of lines from source file 
    static int over; // whether the read process should be over 
    char buffer[MAXSIZE];
    pid_t pid;

    pthread_mutex_t mutex;

    if((srcfp = fopen("/host/study/C/Linux_Huche/read.c", "r")) == NULL) {
	perror("open source file error!\n");
	exit(1);
    }

    if((destfp = fopen("/host/study/C/Linux_Huche/write.c", "w")) == NULL) {
	perror("open destination file error!\n");
	exit(1);
    }

    pthread_mutex_init(&mutex, NULL);

    while(1) {
	if(over) goto End;         // to close the files 
	pid = fork();
	if(pid == 0) {
	    puts("now in the child process!\n");
            if(fgets(buffer, MAXSIZE, srcfp) == NULL) {
		over = 1;
		exit(EXIT_SUCCESS);
	    }

	    sleep(1);
            /*
	     * the critical section 
	     * */
	    pthread_mutex_lock(&mutex);
            if(over) exit(EXIT_SUCCESS);
	    printf("the getpid() is: %d\n", getpid());
	    printf("the getppid() is: %d\n", getppid());
	    printf("num = %d\n", ++num); 
	    puts(buffer);
	    fputs(buffer, destfp);
	    pthread_mutex_unlock(&mutex);
	}
	else {
	    sleep(1);
	    /*
	     * to wait for children processes to be over 
	     * */
	    if(waitpid(pid, NULL, 0) < 0) {
		perror("waitpid error!\n");
		exit(2);
	    }
	    puts("child process is over, now in the parent process!\n");
	    printf("the getpid() is %d\n", getpid());
	    exit(EXIT_SUCCESS);
	}
    }
    /*
     * never not to forget to close the file which is still open 
     * */
End:fclose(srcfp);
    fclose(destfp);

    return 0;
}

四 代码分析

1.  主循环是一个死循环,当over = 1时,退出循环;

2.  当创建子进程成功后,子进程首先检测源文件是否读完,如果读完,直接退出。否则,进入临界区向目标文件写数据。

3. 父进程等待子进程运行结束后执行。

4. 子进程中的sleep函数的使用是休眠自己,Linux内核则调度其它子进程执行。

5. 子进程属于并发执行。

6. 最后程序关闭文件后退出。

五 运行结果

一起来探讨Linux中fork与mutex的混合使用_第1张图片

一起来探讨Linux中fork与mutex的混合使用_第2张图片

一起来探讨Linux中fork与mutex的混合使用_第3张图片

六 运行结果分析

1. 子进程的getppid和父进程的getpid相同;

2. 进程的pid和ppid从小大依次出现,ppid比pid小1;

3. 打开write.c文件,结果如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
	 * the getppid() is the pid of bash process 
	 * */
	printf("getppid() is %d\n", getppid());
	printf("child process exited with status %d\n", status);
    }
    exit(EXIT_SUCCESS);
}

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
	 * the getppid() is the pid of bash process 
	 * */
	printf("getppid() is %d\n", getppid());
	printf("child process exited with status %d\n", status);
    }
    exit(EXIT_SUCCESS);
}

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
	 * the getppid() is the pid of bash process 
	 * */
	printf("getppid() is %d\n", getppid());
	printf("child process exited with status %d\n", status);
    }
    exit(EXIT_SUCCESS);
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
	 * the getppid() is the pid of bash process 
	 * */
	printf("getppid() is %d\n", getppid());
	printf("child process exited with status %d\n", status);
    }
    exit(EXIT_SUCCESS);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
	 * the getppid() is the pid of bash process 
	 * */
	printf("getppid() is %d\n", getppid());
	printf("child process exited with status %d\n", status);
    }
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
	 * the getppid() is the pid of bash process 
	 * */
	printf("getppid() is %d\n", getppid());
	printf("child process exited with status %d\n", status);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
	 * the getppid() is the pid of bash process 
	 * */
	printf("getppid() is %d\n", getppid());
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
	 * the getppid() is the pid of bash process 
	 * */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
	 * the getppid() is the pid of bash process 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
	/*
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
	printf("getpid() is %d\n", getpid());
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
	printf("childpid is %d\n", childpid);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
	waitpid(childpid, &status, 0);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
	puts("in the parent process!\n");
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
	 * */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
	 * @status: the result of child process which the current process waited for 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
	/*
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

    else {
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
    }
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
	exit(EXIT_SUCCESS);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
	printf("getppid() is %d\n", getppid());
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
	printf("getpid() is %d\n", getpid());
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
	 * */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
	 * the getppid() is the same with getpid() which occurs in parent code section
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
	/*
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
	sleep(3);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
	printf("the child pid is = %d\n", childpid);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
	puts("in a child process!\n");
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

    else if(childpid == 0) {
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
    }
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
	exit(1);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
	perror("fork error!\n");
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
    if(childpid < 0) {
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

    childpid = fork();
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
    int status;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
    pid_t childpid;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
void main() {
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
 * */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
 * the instance of waitpid 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
从write.c文件中可以看出,子进程并发执行,去写write.c文件,但是由于加了互斥锁,所以write.c文件依然有序,可是over不能启动有效控制,这是为什么呢?


从一段代码讨论fork函数

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
    pid_t pid1, pid2;

    pid1 = fork();
//    pid2 = fork();
    printf("%d\n",pid1);
  // printf("pid1 = %d, pid2 = %d\n",pid1, pid2);
    return 0;
}

运行结果:

一起来探讨Linux中fork与mutex的混合使用_第4张图片

分析:

当程序执行到pid1 = fork()时,会有如下效果

一起来探讨Linux中fork与mutex的混合使用_第5张图片

由上图可知当执行到pid1 = fork()时,此时父进程p1创建子进程q1,那么p1和q1的地址空间将完全一致,程序分别从p1和q1返回,之后分别执行以下代码,所以会有以上的效果。

改变程序后,执行两个fork,运行效果如下:

一起来探讨Linux中fork与mutex的混合使用_第6张图片

分析如下:

一起来探讨Linux中fork与mutex的混合使用_第7张图片


你可能感兴趣的:(一起来探讨Linux中fork与mutex的混合使用)