嵌入式Linux应用程序开发期末考试题库及答案 下载本文

内容发布更新时间 : 2024/5/5 15:58:00星期一 下面是文章的全部内容请认真阅读。

4

下面的程序使用文件名的方式来查找可执行文件,同时使用参数列表的方式。请选出应填写在空白处的选项。 int main() { if(fork()==0) { /*此处相当于调用“ps -ef”命令*/ if( __________ (\ perror(\ } } A、execl B、execle C、execlp D、execve 3

下面的程序使用使用完整的文件目录来查找对应的可执行文件,同时使用参数列表的方式。请选出应填写在空白处的选项。 int main() { if(fork()==0) { /*注意此处已给出ps命令所在的完整路径*/ if( __________ (\ perror(\ } } A、execl B、execle C、execlp D、execve 1

下面的程序将环境变量添加到新建的子进程中去,这里的“env”是查看当前进程环境变量的命令使用使用完整的文件目录来查找对应的可执行文件,同时使用参数列表的方式。请选出应填写在空白处的选项。 命令的完整路径*/ perror(\ } } A、execl B、execle C、execlp D、execve 2

下面的程序将环境变量添加到新建的子进程中去,这里的“env”是查看当前进程环境变量的命令使用使用完整的文件目录来查找对应的可执行文件。请选出应填写在空白处的选项。 int main() { /*命令参数列表,必须以NULL结尾*/ char *arg[]={\ char *envp[]={\ if(fork()==0) { if( __________ (\ perror(\ } } A、execl B、execle C、execlp D、execve 4

if(

__________

int

main()

{

char

*envp[]={\ if(fork()==0) { /*注意此处已给出env

(\

下面的程序为服务器端应用程序,首先建立起socket,然后调用本地端口进行绑定,接着开始与客户端建立联系,并接收客户端发送的消息。请选出应填写在空白处的选项。 #define SERVPORT 3333 #define BACKLOG 10 #define MAX_CONNECTED_NO 10 #define MAXDATASIZE 5 int main() { struct sockaddr_in server_sockaddr,client_sockaddr; int sin_size,recvbytes; int sockfd,client_fd; char buf[MAXDATASIZE]; /*建立socket连接*/ if( __________ ) { perror(\ exit(1); } printf(\success!,sockfd=%d\\n\ /*设置sockaddr_in 结构体中相关参数*/ server_sockaddr.sin_family=AF_INET;

server_sockaddr.sin_port=htons(SERVPORT);

server_sockaddr.sin_addr.s_addr=INADDR_ANY; bzero(&(server_sockaddr.sin_zero),8); /*绑定函数bind*/ if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))== -1) { perror(\ exit(1); } printf(\success!\\n\ /*调用listen函数*/ if(listen(sockfd,BACKLOG)== -1) { perror(\ exit(1); } printf(\ /*调用if((client_fd=accept(sockfd,(struct

acceptsockaddr

函数,等待客户端的连接*/ *)&client_sockaddr,&sin_size))==

-1)

{

-1)

{ perror(\ exit(1); } /*调用recv函数接收客户端的请求*/ if((recvbytes=recv(client_fd,buf,MAXDATASIZE,0))== A、(sockfd = socket(AF_INET,SOCK_STREAM,0))== -1 B、(sockfd = socket(AF_INET,SOCK_STREAM,0))== 0 C、(sockfd = socket(AF_INET,SOCK_STREAM,-1))== -1 D、(sockfd = socket(AF_INET,SOCK_STREAM,-1))== 0 1

下面的程序为客户器端应用程序,客户端在建立起socket之后调用connect函数来建立连接。请选出应填写在空白处的选项。 #define SERVPORT 3333 #define MAXDATASIZE 100 main(int argc,char *argv[]) { int sockfd,sendbytes; char buf[MAXDATASIZE]; struct hostent *host; struct sockaddr_in serv_addr; if(argc < 2) { fprintf(stderr,\enter the server's hostname!\\n\ exit(1); } /*地址解析函数*/ if((host=gethostbyname(argv[1]))==NULL) { perror(\ exit(1); } /*创建socket*/ if( __________ ) { perror(\ exit(1); } /*设置sockaddr_in

结构体中相关参数*/

serv_addr.sin_family=AF_INET;

in_addr

serv_addr.sin_port=htons(SERVPORT);

serv_addr.sin_addr=*((struct

perror(\

exit(1); } printf(\ close(sockfd); }

*)host->h_addr); bzero(&(serv_addr.sin_zero),8); /*调用connect函数主动发起对服务器端的连接*/ if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))== -1) { perror(\ exit(1); } /*发送消息给服务器端*/ if((sendbytes=send(sockfd,\-1) { perror(\ exit(1); } close(sockfd); }

A、(sockfd = socket(AF_INET,SOCK_STREAM,0))== 0 B、(sockfd = socket(AF_INET,SOCK_STREAM,0))== -1 C、(sockfd = socket(AF_INET,SOCK_STREAM,-1))== 0 D、(sockfd = socket(AF_INET,SOCK_STREAM,-1))== -1 2

下面的程序首先新建一子进程,然后让其子进程暂停5s。接下来对原有的父进程使用阻

塞函数,并使用参数使该父进程不会阻塞。若有子进程退出,则阻塞函数返回子进程号;若没有子进程退出,则阻塞函数返回0,并且父进程每隔一秒循环判断一次。请选出应填写在空白处的选项。 int main() { pid_t pc,pr; __________; if(pc<0) printf(\\\n\ /*子进程*/ else if(pc==0) { /*子进程暂停5s*/ sleep(5); /*子进程正常退出*/ exit(0); } /*父进程*/ else { /*循环测试子进程是否退出*/ do { /*调用waitpid,且父进程不阻塞*/ pr=waitpid(pc,NULL,WNOHANG); /*若子进程还未退出,则父进程暂停1s*/ if(pr==0) { printf(\not exited\\n\ sleep(1); } } while(pr==0); /*若发现子进程退出,打印出相应情况*/ if(pr==pc) printf(\child %d\\n\ else printf(\error occured.\\n\ } } A、pc=fork() B、pc=open(pr) C、pc=pr D、pr=pc 1

下面的程序首先新建一子进程,然后让其子进程暂停5s。接下来对原有的父进程使用阻塞函数,并使用参数使该父进程不会阻塞。若有子进程退出,则阻塞函数返回子进程号;若没有子进程退出,则阻塞函数返回0,并且父进程每隔一秒循环判断一次。请选出应填写在空白处的选项。 int main() { pid_t pc,pr; pc=fork() if(pc<0) printf(\ /*子进程*/ else if(pc==0) { /*子进程暂停5s*/ sleep(5); /*子进程正常退出*/ exit(0); } /*父进程*/ else { /*循环测试子进程是否退出*/ do { /*调用阻塞函数,且父进程不阻塞*/ __________; /*若子进程还未退出,则父进程暂停1s*/ if(pr==0) { printf(\child process has not exited\\n\ sleep(1); } } while(pr==0); /*若发现子进程退出,打印出相应情况*/ if(pr==pc) printf(\ else printf(\ } } A、pc=wait(pc,NULL,WNOHANG) B、pc=waitpid(pc,NULL,WNOHANG) C、pr=wait(pc,NULL,WNOHANG) D、pr=waitpid(pc,NULL,WNOHANG) 4

下面的程序建立一个守护进程,该守护进程每隔10s在/tmp/dameon.log中写入一个字符串。请选出应填写在空白处的选项。 #define MAXFILE 65535 int main() { pid_t pc; int i,fd,len; char *buf=\is a Dameon\\n\ len =strlen(buf); /*第一步*/ __________; if(pc<0) { printf(\\\n\ exit(1); } else if(pc>0) exit(0); /*第二步*/ setsid(); /*第三步*/ chdir(\ /*第四步*/ umask(0); /*第五步*/ for(i=0;i

*/

while(1)

if((fd=open(\

{ perror(\ exit(1); } write(fd, buf, len+1); close(fd); sleep(10); } } A、pc=fork() B、fd=open(pc) C、pc=fd

D、pc=open(buf) 1

下面的程序建立一个守护进程,该守护进程每隔10s在/tmp/dameon.log中写入一个字符串。请选出应填写在空白处的选项。 #define MAXFILE 65535 int main() { pid_t pc; int i,fd,len; char *buf=\is a Dameon\\n\ len =strlen(buf); /*第一步*/ pc=fork(); if(pc<0) { printf(\\\n\ exit(1); } else if(pc>0) exit(0); /*第二步*/ setsid(); /*第三步*/ chdir(\ /*第四步*/ umask(0); /*第五步*/ __________ close(i); /*守护进程创建完成,以下开始正式进入守护进程工作*/ while(1) {

if((fd=open(\

{ perror(\ exit(1); } write(fd, buf, len+1); close(fd); sleep(10); } } A、for(i=3;i< MAXFILE;i--) B、for(i=0;i< MAXFILE;i--) C、for(i=3;i< MAXFILE;i++) D、for(i=0;i< MAXFILE;i++) 4

hello.c和hello.h位于同一目录下,源代码如下所示。 /*hello.c*/ int main() { printf(\everyone!\\n\} /*hello.h*/ #include 要求编写Makefile文件实现对这两个文件的编译,Makefile文件如下所示。请选出应填写在空白处的选项。 /*Makefile*/ hello:hello.c hello.h __________ A、gcc hello.c&hello.h -o hello B、gcc hello.c hello.h -o hello C、make hello.c&hello.h -o hello D、make hello.c hello.h -o hello 2

下面的程序建立一个守护进程,然后在该守护进程中新建一个子进程,该子进程暂停10s,然后自动退出,并由守护进程收集子进程退出的消息。子进程退出后,守护进程循环暂停,其间隔时间为10s。子进程和守护进程的退出消息均在/var/log/messages中输出。请选出应填写在空白处的选项。 #define MAXFILE 65535 int main(void) { pid_t child1,child2; int i; child1 = fork(); if( __________ ) { perror(\fork\ exit(1); } else if( child1 > 0 ) exit( 0 ); openlog(\LOG_PID, LOG_DAEMON); setsid(); chdir( \ umask( 0 ); for( i = 0 ; i < MAXFILE ; i++ ) { close( i ); } child2 = fork(); if( child2 == -1 ) { perror(\fork\ exit(1); } else if( child2 == 0 ) { syslog( LOG_INFO, \ sleep(10); syslog( LOG_INFO, \child2 is going to exit! \ exit(0); } else { waitpid( child2, NULL, 0); syslog( LOG_INFO , \child1 noticed that child2 has exited \ closelog(); while(1) { sleep(10); } } } A、child1 == -1 B、child1 == 0 C、child1 > 0 D、child1 >= 0 1

下面的程序实现对文件属性的查询。请选出应填写在空白处的选项。 static int get_file_size_time(const

char

*filename) { {

struct

stat stat

statbuf; on %s }

if(stat(filename,&statbuf)==-1) if(S_ISDIR(statbuf.st_mode))

printf(\

Error:%s\\n\return(-1);

return(1); if(S_ISREG(statbuf.st_mode))

printf(\ return(0); } int main(int argc,char **argv) { DIR *dirp; struct dirent *direntp; int stats; if( __________ ) { printf(\filename\\n\\a\ exit(1); } if(((stats=get_file_size_time(argv1))==0)||(stats==-1))exit(1); if((dirp=opendir(argv1))==NULL) { printf(\Directory %s Error:%s\\n\

exit(1);

}

while((direntp=readdir(dirp))!=NULL) if(get_file_size_time(direntp->d_name)==-1) break; closedir(dirp); exit(1); } A、argc!=1 B、argc!=2 C、argv!=1 D、argv!=2 1

程序设计题4

下面的程序实现文件的拷贝。请选出应填写在空白处的选项。 #define BUFFER_SIZE 1024 int main(int argc,char **argv) { int from_fd,to_fd; int bytes_read,bytes_write; char bufferBUFFER_SIZE; char *ptr; if( __________ ) { fprintf(stderr,\fromfile

tofile\\n\\a\

exit(1);

} }

if((from_fd=open(argv1,O_RDONLY))==-1) { fprintf(stderr,\%s Error:%s\\n\

exit(1);

if((to_fd=open(argv2,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)

{ fprintf(stderr,\%s Error:%s\\n\ exit(1); } while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))

{ if((bytes_read==-1)&&(errno!=EINTR)) break; else if(bytes_read>0) { ptr=buffer; while(bytes_write=write(to_fd,ptr,bytes_read)) { if((bytes_write==-1)&&(errno!=EINTR)) break; else

if(bytes_write==bytes_read) break; else if(bytes_write>0) {

ptr+=bytes_write;

bytes_read-=bytes_write; } } if(bytes_write==-1) break; } } close(from_fd); close(to_fd); exit(0); } A、argc!=0 B、argc!=1 C、argc!=2 D、argc!=3 4

下面的程序实现对字符串倒序输出。请选出应填写在空白处的选项。 int display1 (char

*string) { printf (\ __________ { char *string2; int size,i; size = strlen (string1); string2 = (char *) malloc (size + 1); for (i = 0; i< size; i++) string2[size+1] = ' '; printf(\{ char string[] = \ display1 (string); display2 (string); } A、int display2 (char *string) B、int display2 (char *string1) C、int display2 (char *string2) 2

下面的程序实现对字符串倒序输出。请选出应填写在空白处的选项。 int display1 (char *string) { printf (\original string is %s \\n\string); } int display2 (char *string1) { char *string2; int size,i; size = strlen (string1); string2 = (char *) malloc (size + 1); for (i = 0; i< size; i++) __________ ; string2[size+1] = ' '; printf(\string afterward is %s\\n\} int main () { char string[] = \ display1 (string); display2 (string); }

A、string2[size - i -1] = string1[i] B、string2[size - i ] = string1[i] C、string2[size - i + 1] = string1[i] D、string2[size - i ] = string1[i+1] 1