Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6.  
  7. int main(int argc, char const* argv[]) {
  8. // strtol(char* num,.., base) - переводит число из массива чаров в long, при этом работая в сс с основанием base
  9. long N = strtol(argv[1], NULL, 10);
  10. int currentNumber = (int)N + 1;
  11.  
  12. if (N == 1){
  13. printf("1\n");
  14. return 0;
  15. }
  16.  
  17. pid_t process_id = 0;
  18. do {
  19. process_id = fork();
  20. if (process_id != -1) {
  21. currentNumber--;
  22. }
  23. }while (process_id == 0 && currentNumber != 1);
  24.  
  25.  
  26. if (process_id > 0) {
  27.  
  28. int return_stat;
  29. waitpid(process_id, &return_stat, 0);
  30.  
  31. if (currentNumber == N)
  32. printf("%d\n", currentNumber);
  33. else
  34. printf("%d ", currentNumber);
  35. }
  36.  
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement