Author Archive

linux下多线程编程初学笔记(1)

02月 26th, 2011

主要了解这三个函数

/*pthread_create函数主要完成线程的创建.
   参数解释: 
     thread参数是一个线程标识符,当新创建一个线程的时候,会以此变量记录线程标识符
     attr参数用于设置线程的属性,一般使用时,常为NULL
     start_routine 函数指针,指向一个函数.也就是这个线程所要运行的函数
     arg参数:线程所指向的函数的参数,如果函数不需要参数,设为NULL
*/
int pthread_create(pthread_t *thread,pthread_attr_t *attr, 
void *(*start_routine)(void *),void *arg); 
/*
  pthread_join : 调用pthread_join的线程将等待参数thread所指向的线程
  结束为止,当pthread_join函数返回时,被等待线程的资源被收回。
  参数解释:
   第一个参数为被等待的线程标识符,
   第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值
*/
int pthread_join(pthread *thread,void **thread_return); 
/*
   pthread_exit 主要用于结束线程.
   下面主要解释一下pthread_exit的参数 retval .当线程调用pthread_exit时,
   线程将退出,retval中保存需要返回的信息.只要pthread_join中的第二个参数thread_return不是NULL,这个值将被传递给thread_return.
*/
void pthread_exit(void *retval);

下面分析一个实例:

#include <pthread .h>
#include <stdio .h>
#include <errno .h>
#include <string .h>
void *thread_function(void *arg);
char message[] = "Hello World";
int main()
{
	int res;
	pthread_t a_thread;
	void *thread_result;
	res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
	if (res != 0)
	{
		perror("Thread creation failed");
		exit(1);
	}
	printf("Waiting for thread to finish...\n");
	res = pthread_join(a_thread, &thread_result); //pthread_join 阻塞执行的线程直到某线程结束
	if (res != 0)
	{
		perror("Thread join failed");
		exit(1);
	}
	printf("Thread joined, it returned %s\n", (char *)thread_result);
	printf("Message is now %s\n", message);
	exit(0);
}
void *thread_function(void *arg)
{
	printf("thread_function is running. Argument was %s\n", (char *)arg);
	sleep(3);
	strcpy(message, "Bye!");
	pthread_exit("Thank you for the CPU time");
}
</string></errno></stdio></pthread>

当主线程执行到pthread_create时,将创建一个新线程.我们将它称之为子线程
这时将根据cpu将主线程和子线程之间调度
当主线程执行到pthread_join的时候,将阻塞主线程的执行,直到a_thread所标示的子线程执行结束后,再执行主线程

执行结果

执行结果

on my way in the Entrance Test of English for Graduate

02月 3rd, 2011

考研路上写过的几篇作文,虽说自己大学四年中没怎么学习英语,不过最后这段时间的作文写作,我感觉还是有所收获的。

The Internet gossip

In the vivid picture, a person describes a bird-like creature to another person, who quickly spread this message through Internet along with imagination and exaggeration. The online message, traveling from one person to another, evolved into a sensational story about a man witnessed a great dinosaur.

What an interesting picture it is, however, the picture also warns us that in the Internet era, people should learn verify the truth of the hearsay. The history of gossip is as long as human beings itself, while, no age can parallel the Internet age in speed, range of spreading rumors. Advanced network tools, including BBS, MSN, chatting room, make it convenient to talk with remote friends, But at the same time, accelerate the diffusion of rumors. What’s more, a lot of people couldn’t identify the reality they heard or just believe it without thinking.

To control the disruptive influence of online gossip, on the one hand, we should improve the ability of indentify what is right in the hearsay and spread the message without over-exaggeration; on the other hand, the supervision department should strengthen the punishment to those people who intentional spread rumors. Only through the both efforts can we create a civilized , friendly Internet environment.

  Read more »

记我的一个梦

10月 28th, 2010

一个夜晚。

