﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>操作系统 &#8211; 学术创新中心</title>
	<atom:link href="https://www.leexinghai.com/aic/category/gcc/2stls/os/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.leexinghai.com/aic</link>
	<description>Academic Innovation Center</description>
	<lastBuildDate>Sat, 10 Jun 2023 10:47:02 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.leexinghai.com/aic/wp-content/uploads/2025/08/cropped-徽标名称-32x32.jpg</url>
	<title>操作系统 &#8211; 学术创新中心</title>
	<link>https://www.leexinghai.com/aic</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>H30615-实验八-主存管理</title>
		<link>https://www.leexinghai.com/aic/h30615-%e5%ae%9e%e9%aa%8c%e5%85%ab-%e4%b8%bb%e5%ad%98%e7%ae%a1%e7%90%86/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Sat, 10 Jun 2023 05:25:08 +0000</pubDate>
				<category><![CDATA[操作系统]]></category>
		<guid isPermaLink="false">https://aic.leexinghai.com/?p=2751</guid>

					<description><![CDATA[一、实验目的 1、学会内存的申请、释放和管理 2、掌握内存的分配方法&#160; （FF,BF,WF） 3用链 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>一、<strong>实验目的</strong></p>



<p>1、学会内存的申请、释放和管理</p>



<p>2、掌握内存的分配方法&nbsp; （FF,BF,WF）</p>



<p>3用链表的形式输出内存申请和释放过程的状态，通过编程进一步了解内存管理。</p>



<p>二、<strong>实验内容</strong></p>



<p>用Vim编写一个程序：仿真实现某个内存管理算法，测试给出结果，解释运行结果。</p>



<p>三、<strong>实验主要步骤</strong></p>



<p>1、记录内存空间使用情况</p>



<p>2、记录空闲分区</p>



<p>3、内存分配算法-最佳分配算法（Best Fit Allocation）的原理是空闲分区列表按照大小排序，在分配时，查找一个合适的分区（分配n字节分区时，查找并使用不小于n的最小空间分区）；在释放时，查找并且合并临近的空闲分区（如果找到的话）。</p>



<p>4、内存释放算法-运行结果-产生测试数据-随机为3个进程分配、释放内存10次以上，即随机产生10组以上数据。</p>



<p>四、<strong>实验结果测试</strong></p>



<p>1.程序源代码如图1-图16所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="626" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-71-626x1024.png" alt="" class="wp-image-2824" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-71-626x1024.png 626w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-71-183x300.png 183w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-71.png 737w" sizes="(max-width: 626px) 100vw, 626px" /><figcaption class="wp-element-caption">图1</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="865" height="920" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-72.png" alt="" class="wp-image-2825" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-72.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-72-282x300.png 282w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-72-768x817.png 768w" sizes="(max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图2</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="865" height="1019" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-73.png" alt="" class="wp-image-2826" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-73.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-73-255x300.png 255w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-73-768x905.png 768w" sizes="(max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图3</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="705" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-74.png" alt="" class="wp-image-2827" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-74.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-74-300x245.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-74-768x626.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图4</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="698" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-75.png" alt="" class="wp-image-2828" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-75.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-75-300x242.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-75-768x620.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图5</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="723" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-76-723x1024.png" alt="" class="wp-image-2830" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-76-723x1024.png 723w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-76-212x300.png 212w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-76-768x1087.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-76.png 854w" sizes="auto, (max-width: 723px) 100vw, 723px" /><figcaption class="wp-element-caption">图6</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="1016" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-77.png" alt="" class="wp-image-2831" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-77.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-77-255x300.png 255w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-77-768x902.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图7</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="700" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-78-700x1024.png" alt="" class="wp-image-2832" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-78-700x1024.png 700w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-78-205x300.png 205w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-78-768x1124.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-78.png 836w" sizes="auto, (max-width: 700px) 100vw, 700px" /><figcaption class="wp-element-caption">图8</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="898" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-79.png" alt="" class="wp-image-2833" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-79.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-79-289x300.png 289w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-79-768x797.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图9</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="761" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-80-761x1024.png" alt="" class="wp-image-2834" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-80-761x1024.png 761w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-80-223x300.png 223w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-80-768x1033.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-80.png 865w" sizes="auto, (max-width: 761px) 100vw, 761px" /><figcaption class="wp-element-caption">图10</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="928" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-81.png" alt="" class="wp-image-2835" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-81.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-81-280x300.png 280w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-81-768x824.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图11</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="753" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-82-753x1024.png" alt="" class="wp-image-2836" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-82-753x1024.png 753w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-82-220x300.png 220w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-82-768x1045.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-82.png 865w" sizes="auto, (max-width: 753px) 100vw, 753px" /><figcaption class="wp-element-caption">图12</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="886" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-83.png" alt="" class="wp-image-2837" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-83.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-83-293x300.png 293w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-83-768x787.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图13</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="979" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-84.png" alt="" class="wp-image-2838" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-84.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-84-265x300.png 265w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-84-768x869.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图14</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="996" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-85.png" alt="" class="wp-image-2839" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-85.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-85-261x300.png 261w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-85-768x884.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图15</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="689" height="145" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-86.png" alt="" class="wp-image-2840" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-86.png 689w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-86-300x63.png 300w" sizes="auto, (max-width: 689px) 100vw, 689px" /><figcaption class="wp-element-caption">图16</figcaption></figure>
</div>


<p>2.使用gcc编译后，运行测试，进行内存的第一次分配操作：创建一个新进程，测试以PPT内容数据为例，分配给进程1的内存大小为24，然后显示内存使用情况，如图17所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="629" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-17.png" alt="" class="wp-image-2759" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-17.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-17-300x218.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-17-768x558.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图17</figcaption></figure>
</div>


<p>3.进行内存的第二次分配操作：继续创建一个新进程，分配给进程2的内存大小为74，然后显示内存使用情况，如图18所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="818" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-18-818x1024.png" alt="" class="wp-image-2760" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-18-818x1024.png 818w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-18-240x300.png 240w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-18-768x962.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-18.png 865w" sizes="auto, (max-width: 818px) 100vw, 818px" /><figcaption class="wp-element-caption">图18</figcaption></figure>
</div>


<p>4.进行内存的第三次分配操作：继续创建一个新进程，分配给进程3的内存大小为36，然后显示内存使用情况，如图19所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="816" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-19-816x1024.png" alt="" class="wp-image-2761" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-19-816x1024.png 816w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-19-239x300.png 239w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-19-768x964.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-19.png 865w" sizes="auto, (max-width: 816px) 100vw, 816px" /><figcaption class="wp-element-caption">图19</figcaption></figure>
</div>


<p>5.进行内存的第一次释放操作：终止已有进程，选择进程3，然后显示内存使用情况，如图20所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="832" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-20-832x1024.png" alt="" class="wp-image-2762" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-20-832x1024.png 832w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-20-244x300.png 244w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-20-768x945.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-20.png 865w" sizes="auto, (max-width: 832px) 100vw, 832px" /><figcaption class="wp-element-caption">图20</figcaption></figure>
</div>


<p>6.进行内存的第二次释放操作：终止已有进程，选择进程1，然后显示内存使用情况，可见此时内存空闲区因为是在连续的地址中存在进程2使用内存，将进程1和进程3的内存空间隔断了，如图21所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="826" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-21-826x1024.png" alt="" class="wp-image-2763" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-21-826x1024.png 826w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-21-242x300.png 242w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-21-768x952.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-21.png 865w" sizes="auto, (max-width: 826px) 100vw, 826px" /><figcaption class="wp-element-caption">图21</figcaption></figure>
</div>


<p>7.进行内存的第四次分配操作：继续创建一个新进程，分配给进程4的内存大小为50，此时虽然有第一段空闲地址，但由于大小不匹配，因此使用了第二段空闲部分的地址。显示内存使用情况，如图22所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="661" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-22.png" alt="" class="wp-image-2764" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-22.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-22-300x229.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-22-768x587.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图22</figcaption></figure>
</div>


<p>8.进行内存的第五次分配操作：继续创建一个新进程，分配给进程5的内存大小为16，此时第一段空闲地址大小符合，因此使用了第一段空闲部分的地址。显示内存使用情况，如图23所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="791" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-23.png" alt="" class="wp-image-2765" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-23.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-23-300x274.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-23-768x702.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图23</figcaption></figure>
</div>


<h2 class="wp-block-heading">五、附件下载</h2>



<div class="wp-block-file"><a id="wp-block-file--media-8f619bb8-8f87-4f86-9e48-23d02a4eb857" href="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/实验8代码.7z">实验8代码</a><a href="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/实验8代码.7z" class="wp-block-file__button wp-element-button" download aria-describedby="wp-block-file--media-8f619bb8-8f87-4f86-9e48-23d02a4eb857">下载</a></div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>H30611-实验七-信号与管道通信</title>
		<link>https://www.leexinghai.com/aic/h30611-%e5%ae%9e%e9%aa%8c%e4%b8%83-%e4%bf%a1%e5%8f%b7%e4%b8%8e%e7%ae%a1%e9%81%93%e9%80%9a%e4%bf%a1/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Sun, 04 Jun 2023 02:25:56 +0000</pubDate>
				<category><![CDATA[操作系统]]></category>
		<guid isPermaLink="false">https://aic.leexinghai.com/?p=2727</guid>

					<description><![CDATA[一、实验目的 1. 加深理解信号和管道的概念及实现进程间通信的原理。 2. 掌握信号通信机制，学会通过信号实现 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>一、<strong>实验目的</strong></p>



<p>1. 加深理解信号和管道的概念及实现进程间通信的原理。</p>



<p>2. 掌握信号通信机制，学会通过信号实现进程间的同步通信。</p>



<p>3. 掌握匿名管道通信机制，学会通过匿名管道实现进程间的数据通信。</p>



<p>二、<strong>实验内容</strong></p>



<p>本实验实现父子进程之间的管道通信。程序中由父进程创建一个匿名管道，并创建了2个子进程。两个子进程都从管道写端（入口）写入一个发给父进程的消息（字符串），父进程则从管道读端（出口）读取发来的两个消息。</p>



<p>三、<strong>实验主要步骤</strong></p>



<p>(1)程序设计</p>



<p>下面是程序的部分代码，请完成该程序的设计。</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p><code>/*test7_1.c：匿名管道*/</code></p>



<p><code># include &lt;stdio.h&gt;</code></p>



<p><code># include &lt;unistd.h&gt;</code></p>



<p><code>int main( )</code></p>



<p><code>{</code></p>



<p><code>int i ,p1 ,p2 , pid , fd[2] ;</code></p>



<p><code>&nbsp;&nbsp; &nbsp;char buf[80];</code></p>



<p><code>&nbsp; &nbsp;&lt;创建一个匿名管道&gt;</code></p>



<p><code>&nbsp; &nbsp;while((p1=fork( ))==-1);</code></p>



<p><code>&nbsp; &nbsp;if (p1==0) {&nbsp; /*子进程1代码*/</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;sprintf (buf,”Message of child_1”); /*在buf中写入消息文本*/</code></p>



<p><code>&nbsp; &nbsp;&lt;向管道写入消息&gt;</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;printf (“Child_1 write an message to pipe!\n”);</code></p>



<p><code>&nbsp;&nbsp; &nbsp;for( i=0; i&lt;99999; i++);</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&lt;终止&gt;</code></p>



<p><code>}</code></p>



<p><code>&nbsp; else{</code></p>



<p><code>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while((p2=fork())==-1);</code></p>



<p><code>          if (p2==0){   /*子进程2代码*/</code></p>



<p><code>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf (buf,”Message of child_2”);</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&lt;向管道写入消息&gt;</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;printf (“Child_2 write an message to pipe!\n”);</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;for( i=0; i&lt;99999; i++);</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&lt;终止&gt;</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}</code></p>



<p><code>&nbsp; else {&nbsp;&nbsp; /*父进程代码*/</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&lt;等待子进程终止&gt;</code></p>



<p><code>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;从管道中先后读出两个消息，并输出相关提示信息&gt;</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&lt;关闭管道&gt;</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;printf(“OVER\n”);</code></p>



<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;}</code></p>



<p><code>               }</code></p>



<p><code>return 0;</code></p>



<p><code>}        </code></p>
</div></div>



<p>要求程序输出结果如下所示：</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p><code>Child_1 write an message to pipe!</code></p>



<p><code>Child_2 write an message to pipe!</code></p>



<p><code>Parent read an message of child_1: Message of child_1</code></p>



<p><code>Parent read an message of child_2: Message of child_2</code></p>



<p><code>OVER</code></p>
</div></div>



<p>其中，前两行的顺序可颠倒，第3和第4行的顺序也可颠倒。</p>



<p>或：</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p><code>Child_1 write an message to pipe!</code></p>



<p><code>Parent read an message of child_1: Message of child_1</code></p>



<p><code>Child_2 write an message to pipe!</code></p>



<p><code>Parent read an message of child_2: Message of child_2</code></p>



<p><code>OVER</code></p>
</div></div>



<p>或：</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p><code>Child_2 write an message to pipe!</code></p>



<p><code>Parent read an message of child_2: Message of child_2</code></p>



<p><code>Child_1 write an message to pipe!</code></p>



<p><code>Parent read an message of child_1: Message of child_1</code></p>



<p><code>OVER</code></p>
</div></div>



<p>(2)编辑、编译、调试该程序。</p>



<p>(3)多次运行test7_1，观察运行结果。</p>



<p>如果出现不符合要求的输出结果，问题出在什么地方？</p>



<p>修改程序，直到无错为止。</p>



<p>四、<strong>实验结果测试</strong></p>



<p>1.修改所给代码，即进行创建一个匿名管道，分别向管道1，2写入消息并设置终止信号，待子进程终止后从管道先后读出两个消息，并输出相关提示信息。最后关闭管道。对于前30行的代码修改，如图1所示，对于31至44行的代码修改，如图2所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="781" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image.png" alt="" class="wp-image-2728" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-300x271.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-768x693.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图1</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="277" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-1.png" alt="" class="wp-image-2729" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-1.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-1-300x96.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-1-768x246.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图2</figcaption></figure>
</div>


<p>2.编译生成代码，结果如图3所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="862" height="353" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-2.png" alt="" class="wp-image-2730" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-2.png 862w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-2-300x123.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/06/image-2-768x315.png 768w" sizes="auto, (max-width: 862px) 100vw, 862px" /><figcaption class="wp-element-caption">图3</figcaption></figure>
</div>


<h2 class="wp-block-heading">六、附件下载</h2>



<div class="wp-block-file"><a id="wp-block-file--media-5d1b5df3-9cba-4164-ba55-a9ce7b310edc" href="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/test7_1.7z">test7_1</a><a href="https://www.leexinghai.com/aic/wp-content/uploads/2023/06/test7_1.7z" class="wp-block-file__button wp-element-button" download aria-describedby="wp-block-file--media-5d1b5df3-9cba-4164-ba55-a9ce7b310edc">下载</a></div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>H30604-实验六-线程同步与互斥</title>
		<link>https://www.leexinghai.com/aic/h30604-%e5%ae%9e%e9%aa%8c%e5%85%ad-%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5%e4%b8%8e%e4%ba%92%e6%96%a5/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Mon, 29 May 2023 00:30:20 +0000</pubDate>
				<category><![CDATA[操作系统]]></category>
		<guid isPermaLink="false">https://aic.leexinghai.com/?p=2648</guid>

					<description><![CDATA[一、实验目的 1. 加深理解线程同步的概念及实现线程同步的原理。 2. 掌握Linux线程库支持的信号量同步机 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p><strong>一、实验目的</strong><strong></strong></p>



<p>1. 加深理解线程同步的概念及实现线程同步的原理。</p>



<p>2. 掌握Linux线程库支持的信号量同步机制和信号量互斥机制，学会通过信号量信号实现线程间的同步与互斥<strong>。</strong></p>



<p><strong>二、实验内容</strong><strong></strong></p>



<p>实验七、线程同步实现公交车司机与售票员同步，</p>



<p>参考实验四，完成该程序的设计<strong></strong></p>



<p><strong>三、原理和实验主要步骤</strong><strong></strong></p>



<p>1、原理：Linux的线程库中定义了同步信号量以及多种同步信号量操作函数（原语）。</p>



<p>POSIX信号量同步信号量的类型为sem_t</p>



<p>基本的同步信号量操作函数有4个：</p>



<p><code>sem_init( )</code></p>



<p><code>sem_wait( )　</code>    （相当于P操作）</p>



<p><code>sem_post( )</code>　    （相当于V操作）</p>



<p><code>sem_destroy( )</code></p>



<p>它们均在头文件semaphore.h中声明。</p>



<p><code>sem_init</code> 函数</p>



<p>原型：<code>int  sem_init(sem_t *sem, int type, int value);</code></p>



<p>其中，参数sem为同步信号量的指针；type为信号量类型，若为0，表示该信号量是现行进程的局部信号量（只在现行进程的线程之间使用），否则，它可为多个进程所共享；value是信号量值的初值。</p>



<p>功能：初始化同步信号量。</p>



<p><code>sem_wait</code> 函数</p>



<p>原型：<code>int  sem_wait(sem_t  *sem);</code></p>



<p>功能：同步信号量的P操作</p>



<p><code>sem_post</code> 函数</p>



<p>原型：<code>int  sem_post(sem_t  *sem);</code></p>



<p>功能：同步信号量的V操作</p>



<p><code>sem_destroy</code> 函数</p>



<p>原型：<code>int  sem_destroy(sem_t  *sem);</code></p>



<p>功能：清理同步信号量</p>



<p>上述函数返回0表示成功，非0表示失败。</p>



<p>2、本实验要求应用Linux线程库支持的信号量互斥机制改进实验4.2，即程序test4_2.c　。</p>



<p>该互斥机制的内容见讲义Unit 4中的4.2节。</p>



<p>本实验的程序框架和部分程序代码如下，请完成该程序的设计，　</p>



<p><strong>/*test6_1.c*/</strong></p>



<p><strong>#include&lt;stdio.h&gt;</strong></p>



<p><strong>#include&lt;pthread.h&gt;</strong></p>



<p><strong>int ticket[10]={1,3,5,7,9,2,4,6,8,10};</strong></p>



<p><strong>int rest=10;</strong></p>



<p><strong>int th_num[2]={1,2};&nbsp; /*</strong><strong>自定义的线程序号*/</strong></p>



<p><strong>&lt;</strong><strong>定义一个互斥信号量&gt;</strong></p>



<p><strong>void *terminal(void *p)&nbsp; /*</strong><strong>售票终端线程代码*/</strong></p>



<p><strong>{ int&nbsp; mynum=*((int *)p);&nbsp; /*</strong><strong>本终端的序号*/</strong></p>



<p><strong>int&nbsp; tot=0;&nbsp; /*</strong><strong>本终端售票计数*/</strong></p>



<p><strong>&lt;</strong><strong>你的设计部分&gt;</strong></p>



<p><strong>printf(“Terminal_%d selled %d tickets .\n”, mynum,tot);</strong></p>



<p><strong>pthread_exit(0);</strong></p>



<p><strong>}</strong></p>



<p><strong>int main( )</strong></p>



<p><strong>{</strong></p>



<p><strong>pthread_t&nbsp; tid[2];</strong></p>



<p><strong>&lt;</strong><strong>初始化信号量&gt;</strong></p>



<p><strong>&nbsp; pthread_create(&amp;tid[0], NULL, terminal, &amp;th_num[0]);</strong></p>



<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</strong><strong>参数“&amp;th_num[0]”</strong><strong>是线程的入口参数*/</strong></p>



<p><strong>&nbsp; pthread_create(&amp;tid[1], NULL, terminal, &amp;th_num[1]);</strong></p>



<p><strong>&nbsp;</strong><strong>&lt;</strong><strong>等待线程终止</strong><strong>&gt;</strong></p>



<p><strong>&nbsp;&lt;</strong><strong>撤消信号量</strong><strong>&gt;</strong></p>



<p><strong>&nbsp;&lt;</strong><strong>结束</strong><strong>&gt;</strong></p>



<p><strong>}</strong></p>



<p>问题描述：</p>



<p>公共汽车上，司机和售票员的活动分别是：</p>



<p>司机的活动：启动车辆；正常行车；到站停车。</p>



<p>售票员的活动：关车门；售票；开车门。</p>



<p>在汽车不断的到站、停站、行驶过程中，用信号量和P,V操作实现它们的同步。</p>



<p>　</p>



<ul class="wp-block-list">
<li><strong>实验结果测试</strong><strong></strong></li>
</ul>



<p>1.对于test6_1的修改，代码如图1-图2所示。其中，使用<code>pthread_mutex_t</code>定义了互斥信号量，使用<code>pthread_mutex_lock</code>对应互斥信号量的P操作，使用<code>pthread_mutex_unlock</code>对应互斥信号量的V操作。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="790" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-247.png" alt="" class="wp-image-2649" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-247.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-247-300x274.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-247-768x701.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图1</figcaption></figure>
</div>


<p></p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="254" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-248.png" alt="" class="wp-image-2650" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-248.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-248-300x88.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-248-768x226.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图2</figcaption></figure>
</div>


<p></p>



<p>2.完成编辑后，使用gcc命令编译文件，运行生成的文件，结果如图3所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="672" height="478" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-249.png" alt="" class="wp-image-2651" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-249.png 672w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-249-300x213.png 300w" sizes="auto, (max-width: 672px) 100vw, 672px" /><figcaption class="wp-element-caption">图3</figcaption></figure>
</div>


<p>思考：◆Linux系统中，信号量值和P、V是怎么样对应的？</p>



<p>答：信号量的值和P、V操作是通过以下方式相互对应的：</p>



<p>信号量的值：每个信号量都有一个整数值，通常表示可用资源的数量或者某种状态。值可以是任意整数。</p>



<p>P操作用于请求获取一个资源。在Linux中，通过调用semop()系统调用实现P操作。当进程执行P操作时，它将尝试将信号量的值减1。如果信号量的值大于0，则成功获取到资源，并将信号量的值减1；如果信号量的值为0，则进程将被阻塞，直到信号量的值变为非零。</p>



<p>V操作用于释放一个资源。在Linux中，通过调用semop()系统调用实现V操作。当进程执行V操作时，它将尝试将信号量的值加1。如果有其他进程因为等待该资源而被阻塞，则其中一个进程将被唤醒并获得该资源；如果没有进程在等待该资源，则信号量的值增加1。</p>



<p>通过对信号量的值进行P和V操作，可以实现多个进程之间的互斥和同步，以确保对共享资源的正确访问和使用。</p>



<h2 class="wp-block-heading">附件下载</h2>



<div class="wp-block-file"><a id="wp-block-file--media-5653d685-d3aa-42cc-a325-08914725cebc" href="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/test6_1.7z">test6_1</a><a href="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/test6_1.7z" class="wp-block-file__button wp-element-button" download aria-describedby="wp-block-file--media-5653d685-d3aa-42cc-a325-08914725cebc">下载</a></div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>H30528D-实验五-线程创建与并发</title>
		<link>https://www.leexinghai.com/aic/h30528d-%e5%ae%9e%e9%aa%8c%e4%ba%94-%e7%ba%bf%e7%a8%8b%e5%88%9b%e5%bb%ba%e4%b8%8e%e5%b9%b6%e5%8f%91/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Mon, 29 May 2023 00:27:01 +0000</pubDate>
				<category><![CDATA[操作系统]]></category>
		<guid isPermaLink="false">https://aic.leexinghai.com/?p=2639</guid>

					<description><![CDATA[一、实验目的 1. 了解Linux的ULT（用户级线程）机制。 2. 学会创建线程的编程方法。 3. 加深理解 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>一、<strong>实验目的</strong></p>



<p>1. 了解Linux的ULT（用户级线程）机制。</p>



<p>2. 学会创建线程的编程方法。</p>



<p>3. 加深理解进程数据的多线程共享概念。</p>



<p>二、<strong>实验内容</strong></p>



<p>实验5.1&nbsp; 创建线程</p>



<p>实验5.2&nbsp; 多线程间共享数据</p>



<p>三、<strong>实验主要步骤</strong></p>



<p>实验5.1&nbsp; 创建线程</p>



<p>①用你的用户账号登录，并进入ostest目录。</p>



<p>　②完成源程序test5_1.c的设计与编辑</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p>　/*test5_1.c ：创建线程*/</p>



<p>　#include&lt;stdio.h&gt;</p>



<p>　#include&lt;pthread.h&gt;</p>



<p>&nbsp;&nbsp; #include&lt;stdlib.h&gt;</p>



<p>　int data=4;</p>



<p>　void *thereadcode() 　/*线程的执行代码*/</p>



<p>　{</p>



<p>&nbsp;　　printf(“theread: data=%d\n”,++data);</p>



<p>&nbsp;　　pthread_exit(0); 　/*线程正常终止*/</p>



<p>　}</p>



<p>int main()</p>



<p>　{</p>



<p>&nbsp;　 pthread_t&nbsp; tid; 　/*声明tid为线程标识号类型*/</p>



<p>&nbsp;　 printf(“data=%d\n”,++data);</p>



<p>&nbsp;　 &lt;创建线程thereadcode&gt;</p>



<p>&nbsp;　 if(&lt;创建失败&gt;){</p>



<p>&nbsp;&nbsp;&nbsp; 　　 printf(“create of theread is fail\n”);</p>



<p>&nbsp;&nbsp;&nbsp; 　　 exit(-1);&nbsp;&nbsp; /*非正常终止*/</p>



<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>



<p>&nbsp;　 &lt;等待线程的终止&gt;</p>



<p>&nbsp;　 printf(“data=%d\n”,++data);</p>



<p>&nbsp;　 return 0;</p>



<p>　}</p>
</div></div>



<p>　③用gcc编译test5_1.c</p>



<p>　　　由于头文件pthread.h实际上不是Linux默认的库，故在gcc命令行中需要使用选项“-l”链接该库，如：</p>



<p>  　　  <code>gcc  –o  test5_1  –l  pthread  test5_1.c</code></p>



<p>　④运行test5_1观察输出结果。</p>



<p>　⑤修改test5_1.c，将语句“<code>int data=4;</code>”移到main函数内的首部。观察运行结果。</p>



<p>实验5.2&nbsp; 多线程间共享数据</p>



<p>设计一个售票模拟系统进程，在其main函数中创建2个售票终端线程，这2个线程共享进程的全局数据——余票数据库ticket和余票数rest。</p>



<p>　　　下面给出部分程序，请设计出其余部分，并完成编辑、编译及调试运行。</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p>　/*test5_2.c ：售票模拟系统*/</p>



<p>　#include&lt;stdio.h&gt;</p>



<p>　#include&lt;pthread.h&gt;</p>



<p>　int ticket[10]={1,3,5,7,9,2,4,6,8,10};</p>



<p>　int rest=10;</p>



<p>void *terminal(void *num) 　/*终端线程共享代码*/</p>



<p>　{ int&nbsp; tn= (int *)num; /*num所指单元里存放的是终端线程编号*/</p>



<p>　　int&nbsp; tot=0;&nbsp; long&nbsp; i ;</p>



<p>　　while()</p>



<p>&nbsp;&nbsp; 　　if(rest&gt;0)</p>



<p>　　　&nbsp; {rest--;</p>



<p>&nbsp;&nbsp;&nbsp;&nbsp; 　　&nbsp;&nbsp; printf(“Terminal_%d:sell a ticket, num=%d\n”, tn,</p>



<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ticket[rest]);</p>



<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tot++;</p>



<p>&nbsp;&nbsp;&nbsp;&nbsp; 　　&nbsp;&nbsp; for(i=0; i&lt;999999; i++);</p>



<p>　　　&nbsp;&nbsp;&nbsp; }</p>



<p>&nbsp;&nbsp; 　　 else break;&nbsp;</p>



<p>　　 printf(“Selled %d tickets from terminal_%d\n”, tot, tn);</p>



<p>　　 pthread_exit(0); 　</p>



<p>　 }</p>



<p>　int main()</p>



<p>　{ pthread_t&nbsp; tid;&nbsp;</p>



<p>　　int&nbsp; num=1; /*num为终端线程编号*/</p>



<p>　　&lt;创建售票终端线程1和售票终端线程2 &gt;</p>



<p>　　&lt;等待线程的终止&gt;</p>



<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(“Over!\n”);</p>



<p>　　return 0; 　</p>



<p>&nbsp;　}</p>
</div></div>



<p>　　多次运行程序，观察运行结果。</p>



<p>　　结果都正确吗？有什么问题？能解决吗？</p>



<p>　</p>



<p>四、<strong>实验结果测试</strong></p>



<p>1.修改补充test5_1.c的代码，需要修改的内容如图1中红色框线所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="738" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-241.png" alt="" class="wp-image-2640" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-241.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-241-300x256.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-241-768x655.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图1</figcaption></figure>
</div>


<p></p>



<p>2.完成后，使用<code>gcc test5_1.c -l pthread -o test5_1.out</code>编译该代码文件，并运行test5_1.out文件，输出结果如图2所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="594" height="145" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-242.png" alt="" class="wp-image-2641" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-242.png 594w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-242-300x73.png 300w" sizes="auto, (max-width: 594px) 100vw, 594px" /><figcaption class="wp-element-caption">图2</figcaption></figure>
</div>


<p></p>



<p>3.尝试将int data=4;命令移到main函数的首部，如图3所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="939" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-243.png" alt="" class="wp-image-2642" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-243.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-243-276x300.png 276w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-243-768x834.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图3</figcaption></figure>
</div>


<p></p>



<p>4．再次使用第2步的命令编译，发现编译出错，原因是变量声明不明确，结果如图4所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="130" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-244.png" alt="" class="wp-image-2643" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-244.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-244-300x45.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-244-768x115.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图4</figcaption></figure>
</div>


<p></p>



<p>5.对于公交售票系统，代码如图5所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="845" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-245.png" alt="" class="wp-image-2644" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-245.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-245-300x293.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-245-768x750.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图5</figcaption></figure>
</div>


<p></p>



<p>6.使用第2步的命令编译并多次运行，结果如图6所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="622" height="983" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-246.png" alt="" class="wp-image-2645" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-246.png 622w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-246-190x300.png 190w" sizes="auto, (max-width: 622px) 100vw, 622px" /><figcaption class="wp-element-caption">图6</figcaption></figure>
</div>


<h2 class="wp-block-heading">附件下载</h2>



<div class="wp-block-file"><a id="wp-block-file--media-92f25825-f8fc-414d-a820-06c0eb0a2b2d" href="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/实验五.7z">实验五代码文件</a><a href="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/实验五.7z" class="wp-block-file__button wp-element-button" download aria-describedby="wp-block-file--media-92f25825-f8fc-414d-a820-06c0eb0a2b2d">下载</a></div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>H30521-实验四-进程同步</title>
		<link>https://www.leexinghai.com/aic/h30521-%e5%ae%9e%e9%aa%8c%e5%9b%9b-%e8%bf%9b%e7%a8%8b%e5%90%8c%e6%ad%a5/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Fri, 12 May 2023 02:26:34 +0000</pubDate>
				<category><![CDATA[操作系统]]></category>
		<guid isPermaLink="false">https://aic.leexinghai.com/?p=2349</guid>

					<description><![CDATA[一．实验目的 1. 加深理解进程同步的概念及实现线程同步的原理。 2. 掌握Linux进程库支持的信号量同步机 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>一．实验目的</p>



<p>1. 加深理解进程同步的概念及实现线程同步的原理。</p>



<p>2. 掌握Linux进程库支持的信号量同步机制和信号量互斥机制，学会通过信号量信号实现进程间的同步与互斥。</p>



<p>二．实验内容</p>



<p>实验4进程实现公交车司机与售票员同步</p>



<p>三．实验主要步骤</p>



<p>问题描述：</p>



<p>公共汽车上，司机和售票员的活动分别是：</p>



<p>司机的活动：启动车辆；正常行车；到站停车。</p>



<p>售票员的活动：关车门；售票；开车门。</p>



<p>在汽车不断的到站、停站、行驶过程中，用信号量和P,V操作实现它们的同步。</p>



<p>&nbsp;key1=ftok(".",36);</p>



<p>&nbsp;key2=ftok(".",98);</p>



<p>semid1=semget(key1,1,IPC_CREAT|IPC_EXCL|0666); //司机同步信号量</p>



<p>semid2=semget(key2,1,IPC_CREAT|IPC_EXCL|0666);//售票员同步信号量</p>



<p>问题解决：</p>



<p>&nbsp;&nbsp;&nbsp; 我们可以使用两个信号量来实现司机与售票员间的同步，具体如下：</p>



<p>1.semctl函数进行初始化信号量为0 , v1.val=0; v2.val=0;</p>



<p>&nbsp;&nbsp;&nbsp; r1=semctl(semid1,0,SETVAL,v1);//v1参数是个semun共用体</p>



<p>2. semop函数进行PV设置;</p>



<p>3.实现司机与售票员进程同步PV操作</p>



<p>4.semctl函数进行删除信号量：</p>



<p>semctl(semid1,0,IPC_RMID);</p>



<p>semctl(semid2,0,IPC_RMID);</p>



<p><strong>四、实验结果测试</strong></p>



<p>1.本实验所涉及的部分关键代码如图1所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="769" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-53-769x1024.png" alt="" class="wp-image-2353" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-53-769x1024.png 769w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-53-225x300.png 225w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-53-768x1023.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-53.png 865w" sizes="auto, (max-width: 769px) 100vw, 769px" /><figcaption class="wp-element-caption">图1</figcaption></figure>
</div>


<p>2.在Linux系统中，尝试使用<code>ipcs</code>命令查看当前进程间通信的信息：通过观察【信号量数组】可以看到：当前有一个semid为1的信号量，权限为600（自己可读写，其余均不可以进行任何操作）；此时将编译好的代码运行，控制台中输出了司机、乘客、售票员的动作，强制结束该程序后，再次使用<code>ipcs</code>命令查看进程间通信情况，可以看到有两个新的信号量被创建，权限为666（在代码中进行了设置：所有人均可以读写），如图2所示。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img loading="lazy" decoding="async" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-54.png" alt="" class="wp-image-2354" width="805" height="878" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-54.png 805w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-54-275x300.png 275w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-54-768x838.png 768w" sizes="auto, (max-width: 805px) 100vw, 805px" /><figcaption class="wp-element-caption">图2</figcaption></figure>
</div>


<p><strong>分析与思考</strong><strong></strong></p>



<p>◆如何删除进程信号？</p>



<p>答：使用命令<code>ipcrm -s [进程信号ID]</code>即可删除相应的进程信号</p>



<h2 class="wp-block-heading">附件下载</h2>



<div class="wp-block-file"><a id="wp-block-file--media-bdf69087-3efd-4705-a3e4-5417038e318f" href="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/test4.7z">test4</a><a href="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/test4.7z" class="wp-block-file__button wp-element-button" download aria-describedby="wp-block-file--media-bdf69087-3efd-4705-a3e4-5417038e318f">下载</a></div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>H30507-实验三-进程的创建与并发执行之二</title>
		<link>https://www.leexinghai.com/aic/h30507-%e5%ae%9e%e9%aa%8c3-%e8%bf%9b%e7%a8%8b%e7%9a%84%e5%88%9b%e5%bb%ba%e4%b8%8e%e5%b9%b6%e5%8f%91%e6%89%a7%e8%a1%8c%e4%b9%8b%e4%ba%8c/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Sat, 06 May 2023 01:23:05 +0000</pubDate>
				<category><![CDATA[操作系统]]></category>
		<guid isPermaLink="false">https://aic.leexinghai.com/?p=2240</guid>

					<description><![CDATA[一．实验目的 1. 学会使用fork()/exec()模式创建进程（具有独立映象）的编程方法。 2. 加深理解 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>一．实验目的</p>



<p>1. 学会使用fork()/exec()模式创建进程（具有独立映象）的编程方法。</p>



<p>2. 加深理解进程的并发特性，掌握创建进程的正确编程方法。</p>



<p>3.完成实验3.1&nbsp;&nbsp; 用fork()模式创建进程；实验3.2&nbsp;&nbsp; 用fork/exec模式创建进程。</p>



<p>二．实验内容</p>



<p>实验3.1&nbsp;&nbsp; 用fork()模式创建进程</p>



<p>实验3.2&nbsp;&nbsp; 用fork/exec模式创建进程</p>



<p>三．实验主要步骤</p>



<p>实验3.1 用fork()模式创建进程</p>



<p>Linux中创建新进程的唯一方法是使用fork()函数。</p>



<p>C语言中， if else分支语句只会根据条件进入一个分支执行，但fork()执行一次却有两个返回值(if、else两个分支的语句都执行了)。</p>



<p>试运行以下程序，观察运行后的结果。</p>



<p>四、实验结果测试</p>



<p>1.在ostest文件夹下使用vim命令创建test3_1.c,test3_2.c,subp1.c,subp2.c,subp3.c；并根据指定代码内容编辑相应的文件。使用命令gcc 【c文件】 -o 【out文件名】进行编译。编译结果如图1所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="865" height="223" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-6.png" alt="" class="wp-image-2289" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-6.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-6-300x77.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-6-768x198.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /></figure>



<p>图1</p>



<p>2.test3_1.c的代码内容和尝试运行1次的结果如图2所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="654" height="431" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-7.png" alt="" class="wp-image-2290" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-7.png 654w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-7-300x198.png 300w" sizes="auto, (max-width: 654px) 100vw, 654px" /></figure>



<p>图2</p>



<p>3.subp1.c的代码内容和尝试运行1次的结果如图3所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="727" height="431" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-8.png" alt="" class="wp-image-2291" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-8.png 727w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-8-300x178.png 300w" sizes="auto, (max-width: 727px) 100vw, 727px" /></figure>



<p>图3</p>



<p>4. subp2.c与subp3.c的代码内容和分别尝试运行1次的结果如图4所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="773" height="838" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-9.png" alt="" class="wp-image-2292" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-9.png 773w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-9-277x300.png 277w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-9-768x833.png 768w" sizes="auto, (max-width: 773px) 100vw, 773px" /></figure>



<p>图4</p>



<p>5.test3_2.c的代码内容如图5所示。</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="819" height="1024" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-10-819x1024.png" alt="" class="wp-image-2293" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-10-819x1024.png 819w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-10-240x300.png 240w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-10-768x961.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-10.png 865w" sizes="auto, (max-width: 819px) 100vw, 819px" /></figure>



<p>图5</p>



<p>6. 尝试运行3次test3_2.out的结果如图6所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="733" height="849" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-11.png" alt="" class="wp-image-2294" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-11.png 733w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-11-259x300.png 259w" sizes="auto, (max-width: 733px) 100vw, 733px" /></figure>



<p>图6</p>



<p>7. 尝试运行3次test2.out的结果如图7所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="713" height="725" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-12.png" alt="" class="wp-image-2295" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-12.png 713w, https://www.leexinghai.com/aic/wp-content/uploads/2023/05/image-12-295x300.png 295w" sizes="auto, (max-width: 713px) 100vw, 713px" /></figure>



<p>图7</p>



<h2 class="wp-block-heading">附件下载</h2>



<div class="wp-block-file"><a id="wp-block-file--media-55f4b5c9-ad60-4619-b608-8f5c860c317c" href="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/SourceCode.7z">SourceCode</a><a href="https://www.leexinghai.com/aic/wp-content/uploads/2023/05/SourceCode.7z" class="wp-block-file__button wp-element-button" download aria-describedby="wp-block-file--media-55f4b5c9-ad60-4619-b608-8f5c860c317c">下载</a></div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>H30423-实验二-进程的创建与并发执行之一</title>
		<link>https://www.leexinghai.com/aic/h30423-%e5%ae%9e%e9%aa%8c%e4%ba%8c-%e8%bf%9b%e7%a8%8b%e7%9a%84%e5%88%9b%e5%bb%ba%e4%b8%8e%e5%b9%b6%e5%8f%91%e6%89%a7%e8%a1%8c%e4%b9%8b%e4%b8%80/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Mon, 17 Apr 2023 02:30:57 +0000</pubDate>
				<category><![CDATA[操作系统]]></category>
		<guid isPermaLink="false">https://aic.leexinghai.com/?p=2110</guid>

					<description><![CDATA[一．实验目的 （1）理解进程与程序的联系与区别以及并发程序的基本特性。 （2）学会在在程序中使用fork()  [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">一．实验目的</h2>



<p>（1）理解进程与程序的联系与区别以及并发程序的基本特性。</p>



<p>（2）学会在在程序中使用fork() 函数创建克隆式子进程的基本编程方法。</p>



<h2 class="wp-block-heading">二．实验内容</h2>



<p>命令进程的创建与并发执行</p>



<h2 class="wp-block-heading">三．实验主要步骤</h2>



<p>实验2.1&nbsp; 命令进程的创建与并发执行</p>



<p>①启动Linux，进入黑屏界面；</p>



<p>②在tty1上用root账号登录，在tty2和tty3上则分别以普通用户账号登录；</p>



<p>③在tty1上输入命令“ps --help”，即在tty1上创建命令进程ps，以查看ps命令的help信息；</p>



<p>④在tty2上输入命令“man man”，即在tty2上创建命令进程man，以查看man命令的help信息，不要终止该man命令；</p>



<p>⑤在tty3上执行命令“man pstree”，即在tty3上创建命令进程man，查看pstree命令的help信息，同样也不要终止该man命令；</p>



<p>⑥在tty1上执行ps命令，查看本控制台上所有进程的基本信息；</p>



<p>⑦在tty1上执行pstree命令，查看进程的分层结构。</p>



<p>⑧再在tty1上执行ps命令，查看本系统的所有进程的详细信息，并从中找到并记下tty2上的man命令进程的PID；你能看到ps命令输出的全部信息吗？</p>



<p>如果不能，可以利用Linux的“重定向”或“管道”功能，例如，可输入如下管道命令行：ps –aux | more</p>



<p>该命令行的语义是：将ps命令的输出结果作为输入传递给more命令（分页显示），并执行more命令。)</p>



<p>⑨撤消tty2上的man命令进程；</p>



<p>⑩再执行ps命令，查看被撤消的进程是否还存在；</p>



<p>⑾切换到tty2，观察man命令是否还在执行或已终止；再切换到tty3，观察man命令是否还在执行或已终止；</p>



<p>⑿注销 （不要关机）。</p>



<p>实验2.2&nbsp; 使用fork创建进程</p>



<p>本实验使用系统调用fork()创建多个子进程，实现多进程并发执行。</p>



<p>参考步骤：</p>



<p>1.在tty1上使用root用户登录，如图1所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="625" height="469" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-83.png" alt="" class="wp-image-2111" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-83.png 625w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-83-300x225.png 300w" sizes="auto, (max-width: 625px) 100vw, 625px" /><figcaption class="wp-element-caption">图1</figcaption></figure>



<p>2.在tty2以普通账户登录，结果如图2所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="625" height="469" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-84.png" alt="" class="wp-image-2112" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-84.png 625w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-84-300x225.png 300w" sizes="auto, (max-width: 625px) 100vw, 625px" /><figcaption class="wp-element-caption">图2</figcaption></figure>



<p>3.在tty3也用普通用户登录，结果如图3所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="625" height="469" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-85.png" alt="" class="wp-image-2113" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-85.png 625w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-85-300x225.png 300w" sizes="auto, (max-width: 625px) 100vw, 625px" /><figcaption class="wp-element-caption">图3</figcaption></figure>



<p>4.在tty1输入命令 ps --help 创建ps进程，如图4所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="625" height="469" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-86.png" alt="" class="wp-image-2114" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-86.png 625w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-86-300x225.png 300w" sizes="auto, (max-width: 625px) 100vw, 625px" /><figcaption class="wp-element-caption">图4</figcaption></figure>



<p>5.在tty2输入man man 查看man命令的帮助信息，结果如图5所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="625" height="469" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-87.png" alt="" class="wp-image-2115" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-87.png 625w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-87-300x225.png 300w" sizes="auto, (max-width: 625px) 100vw, 625px" /><figcaption class="wp-element-caption">图5</figcaption></figure>



<p>6.在tty3上执行man pstree命令，结果如图6所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="625" height="469" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-88.png" alt="" class="wp-image-2116" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-88.png 625w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-88-300x225.png 300w" sizes="auto, (max-width: 625px) 100vw, 625px" /><figcaption class="wp-element-caption">图6</figcaption></figure>



<p>7.在tty1上使用ps命令查看本控制台的进程信息，结果如图7所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="625" height="469" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-89.png" alt="" class="wp-image-2117" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-89.png 625w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-89-300x225.png 300w" sizes="auto, (max-width: 625px) 100vw, 625px" /><figcaption class="wp-element-caption">图7</figcaption></figure>



<p>8.在tty1使用ps tree查看进程树信息，回显结果如图8所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="625" height="469" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-90.png" alt="" class="wp-image-2118" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-90.png 625w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-90-300x225.png 300w" sizes="auto, (max-width: 625px) 100vw, 625px" /><figcaption class="wp-element-caption">图8</figcaption></figure>



<p>9.使用ps -aux | more 查看tty2的man man命令进程的详细信息，结果如图9所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="865" height="717" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-91.png" alt="" class="wp-image-2119" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-91.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-91-300x249.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-91-768x637.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图9</figcaption></figure>



<p>10.用kill命令撤销第10步中的进程信息，命令如图10所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="865" height="74" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-92.png" alt="" class="wp-image-2120" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-92.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-92-300x26.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-92-768x66.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图10</figcaption></figure>



<p>11.再次使用ps -aux查看进程信息，发现进程号为1725的指定进程已经没有了，如图11所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="865" height="88" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-93.png" alt="" class="wp-image-2121" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-93.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-93-300x31.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-93-768x78.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图11</figcaption></figure>



<p>12.使用fork创建进程：通过老师提供的唯一指定代码写入到用户家目录下的test2.c文件中，该文件代码如图12所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="675" height="998" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-94.png" alt="" class="wp-image-2122" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-94.png 675w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-94-203x300.png 203w" sizes="auto, (max-width: 675px) 100vw, 675px" /><figcaption class="wp-element-caption">图12</figcaption></figure>



<p>13.使用gcc test2.c命令编译该程序，然后多次运行该程序，结果如图13所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="482" height="609" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-95.png" alt="" class="wp-image-2123" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-95.png 482w, https://www.leexinghai.com/aic/wp-content/uploads/2023/04/image-95-237x300.png 237w" sizes="auto, (max-width: 482px) 100vw, 482px" /><figcaption class="wp-element-caption">图13</figcaption></figure>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>H30326-实验一-熟悉Linux环境</title>
		<link>https://www.leexinghai.com/aic/h30326-%e7%86%9f%e6%82%89linux%e7%8e%af%e5%a2%83/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Mon, 20 Mar 2023 09:05:55 +0000</pubDate>
				<category><![CDATA[操作系统]]></category>
		<guid isPermaLink="false">https://aic.leexinghai.com/?p=1955</guid>

					<description><![CDATA[实验目的 了解Linux的文本用户界面，掌握用户的注册、登录及注销。 熟悉 vi 编辑器的基本使用方法。 学会 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading"><strong>实验目的</strong></h2>



<p>了解Linux的文本用户界面，掌握用户的注册、登录及注销。</p>



<p>熟悉 vi 编辑器的基本使用方法。</p>



<p>学会在Linux上开发C 程序的一般方法。</p>



<h2 class="wp-block-heading"><strong>实验内容</strong><strong></strong></h2>



<p>实验1.1&nbsp; 用户的注册、登录及注销</p>



<p>实验1.2&nbsp; 开发简单的C程序</p>



<h2 class="wp-block-heading"><strong>实验主要步骤</strong><strong></strong></h2>



<p><strong>实验</strong><strong>1.1&nbsp; </strong><strong>用户的注册、登录及注销</strong><strong></strong></p>



<p><strong>Step1. </strong><strong>启动</strong><strong>Linux</strong></p>



<p>①启动VMware，在VMware的主窗口启动Linux；</p>



<p>②在用户界面中用超级用户账号登录。</p>



<p>　　　　用户名：root　　　　密码：　xxxxxxx</p>



<p>③在桌面上打开终端shell界面；</p>



<p><strong>Step2. </strong><strong>新用户注册</strong><strong></strong></p>



<p>①用groupadd命令建立两个用户组：group1和group2；</p>



<p>②用useradd命令和passwd命令<strong>建立</strong><strong>4</strong><strong>个新的用户账号，其中一个是你本人的账号</strong><strong>! </strong>　如：fyl、wang、zhang、student，初始密码可均设为“654321”，并指定前2个账号为group1组成员，后2个账号为group2组成员；</p>



<p>③用 vi 编辑器打开系统配置文件“/etc/passwd”，查看新账号是否已生成以及他们各自的用户标识号UID。</p>



<p>④用 vi 编辑器打开系统配置文件“/etc/group”，查看group1组和group2组以及它们的组标识号GID；新的用户账号是否已加入到组中？</p>



<p>⑤用usermod命令将新用户账号添加到各自的组中；</p>



<p>⑥再打开/etc/group文件，确定新的用户账号已加入到各自的组中。</p>



<p><strong>Step3. </strong><strong>多用户登录</strong><strong></strong></p>



<p>①用Ctrl+Alt+F2切换到虚拟控制台tty2，用你的账号登录，并用passwd命令修改初始密码（记住！）。</p>



<p>②再切换到tty3上，用另一普通用户账号登录并修改其初始密码。</p>



<p>③使用who命令查看本系统当前有哪些在线用户，只需在命令提示符后输入命令行</p>



<p>　who <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2199.png" alt="↙" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>④在你的账号下用mkdir命令建立一个目录：ostest。</p>



<p><strong>Step4. </strong><strong>注销与关机</strong></p>



<ul class="wp-block-list">
<li>普通用户用logout命令注销；</li>
</ul>



<p>②超级用户用shutdown命令注销并关机。</p>



<p><strong>实验</strong><strong>1.2&nbsp; </strong><strong>开发简单的</strong><strong>C</strong><strong>程序</strong><strong></strong></p>



<p>①启动Linux，用你的普通账号登录；</p>



<p>②进入子目录ostest；</p>



<p>③用vi 编辑器编辑一个简单的C源程序test1_1.c，参考源代码如下：</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p>　<code>　　#include &lt;stdio.h&gt;</code></p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p>　　　<code>main()</code></p>



<p>　　　<code>{</code></p>



<p>&nbsp; 　　　<code>int i, sum=0;</code></p>



<p>&nbsp; 　　　<code>for( i=1; i&lt;=10; i++)</code></p>



<p>&nbsp;&nbsp;&nbsp;&nbsp; 　　　　<code>sum+=i;</code></p>



<p>&nbsp; 　　　<code>printf(”sum=%d\n”, sum);</code></p>



<p>　　　<code>}</code></p>
</div></div>
</div></div>
</div></div>



<p>④用命令“gcc test1_1.c”对test1_1.c进行编译；</p>



<p>⑤若成功，则不会有任何信息显示；否则作适当修改，直到成功；</p>



<p>⑥执行test1_1对应的可执行程序；</p>



<p>⑦修改test1_1.c：求1到100之和。并将新程序命名为test1_2.c；</p>



<p>⑧编译test1_2.c，要求其可执行程序的名字为test1_2；</p>



<p>⑨执行test1_2。</p>



<p>⑩注销。</p>



<h2 class="wp-block-heading">参考步骤</h2>



<p>1.启动Linux，使用root账户登录，如图1所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="865" height="433" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-30.png" alt="" class="wp-image-1959" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-30.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-30-300x150.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-30-768x384.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图1-linux用户登录界面</figcaption></figure>



<p></p>



<p>2.打开shell程序，界面如图2所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="865" height="454" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-31.png" alt="" class="wp-image-1960" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-31.png 865w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-31-300x157.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-31-768x403.png 768w" sizes="auto, (max-width: 865px) 100vw, 865px" /><figcaption class="wp-element-caption">图2-shell窗口</figcaption></figure>



<p></p>



<p>3.完成group1，group2组的新建；添加4个新用户和设置密码；将4个新用户按指定要求添加到对应的组内，具体命令如图3所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="796" height="565" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-32.png" alt="" class="wp-image-1961" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-32.png 796w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-32-300x213.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-32-768x545.png 768w" sizes="auto, (max-width: 796px) 100vw, 796px" /><figcaption class="wp-element-caption">图3-组和用户的创建与分配</figcaption></figure>



<p></p>



<p>4.使用<code>vi /etc/passwd</code>查看用户信息，如图4所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="624" height="199" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-33.png" alt="" class="wp-image-1962" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-33.png 624w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-33-300x96.png 300w" sizes="auto, (max-width: 624px) 100vw, 624px" /><figcaption class="wp-element-caption">图4-passwd文件</figcaption></figure>



<p></p>



<p>5.使用<code> vi /etc/group</code>查看组信息，如图5所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="624" height="199" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-34.png" alt="" class="wp-image-1963" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-34.png 624w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-34-300x96.png 300w" sizes="auto, (max-width: 624px) 100vw, 624px" /><figcaption class="wp-element-caption">图5-group文件</figcaption></figure>



<p></p>



<p>6.使用tty2控制台登录自己账号并修改密码：因控制台视图不可以正确显示中文，因此控制台中修改密码的信息提示参见图6，修改密码的过程如图7所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="308" height="193" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-35.png" alt="" class="wp-image-1965" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-35.png 308w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-35-300x188.png 300w" sizes="auto, (max-width: 308px) 100vw, 308px" /><figcaption class="wp-element-caption">图6-控制台命令信息对比图</figcaption></figure>



<p></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="793" height="750" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-36.png" alt="" class="wp-image-1966" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-36.png 793w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-36-300x284.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-36-768x726.png 768w" sizes="auto, (max-width: 793px) 100vw, 793px" /><figcaption class="wp-element-caption">图7-控制台修改密码</figcaption></figure>



<p></p>



<p>7.切换到tty3控制台登录另一个用户，修改密码的过程与步骤6相同，使用who命令查看在线用户，如图8所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="659" height="750" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-37.png" alt="" class="wp-image-1967" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-37.png 659w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-37-264x300.png 264w" sizes="auto, (max-width: 659px) 100vw, 659px" /><figcaption class="wp-element-caption">图8-查看在线用户数</figcaption></figure>



<p></p>



<p>8.在自己名称账户下建立ostest目录，如图9所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="489" height="80" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-38.png" alt="" class="wp-image-1968" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-38.png 489w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-38-300x49.png 300w" sizes="auto, (max-width: 489px) 100vw, 489px" /><figcaption class="wp-element-caption">图9-新建文件夹</figcaption></figure>



<p></p>



<p>9.普通用户使用exit（kali系统）注销用户和root用户使用shutdown关机，命令如图10所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="453" height="101" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-39.png" alt="" class="wp-image-1969" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-39.png 453w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-39-300x67.png 300w" sizes="auto, (max-width: 453px) 100vw, 453px" /><figcaption class="wp-element-caption">图10-注销和关机操作</figcaption></figure>



<p></p>



<p>10.在ostest目录下创建并编译test1_1.c文件，命令如图11所示；test1_1.c文件内容如图12所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="776" height="85" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-40.png" alt="" class="wp-image-1970" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-40.png 776w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-40-300x33.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-40-768x84.png 768w" sizes="auto, (max-width: 776px) 100vw, 776px" /><figcaption class="wp-element-caption">图11-创建并编译文件</figcaption></figure>



<p></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="796" height="330" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-41.png" alt="" class="wp-image-1971" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-41.png 796w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-41-300x124.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-41-768x318.png 768w" sizes="auto, (max-width: 796px) 100vw, 796px" /><figcaption class="wp-element-caption">图12-test1_1.c文件内容</figcaption></figure>



<p></p>



<p>11.编译完成后，会在同目录下生成a.out文件。执行该文件可查看显示结果，如图13所示。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="519" height="84" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-42.png" alt="" class="wp-image-1972" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-42.png 519w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-42-300x49.png 300w" sizes="auto, (max-width: 519px) 100vw, 519px" /><figcaption class="wp-element-caption">图13-观察输出结果</figcaption></figure>



<p></p>



<p>12.复制test1_1文件，新文件为test1_2.c,编辑文件修改程序功能为求1-100和，命令内容如图15所示。完成后使用gcc -o参数指定可执行程序名字为test1_2,运行该输出文件，获得输出结果，如图14所示。</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-43.png" alt="" class="wp-image-1973" width="693" height="125" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-43.png 826w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-43-300x54.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-43-768x139.png 768w" sizes="auto, (max-width: 693px) 100vw, 693px" /><figcaption class="wp-element-caption">图14-修改程序并编译</figcaption></figure>



<p></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="586" height="217" src="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-44.png" alt="" class="wp-image-1974" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-44.png 586w, https://www.leexinghai.com/aic/wp-content/uploads/2023/03/image-44-300x111.png 300w" sizes="auto, (max-width: 586px) 100vw, 586px" /><figcaption class="wp-element-caption">图15-求1-100和的代码内容</figcaption></figure>



<p>怎么样理解Linux系统的多用户和多任务特点？有哪些优点？（下述内容来自于New Bing）</p>



<p>Linux系统的多用户和多任务特点是指Linux可以同时支持多个用户对同一台计算机进行访问，且每个用户能够同时运行多个任务。这些特点有以下优点：</p>



<p>开放性：Linux遵守GNU规范，代码共享，易于实现系统的裁剪和客制化的开发。</p>



<p>安全性：Linux提供了不同用户之间的权限管理，防止恶意操作或病毒攻击。</p>



<p>稳定性：Linux内核负责程序之间的调度和资源分配，保证了系统的高效运行。</p>



<p>兼容性：Linux支持多种编程语言和硬件平台，可以用于服务器、桌面、嵌入式设备等不同领域。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
