线程编程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=0;i<20;i++)
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=0;i<20;i++)
{
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:
㈡ Linux多线程编程,替代sleep的几种方式
我只想要进程的某个线程休眠一段时间的,可是用sleep()是将整个进程都休眠的,这个可能就达不到,我们想要的效果了。 目前我知道有三种方式:
1 usleep
这个是轻量级的, 听说能可一实现线程休眠, 我个人并不喜欢这种方式,所以我没有验证它的可行信(个人不推荐)。
2 select
这个可以,我也用过这种方式, 它是在轮询。
3 pthread_cond_timedwait
采用pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t *mutex, const struct timespec *abstime)可以优雅的解决该问题,设置等待条件变量cond,如果超时,则返回;如果等待到条件变量cond,也返回。本文暂不将内部机理,仅演示一个demo。
首先,看这段代码,thr_fn为一个线程函数:
#include <stdio.h>#include <stdlib.h>int flag = 1;void * thr_fn(void * arg) {while (flag){printf("******\n");sleep(10);}printf("sleep test thread exit\n");}int main() {pthread_t thread;if (0 != pthread_create(&thread, NULL, thr_fn, NULL)) {printf("error when create pthread,%d\n", errno);return 1;}char c ;while ((c = getchar()) != 'q');printf("Now terminate the thread!\n");flag = 0;printf("Wait for thread to exit\n");pthread_join(thread, NULL);printf("Bye\n");return 0;}
输入q后,需要等线程从sleep中醒来(由挂起状态变为运行状态),即最坏情况要等10s,线程才会被join。采用sleep的缺点:不能及时唤醒线程。
采用pthread_cond_timedwait函数实现的如下:
#include <stdio.h>#include <sys/time.h>#include <unistd.h>#include <pthread.h>#include <errno.h>static pthread_t thread;static pthread_cond_t cond;static pthread_mutex_t mutex;static int flag = 1;void * thr_fn(void * arg) {struct timeval now;struct timespec outtime;pthread_mutex_lock(&mutex);while (flag) {printf("*****\n");gettimeofday(&now, NULL);outtime.tv_sec = now.tv_sec + 5;outtime.tv_nsec = now.tv_usec * 1000;pthread_cond_timedwait(&cond, &mutex, &outtime);}pthread_mutex_unlock(&mutex);printf("cond thread exit\n");}int main(void) {pthread_mutex_init(&mutex, NULL);pthread_cond_init(&cond, NULL);if (0 != pthread_create(&thread, NULL, thr_fn, NULL)) {printf("error when create pthread,%d\n", errno);return 1;}char c ;while ((c = getchar()) != 'q');printf("Now terminate the thread!\n");pthread_mutex_lock(&mutex);flag = 0;pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);printf("Wait for thread to exit\n");pthread_join(thread, NULL);printf("Bye\n");return 0;}
pthread_cond_timedwait()函数阻塞住调用该函数的线程,等待由cond指定的条件被触发(pthread_cond_broadcast() or pthread_cond_signal())。
当pthread_cond_timedwait()被调用时,调用线程必须已经锁住了mutex。函数pthread_cond_timedwait()会对mutex进行【解锁和执行对条件的等待】(原子操作)。这里的原子意味着:解锁和执行条件的等待是原则的,一体的。(In this case, atomically means with respect to the mutex andthe condition variable and other access by threads to those objectsthrough the pthread condition variable interfaces.)
如果等待条件满足或超时,或线程被取消,调用线程需要在线程继续执行前先自动锁住mutex,如果没有锁住mutex,产生EPERM错误。即,该函数返回时,mutex已经被调用线程锁住。
等待的时间通过abstime参数(绝对系统时间,过了该时刻就超时)指定,超时则返回ETIMEDOUT错误码。开始等待后,等待时间不受系统时钟改变的影响。
尽管时间通过秒和纳秒指定,系统时间是毫秒粒度的。需要根据调度和优先级原因,设置的时间长度应该比预想的时间要多或者少点。可以通过使用系统时钟接口gettimeofday()获得timeval结构体。
㈢ Linux 的多线程编程的几点注意事项
1、不要在子线程操作UI控件
2、如果你操作了,也绝对不能调用UpdateData来更新界面,否则程序Crash
3、这一条建立在第一条基础上---你在子线程操作UI控件,不可以让主线程等待某些条件(如等待子线程关闭,而子线程正在操作UI、等待进
入临界区,而子线程已经进入,并且操作UI),否则会出现假死...
4、最好方案:子线程操作数据,完成之后,通知主线程进行更新....
㈣ Linux下如何实现shell多线程编程以提高应用程序的响应
Linux中多线程编程拥有提高应用程序的响应、使多cpu系统更加有效等优点,下面小编将通过Linux下shell多线程编程的例子给大家讲解下多线程编程的过程,一起来了解下吧。
#!/bin/bash
#———————————————————————————–
# 此例子说明了一种用wait、read命令模拟多线程的一种技巧
# 此技巧往往用于多主机检查,比如ssh登录、ping等等这种单进程比较慢而不耗费cpu的情况
# 还说明了多线程的控制
#———————————————————————————–
function a_sub
{
# 此处定义一个函数,作为一个线程(子进程)
sleep 3 # 线程的作用是sleep 3s
}
tmp_fifofile=“/tmp/$.fifo” mkfifo $tmp_fifofile # 新建一个fifo类型的文件
exec 6《》$tmp_fifofile # 将fd6指向fifo类型
rm $tmp_fifofile thread=15 # 此处定义线程数
for
((i=0;i《$thread;i++));do echo
done 》&6 # 事实上就是在fd6中放置了$thread个回车符
for
((i=0;i《50;i++));do # 50次循环,可以理解为50个主机,或其他
read -u6 # 一个read -u6命令执行一次,就从fd6中减去一个回车符,然后向下执行,
# fd6中没有回车符的时候,就停在这了,从而实现了线程数量控制
{ # 此处子进程开始执行,被放到后台
a_sub &&
{ # 此处可以用来判断子进程的逻辑
echo “a_sub is finished”
}
||
{ echo “sub error”
}
echo 》&6 # 当进程结束以后,再向fd6中加上一个回车符,即补上了read -u6减去的那个
}
& done wait # 等待所有的后台子进程结束
exec 6》&- # 关闭df6 exit 0
说明:
此程序中的命令
mkfifo tmpfile
和linux中的命令
mknod tmpfile p
效?果相同。区别是mkfifo为POSIX标准,因此推荐使用它。该命令创建了一个先入先出的管道文件,并为其分配文件标志符6。管道文件是进程之间通信的一种方式,注意这一句很重要
exec 6《》$tmp_fifofile # 将fd6指向fifo类型
如果没有这句,在向文件$tmp_fifofile或者&6写入数据时,程序会被阻塞,直到有read读出了管道文件中的数据为止。而执行了上面这一句后就可以在程序运行期间不断向fifo类型的文件写入数据而不会阻塞,并且数据会被保存下来以供read程序读出。
通过运行命令:
time 。/multithread.sh 》/dev/null
最终运算时间: 50/15 = 3组(每组15)+1组(5个《15 组成一个组)= 4组,每组花费时间:3秒,
则 3 * 4 = 12 秒。
传统非多线程的代码 运算时间: 50 * 3 = 150 秒。
上面就是Linux下shell多线程编程的实例介绍了,使用多线程编程还能够改善程序结构,有兴趣的朋友不妨试试看吧。
㈤ linux下多线程编程问题,求各种优化
while((p=fork())==-1);//创建进程
if(p==0)
{
ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);//创建线程
if(ret!=0) perror("线程1创建失败");
ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);
if(ret!=0) perror("线程2创建失败");
ret=pthread_create(&id3,NULL,(void *)pthread3, NULL);
if(ret!=0) perror("线程3创建失败");
......
pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_join(id3,NULL);
..........//结束线程
exit(0);
}
void pthread1(void *arg)
{
while(time(NULL) < time_flag) //可以在这里判断总接入次数,现在是时间判断
{
if(pthread_mutex_lock(&mutex)!=0)//锁定数据 可以在这里判断接入次数,现在是写锁定判断
{
perror("锁定失败");
}
else printf("线程1:锁定数据量\n");
{
}
if(pthread_mutex_unlock(&mutex)!=0) //数据解锁 这里可以判断不超过3次锁定解锁
{
perror("解锁失败");
}
else
printf("线程1:我已解锁\n");
sleep(4);
}
}
其他的你自己补充吧,自己定义几个全局变量控制线程锁定解锁逻辑关系就行
㈥ Linux 的多线程编程中,如何给线程发信号
不管是在进程还是线程,很多时候我们都会使用一些定时器之类的功能,这里就定时器在多线程的使用说一下。首先在linux编程中定时器函数有alarm()和setitimer(),alarm()可以提供一个基于秒的定时功能,而setitimer可以提供一个基于微妙的定时功能。
alarm()原型:
#include <unistd.h>
unsigned int alarm(unsigned int seconds);
这个函数在使用上很简单,第一次调用这个函数的时候是设置定时器的初值,下一次调用是重新设置这个值,并会返回上一次定时的剩余时间。
setitimer()原型:
#include <sys/time.h>
int setitimer(int which, const struct itimerval *value,struct itimerval *ovalue);
这个函数使用起来稍微有点说法,首先是第一个参数which的值,这个参数设置timer的计时策略,which有三种状态分别是:
ITIMER_REAL:使用系统时间来计数,时间为0时发出SIGALRM信号,这种定时能够得到一个精准的定时,当然这个定时是相对的,因为到了微秒级别我们的处理器本身就不够精确。
ITIMER_VIRTUAL:使用进程时间也就是进程分配到的时间片的时间来计数,时间为0是发出SIGVTALRM信号,这种定时显然不够准确,因为系统给进程分配时间片不由我们控制。
ITIMER_PROF:上面两种情况都能够触发
第二个参数参数value涉及到两个结构体:
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
在结构体itimerval中it_value是定时器当前的值,it_interval是当it_value的为0后重新填充的值。而timeval结构体中的两个变量就简单了一个是秒一个是微秒。
上面是这两个定时函数的说明,这个函数使用本不是很难,可以说是很简单,但是碰到具体的应用的时候可能就遇到问题了,在多进程编程中使用一般不会碰到什么问题,这里说的这些问题主要体现在多线程编程中。比如下面这个程序:
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
void sig_handler(int signo)
{
alarm(2);
printf("alarm signal\n");
}
void *pthread_func()
{
alarm(2);
while(1)
{
pause();
}
}
int main(int argc, char **argv)
{
pthread_t tid;
int retval;
signal(SIGALRM, sig_handler);
if((retval = pthread_create(&tid, NULL, pthread_func, NULL)) < 0)
{
perror("pthread_create");
exit(-1);
}
while(1)
{
printf("main thread\n");
sleep(10);
}
return 0;
}
这个程序的理想结果是:
main thread
alarm signal
alarm signal
alarm signal
alarm signal
alarm signal
main thread
可事实上并不是这样的,它的结果是:
main pthread
alarm signal
main pthread
alarm signal
main pthread
㈦ Linux下如何实现shell多线程编程
程序代码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=0;i<20;i++)
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=0;i<20;i++)
{
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:
㈧ 关于linux下多线程编程
pthread_join 线程停止等待函数没有调用
pthread_create 线程生成后,没有等子线程停止,主线程就先停止了。
主线程停止后,整个程序停止,子线程在没有printf的时候就被结束了。
结论:不是你没有看到结果,而是在子线程printf("..................\n");之前整个程序就已经停止了。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define FALSE -1
#define TRUE 0
void *shuchu( void *my )
{
int j;
printf("..................\n");
}
int main()
{
int i = 0;
int rc = 0;
int ret1;
pthread_t p_thread1;
if(0!=(ret1 = pthread_create(&p_thread1, NULL, shuchu, NULL)))printf("sfdfsdfi\n");
printf("[%d]\n",p_thread1);
pthread_join(p_thread1, NULL);
return TRUE;
}
㈨ Linux中为什么要使用多线程编程
你好,多进程或多线程,都不会阻塞当前语句代码。为了您的理解,我就大胆举下面两个例子:多进程:你可以看成是本来是一条路的,现在从中间拆成两条,然后每一条路都有属于自己这条路的代码在运行。
多线程:你可以看成是一条路,然后分出车道,比如左车道和右车道甚至是停车道,然后每条车道都单独通车,其他车道的不能对这条车道进行干扰。
所以,把一条路从中间拆成两条,成本是很高的。但是把一条路分车道,成本就不是很高了。
对于您提出的main函数的疑问,当main函数最后执行完毕,程序退出后,所有的进程包括线程,都会被关闭的,哪怕你的程序中没有关闭,操作系统也会帮你关闭的,现在的操作系统都非常的完善了。当然,也存在有线程或进程不被释放的特殊情况,最好在编程中要记得释放。