我一人走在街上,然后被一个漩涡卷上天,瞬息直上,周围乌云密布,雷电交加。我的内心中充满了恐惧,那种恐惧,在我的梦中,让我感到心头紧缩,惶惶无终,这是最深刻的恐惧。雷电在我脚上奔袭,黑的夜,一次次的照亮我恐惧的脸。最终我被卷进了一个UFO中,他像一个胶囊 .冰冷的玻璃和铁器在我的旁边,周围不见一个人,脚下的圆形舱口仍未关闭,风在脚下,时刻怕被掉下去,最终还是掉下去了。。。。

功课:BCD码完成大数计算

10月 2nd, 2010

复习组成原理到这一章节,突然想起当初利用数组和文件完成大数阶乘,给自己下个功课,毕业前(时间比较久远)完成利用BCD码实现大数的乘法。总体思路上,自己还是把握到了,就是不知道c语言是否可以实现这样的功能。听说8421BCD在会计事务中很常见,计算很方便,尝试一下。

递归可以实现的算法,均可用非递归完成。

09月 9th, 2010

在学习data structure的时候,我们总是能够发现有很多时候,用递归来实现,算法会变得异常简单,其实,能够用递归实现的算法,都能够用非递归来实现,原因是因为在支持递归的编译程序中,在执行时,都是在内存中开辟一段栈,在函数调用子函数,子函数返回函数的时候,存在着一个入栈和出栈的操作。对于递归,自然是函数调用函数的过程,因此,我们只要能够搞明白内存中的栈的进出,就能够自己利用栈来编写非递归的算法。

低下头,抬起头。

08月 27th, 2010

六级没过,错了几分,预料中。也算是给自己提个醒,我的英语写作还是有问题的,不够成熟,不够熟练。想凭借所谓的背诵和技巧,以及临时的突击来完成考试,其实与投机者无疑,注定无法成就。

感谢这次六级,暴露出来的缺点。考研的路上英语仍旧是重要的一环,玩了好多天了。接着自习,接着努力。

假期因为有课,所以不会感觉没时间学习,开学了,没有了辅导班的约束,我更应该克制自己玩的冲动和懒惰,早上读书,让它成为一种习惯吧。效率与开心并存,人最怕的时候,就是过程的中间,这一刻,疲惫与熟悉并存,也许就会忘记目标,偏离方向吧。当你在迷茫时,要继续低头往前走。当你熟悉与疲惫时,要抬头看一看方向。

那属于任何时代的狂飙突进者

08月 25th, 2010

这两天辅导班的课程结束了,自己获得了一个可以自在呼吸的机会。然,这样的机会却也让我不习惯。对着电脑,却不想玩,也许这样的舒压方式对我来说已经变得那么的不习惯。因为我明白,对于学习计算机的我,对着电脑做一天的无聊事情(电影,游戏),到头来,头脑发昏,两眼布满血丝,反倒让我痛苦不堪。

于是乎,我开始浏览blog,体味这些时代的前沿者的文字,探究他们的观念。看看那些在各个领域的elites 的工作学习方式。昨天晚上,一晚上,我都在浏览新东方的一位名师的博客,他的英语教学,独辟蹊径,让我受益良多。词汇,语法,简单,使用。今天早上起来,我又看了几篇英语的作文,考研路上,感觉时刻都应该准备着,k哥的作文,讲解的真的是实用。同时,今天偶然间,看了一点鲁迅的文章,感觉就是放到现在,依旧是能够凸显社会问题的。

他们,这些属于时代中的精英分子们,都有着狂热的激情,所以,我的路上并不孤单。继续奋斗。

使动词,动形容词

08月 18th, 2010

QZone Editor

转载自:http://blog.sina.com.cn/s/articlelist_1234396710_4_1.html

一类特立独行的词——使动词

英语江湖中,有一个简单却传奇般的问题,如分辨下面的两句:
YOS is interesting.(YOS 是个很有趣的人,重在外人的感觉。)
YOS is interested.(YOS 感到感到,重点在于自己的感觉。)
*注:YOS为“黄老鞋”英文(Yellow Old Shoes)之简称。

曾经各路神仙进行过辨析,比如说哪个用于描述人,哪个又多用于描述物,等等等等。一则难记,二则如上所示,两个句子都是描述黄老鞋这个人,毫无问题。——此说不得要领!
其实这个问题涉及到一类动词,其用法稍有独特处。待老鞋解剖之,则不但上面这个小问题,还有一堆相关问题,自可迎刃而解。
此类动词,谓之“使动词”。

一、 感觉类动词往往是使动词,意为“使……怎样”
若问surprise是什么意思,答曰:惊奇。其实作动词的时候,其意思是“使……惊奇”,永远是及物动词。所以,
√It surprised me. 它使我惊奇。
×I surprised. 我很惊奇。
而这样的词绝非个例,我们经常见到。老鞋法眼瞧处,发现:
感觉类的动词往往是使动词。
如excite, interest, satisfy, puzzle, upset, disgust, bore等。
上面的词,要说成“使/令……兴奋/感兴趣/满意/迷惑/烦恼/恶心/乏味”。
所以,“这个问题使我迷惑”,不能说成
The problem made me puzzle.
而只用说
The problem puzzled me.

it interested me .

the rumor excite her curiosity.

(使动词做动词的时候,主语往往是不是人。)

二、使动词的-ing型和-ed型分词往往当形容词来用,描述事物特征。
我们都知道,be doing或be done一般分别是进行体和被动态;分词作形容词,只是偶尔有之。且看且想,老鞋看出:
使动词的分词形式往往当形容词来用。
试看:
He is swimming.
He is interesting.
两句结构相同。前一句是普通动作的进行体“他正在游泳”;后一句是感觉类动词,是说“他这个人很有意思”。
再看:
She was killed.
She was satisfied.
前一句动作:她被杀了;后一句是描述:她感到满意。

三、-ing型的基本意义是“使/令……怎样的”,-ed表示“感到……的”。
把握了使动词的根本含义,再分辨由它变来的两种分词,就顺理成章了。
-ing形容词,表示“使人……的”,也就是在动词原形意义后加个“的”字。
-ed形容词,表示“感到……的”。

试依以上原理,准确分辨下面几对句子。
The boy is upset.男孩感到失望。
The boy is upsetting.男孩是个让人感到失望的家伙。

The students are satisfying.这群学生让(别人)感到满意
The students are satisfied.这群学生(自己)感到满意。

The teacher is boring.这个老师是令人讨厌的。
The teacher is bored.这个老师感到讨厌。

细心的人可能会发现,上面几对句子都是拿人来做主语。也就是两种形容词都可以用来描述人。如果拿物来做主语,就只有一个能够合格。
√The speech is boring.
×The speech is bored.

老鞋似乎回到了老路,考察起哪个能够修饰谁的问题了。其实这个只是顺便提及,并不是问题的要害。因为明白了上面所述的基本原理,谁能修饰谁,就是不言而喻的。
既然过去分词表示“感到……”,当然只能描述有感觉的事物,不能说一块石头感觉如何吧!而现在分词既然表示“令人有某种感觉”,则其空间是无限的,因为任何事物都可能引起人的某种感觉,岂止于人乎?

我梦想有一天,深谷弥合,高山夷平,歧路化坦途,曲径成通衢,上帝的光华再现,普天下生灵共谒。

07月 17th, 2010

I have a dream that one day every valley shall be exalted: every hill an d mountain shall be made low ,the rough places will be made plane, and the crooked places will be made straight, and the glory of the Lord shall be revealed, and all flesh shall see it together

豆瓣url脑图分析。

07月 6th, 2010
豆瓣url分析图
豆瓣url分析图

关于豆瓣的一个url分析,保存一份,供以后在分析