Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Created December 28, 2012 07:43
Show Gist options
  • Select an option

  • Save alphaKAI/4395538 to your computer and use it in GitHub Desktop.

Select an option

Save alphaKAI/4395538 to your computer and use it in GitHub Desktop.
fork関数つかって簡単なカウントダウンしてEnterが押されたら終了するプログラム書いてみた
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int main(void){
int p_id;
int status;
int return_code = 0;
int i;
int c;
if ((p_id = fork()) == 0){
for(i=1;i<10;i++){
printf("%d\n", i);
sleep(1);
}
}
else {
if (p_id != -1) {
c=getchar();
kill(p_id,SIGKILL);
printf("EXIT\n");
}
else{
printf("ERROR:\n%d\n", p_id);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment