Pensando en la función de salida

1 salida vs. vuelta

Si la función principal u otras funciones llaman a exit, finalizará el proceso. La
función principal llama a return para finalizar el programa, pero otras llamadas a funciones no lo harán.

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 int fun()
  4 {
    
    
  5 // exit(0);
  6    return 0;
  7 }
  8 int main()
  9 {
    
    
 10         printf("111\n");
 11         printf("222\n");
 12         fun();
 13         printf("333\n");
 14         printf("4444\n");
 15 }

Son la impresión de salida y regreso respectivamente

[email protected]:~/work$ ./a.out
111
222
[email protected]:~/work$ gcc test_exit.c 
[email protected]:~/work$ ./a.out
111
222
333
4444

2 salir finaliza el proceso en el que se encuentra

  1 /* 
  2  *  fork_test.c 
  3  *  version 1 
  4  *  Created on: 2010-5-29 
  5  *      Author: wangth 
  6  */
  7 #include <unistd.h>
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 int main ()
 11 {
    
       
 12     pid_t fpid; //fpid表示fork函数返回的值  
 13     int count=0;
 14     fpid=fork(); 
 15     if (fpid < 0)   
 16         printf("error in fork!");
 17     else if (fpid == 0) {
    
      
 18         printf("i am the child process, my process id is %d\n",getpid());
 19         exit(0);
 20         printf("我是爹的儿子\n");//对某些人来说中文看着更直白。  
 21         count++;
 22     }  
 23     else {
    
    
 24         while(1)
 25         {
    
           
 26                 sleep(2);    
 27                 printf("i am the parent process, my process id is %d\n",getpid());
 28                 printf("我是孩子他爹\n");
 29                 count++;
 30         }
 31     }  
 32     printf("统计结果是: %d\n",count);
 33     return 0;
 34 }  

Supongo que te gusta

Origin blog.csdn.net/aningxiaoxixi/article/details/113482759
Recomendado
Clasificación