|
簡答題 1.采用阻塞I/O模型時,套接字函數read()的返回值有哪幾種?分別對應什么情況?
返回值n | 對應情況 | 無數據
| 阻塞
| n >= len
| 讀取len個字節
| 0 < n < len
| 讀取n個字節
| n = 0
| 讀通道關閉
| n = -1 且 ERRON = EINTR
| 讀中斷引起錯誤
| n = -1 且 ERRON = ECONNREST
| 網絡連接有問題
|
2.為什么客戶機通常不需要綁定自己的端口號?
當客戶機請求連接時,系統會自動為它選擇一個未用的端口號,并且用本地的IP地址設置嵌套字地址的相應項。
3.close()函數和shutdown()函數有何差別?
①shutdown操作連接通道,其他進程不能再使用已被關閉的通道;close操作描述符,其他進程仍然可以使用該socket描述符
②close關閉應用程序與socket的接口,調用close之后進程不能再讀寫這個socket;shudown可以只關閉一個通道,另一個通道仍然可以操作
4.僵尸進程有什么危害?清除僵尸進程的方法有哪些?(至少4種)
僵尸進程雖然對其他進程幾乎沒有什么影響,不占用CPU時間,消耗的內存也幾乎可以忽略不計,但Linux系統中進程數目是有限的,在一些特殊的情況下,如果存在太多的僵尸進程,會影響到新進程的產生。
清除方法:
①忽略SIGCHLD信號,系統將清除子進程的進程表項,這種方法依賴于Linux版本的實現
②調用函數wait()或waitpid()等待子進程。這種方法沒有兼容性問題,但主程序進入等待循環后不能做任何事情
③捕獲SIGCHLD信號,在相應的處理函數中調用wait()或waitpid()對子進程進行處理
④調用fork()兩次,使子進程成為孤兒進程,由init進程管理
5.Linux系統默認的I/O模型是什么?請給出兩種將阻塞式模型轉換為非阻塞式模型的方法
默認的I/O模型是阻塞式I/O模型
方法一:fcntl
int flags = fcntl(sockfd, F_GETF, 0);
flags |= O_NONBLOCK;
fcntl(sockfd, F_SETF, flags);
方法二:ioctl
int on = 1;
Ioctl(sockfd, FIONBIO, &on);
6.函數fork()與exec()的差別
①fork()是執行一個新的任務,exec()執行另一個程序
②fork()的子進程共享正文,exec()將改變子進程的正文
③fork()在創建完進程后立即返回,exec執行時函數不返回,只有錯誤時才返回
7.創建一個守護進程有哪些步驟
①調用fork(),父進程退出,子進程繼續運行
②調用setsid()創建新的session
③忽略信號SIGHUP,再次調用fork(),父進程(session的頭進程)退出
④調用chdir("/"),使進程不使用任何目錄
⑤調用unmask(0),使進程對任何內容都有寫權限
⑥關閉所有打開的文件描述符,為標準輸入、標準輸出、標準錯誤輸出打開新的文件描述符0、1、2
8.列舉出不少于5項的進程間通信方式
①管道②信號量③消息隊列④消息郵箱⑤共享內存⑥內存映像文件⑦Unix域Socket
9.請給出利用Socket接口實現面向連接的網絡編程模型
在51黑附件中
10.網絡編程時,為什么要考慮字節順序問題
不同機器表示數據的字節順序是不同的:
Intel處理器:低字節在前,高字節在后,稱為小端存儲
Motorola處理器:高字節在前,低字節在后,稱為大端存儲
11.在使用UDP套接字時也可以使用connnect()函數,連接UDP套接字有哪些特點?
使得在無連接的UDP通信中,發送時不必帶目的地址,接收時不必帶源地址
12.服務器經常使用說明綁定方式,為什么
常使用綁定任意地址方式(INADDR_ANY)
這樣服務器可以接收發送到服務器的任意IP地址的數據
13.試比較pipe和Unix域套接字的差異
Pipe是單工通信,Unix域套接字是雙工通信
編程題 1.請給出實現一個守護進程的程序代碼
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
void init_daemon(void)
{
int pid, i;
/*
步驟一:調用fork(),父進程退出,子進程繼續
*/
if(pid = fork())
exit(0); //父進程直接退出
else if(pid < 0)
exit(1); //fork()失敗退出
/*
步驟二:調用setsid()創建新的會話
*/
//第一子進程
setsid(); //第一子進程成為新的會話組組長,并與控制臺終端分離
/*
步驟三:再次調用fork(),會話組組長退出
*/
if(pid = fork())
exit(0); //是第一子進程,退出
else if(pid < 0)
exit(1); //fork()失敗退出
//第二子進程(孫進程),不再是會話組組長
for(i = 0; i < NOFILE; i++)
close(i); //關閉打開的文件描述符
/*
步驟四:釋放掛載的文件系統
*/
chdir("\n"); //改變當前目錄到系統目錄下,釋放掛載的文件系統
/*
步驟五:提升進程權限
*/
umask(0); //使守護進程擁有高權限
}
int main()
{
FILE *fp;
time_t t;
init_daemon();
while(1)
{
sleep(3);
if((fp = fopen("test.log", "a")) >= 0)
{
t = time(0);
fprintf(fp, "daemon process logged at: %s \n",asctime(localtime(&t)));
fclose(fp);
}
}
return 0;
}
2.編程產生2個進程:子進程1和子進程2,然后利用管道,將字符串“abcde”從子進程1傳到子進程2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main()
{
int fd[2], pid;
if(pipe(fd) == -1)
perror("pipe()");
if((pid = fork()) == -1)
perror("fork()");
else if(pid > 0)
{
close(fd[0]);
if(write(fd[1], "abcde", 5) == -1)
perror("write()");
printf("this is parent process!\n");
}
else if(pid == 0)
{
close(fd[1]);
char buf[6];
if(read(fd[0], buf, 5) == -1)
perror("read()");
buf[6] = 0;
printf("this is son process, parent says: %s\n", buf);
}
sleep(3);
}
3.使用UDP套接字完成如下服務器和客戶機的編程工作,客戶機發送一個整數,UDP循環服務器將這個整數加上100,結果返回給客戶機
服務器:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVERPORT 3000
#define BUFFERSIZE 10
int main(int argc, char** argv)
{
int sockfd;
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
perror("socket()");
struct sockaddr_in srvaddr, clientaddr;
srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons(SERVERPORT);
srvaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd, (struct sockaddr*)&srvaddr, sizeof(srvaddr)) == -1)
perror("bind()");
char buf[BUFFERSIZE];
socklen_t clientaddr_len = sizeof(clientaddr);
while(1)
{
recvfrom(sockfd, buf, BUFFERSIZE, 0, (struct sockaddr*)&clientaddr,&clientaddr_len);
sprintf(buf, "%d", atoi(buf) + 100);
sendto(sockfd, buf, BUFFERSIZE, 0, (struct sockaddr*)&clientaddr,clientaddr_len);
}
return0;
}
客戶端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVERPORT 3000
#define SERVERADDR "127.0.0.1"
#define BUFFERSIZE 10
int main(int argc, char** argv)
{
int sockfd;
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
perror("socket()");
struct sockaddr_in srvaddr;
srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons(SERVERPORT);
if(inet_aton(SERVERADDR, &srvaddr.sin_addr) == -1)
perror("inet_aton()");
char buf[BUFFERSIZE];
sprintf(buf, "%s", argv[1]);
socklen_t srvaddr_len = sizeof(srvaddr);
sendto(sockfd, buf, sizeof(buf), BUFFERSIZE, (structsockaddr*)&srvaddr, sizeof(srvaddr));
recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL);
printf("%d\n",atoi(buf));
}
4.采用預創建10個子進程的方式,編程實現一個TCP并發服務器
服務器:
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <errno.h>
- #include <signal.h>
- #define SERVER_PORT 8080
- #define BACKLOG 5
- #define CLDNUM 10
- void theEnd(int n) //根據實際進程數清除子進程
- {
- int i;
- for(i=0; i< n; i ++)
- if(pid[i]>0)
- kill(pid[i],SIGTERM);
- while(wait(NULL)>0);
- }
- int pid[CLDNUM];
- int i;
- int main()
- {
- int listenfd,connfd;
- struct sockaddr_in servaddr;
- char cmd[10];
- int nErr=0;
- int buf_recv[1024];
- int buf_send[1024];
-
- listenfd=socket(AF_INET,SOCK_STREAM,0);
- if(listenfd<0)
- fprintf(stderr,"Socket error");
-
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
- servaddr.sin_port=htons(SERVER_PORT);
-
- int n=1;
- setsockopt( listenfd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof(n));
- if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
- fprintf(stderr,"Bind error");
- if(listen(listenfd,BACKLOG)<0)
- fprintf(stderr,"Listen error");
- for(i=0; i < CLDNUM; i ++)
- {
- if((pid[i]=fork())==0)
- {
- for(;;) //預創建的子進程
- {
- connfd=accept(listenfd,NULL,NULL);
- ......
- nbytes=read_all(connfd,buf_recv,256);
- process(......); //數據處理
- nbytes=write_all(connfd,buf_send,256);
- ......
- }
- }
- else if(pid[i]<0)
- {
- nErr=i+1; //從第i+1個進程開始,創建失敗
- break;
- }
- }
- if(nErr!=CLDNUM) //創建子進程組失敗
- {
- theEnd(nErr);
- exit(1);
- }
- int m=1;
- while(m) //鍵盤輸入,"theEnd",要求進程結束
- {
- gets(cmd);
- m=strcmp(cmd,"theEnd");
- }
- theEnd(nErr);
- exit(0);
- }
復制代碼
5.編程產生子進程,孫進程,并使進程按“孫-子-父”的順序結束
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- int main()
- {
- int son_pid, frandson_pid;
- if((son_pid = fork()) == 0)
- { //子進程
- if((frandson_pid = fork()) == 0)
- { //孫進程
- printf("this is frandson process!\n");
- exit(0);
- }
- else
- { //子進程
- printf("this is son process!\n");
- wait();
- printf("frandson process exit!\n");
- exit(0);
- }
- }
- else
- { //父進程
- printf("this is father process!\n");
- wait();
- printf("son process exit!\n");
- sleep(3);
- }
- return 0;
- }
復制代碼
|
-
-
整理的網絡程序設計提綱.rar
2016-3-14 16:19 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
46.41 KB, 下載次數: 2, 下載積分: 黑幣 -5
|