ctl_sig2.c
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
void ouch(int sig)
{
printf("get the signal is %d\n",sig);
printf("SIGKILL%d\n",SIGKILL);
printf("SIGQUIT %d\n",SIGQUIT);
}
int main()
{
struct sigaction act;
act.sa_handler = ouch;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT,&act,0);
while(1)
{
printf("hello\n");
sleep(1);
}
exit(0);
}
ctl_sig.c
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
void ouch(int sig)
{
printf("get the signal is %d\n",sig);
(void) signal(SIGINT,SIG_DFL);
}
int main()
{
(void) signal(SIGINT,ouch);
while(1)
{
printf("hello\n");
sleep(1);
}
exit(0);
}
signal1.c
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
void SignHandler(int iSignN)
{
printf("Capture sign no:%d\n",iSignN);
}
int main()
{
signal(SIGINT,SignHandler);
while(1)
sleep(1);
return 0;
}
signal2.c
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("run the ps with execlp ");
execlp("cd","cd","..",0);
printf("done");
return 0;
}
signal.c
#include<stdlib.h>
#include<stdio.h>
int main()
{
printf("running ps with system\n");
system("ps ax ");
printf("done.\n");
return 0;
}
fork.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<sys/types.h>
int main()
{
pid_t new_pid;
char *message;
int n;
int exit_code;
new_pid = fork();
switch(new_pid)
{
case -1:
perror("fork failed"); exit(1);
case 0:
message = "it is a child";
exit_code = 37;
n = 2;
break;
default:
message = "it is a father";
n = 5;
exit_code = 0;
break;
}
for(;n>0;n--)
{
puts(message);
sleep(2);
}
if(new_pid!=0)
{
pid_t child_id;
int stat_val;
child_id = wait(&stat_val);
printf("child has finished:PID = %d\n",child_id);
if(WIFEXITED(stat_val))
printf("the child exit with code %d\n",WEXITSTATUS(stat_val));
else
printf("the child terminated abnormally");
}
exit(exit_code);
}
alarm.c
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
#include<sys/types.h>
static int alarm_fire = 0;
void ding(int sig)
{
alarm_fire = 1;
}
int main()
{
pid_t pid;
int n;
//n=alarm(0);
printf("the application is working\n");
pid = fork();
switch(pid)
{
case -1:
perror("fork error\n");
exit(1);
case 0:
//sleep(5);
//(void) kill(getppid(),SIGALRM);
n=alarm(15);
printf("n=%d\n",n);
exit(0);
}
printf("waiting the signal\n");
(void) signal(SIGALRM,ding);
pause();
if(alarm_fire)
printf("DING\n");
printf("Done\n");
exit(0);
}