ulvis.paste.net

Paste Search Dynamic
Recent pastes
kode ini hanya dieksekusi pro
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7. int main(void) {
  8.  
  9. pid_t child_pid;
  10. int status;
  11. pid_t wait_result;
  12.  
  13. child_pid = fork();
  14.  
  15. if (child_pid == 0) {
  16. /* kode ini hanya dieksekusi proses child */
  17.  
  18. cout << "I am a child and my pid = " << getpid() << endl; execl("fork3", "goose", null);
  19.  
  20. /* jika execl berhasil kode ini tidak pernah digunakan */ cout << "Could not execl file fork3" << endl;
  21.  
  22. exit(1);
  23.  
  24. /* exit menghentikan hanya proses child */
  25. }
  26. else if (child_pid > 0) {
  27.  
  28. /* kode ini hanya mengeksekusi proses parent */ cout << "I am the parent and my pid = " << getpid()
  29.  
  30. << endl;
  31. cout << "My child has pid = " << child_pid << endl;
  32. }
  33.  
  34. else {
  35.  
  36. cout << "The fork system call failed to create a new process" << endl;
  37. exit(1);
  38. }
  39.  
  40. /* kode ini hanya dieksekusi oleh proses parent karena
  41.  
  42. child mengeksekusi dari “fork3” atau keluar */ cout << "I am a happy, healthy process and my pid = "
  43.  
  44. << getpid() << endl;
  45.  
  46. if (child_pid == 0) {
  47.  
  48. /* kode ini tidak pernah dieksekusi */ printf("This code will never be executed!\n");
  49.  
  50. }
  51.  
  52. else {
  53.  
  54. /* kode ini hanya dieksekusi oleh proses parent */ cout << "I am a parent and I am going to wait for my
  55.  
  56. child" << endl;
  57. do {
  58. /* parent menunggu sinyal SIGCHLD mengirim tanda bila proses child diterminasi */
  59.  
  60. wait_result = wait(&status);
  61.  
  62. } while (wait_result != child_pid);
  63. cout << "I am a parent and I am quitting." << endl;
  64. }
  65. return 0;
  66. }
Parsed in 0.011 seconds