mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-05-05 19:53:25 +08:00
补充课后作业
This commit is contained in:
26
ch10/homework/kehou3.c
Normal file
26
ch10/homework/kehou3.c
Normal 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
33
ch10/homework/kehou5.c
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user