Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. /* ヘッダファイルのインクルード */
  2. #include <stdio.h> /* 標準入出力 */
  3. #include <string.h> /* 文字列処理 */
  4. #include <sys/types.h> /* 派生型 */
  5. #include <sys/wait.h> /* 待機 */
  6. #include <unistd.h> /* UNIX標準 */
  7.  
  8. /* main関数の定義 */
  9. int main(void){
  10.  
  11. /* 変数の宣言 */
  12. pid_t pid; /* プロセスID */
  13. pid_t child; /* 子プロセスID */
  14. pid_t res; /* 終了した子プロセスID */
  15. int status; /* waitに渡す子プロセス終了情報status. */
  16. int pipe1[2]; /* 親のパイプ.(読み込みと書き込み2つ必要.) */
  17. int pipe2[2]; /* 子のパイプ.(読み込みと書き込み2つ必要.) */
  18.  
  19. /* fork前の自分のプロセスIDを取得. */
  20. pid = getpid(); /* getpidで自分のプロセスIDを取得. */
  21. printf("pid = %d\n", pid); /* printfでpidを出力. */
  22.  
  23. /* パイプpipe1を生成. */
  24. if (pipe(pipe1) < 0){ /* pipeでパイプを作成. */
  25. /* 失敗ならエラー処理. */
  26. printf("create pipe1 failed!\n"); /* "create pipe1 failed!"と出力. */
  27. return -1;/* -1を返す. */
  28. }
  29. /* パイプpipe2を生成. */
  30. if (pipe(pipe2) < 0){ /* pipeでパイプを作成. */
  31. /* 失敗ならエラー処理. */
  32. printf("create pipe2 failed!\n"); /* "create pipe2 failed!"と出力. */
  33. close(pipe1[0]); /* pipe1(読み込み)を閉じる. */
  34. close(pipe1[1]); /* pipe1(書き込み)を閉じる. */
  35. return -1;/* -1を返す. */
  36. }
  37.  
  38. /* プロセスを複製. */
  39. child = fork(); /* forkで子プロセスを生成し, 戻り値をchildに格納. */
  40. if (child < 0){ /* エラー */
  41.  
  42. /* エラーによる異常終了. */
  43. printf("fork failed!\n"); /* "fork failed!n"と出力. */
  44. close(pipe2[0]); /* pipe2(読み込み)を閉じる. */
  45. close(pipe2[1]); /* pipe2(書き込み)を閉じる. */
  46. close(pipe1[0]); /* pipe1(読み込み)を閉じる. */
  47. close(pipe1[1]); /* pipe1(書き込み)を閉じる. */
  48. return -1; /* -1を返す. */
  49.  
  50. }
  51. else if (child > 0){ /* 自分が親プロセスの場合. */
  52.  
  53. /* 変数の初期化 */
  54. char *msg = "I am parent! Are you child?"; /* char *型文字列ポインタ"I am parent! Are you child?". */
  55. char buf[256]; /* char型配列buf(長さ256) */
  56.  
  57. /* プロセスIDの出力. */
  58. printf("I am parent!, getpid = %d, child = %d\n", getpid(), child); /* printfでgetpidの値とchildの値を出力. */
  59.  
  60. /* 3秒待つ. */
  61. sleep(3); /* sleepで3秒待つ. */
  62.  
  63. /* 親の書き込み専用パイプに書き込む. */
  64. write(pipe1[1], msg, strlen(msg)); /* writeでmsgをpipe1[1]に書き込む. */
  65.  
  66. /* 親が書き込んだことを出力. */
  67. printf("parent written!, %s\n", msg); /* "parent written!"と出力. */
  68.  
  69. /* 9秒待つ. */
  70. sleep(9); /* sleepで9秒待つ. */
  71.  
  72. /* 子の読み込み専用パイプから読み込む. */
  73. memset(buf, 0, sizeof(buf)); /* bufを0で埋める. */
  74. read(pipe2[0], buf, sizeof(buf)); /* readでbufにpipe2[0]から読み込む. */
  75.  
  76. /* 読み込んだ内容を出力. */
  77. printf("parent read!, %s\n", buf); /* "parent read!"と出力. */
  78.  
  79. /* 子プロセスの終了まで待つ. */
  80. res = wait(&status); /* waitで子プロセス終了まで待つ. */
  81. if (res == -1){
  82.  
  83. /* エラー処理 */
  84. printf("wait error!\n"); /* "wait error!"と出力. */
  85. close(pipe2[0]); /* pipe2(読み込み)を閉じる. */
  86. close(pipe2[1]); /* pipe2(書き込み)を閉じる. */
  87. close(pipe1[0]); /* pipe1(読み込み)を閉じる. */
  88. close(pipe1[1]); /* pipe1(書き込み)を閉じる. */
  89. return -1; /* -1を返す. */
  90.  
  91. }
  92.  
  93. /* 子プロセスの終了. */
  94. printf("%d finished!\n", res); /* 終了した子プロセスIDのresを出力. */
  95.  
  96. /* 親プロセスのIDも出力. */
  97. printf("getpid() = %d\n", getpid()); /* getpidの値を出力. */
  98.  
  99. }
  100. else{ /* 自分が子プロセスだった場合. */
  101.  
  102. /* 変数の初期化 */
  103. char *msg = "I am child! Are you parent?"; /* char *型文字列ポインタ"I am child! Are you parent?". */
  104. char buf[256]; /* char型配列buf(長さ256) */
  105.  
  106. /* プロセスIDの出力. */
  107. printf("I am child!, getpid = %d\n", getpid()); /* printfでgetpidの値を出力. */
  108.  
  109. /* 6秒待つ. */
  110. sleep(6); /* sleepで6秒待つ. */
  111.  
  112. /* 親の読み込み専用パイプから読み込む. */
  113. memset(buf, 0, sizeof(buf)); /* bufを0で埋める. */
  114. read(pipe1[0], buf, sizeof(buf)); /* readでbufにpipe1[0]から読み込む. */
  115.  
  116. /* 読み込んだ内容を出力. */
  117. printf("child read!, %s\n", buf); /* "child read!"と出力. */
  118.  
  119. /* 3秒待つ. */
  120. sleep(3); /* sleepで3秒待つ. */
  121.  
  122. /* 子の書き込み専用パイプに書き込む. */
  123. write(pipe2[1], msg, strlen(msg)); /* writeでmsgをpipe2[1]に書き込む. */
  124.  
  125. /* 子が書き込んだことを出力. */
  126. printf("child written!, %s\n", msg); /* "child written!"と出力. */
  127.  
  128. /* 5秒待つ. */
  129. sleep(5); /* sleepで5秒待つ. */
  130.  
  131. }
  132.  
  133. /* 正常終了 */
  134. close(pipe2[0]); /* pipe2(読み込み)を閉じる. */
  135. close(pipe2[1]); /* pipe2(書き込み)を閉じる. */
  136. close(pipe1[0]); /* pipe1(読み込み)を閉じる. */
  137. close(pipe1[1]); /* pipe1(書き込み)を閉じる. */
  138.  
  139. /* プログラムの終了 */
  140. return 0;
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement