Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8.  
  9. using namespace std;
  10.  
  11. // Вариант №17, Verenuk A. A.
  12.  
  13. int main()
  14. {
  15.     int pid, k;
  16.     char str[100];
  17.  
  18.     fstream file;
  19.     file.open("pid.txt",ios::in);
  20.  
  21.     if(!file) {
  22.         cout << "Невозможно открыть ФАЙЛ.\n";
  23.         exit(0);
  24.     }
  25.  
  26.     file.getline(str,100);
  27.     k = atoi(str);
  28.     file.close();
  29.  
  30. //  cout << "k: " << k << endl;
  31. //  cout << "ppid: " << getppid() << endl;
  32. //  cout << "pid: "  << getpid() << endl;
  33.  
  34.         file.open("pid.txt",ios::out|ios::trunc);
  35.  
  36.         if(!file)
  37.     {
  38.         cout << "Невозможно создать ФАЙЛ.\n";
  39.             exit(0);
  40.         }
  41.  
  42.     if(k!=0)
  43.     {
  44.                 kill(k,15);
  45.                 file << "0";
  46.     }else{
  47.         if ((pid=fork())!=0)
  48.         {
  49. //          cout << "forkpid: " << pid << endl;
  50.                         file << pid;
  51.         }else{
  52.                     for(int i=1;i>0;i++)
  53.                     {
  54.                             sleep(100);
  55.                     }
  56.  
  57.         }
  58.        
  59.     }
  60.  
  61.     file.close();
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement