linux下dup/dup2函数的用法
摘要:系统调用dup和dup2能够复制文件描述符。dup返回新的文件文件描述符(没有用的文件描述符最小的编号)。dup2可以让用户指定返回的文件描述符的值,如果需要,则首先接近newfd的值,他通常用来重新打开或者重定向一个文件描述符。他的原型如下:#include int dup(int oldfd);...
阅读全文
posted @
2014-12-31 16:37
张武亮
阅读(1099)
推荐(0)
匿名本地网络实现进程间通信
摘要:#include "stdio.h"#include "sys/socket.h"#include "unistd.h"int main(){ int s[2]; socketpair(AF_LOCAL, SOCK_STREAM, 0, s); int n; char buf[100]; ...
阅读全文
posted @
2014-12-30 16:23
张武亮
阅读(251)
推荐(0)
共享内存实现进程间通信
摘要:#include "stdio.h"#include "string.h"#include "unistd.h"#include "sys/shm.h"int main(){ int id = shmget(0x8888, 100, IPC_CREAT|0644); if (id==-1) {...
阅读全文
posted @
2014-12-30 16:23
张武亮
阅读(250)
推荐(0)
多线程实例
摘要:#include "stdio.h"#include "unistd.h"#include "pthread.h"void *func(void *p){ *(int*)p=200; int i; for (i=0; i<20; i++) { write(1, ".", 1); ...
阅读全文
posted @
2014-12-30 16:22
张武亮
阅读(154)
推荐(0)
线程传递参数
摘要:#include "stdio.h"#include "unistd.h"#include "pthread.h"void *func(void *p){ int i; int *a=(int*)p; for (i=0; i<20; i++) { a[i]=100+i; slee...
阅读全文
posted @
2014-12-26 15:50
张武亮
阅读(174)
推荐(0)
Linux进程间通信方法总结
摘要:①匿名管道(pipe)匿名管道(pipe)管道是一种半双工的通信方式,数据只能单向流动。如果要进行双工通信,需要建立两个管道。管道只能在具有亲缘关系的进程间使用,例如父子进程或兄弟进程。②有名管道(mkfifo)有名管道也是双半工的通信方式,但它允许无亲缘关系的进程间使用。③信号量(semophor...
阅读全文
posted @
2014-12-26 15:44
张武亮
阅读(808)
推荐(0)
通过inotify监控linux文件系统变化
摘要:http://www.mjmwired.net/kernel/Documentation/filesystems/inotify.txthttp://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html?ca=drs...
阅读全文
posted @
2014-12-24 15:21
张武亮
阅读(1143)
推荐(0)
多线程数据安全
摘要://Test01.c#include #include #include #include int num = 100;void* thread1_handler(void* param){ while (1) { ++num; printf("Thread1 num=%d\n", num); }}...
阅读全文
posted @
2014-12-24 15:13
张武亮
阅读(269)
推荐(0)
C语言函数参数既做出参又做入参的代表
摘要://使用fcntl对文件进行加锁#include "stdio.h"#include "unistd.h"#include "fcntl.h"intmain(){ intfd; structflocklk; intr; fd=open("a.txt",O_RDWR); if(fd==-1) { fd...
阅读全文
posted @
2014-12-23 10:50
张武亮
阅读(1644)
推荐(0)