﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-GLORY | 学习·记录-随笔分类-Linux</title><link>http://www.cppblog.com/meglory/category/15756.html</link><description>coding for life</description><language>zh-cn</language><lastBuildDate>Tue, 28 Jun 2011 06:01:17 GMT</lastBuildDate><pubDate>Tue, 28 Jun 2011 06:01:17 GMT</pubDate><ttl>60</ttl><item><title>Shell脚本中的局部变量</title><link>http://www.cppblog.com/meglory/archive/2011/06/24/149406.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Fri, 24 Jun 2011 10:57:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2011/06/24/149406.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/149406.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2011/06/24/149406.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/149406.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/149406.html</trackback:ping><description><![CDATA[<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000FF; ">function</span><span style="color: #000000; ">&nbsp;afunc<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">&nbsp;fnction:&nbsp;$</span><span style="color: #000000; ">0</span><span style="color: #000000; ">&nbsp;$</span><span style="color: #000000; ">1</span><span style="color: #000000; ">&nbsp;$</span><span style="color: #000000; ">2</span><span style="color: #000000; "><br />
&nbsp;&nbsp;&nbsp;&nbsp;var1</span><span style="color: #000000; ">=</span><span style="color: #000000; ">"</span><span style="color: #000000; ">in&nbsp;function</span><span style="color: #000000; ">"</span><span style="color: #000000; "><br />
&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;var1:&nbsp;$var1<br />
}<br />
<br />
var1</span><span style="color: #000000; ">=</span><span style="color: #000000; ">"</span><span style="color: #000000; ">outside&nbsp;function</span><span style="color: #000000; ">"</span><span style="color: #000000; "><br />
echo&nbsp;var1:&nbsp;$var1<br />
echo&nbsp;$</span><span style="color: #000000; ">0</span><span style="color: #000000; ">:&nbsp;$</span><span style="color: #000000; ">1</span><span style="color: #000000; ">&nbsp;$</span><span style="color: #000000; ">2</span><span style="color: #000000; "><br />
afunc&nbsp;funcarg1&nbsp;funcarg2<br />
echo&nbsp;var1:&nbsp;$var1<br />
echo&nbsp;$</span><span style="color: #000000; ">0</span><span style="color: #000000; ">:&nbsp;$</span><span style="color: #000000; ">1</span><span style="color: #000000; ">&nbsp;$</span><span style="color: #000000;">2<br /><br />OUTPUT:<br /><br />./ascript: arg1 arg2<br />in fnction: ./ascript funcarg1 funcarg2<br /></span><span style="color: red;">var1: in function<br /></span><span style="color: red;">var1: in function
</span><span style="color: red;"><br /></span><span>./ascript: arg1 arg2</span><span style="color: red;"><br /><br /></span><span>说明</span><span>var1在afunc内部被改变了。shell的局部变量跟c语言有些差别，这里默认所有外部定义的变量，在函数内部可以访问并且可以改变。即外部定义的变量默认为全局变量。<br />若想在afunc内部定义一个局部变量，则需要显式的加上local var1.<br /><br />待续<br /></span></div><img src ="http://www.cppblog.com/meglory/aggbug/149406.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2011-06-24 18:57 <a href="http://www.cppblog.com/meglory/archive/2011/06/24/149406.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 457 | 什么是Core Dump？</title><link>http://www.cppblog.com/meglory/archive/2011/03/14/141755.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Mon, 14 Mar 2011 00:24:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2011/03/14/141755.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/141755.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2011/03/14/141755.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/141755.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/141755.html</trackback:ping><description><![CDATA[<p>UVA457是个水题，但是我还是碰到了一些小问题。<br><br>1.开了一个state[50][42]数组在main函数里面，结果提交之后发现runtime error，觉着是堆栈溢出了。突然想起来50*42&gt;2000，这种数组一定是开在main外面的，不然必然堆栈溢出。<br>2.是读题不仔细，当成了多重输入，搞了一个while(scanf(...))，结果超时了。第一次在UVA OJ上面超时。<br><br>但是这篇文章重点不是讲这个，而是讲我在运行过程中经常遇到的core dumped现象。我在网上找到一点资料，贴在下面：<br><br>原帖：<a href="http://blogold.chinaunix.net/u3/98822/showart_2093542.html">http://blogold.chinaunix.net/u3/98822/showart_2093542.html</a><br><br><br><strong>什么是Core Dump?<br></strong>Core的意思是内存, Dump的意思是扔出来, 堆出来.<br>开发和使用Unix程序时, 有时程序莫名其妙的down了, 却没有任何的提示(有时候会提示core dumped). 这时候可以查看一下有没有形如core.进程号的文件生成, 这个文件便是操作系统把程序down掉时的内存内容扔出来生成的, 它可以做为调试程序的参考.<br>core dump又叫核心转储, 当程序运行过程中发生异常, 程序异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 叫core dump.<br><br><strong>如何使用core文件?</strong><br>gdb -c core文件路径 [应用程序的路径]<br>进去后输入where回车, 就可以显示程序在哪一行当掉的, 在哪个函数中.<br><br><strong>为什么没有core文件生成呢?</strong><br>有时候程序down了, 但是core文件却没有生成. core文件的生成跟你当前系统的环境设置有关系, 可以用下面的语句设置一下, 然后再运行程序便成生成core文件.<br>ulimit -c unlimited<br>【<span style="TEXT-DECORATION: underline">没有找到core文件，我们改改ulimit的设置，让它产生。1024是随便取的，要是core文件大于1024个块，就产生不出来了。）</span><br style="TEXT-DECORATION: underline"><span style="TEXT-DECORATION: underline">$ ulimit -c 1024 （转者注: 使用-c unlimited不限制core文件大小</span>】<br><br>core文件生成的位置一般于运行程序的路径相同, 文件名一般为core.进程号<br><br>4. 用gdb查看core文件:<br>下面我们可以在发生运行时信号引起的错误时发生core dump了.<br>发生core dump之后, 用gdb进行查看core文件的内容, 以定位文件中引发core dump的行.<br><span class=Code>gdb [exec file] [core file]</span><br>如:<br><span class=Code>gdb ./test test.core</span><br>在进入gdb后, 用bt命令查看backtrace以检查发生程序运行到哪里, 来定位core dump的文件-&gt;行.<br><br>===========================================================================<br></p>
<p align=left>造成程序core dump的原因很多，这里根据以往的经验总结一下：</p>
<p align=left>1 内存访问越界</p>
<p align=left>&nbsp; a) 由于使用错误的下标，导致数组访问越界</p>
<p align=left>&nbsp; b) 搜索字符串时，依靠字符串结束符来判断字符串是否结束，但是字符串没有正常的使用结束符</p>
<p align=left>&nbsp; c) 使用strcpy, strcat, sprintf, strcmp, strcasecmp等字符串操作函数，将目标字符串读/写爆。应该使用strncpy, strlcpy, strncat, strlcat, snprintf, strncmp, strncasecmp等函数防止读写越界。</p>
<p align=left>2 多线程程序使用了线程不安全的函数。</p>
<p align=left>应该使用下面这些可重入的函数，尤其注意红色标示出来的函数，它们很容易被用错：</p>
<p align=left>asctime_r(3c) gethostbyname_r(3n) getservbyname_r(3n) ctermid_r(3s) gethostent_r(3n) getservbyport_r(3n) ctime_r(3c) getlogin_r(3c) getservent_r(3n) fgetgrent_r(3c) getnetbyaddr_r(3n) getspent_r(3c) fgetpwent_r(3c) getnetbyname_r(3n) getspnam_r(3c) fgetspent_r(3c) getnetent_r(3n) gmtime_r(3c) gamma_r(3m) getnetgrent_r(3n) lgamma_r(3m) getauclassent_r(3) getprotobyname_r(3n) localtime_r(3c) getauclassnam_r(3) etprotobynumber_r(3n) nis_sperror_r(3n) getauevent_r(3) getprotoent_r(3n) rand_r(3c) getauevnam_r(3) getpwent_r(3c) readdir_r(3c) getauevnum_r(3) getpwnam_r(3c) strtok_r(3c) getgrent_r(3c) getpwuid_r(3c) tmpnam_r(3s) getgrgid_r(3c) getrpcbyname_r(3n) ttyname_r(3c) getgrnam_r(3c) getrpcbynumber_r(3n) gethostbyaddr_r(3n) getrpcent_r(3n)</p>
<p align=left>3 多线程读写的数据未加锁保护。</p>
<p align=left>对于会被多个线程同时访问的全局数据，应该注意加锁保护，否则很容易造成core dump</p>
<p align=left>4 非法指针</p>
<p align=left>&nbsp; a) 使用空指针</p>
<p align=left>&nbsp; b) 随意使用指针转换。一个指向一段内存的指针，除非确定这段内存原先就分配为某种结构或类型，或者这种结构或类型的数组，否则不要将它转换为这种结构或类型的指针，而应该将这段内存拷贝到一个这种结构或类型中，再访问这个结构或类型。这是因为如果这段内存的开始地址不是按照这种结构或类型对齐的，那么访问它时就很容易因为bus error而core dump.</p>
<p align=left>5 堆栈溢出</p>
<p>不要使用大的局部变量（因为局部变量都分配在栈上），这样容易造成堆栈溢出，破坏系统的栈和堆结构，导致出现莫名其妙的错误。 <br><br>-------<br>我自己程序core dumped就是因为第5个原因，堆栈溢出。我的局部数组开的过大，而局部变量分配在栈上，导致堆栈溢出。</p>
<img src ="http://www.cppblog.com/meglory/aggbug/141755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2011-03-14 08:24 <a href="http://www.cppblog.com/meglory/archive/2011/03/14/141755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>硬链接和软连接</title><link>http://www.cppblog.com/meglory/archive/2010/12/22/137197.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Wed, 22 Dec 2010 08:12:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/12/22/137197.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/137197.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/12/22/137197.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/137197.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/137197.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt; font-family: Times New Roman;">文章转自http://www.ugrad.cs.ubc.ca/~cs219/CourseNotes/Unix/commands-links.html
<br>Hard links and Soft links
<p>Links
</p>
<p>As was    mentioned in the  section   on <a href="http://www.ugrad.cs.ubc.ca/%7Ecs219/CourseNotes/Unix/unix-fileStructure.html">file system
structure</a>, every  file has a  data structure (record)  known as an i-node  that stores
information about the file, and  the filename is  simply used as a  reference to that data
structure. A  link is  simply  a way to refer  to the
contents of a file. There are two types of links:
</p>
<ul>
    <li> Hard links: a hard link is a pointer
    to the file's i-node.    For  example,     suppose    that   we      have   a   file
    a-file.txt   that   contains  the     string   "The  file
    a-file.txt":
    <pre class="program">% cat a-file.txt<br>The file a-file.txt<br>%<br></pre>
    <p>Now  we use the  ln command to create a link
    to                   a-file.txt                         called
    b-file.txt:
    </p>
    <pre class="program"><span style="font-size: 10pt;">% ls<br>./  ../  a-file.txt<br>% ln a-file.txt b-file.txt<br>% ls<br>./  ../  a-file.txt  b-file.txt</span><br></pre>
    <p>
    </p>
    <center><img src="http://www.ugrad.cs.ubc.ca/%7Ecs219/CourseNotes/Unix/images/hardlink.jpg" alt="Hard Links"></center>
    <p>The           two        names     a-file.txt      and
    b-file.txt now refer to the same data:
    </p>
    <pre class="program">% cat b-file.txt<br>The file a-file.txt<br>%<br></pre>
    <p>If we modify the  contents of file b-file.txt, then we
    also modify the contents of file a-file.txt:
    </p>
    <pre class="program">% vi b-file.txt<br>...<br>% cat b-file.txt<br>The file a-file.txt has been modified.<br>% cat a-file.txt<br>The file a-file.txt has been modified.<br>%<br></pre>
    <p>and vice versa:
    </p>
    <pre class="program">% vi a-file.txt<br>...<br>% cat a-file.txt<br>The file a-file.txt has been modified again!<br>% cat b-file.txt<br>The file a-file.txt has been modified again!<br>%<br></pre>
    </li>
    <li>Soft links (symbolic     links): a
    soft link, also called symbolic link, is a  file that contains  the <em>name</em> of another
    file.  We can then access  the contents of the other  file through that   name. That is, a
    symbolic link  is  like a pointer  to  the pointer to the   file's contents. For instance,
    supposed      that     in       the  previous      example,      we     had      used  the
    -s           option       of              the
    ln to create a soft link:
    <pre class="program">% ln -s a-file.txt b-file.txt<br></pre>
    On disk, the file system would look like the following picture:
    <p>
    </p>
    <center><img src="http://www.ugrad.cs.ubc.ca/%7Ecs219/CourseNotes/Unix/images/softlink.jpg" alt="Soft Links"></center>
    </li>
