当前位置:文档之家› Linux课程大作业

Linux课程大作业

Linux课程设计报告题目 Linux课程大作业院系班级姓名指导教师一、基础篇(给出源程序和编译运行的结果)1、编写一个简单的c语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。

源代码(c):源代码(汇编):.file "sum.c".text.globl average.type average, @functionaverage:.LFB0:.cfi_startprocpushl %ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl %esp, %ebp.cfi_def_cfa_register 5subl $8, %espmovl 12(%ebp), %eaxmovl 8(%ebp), %edxaddl %edx, %eaxmovl %eax, -4(%ebp)fildl -4(%ebp)fldl .LC0fdivrp %st, %st(1)leave.cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE0:.size average, .-average.section .rodata.align 4.LC2:.string"\350\257\267\350\276\223\345\205\245\344\270\244\344\270\252\346\225\260\ 357\274\214\345\233\236\350\275\246\345\210\206\345\211\262".LC3:.string "%d".LC4:.string"%d\344\270\216%d\347\232\204\345\271\263\345\235\207\345\200\274\346\23 0\257\357\274\232%lf\n".text.globl main.type main, @functionmain:.LFB1:.cfi_startprocpushl %ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl %esp, %ebp.cfi_def_cfa_register 5 andl $-16, %espsubl $48, %espmovl $0, 44(%esp) movl $.LC2, (%esp) call putsmovl $.LC3, %eax leal 40(%esp), %edx movl %edx, 4(%esp) movl %eax, (%esp) call __isoc99_scanf movl $.LC3, %eax leal 44(%esp), %edx movl %edx, 4(%esp) movl %eax, (%esp) call __isoc99_scanf movl 44(%esp), %edx movl 40(%esp), %eax movl %edx, 4(%esp) movl %eax, (%esp) call averagemovl 44(%esp), %ecx movl 40(%esp), %edx movl $.LC4, %eax fstpl 12(%esp)movl %ecx, 8(%esp) movl %edx, 4(%esp) movl %eax, (%esp)call printfleave.cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE1:.size main, .-main.section .rodata.align 8.LC0:.long 0.long 1073741824.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3".section .note.GNU-stack,"",@progbits执行结果:2、编写一个c语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤,至少十步以上)。

所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。

例如,153是一水仙花数,因为153=1³+5³+3³。

源代码:#include<stdio.h>int isnumber(int number){int ge=(number%100)%10;int shi=(number/10)%10;int bai=(number/100);printf("%d\n",ge);printf("%d\n",shi);printf("%d\n",bai);int temp=ge*ge*ge+shi*shi*shi+bai*bai*bai;if(temp==number){return 1;}else{return 0;}}int main(){int number=0;printf("input number!\n");scanf("%d",&number);if(number>1000&&number<0){printf("number limits outbounds!\n");}if(isnumber(number)){printf("number is format!\n");}else{printf("number is not format!\n");}return 0;}执行结果:3、设计一个程序,要求输出n个整数(n也由键盘输入)中的最大值,并为它编写makefile文件,用make编译后修成返回最小值,再编译,观察有多少文件不需要重新编译。

源代码:makefile文件:test:max.o main.ogcc max.o main.o -o testmain.o:main.c define.hgcc main.c -cmax.o:max.cgcc max.c -c结果图:改变程序后,只有max.c,main.c文件程序需要重新编译;4、编写一个程序,求2-n间的素数,n由键盘输入,循环变量分别从2到n、2到(int)sqrt(n),分别测出两个循环的所用时间。

源代码:#include "stdio.h"#include "math.h"int main(){int t1=0,t2=0;int n;int j,k,j2,k2;编译运行效果:5、设计一个程序,要求将10分别以十进制、八进制和十六进制输出。

程序设计:#include<stdio.h>int main(){int number=10;printf("十进制值:%d\n",number);printf("八进制值:%o\n",number);printf("十六进制值:%x\n",number);}运行结果:一、提高篇(三选二,划出程序流程图,给出程序源代码和编译运行的结果)1、设计两个程序,要求用命名管道FIFO,实现简单的文本文件或图片文件的传输功能。

流程图:源代码:#include <stdio.h>#include <signal.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>int main(){char s[128];int fd; FILE *fp;fp = fopen("./a.txt", "r");mkfifo("/tmp/fifo.tst", 0644);close(fd2);close(fd);return 0;}运行结果:2、设计两个程序,要求用消息队列,实现聊天程序,每次发言后自动在后面增加当前系统时间。

增加结束字符,比如最后输入“88”后结束进程。

流程图:接收端源代码:运行结果:3、设计两个程序,要求用mmap系统,实现简单的聊天程序。

二、应用篇给出程序设计思路和程序设计流程图,并给出程序源代码和编译运行的结果。

设计思路:要求生产者-消费者在固定的仓库空间条件下,生产者每生产一个产品将占用一个仓库空间,生产者生产的产品库存不能越过仓库的存储量,消费者每消费一个产品将增加一个仓库空间,消费者在仓库产品为零时不能再消费。

以下使用了两个信号量,一个用来管理消费者(sem_produce),一个用来管理生产者(sem_custom),sem_produce表示当前仓库可用空间的数量,sem_custom 表示当前仓库中产品的数量。

对于生产者来说,其需要申请的资源为仓库中的剩余空间,因此,生产者在生产一个产品前需申请sem_produce信号量,当此信号量的值大于零时,即有可用空间,将生产产品,并将sem_produce值减1,同时,当其生产一个产品后,当前仓库的产品数量加1,需要将sem_custom的值自动加1.对于消费者来说,其需要申请的资源为仓库中的产品,因此,消费者在消费一个产品前需申请sem_custom信号量,当此信号量的值大于零时,即有可用产品,将消费一个产品,并将sem_custom值减1,同时,当其消费一个产品后,当前仓库的剩余空间加1,需要sem_produce的值自动加。

1、生产者-消费者问题模拟程序。

流程图:生产者源代码:#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/sem.h>#include <errno.h>int sem_id;void init(){key_t key;int ret;unsigned short sem_array[2];union semun{int val;struct semid_ds *buf;unsigned short *array;消费者源代码:sops[1].sem_num = 1;sops[1].sem_op = 1;sops[1].sem_flg = 0;init();printf("this is customer\n");while(1){printf("\n\nbefore consume:\n");printf("productor is %d\n",semctl(sem_id,0,GETV AL));printf("space is %d\n",semctl(sem_id,1,GETV AL));semop(sem_id,(struct sembuf *)&sops[0],1); //get the productor to cusumeprintf("now consuming......\n");semop(sem_id,(struct sembuf *)&sops[1],1); //now tell the productor can bu produceprintf("\nafter consume\n");printf("products number is %d\n",semctl(sem_id,0,GETV AL));printf("space number is %d\n",semctl(sem_id,1,GETV AL));sleep(3);}}编译运行结果:3、实现基于GTK+的网络聊天室,实现通信双方的实时通信。

相关主题