11. Process Control

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <stdlib.h>
 4  
5  // Get the properties of the thread 
6  void TestPid()
 7  {
 8          // Process thread number 
9          printf( " My Pid: %d\n " ,getpid());
 10          // The thread number of the parent process is 
11          printf( " Parent Pid: %d\n " ,getppid());
 12          // The thread number of the same group is 
13          printf( " groutp pid:%d\n " ,getpgrp());
 14          
15         // user id 
16          printf( " uid:%d\n " ,getuid());
 17          // effective user id 
18          printf( " euid:%d\n " ,geteuid());
 19          // process to group id 
20          printf( " gid:%d\n " ,getgid());
 21          // process to effective group id 
22          printf( " egid:%d\n " ,getegid());
 23          
24          // chmod u+ s uid_demo Change the running permission of the running program, so that the program temporarily has the file owner to the user permission 
25  }
 26  
27  // fork
28  void TestFork()
 29  {
 30          pid_t pid;
 31          
32          pid = getpid();
 33          printf( " MyPid:%d\n " ,pid);
 34          
35          pid = fork(); // Split into two processes for execution The following statement does not share process resources, and calls
 36          // pid = vfork(); // Share process resources, the child process executes first, and then executes the parent process after execution 
37          
38          if (pid < 0 )
 39          {
 40                  perror( " Fail to fork " );
 41                  return;
 42          }
 43          
44          // If it is a child process 
45          if (pid == 0 )
 46          {
 47                  printf( " This is a child process [%d]\n " ,getpid());
 48                  // Execute another program As the original process to the child process, the following statements will not be executed 
49                  execl( " ./file " , " ./file " ,NULL);
 50                  printf( " 2134 " );
 51                  
52          }
 53          // If it is the parent process
54          else 
55          {
 56              printf( " This is a parent process [%d]\n " ,getpid());
 57          }
 58  
59          // Exit, the resource is released by the parent process. , if the class will not call the destructor 
60          _exit( 0 );
 61          // exit(0), the resource is released by the child process
 62          // return The resource is popped off the stack, and the destructor will be called 
63  }
 64  
65  // execl execv 
66  void TestExec()
 67  {
 68          pid_t pid = fork();
 69          
70          if (pid ==0)
71         {
72             //child
73             printf("I am child [%d]\n",getpid());
74             
75             //execl("/bin/ls","ls","-l",NULL);
76             char *argv[3] = {"ls","-l"};
77             execv("/bin/ls",argv);
78             printf("I can not to be printed");
else80        }
79          
81         {
82             printf("I am father [%d]\n",getpid());
83         }
84 }
85 
86 int main()
87 {
88     //TestPid();
89     //TestFork();
90     TestExec();
91     
92     return 0;
93 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325216141&siteId=291194637