</ul>
<p>But what are the differences  between the two types of  links, in practice? Let us look
at an example that  highlights these differences. The  directory currently looks like this
(let       us        assume         that       a-file.txt
b-file.txt are both hard links to the same file):
</p>
<pre class="program">% ls<br>./  ../  a-file.txt  b-file.txt<br></pre>
<p>Let         us    first   add        another          symbolic   link  using        the
-s option:
</p>
<pre class="program">% ln -s a-file.txt Symbolicb-file.txt<br>% ls -F<br>./  ../  a-file.txt  b-file.txt  Symbolicb-file.txt@<br></pre>
<p>A symbolic link, that  ls -F displays with a
@ symbol, has been added to the  directory. Let us examine  the contents of the file:
</p>
<pre class="program">% cat Symbolicb-file.txt <br>The file a-file.txt has been modified again!<br></pre>
<p>If we  change the  file Symbolicb-file.txt,  then the
file a-file.txt is also modified.
</p>
<pre class="program">% vi Symbolicb-file.txt<br>...<br>% cat Symbolicb-file.txt<br>The file a-file.txt has been modified a third time!<br>% cat a-file.txt<br>The file a-file.txt has been modified a third time!<br>% cat b-file.txt<br>The file a-file.txt has been modified a third time!<br>%<br></pre>
<p>If we remove the   file a-file.txt, we can no   longer
access              the       data        through           the      symbolic         link
Symbolicb-file.txt:
</p>
<pre class="program">% ls -F<br>./  ../  a-file.txt  b-file.txt  Symbolicb-file.txt@<br>% rm a-file.txt<br>rm: remove `a-file.txt'? y<br>% ls -F<br>./  ../  b-file.txt  Symbolicb-file.txt@<br>% cat Symbolicb-file.txt<br>cat: Symbolicb-file.txt: No such file or directory<br></pre>
<p>The   link  Symbolicb-file.txt     contains  the name
a-file.txt, and there  no longer is a  file with that name. On
the other hand, b-file.txt has  its  own pointer to  the
contents of  the file we called a-file.txt,  and  hence we can
still use it to access the data.
</p>
<pre class="program">% cat b-file.txt<br>The file a-file.txt has been modified a third time!<br></pre>
<p>Although it may seem like  symbolic links are not  particularly useful, hard links have
their drawbacks. The  most significant drawback is  that  hard links cannot  be created to
link a  file from one file  system to another file   on another file  system. A  Unix file
structure hierarchy can  consist of several different   file systems (possibly  on several
physical disks). Each file  system maintains its  own  information regarding the  internal
structure of the system and the individual files on the system.  Hard links only know this
system-specific information,  which make  hard links  unable  to  span file  systems. Soft
links, on the other hand, know the name of the file,  which is more  general, and are able
to span file systems.
</p>
<p>For a concrete analogy, suppose that our friend Joel User is a student  at both UBC and
SFU. Both  universities assign him  a student number. If  he tries to  use his UBC student
number at SFU, he will not meet with any success. He will also fail if he tries to use his
SFU  student number at UBC.  But  if he uses his   legal name, <em>Joel  User</em>, he  will
probably be successful. The student  numbers are system-specific  (like hard links), while
his legal name spans both of the systems (like soft links).
</p>
<p>Here is an example that demonstrates a situation where a hard link cannot be used and a
symbolic  link is  needed.  Suppose that we  try  to create a  hard link  from the current
working directory to the  C header stdio.h.
</p>
<pre class="program">% ln /usr/include/stdio.h stdio.h<br>ln: creating hard link `stdio.h' to `/usr/include/stdio.h': Invalid cross-device link<br>% <br></pre>
<p>The   ln      command        fails   because
stdio.h is stored on a different  file system. If we want
to create a link to it, we will have to use a symbolic link:
</p>
<pre class="program">% ln -s /usr/include/stdio.h stdio.h<br>% ls -l<br>lrwxrwxrwx    1 a1a1 guest          20 Apr 20 11:58 stdio.h -&gt; /usr/include/stdio.h<br>% ls<br>./  ../  stdio.h@<br>% <br></pre>
<p>Now we  can view the  file stdio.h just  as  if it was
located in the working directory. For example:
</p>
<pre class="program">% cat stdio.h <br>/* Copyright (c) 1988 AT&amp;T */ <br>/* All Rights Reserved */ <br><br>/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&amp;T */ <br>/* The copyright notice above does not evidence any */ <br>/* actual or intended publication of such source code. */ <br><br>/* <br>* User-visible pieces of the ANSI C standard I/O package. <br>*/ <br><br>#ifndef _STDIO_H <br>#define _STDIO_H <br>... <br>% <br></pre>
<p>The entire output of the cat command was not
included to save space.
</p>
<p>Note that the long listing (ls -l) of a soft
link does  not accurately reflect  its associated permissions.  To view the permissions of
the   file   or       directory     that   the    symbolic     link    references,     the
-L      options                   of        the
ls command can be used. For example:
</p>
<pre class="program">% ln -s /usr/include/stdio.h stdio.h<br><br>% ls -l stdio.h<br>lrwxrwxrwx   1 a1a1     undergrad     20 May 10 15:13 stdio.h -&gt; /usr/include/stdio.h<br><br>% ls -l /usr/include/stdio.h<br>-rw-r--r--   1 root     bin        11066 Jan  5  2000 /usr/include/stdio.h<br><br>% ls -lL stdio.h<br>-rw-r--r--   1 root     bin        11066 Jan  5  2000 stdio.h<br></pre>
<br>  </span>  <img src ="http://www.cppblog.com/meglory/aggbug/137197.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-12-22 16:12 <a href="http://www.cppblog.com/meglory/archive/2010/12/22/137197.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>