Advertisement
Sephinroth

cmd args, environment variables, getting pid and ppid, simple fork

Oct 17th, 2023
1,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4.  
  5. int main(int argc, char **argv)
  6. {
  7.     printf("argc = %d\n\n", argc);
  8.     for (int i = 0; i < argc; i++)
  9.         printf("argv[%d]: %s\n", i, argv[i]);
  10.     return 0;
  11. }
  12.  
  13. #include <stdio.h>
  14.  
  15. int main(int argc, char **argv, char **env){
  16.     while (*env != 0)
  17.         printf("%s\n", *env++);
  18.     return 0;
  19. }
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #define NAME "PATH"
  24.  
  25. int main(){
  26.     char *s;
  27.     s = getenv(NAME);
  28.     (s == 0) ? printf("Variable not defined!\n") : printf("%s = %s\n", NAME, s);
  29.     return 0;
  30. }
  31.  
  32.  
  33. #include <stdio.h>
  34.  
  35. extern char **environ;
  36.  
  37. int main(){
  38.     for (int i = 0; environ[i] != 0; i++)
  39.         printf("%s\n", environ[i]);
  40.     return 0;
  41. }
  42.  
  43. #include <sys/types.h>
  44. #include <unistd.h>
  45. #include <stdio.h>
  46.  
  47. int main(){
  48.     pid_t pid, ppid;
  49.     pid = getpid();
  50.     ppid = getppid();
  51.     printf("PID = %d, PPID = %d\n", pid, ppid);
  52.     return 0;
  53. }
  54.    
  55. #include <stdio.h>
  56. #include <sys/types.h>
  57. #include <unistd.h>
  58.  
  59. int main(){
  60.     pid_t pid;
  61.     printf("Parent\n");
  62.     pid = fork();
  63.     if (pid == 0)
  64.         printf("Hello, child %d\n", getpid());
  65.     else
  66.         printf("Hello, parent %d\n", getpid());
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement