补充课后作业

This commit is contained in:
caixiongjiang
2022-04-14 16:28:46 +08:00
parent 704226a684
commit 4e8142a987
14 changed files with 979 additions and 0 deletions

26
ch10/homework/kehou3.c Normal file
View File

@@ -0,0 +1,26 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
pid_t pid;
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
pid = fork();
if(pid == 0)
{
printf("Child sockfd: %d \n", sockfd);
}
else
{
printf("Parent sockfd: %d \n", sockfd);
}
return 0;
}
/*
结果:
Parent sockfd: 3
Child sockfd: 3
*/

33
ch10/homework/kehou5.c Normal file
View File

@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void ctrl_handling(int sig);
int main(int argc, char *argv[])
{
struct sigaction act;
act.sa_handler = ctrl_handling;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);//输入ctrl+c发出信号
while(1)
{
sleep(1);
puts("美好的一天!");
}
return 0;
}
void ctrl_handling(int sig)
{
char c;
if(sig == SIGINT)
{
fputs("Do you want to exit(Y to exit)?", stdout);
scanf("%c", &c);
if(c == 'y' || c == 'Y')
exit(1);
}
}