Skip to content

Instantly share code, notes, and snippets.

@swcho
Last active August 29, 2015 14:27
Show Gist options
  • Save swcho/a0d868f81c7c7ea06edd to your computer and use it in GitHub Desktop.
Save swcho/a0d868f81c7c7ea06edd to your computer and use it in GitHub Desktop.
kill test
//#include <QCoreApplication>
//int main(int argc, char *argv[])
//{
// QCoreApplication a(argc, argv);
// return a.exec();
//}
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
/* When a SIGUSR1 signal arrives, set this variable. */
volatile sig_atomic_t usr_interrupt = 0;
void
synch_signal (int sig)
{
usr_interrupt = 1;
}
/* The child process executes this function. */
void
child_function (void)
{
/* Perform initialization. */
printf ("I'm child!!! My pid is %d.\n", (int) getpid ());
sleep(10000);
/* Let parent know you’re done. */
// kill (getppid (), SIGUSR1);
/* Continue with execution. */
puts ("Bye, now....");
// exit (0);
}
int
main (void)
{
// pid_t pid; //pid_t 선언
// pid = fork(); //fork 발생
// if(pid == -1) { //-1 이면 fork생성 에러
// printf("can't fork, erro\n");
// exit(0);
// }
// if(pid == 0) { //0이면 자식 프로세스
// int j;
// for(j = 0; j < 10; j++) {
// printf("child: %d\n", j);
// sleep(1);
// }
// exit(0);
// } else { //부모프로세스
// int i;
// for(i = 0; i < 10; i++) {
// printf("parent : %d\n", i);
// sleep(1);
// }
// exit(0);
// }
// return 0;
struct sigaction usr_action;
sigset_t block_mask;
pid_t child_id;
/* Establish the signal handler. */
sigfillset (&block_mask);
usr_action.sa_handler = synch_signal;
usr_action.sa_mask = block_mask;
usr_action.sa_flags = 0;
sigaction (SIGUSR1, &usr_action, NULL);
/* Create the child process. */
child_id = fork ();
if (child_id == 0) {
child_function (); /* Does not return. */
} else {
printf("parent goes child is %d\n", child_id);
int ret = kill(child_id, SIGHUP);
printf("kill 1 with %d\n", ret);
ret = kill(child_id, SIGHUP);
printf("kill 2 with %d\n", ret);
// sleep(10000);
}
/* Now continue execution. */
puts ("That's all, folks!");
return 0;
}
all:
gcc main.cpp -o kill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment