
1、先了解了解基础吧,找一本薄一点的,浅一点的书,了解一下什么是linux。有个基本了解即可。不建议马上看《鸟哥的Linux私房菜》,这本书很不错,如果你了解linux基础之后再看,对你可以说是不受益匪浅都不行。没基础看《鸟哥的Linux私房菜》比较痛苦,内容很多。
2、了解之后,在自己电脑上弄个虚拟机,装上linux系统(redhat或ubuntu都行)。专攻《鸟哥的Linux私房菜》。当你把这本书攻下来时,才知道原来命令行并不可怕,自己打着打着就很长啦。
学linux最主要靠实践,不实践,只看书对你没好处的哦。自己在虚拟机上慢慢玩吧,玩多了就知道咋回事啦。不懂的多找资料弄懂,弄懂之后再实践,这才会有收获。
程序代码test.c共两个线程,一个主线程,一个读缓存区的线程:#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char globe_buffer[100]
void *read_buffer_thread(void *arg) //这里先声明一下读缓存的线程,具体实现写在后面了
int main()
{
int res,i
pthread_t read_thread
for(i=0i<20i++)
globe_buffer[i]=i
printf("\nTest thread : write buffer finish\n")
sleep(3)\\这里的3秒是多余,可以不要。
res = pthread_create(&read_thread, NULL, read_buffer_thread, NULL)
if (res != 0)
{
printf("Read Thread creat Error!")
exit(0)
}
sleep(1)
printf("waiting for read thread to finish...\n")
res = pthread_join(read_thread, NULL)
if (res != 0)
{
printf("read thread join failed!\n")
exit(0)
}
printf("read thread test OK, have fun!! exit ByeBye\n")
return 0
}
void *read_buffer_thread(void *arg)
{
int i,x
printf("Read buffer thread read data : \n")
for(i=0i<20i++)
{
x=globe_buffer[i]
printf("%d ",x)
globe_buffer[i]=0//清空
}
printf("\nread over\n")
}
---------------------------------------------------------------------------------
以上程序编译:
gcc -D_REENTRANT test.c -o test.o –lpthread
运行这个程序:
$ ./test.o:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)