和我一起来探讨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
二 问题描述
开启子进程去并发读文件内容,然后写入到另一个文件中,父进程不参与文件的读、写。
三 代码
六 运行结果分析
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不能启动有效控制,这是为什么呢?