标准I/O提供的三种类型的缓冲
    全缓冲:在填满io缓冲区后,才进行实际的Io操作
    行缓冲
    不带缓冲
    image.png
    image.png
    image.pngimage.png
    image.png

    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. #include<unistd.h>
    4. #include<sys/types.h>
    5. #include<sys/wait.h>
    6. int main(int argc, char *argv[])
    7. {
    8. pid_t pid;
    9. pid=fork();//創建一個新進程
    10. if(pid<0)
    11. perror("fork");
    12. if(pid==0)
    13. {
    14. int i = 0;
    15. for(i=0;i<5;i++)
    16. {
    17. printf("this is son process\n");
    18. sleep(1);
    19. }
    20. _exit(2);//終止進程
    21. }
    22. else
    23. {
    24. int status = 0;
    25. wait(&status);//將父進程掛起,等待子進程終止
    26. //當子進程正常退出時,返回真值
    27. if(WIFEXITED(status)!=0)
    28. {
    29. printf("son process return %d\n",WEXITSTATUS(status));
    30. }
    31. printf("this is father process\n");
    32. }
    33. return 0;
    34. }

    image.png

    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. #include<unistd.h>
    4. void clear_fun1(void)
    5. {
    6. printf("perform clear fun1\n");
    7. }
    8. void clear_fun2(void)
    9. {
    10. printf("perform clear fun2\n");
    11. }
    12. void clear_fun3(void)
    13. {
    14. printf("perform clear fun3\n");
    15. }
    16. int main(int argc, char *argv[])
    17. {
    18. atexit(clear_fun1);
    19. atexit(clear_fun2);
    20. atexit(clear_fun3);
    21. printf("process exit 3 sec later!!!\n");
    22. sleep(3);
    23. return 0;
    24. }

    image.png
    image.png