admin 管理员组

文章数量: 1184232


2024年3月5日发(作者:句柄类)

山东科技大学2016—2017学年第2学期

《嵌入式系统开发及应用》考试

题号

得分

总得分

评卷人

审核人

一、选择题(共20分)

1.对于Linux而言,所有对设备和文件的操作都使用 文件描述符 来进行。

2.设置文件的存取权限,分为 读 、 写 和 执行 3类。每类分为 用户 、

和 系统 权限。

3.当打开一个流时,标准I/O函数返回一个 指向文件流结构的指针 。

4.有3种类型的无格式I/O函数可用来读写流,它们是 字符I/O 、 块I/O 和 行I/O 。

5.进程在其生存期内可能处于3种基本状态: 就绪态 、 执行态 和 等待态 。

6.Linux支持UNIX System V中的3种进程间通信机制,它们是 共享内存、 消息队列

和 信号集 。

7.线程可分为 用户 态线程和 内核态 线程。

8. TCP 套接字定义了一种可靠的面向连接的服务,实现了无差错无重复的顺序数据传输。 UDP 套接字定义了一种无连接的服务,数据通过相互独立的报文进行传输。

二、程序填空题(共20分)

1.下列是建立管道通信程序,父进程等待子进程退出。请补充完整。

#define MAX_DATA_LEN 256

int main()

{

pid_t pid;

int pipe_fd[2];

char buf[MAX_DATA_LEN];

char data[]="Pipe Test Program";

int real_read,real_write;

memset((void*)buf,0,sizeof(buf));

if(pipe(pipe_fd)<0)

{

- 1 -

printf("pipe create errorn");

exit(1);

}

if((pid=fork())==0)

{

close(pipe_fd[1]);

if((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0)

printf("%d bytes read from the pipe is'%s'n",real_read,buf);

close(pipe_fd[0]);

exit(0);

}else if(pid>0){

close(pipe_fd[0]);

if((real_write=write(pipe_fd[1],data,strlen(data)))!=-1)

printf("Parent wrote%d bytes:'%s'n",real_write,data);

close(pipe_fd[1]);

wait(NULL) ;

exit(0);

}

}

2.下列是建立守护进程程序。请补充完整。

int main()

{

pid_t pid;

int i,fd;

char*buf="This is a Daemonn";

pid=fork();

if(pid<0){

printf("Error forkn");

exit(1);

}

else if(pid>0)

(1) ;

setsid();

chdir("/");

umask(0);

for(i=0;i

(2) ;

/*这时创建完守护进程,以下开始正式进入守护进程工作*/

- 2 -

三、程序阅读题(20分)

1.阅读程序,请写出程序实现的功能。

#include

#include

#include

#include

#include

void main()

{

pid_t pc,pr;

pc=fork();

if (pc==0) {

printf("this is child process with pid of %dn",getpid());

sleep(10);

}

else if (pc>0)

{

pr=wait(NULL);

printf("I catched a child process with pid of %dn",pr)

}

exit(0);

}

定义了两个进程描述符pc、pr,并创建了一个进程,在子进程中显示:this

is child prodess with pid of,并使用getpid函数获得当前进程的进程标识号,父进程中等待子进程结束显示:I catched a child process with pid of,使用wait等待子进程结束并获得退出状态值。

2、阅读程序,增加注释并给出程序功能

#include

#include

#include

int main(int argc,char **argv)

{

double y; //定义双精度变量

sigset_t intmask; //定义sigset_t结构中变量

int i,repeat_factor; //两个整形变量

if(argc!=2) //判断输入指令的个数是否满足要求

{

printf("Usage:%s repeat_factorna",argv[0]);//如果不满足,输出错误提示信息

- 3 -

exit(1); //退出

}

if((repeat_factor=atoi(argv[1]))<1)

repeat_factor=10; //使用atoi函数把字符串转换为整形,如果失败使repeat_factor赋值为10

sigemptyset(&intmask);//清空intmask

sigaddset(&intmask,SIGINT); //把SIGINT信号增加到intmask中

while(1) //循环执行下边的代码

{

sigprocmask(SIG_BLOCK,&intmask,NULL); //SIG_BLOCK表示加入SIG_INT到intmask中信号屏蔽

printf("SIGINT signal blockedn"); //输出SIG_INT这个信号已经被屏蔽

for(i=0;i

{

printf("Blocked calculation is finishedn"); //输出该行信息

sleep(2);//睡眠2秒

}

sigprocmask(SIG_UNBLOCK,&intmask,NULL); //SIG_UNBLOCK表示intmask信号屏蔽集中删除信号

printf("SIGINT signal unblockedn"); //输出此行信息,SIG_INT信号已被解除

for(i=0;i

{

printf("Unblocked calculation is finishedn"); //输出该行信息

}

}

exit(0); //程序退出

}

四、编程题(共40分)

1.多进程编程:创建第一子进程并用exec函数列出当前文件夹中文件的详细信息;创建第二子进程并休眠5s退出;父进程收集第一子进程的退出信息并显示退出状态值;父进程多次判断第二子进程是否结束,如果未结束继续查询,如果结束收集其退出信息(waitpid函数)。

- 4 -

#include

#include

#include

#include

#include

#include

void handle(int sig)

{

if(sig==SIGCHLD)

{

int pid;

int status;

while((pid=waitpid(-1,&status,NULL))>0)

{}

}

}

int main(int argc, const char *argv[])

{

pid_t pid1,pid2,pr;

int i;

if(pid1=fork()<0)

{

printf("fork the first pid1 errorn");

exit(1);

}

else if(pid1==0)

{

exec();

exit(0);

}

else

{

pr=wait(NULL);

printf("第一子进程的推出状态值:n",pr);

exit(0);

if(pid2=fork()<0)

{

printf("fork the second pid2 errprn");

- 5 -

exit(1);

}

if(pid2==0)

{

sleep(5);

exit(0);

}

else

{

int ret;

signal(SIGCHLD,handle);

deadn");

}

}

return 0;

}

while(1)

{

sleep(1);

ret=kill(pid2,0);

if(ret<0)

{

printf("the child has been }

}

- 6 -


本文标签: 进程 函数 退出 文件 程序