Linux C实验报告书模板(2)

2020-04-18 02:53

第二部分:实验调试与结果分析(可加页) 一、调试过程(包括调试方法描述、实验数据记录,实验现象记录,实验过程发现的问题等) 1. 设计并编码实现 1.1下面这个是进程创建的方法 #include #include int main(int args,char argv[]){ pid_t pid = fork(); if(pid == 0){ printf(\ }else{ printf(\ } return 0; } 1.2下面这个是多线程的源代码实现 #include #include #include #define MAX_THREAD 10 /// thread handler,just used to print some information and then exit. /// And threadPara in here is a child thread id. void *hello_thread(void *threadPara){ int tid = (int) threadPara; printf(\ thread which has id=%d \ // now exit the curent thread. pthread_exit(0); } int main(int args,char * argv[]){ // To store the thread handler but they can't be used in this // demo project so there is no need to store them. pthread_t threads[MAX_THREAD]; // to create threads int loop = 0; for(;loop < MAX_THREAD;loop++ ){ pthread_create(&threads[loop],0,hello_thread,(void *)loop);

} return 0; } 这个是多线程的makefile脚本代码 vpath %.c src main.out:main.o gcc $< -o $@ -lpthread main.o:main.c gcc -c $< .PHONY:clean rm -f *.o main.out 2. 调试运行程序 3. 程序运行结果 3.1下面的截图是进程创建的程序输出的结果: 3.2 下面这些是多线程程序运行输出的结果 Hi everybody this is a information come from a child thread which has id=1 Hi everybody this is a information come from a child thread which has id=0 Hi everybody this is a information come from a child thread which has id=3 Hi everybody this is a information come from a child thread which has id=2 Hi everybody this is a information come from a child thread which has id=4 Hi everybody this is a information come from a child thread which has id=5 Hi everybody this is a information come from a child thread which has id=6 Hi everybody this is a information come from a child thread which has id=8 Hi everybody this is a information come from a child thread which has id=7 Hi everybody this is a information come from a child thread which has id=9 二、实验结果及分析(包括结果描述、实验现象分析、影响因素讨论、综合分析和结论等) 从以上两个程序的输出结果来看程序已经达到了预期的结果,另外程序运行过程中并未抛出任何异常。此时实验中并未使用到任何进程间通信的而技术,所以整个程序相对来说是比较简单的。 三、实验小结、建议及体会 通过这个实验,让我对多线程的概念有了更深一层的理解; 一个线程包括程序计数器,堆栈和一些列寄存器,其他的数据结构等资源属于进程所有。

实验课程名称: Linux 应用开发技术

实验项目名称 实验者 同组者 Linux network socket programming 徐泽前 专业班级 软件0803 实验成绩 组别 实验日期 年 月 日 第一部分:实验分析与设计(可加页) 一、实验内容描述(问题域描述) Socket接口是TCP/IP网络的API,Socket接口定义了许多函数或例程,程序员可以用它们来开发TCP/IP网络上的应用程序。要学Internet上的TCP/IP网络编程,必须理解Socket接口。 Socket接口设计者最先是将接口放在Unix操作系统里面的。如果了解Unix系统的输入和输出的话,就很容易了解Socket了。网络的 Socket数据传输是一种特殊的I/O,Socket也是一种文件描述符。Socket也具有一个类似于打开文件的函数调用Socket(),该函数返 回一个整型的Socket描述符,随后的连接建立、数据传输等操作都是通过该Socket实现的。常用的Socket类型有两种:流式Socket (SOCK_STREAM)和数据报式Socket(SOCK_DGRAM)。流式是一种面向连接的Socket,针对于面向连接的TCP服务应用;数据 报式Socket是一种无连接的Socket,对应于无连接的UDP服务应用。 实验目的 通过实验了解linux下的socket通信,并掌握linux下socket编程的基本方法。 三、基本原理与设计(包括实验方案设计,实验手段的确定,试验步骤等,用硬件逻辑或者算法描述) 1.了解linux 下socket通信的基本原理与编程方法 2.编写项目源代码,这里是实现一个及其简单的socket点对点通信,分别实现server和client端 3.运行、调试程序 4.分析程序运行结果 三、主要仪器设备及耗材 设备:计算机一台 OS: Linux ubuntu Editor: VIM

第二部分:实验调试与结果分析(可加页) 一、调试过程(包括调试方法描述、实验数据记录,实验现象记录,实验过程发现的问题等) 1. 设计实现服务端和客户端 ----------------------------------------------------以下是客户端的代码------------------------------------- #include #include #include #include #include // for bzero() #include // strlen(); #include void client(){ // create a client socket struct sockaddr_in sock_cl; bzero(&sock_cl,sizeof(struct sockaddr_in)); int client_fd = socket(AF_INET,SOCK_STREAM,0); if(-1!= client_fd){ sock_cl.sin_family = AF_INET; sock_cl.sin_port = htons(8080); inet_aton(\ // now connecting to server int rec = connect(client_fd,&sock_cl,sizeof(struct sockaddr_in)); if(rec!= -1){ char buf[1024]; while(1){ memset(buf,'\\0',1024); int r = read(client_fd,buf,1024); if(r>0){ printf(\ } memset(buf,'\\0',1024); printf(\ scanf(\ write(client_fd,buf,strlen(buf));

} }else{ printf(\ } }else{ } } int main(int argc,char *argv[]){ client(); return 0; } ----------------------------------------------------以下是服务端的代码------------------------------------- #include #include #include #include #include // for bzero() #include // strlen(); #include void server(){ struct sockaddr_in svr; // clear the struct bzero(&svr,sizeof(struct sockaddr_in)); // create an server socket. int sockfd = socket(AF_INET,SOCK_STREAM,0); if(-1!=sockfd){ svr.sin_family = AF_INET; svr.sin_addr.s_addr = htonl(INADDR_ANY); svr.sin_port = htons(8080); // bind the port bind(sockfd,&svr,sizeof(svr)); // now begin to listen the special port listen(sockfd,5); printf(\ // handle the client's requst int client = accept(sockfd,NULL,NULL); if(client !=-1){ char buf[1024];


Linux C实验报告书模板(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:钢材质保书及复试报告汇总表

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: