中南大学linu程序设计环境实验四

上传人:jin****ng 文档编号:110723740 上传时间:2022-06-19 格式:DOC 页数:9 大小:197.50KB
返回 下载 相关 举报
中南大学linu程序设计环境实验四_第1页
第1页 / 共9页
中南大学linu程序设计环境实验四_第2页
第2页 / 共9页
中南大学linu程序设计环境实验四_第3页
第3页 / 共9页
亲,该文档总共9页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
实验4 Linux高级程序设计1、实验目的(1) 了解Linux操作系统下应用程序开发流程(2) 掌握gun工具链的使用(3) 了解Linux高级编程技巧(例如IPC机制、系统调用等)2、实验内容(1) 编写一个简单的C语言程序,编写Makefile文件。了解编译过程,并用 gdb进行调试。(2) 编写一段程序,适用系统调用fork()创建两个新的子进程,此程序运行使 系统中有三个活动进程存在。控制让一个进程在屏幕上显示一个字符:父进程显 示“a”,子进程分别显示字符“b”和字符“c”。试观察屏幕上的显示结果,并 对输出结果进行分析。在实验报告中写出说明和分析。(3) 编写一段程序实现父进程创建一个子进程,通过系统调用使子进程和父 进程执行不同的任务。(提示:利用system。, exec系列函数)试思考,怎样实现进程间的同步?(4) 编写一段C程序完成:父进程创建一个子进程,父进程对子进程设置一 个报警信号,然后父进程等待子进程的结束,如果此时报警信号先到,就终止子 进程。3、实验要求(1) 写出源程序,并编译运行(2) 详细记录程序调试及运行结果4、实验思路及过程File Edit View Terminal Tabs 旦Elpqinpengchengtaso ft ware $ suPassword:rootsoftware rootsoftwareoot 0sof 1:诃日Erootsoftwarerootsoftware-# cd /# mkdir c/# cd /cc# vi makefileroot(asof twaremake: * No rule to make target main.cr needed by main. Stop. oot电softwareroot(asof twareroot(asof twareoot电twareoot电5oftwareroot(asof twarec#make -f malkefiletouch a.h touch b.h touch c.h vivivimain.c1. c2. cmakefile 文件:Q main.c 耳main.c +7include include include a.hextern void functiononef); extern void function_two();int main(function_one(;function_two(; exit (EXITSUCCESS);1.C文件:0 1C K /* l.C */include include include #includea.K”b.h” void function_one() printf( function one .2. c文件:|/* 2.C */include #include #include include“b.h”c.b void functiontwof) printf(functiontwo.-o写成-c,函数库导入不正确:root(asoftware c# make -f makefilegcc -c main.cgcc -c 1.cl. c: In function function onef :1. c:6: warning: incampatible implicit declaration of built-in function printf gcc -c 2.c2. c: In function function twoF:2. c:6: warning: incampatible implicit declaration of built-in function printf gcc -c myapp main.o 1.o 2.ogcc: myapp: No such file directorymake: * imyapp Error 1 root(asof tware c# vi 2. croot(3sof tware c# vi main. coot电software c# make -f makefile gcc -c main.cgcc -c 1.cgcc -c 2.cgcc -o myapp irain. 1. o 2. orootsoftware c# |oot 电 soft ware c# l_s -1_ total 80w1rootroot132Jan108461. c-rw-r-1rootroo 1:860Jan108471.0w1rootroot133Jan108462.cw1rootroot860Jan108472.0-rw-r-1rootroo 1:0Dec311857a. hw1rootroot0Dec311857b.hw1rootroot0Dec311858c. h-rw-r-1rootroo 1:197Jan10847main. cw-r-1rootroot056Jan10847main.ow-r-1rootroot144Jan10834makefile-rwxr-xr-x1ootoot5068Jan10847myapprootsoftware c#rootfasoftware c# gcc -o myapp main.c/tmp/ccQ9Ys9n.o: In function main:umin.e:仁 tExt+0x12) : undefined reference to function. onemain.c:(.+0x17): undefined reference to function. two colLect2: Id eturned 1 exit Etmtumroot世software c# gdb myappGNU gdb GDB Red Hat Enterprise Linux 7.0.1-23.e!5)Copyright (C) 2009 Free Software Foundationr Inc.License GPLv3+: GNU GPL version 3 or LmtE vhttp:/gnu.org/licenses/gpl.html This is free moftware: you are free to change and redistribute it.There is NO WARRANTYr to the extent permitted by law. Type show copying and show warranty fo details.This GDB was configured as 1386-redhat-linux-gnu.For bug reporting instructionsr please see:vhttp:ftware/gdb/bugs/.myapp: No such file or directoy.qdb) 1Q fork.c#includemainf)int plTp2; if(pl=fork() putcha r(b);elseif (p2=fork() putchar( c);else putchar(1 a); qinpengcheng(aso ft ware - $ gcc -o fork f ork. c qinpengcheng(asoftware -$ ./forkacb qinpengcheng(3so ft ware. /forkabqinpengcheng(3software -$ c./fork acb qinpengcheng(3so ft ware. /forkacb qinpengcheng(aso ft ware - $ . /fork bqinpengcheng(3sof$ ca./fork-$./fork-$./fork-$./fork-$./fork-$./fork-$./fork-$./forkacb qinpengcheng(3so ft ware acb qinpengcheng(3so ft ware acb qinpengcheng(3so ft ware acb qinpengcheng(aso ft ware acb qinpengcheng(aso ft ware acb qinpengcheng(asoftware acb qinpengcheng(3so ft wareb qinpengcheng(3so ft ware$ ac出现acb的结果:pl=fork()会创建一个新的进程,父进程遇到p2=fork() 创建又一个新的进程,此时,父进程运行完毕输出a, p2=fork()创建的子进程运行完毕输出c,最后p1=fork()创建的子进程运行完毕输出b;出现abc的结果:1=fork()会创建一个新的进程,父进程遇到p2=fork() 创建又一个新的进程,此时,父进程运行完毕输出a,p1=fork()创建 的子进程运行完毕输出b,最后p2=fork()创建的子进程运行完毕输出c;出现bca的结果:pl=fork()会创建一个新的子进程,运行完毕输出b, 父进程遇到p2=fork()创建又一个新的子进程,运行完毕输出c,最后 父进程运行完毕输出a;出现bac的结果:p1=fork()会创建一个新的子进程,运行完毕输出b, 父进程遇到p2=fork()创建又一个新的子进程,父进程运行完毕输出a, 最后子进程运行完毕输出c;(3)Q process.c 耳include include include int main()int result;printf(Running Is with system:n; result = system(Is /home);printf(system result=%dn#result;printf(system has done!);printf(Running Is with exec:n); result = execlpCls, Is,printf(exec result=%dn;result);printf(exec has done!);exit(0);qinpengchengtasoftware -$ gcc -o process process.cqinpengcheng(asoftware./processRunning Is with systeni:opt qinpengchengsystem resdl_t=0systeni has done! Running Is with exec: bindevgcc461 lost+found mntprocselinuxtmpboot etchomemedianetrootsrvsrcg+4&l libmiscoptsbinvarqinpengcheng(asoftware |system是用shell来调用程序二fork+exec+waitpid,而exec是直接让你的 程序代替原来的程序运行。system是在单独的进程中执行命令,完了还会回到 你的程序中。而exec函数是直接在你的进程中执行新的程序,新的程序会把你 的程序覆盖,除非调用出错,否则你再也回不到exec后面的代码。这就是为什么看不到之后的两条打印语句的原因。用P, V原语操作实现进程间的同步:1. 为各并发进程设置私用信号量2. 为私用信号量赋初值3. 利用P、v原语和私用信号量规定各进程的执行顺序。例:设进程PA和PB通过缓冲区队列传递数据。PA为发送进程、PB为接收 进 程。PA发送数据时调用发送过程 deposit(data),PB接收数据时调用过程 remove(data)。且数据的发送和接收过程满足如下条件:1)在PA至少送一块数 据入一个缓冲区之前,PB不可能从缓冲区中取出数据(假定数据块长等于缓冲区 长度),2)PA往缓冲队列发送数据时,至少有一个缓冲区是空的;3)由PA发 送的数据块在缓冲队列中按先进先出(FIFO)方式排列。描述发送过程 deposit(data)和接收过程 remove(data)。(4)#include #include #include #include #include #include int main()int id = fork();(void) signal(SIGALRM,ding);if(id 0)int statloc;kill(id,SIGALRM); wait(&statl oc); pri ntf (s tatl oc:%dn,s tatl oc); if(WIFEXITED(statloc)printf(EXITWITH:%dn,WEXITSTATUS(statloc);if(WIFSIGNALED(statloc)printf(KILLED WITH:%dn,WTERMSIG(statloc);exi t(0);alarm c:13: tpwarning:incompatibleimplicitdeclarationofbuilt-infunctionJexiLalarm.c:24:warning:incompatibleimplicitdeclarationofbuilt-infunctionJprintfralarm.c:30:warning:incompatibleimplicitdeclarationofbuilt-infunctionJexiqinpenacheng(3so ft ware - $5、心得体会本次实验加深了 linux中对C语言编程的理解,了解了一些常用的函数,会 编写一些简单的程序,但是不够熟练,需要日后的练习强化。
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 活动策划


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!