编程相关术语理解记录
2015-08-18 00:03
337 查看
本文记录Linux编程中会遇到的很多的相关的术语,持续更新
1. 同步和异步
这里举windows编程的消息发送函数很恰当,
SendMessage(). // 发送指定消息直到消息处理函数处理了该消息才将结果返回。可以理解为,要执行的步骤比较多,一直等到执行完才往下执行其他步骤,代码如下
2. 阻塞和非阻塞
阻塞是在要访问的资源不确定是否存在的情况下等待,阻塞和同步的区别在于阻塞并不知道要等待多久,也许一直等待。
非阻塞是查询式的,如果没有资源则立马返回报错或定时下次再去查询。
举个管道的例子来说明阻塞
而epoll就是一个非阻塞的读文件的方法
3. 并发和并行
并发指任务的调度是有先后顺序的,执行任务的切换时间很快,对调用者来说好像是同时执行的,实际上是串行的。
并行指两个线程同时执行,只有在多核或者多cpu的情况下存在
4.线程同步和互斥
两个线程A, B
互斥: 访问临界资源, 比如A,B都要访问打印机,则在某一刻只能有一个线程访问, <在上面的例子中进行改写>
同步: 控制两个线程的执行顺序, 如果 A 是 向缓存区中写数据,B从其中读, 则应该要保证A先写入然后 B 执行,<下面的程序保证B在A前面执行操作>
pthread_cond_signal(&Cond); sleep(3); // this time the mutex is be locked by thrB, so thrA will wait for B's unlock pthread_mutex_unlock(&Mutex); printf("thrB end...\n"); pthread_exit((void *)34);}int main(){ pthread_t ntidA, ntidB; pthread_create(&ntidA,
NULL, thr_fnA, NULL); pthread_create(&ntidB, NULL, thr_fnB, NULL); pthread_join(ntidA, NULL); pthread_join(ntidB, NULL); printf("The i is %d\n", i); exit(0);}
1. 同步和异步
这里举windows编程的消息发送函数很恰当,
SendMessage(). // 发送指定消息直到消息处理函数处理了该消息才将结果返回。可以理解为,要执行的步骤比较多,一直等到执行完才往下执行其他步骤,代码如下
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> void printSync(const char *str) { sleep(1); // assume this will do lots of thing puts(str); sleep(1); // assume this also will do lots of thing } void *thread_proc(void *data) { const char *str = data; sleep(1); puts(str); sleep(1); return (void *)0; } pthread_t tid; void printAsync(const char *str) { pthread_create(&tid, NULL, thread_proc, (void *)str); } int main() { printSync("I am Sync call"); printAsync("I am a Async call"); puts("end"); pthread_join(tid, NULL); return 0; }上面的例子可以反应出同步调用和异步调用的区别,只是一般的异步调用都会采用异步回调的机制,只需要将函数指针写到参数然后创建的新线程解析出来后当线程执行完成时调用指定回调函数即可。
2. 阻塞和非阻塞
阻塞是在要访问的资源不确定是否存在的情况下等待,阻塞和同步的区别在于阻塞并不知道要等待多久,也许一直等待。
非阻塞是查询式的,如果没有资源则立马返回报错或定时下次再去查询。
举个管道的例子来说明阻塞
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { char buf[1024] = {0,}; int fd[2]; if (0 != pipe(fd)) { perror("pipe error"); exit(1); } // read the pipe[0] when the pipe[1] not be writed read(fd[0], buf, 1024); // all will not be exec puts("read over"); close(fd[0]); close(fd[1]); return 0; }# ./a.out 会一直等待,读管道阻塞
而epoll就是一个非阻塞的读文件的方法
3. 并发和并行
并发指任务的调度是有先后顺序的,执行任务的切换时间很快,对调用者来说好像是同时执行的,实际上是串行的。
并行指两个线程同时执行,只有在多核或者多cpu的情况下存在
4.线程同步和互斥
两个线程A, B
#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *thr_fnA(void *arg) { printf("thrA running...\n"); return (void *)23; } void *thr_fnB(void *arg) { printf("thrB running...\n"); pthread_exit((void *)34); } int main() { pthread_t ntidA, ntidB; pthread_create(&ntidA, NULL, thr_fnA, NULL); pthread_create(&ntidB, NULL, thr_fnB, NULL); <pre name="code" class="cpp"> pthread_join(ntidA, NULL);pthread_join(ntidB, NULL); exit(0);}
互斥: 访问临界资源, 比如A,B都要访问打印机,则在某一刻只能有一个线程访问, <在上面的例子中进行改写>
#include <stdio.h> #include <stdlib.h> #include <pthread.h> <span style="color:#FF0000;"> pthread_mutex_t Mutex = PTHREAD_MUTEX_INITIALIZER;</span> int i = 0; void *thr_fnA(void *arg) { printf("thrA running...\n"); <span style="color:#FF0000;">pthread_mutex_lock(&Mutex);</span> i++; i++; i++; i++; sleep(3); // when thrA hold the lock, thrB will wait i++; i++; i++; i++; <span style="color:#FF0000;">pthread_mutex_unlock(&Mutex);</span> printf("thrA end...\n"); return (void *)23; } void *thr_fnB(void *arg) { sleep(1); // be sure thrA run before B out printf("thrB running...\n"); <span style="color:#FF0000;">pthread_mutex_lock(&Mutex);</span> i++; i++; i++; i++; i++; i++; i++; i++; <span style="color:#FF0000;">pthread_mutex_unlock(&Mutex);</span> printf("thrB end...\n"); pthread_exit((void *)34); } int main() { pthread_t ntidA, ntidB; pthread_create(&ntidA, NULL, thr_fnA, NULL); pthread_create(&ntidB, NULL, thr_fnB, NULL); pthread_join(ntidA, NULL); pthread_join(ntidB, NULL); printf("The i is %d\n", i); exit(0); }
同步: 控制两个线程的执行顺序, 如果 A 是 向缓存区中写数据,B从其中读, 则应该要保证A先写入然后 B 执行,<下面的程序保证B在A前面执行操作>
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t Mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t Cond = PTHREAD_COND_INITIALIZER; int i = 0; void *thr_fnA(void *arg) { printf("thrA running...\n"); pthread_mutex_lock(&Mutex); <pre name="code" class="cpp"> // when the wait begin, the mutex will be unlock, so the thrB can lock the mutex, // and when the wait is over, thrA will try to lock mutex againpthread_cond_wait(&Cond, &Mutex); pthread_mutex_unlock(&Mutex); printf("thrA end...\n"); return (void *)23;}void *thr_fnB(void *arg){ usleep(1); // be sure thrA will run before thrB running printf("thrB running...\n"); pthread_mutex_lock(&Mutex);
pthread_cond_signal(&Cond); sleep(3); // this time the mutex is be locked by thrB, so thrA will wait for B's unlock pthread_mutex_unlock(&Mutex); printf("thrB end...\n"); pthread_exit((void *)34);}int main(){ pthread_t ntidA, ntidB; pthread_create(&ntidA,
NULL, thr_fnA, NULL); pthread_create(&ntidB, NULL, thr_fnB, NULL); pthread_join(ntidA, NULL); pthread_join(ntidB, NULL); printf("The i is %d\n", i); exit(0);}
相关文章推荐
- google C++编程风格指南之头文件的包含顺序
- google C++编程风格指南之头文件的包含顺序
- Asp.Mvc 2.0用户的编辑与删除实例讲解(5)
- 深入讲解PHP Session及如何保持其不过期的方法
- 详解PHP中的mb_detect_encoding函数使用方法
- PHP的命令行命令使用指南
- 深入探究PHP的多进程编程方法
- 以实例全面讲解PHP中多进程编程的相关函数的使用
- 插入排序,选择排序,冒泡排序等常用排序算法(java实现)
- 在struts中配置文件中配置异常的跳转路径
- struts1权限拦截
- struts1权限拦截——传智播客
- sublime text 搭建java运行运行环境
- C++零基础教程,游戏开发入门
- 运行时动态编译Java源代码并加载利用
- spring-test的简单实用方式
- Java系列笔记 - JVM监控与调优
- golang substring
- java基本输入输出编程:文本界面的输入与输出
- C/C++获取本地时间常见方法