摘要: void swap(void *v[], int a, int b){ void* tmp; tmp = v[a]; v[a]=v[b]; v[b]=tmp;}/* qsort: sort v[left]...v[right] into increasing order */void qsort(void *v[], int left, int right, int (*comp)(void *, void *)){ int i, last; if (left >= right) /* do nothing if array contains */ retur... 阅读全文
posted @ 2014-03-25 18:57 samu 阅读(243) 评论(0) 推荐(0) 编辑
摘要: 一、利用select多路复用I/O的Web服务应用模型/* 可读、可写、异常三种文件描述符集的申明和初始化。*/ fd_set readfds, writefds, exceptionfds; FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptionfds); int max_fd; /* socket配置和监听。*/ sock = socket(...); bind(sock, ...); listen(sock, ...); /* 对socket描述符上发生关心的事件进行注册。*/ FD_SET(&r 阅读全文
posted @ 2014-03-21 20:23 samu 阅读(689) 评论(0) 推荐(0) 编辑
摘要: #include #include int main(){ FILE* stream = popen ("sort", "w"); fprintf (stream, "This is a test.\n" ); fprintf (stream, "Hello, world.\n"); fprintf (stream, "My dog has fleas.\n"); fprintf (stream, "This program is great.\n"); fprintf (s 阅读全文
posted @ 2014-03-21 19:55 samu 阅读(209) 评论(0) 推荐(0) 编辑
摘要: #include #include #include #include int main(){ int fds[2]; pid_t pid; pipe( fds ); pid = fork(); if( pid == (pid_t)0 ) { close( fds[1] ); dup2(fds[0], STDIN_FILENO ); execlp( "sort", "sort", 0 ); } ... 阅读全文
posted @ 2014-03-21 19:51 samu 阅读(241) 评论(0) 推荐(0) 编辑
摘要: flags must include read,write,execute permission.for examples:semget( 3333, 1, IPC_CREAT | IPC_EXCL | 0666 );if there is not a '0666', then 'semop' wait will succed always. 阅读全文
posted @ 2014-03-21 14:10 samu 阅读(163) 评论(0) 推荐(0) 编辑
摘要: #include #include #include int main (){int segment_id;char* shared_memory;struct shmid_ds shmbuffer;int segment_size;const int shared_segment_size = 0x6400;/* Allocate a shared memory segment. */segment_id = shmget (IPC_PRIVATE, shared_segment_size,IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);/* Attach 阅读全文
posted @ 2014-03-20 20:30 samu 阅读(378) 评论(0) 推荐(0) 编辑
摘要: Shared memory permits processes to communicate by simply reading and writing to a specified memory location.Mapped memory is similar to shared memory, except that it is associated with a file in the filesystem.Pipes permit sequential communication from one process to a related process.FIFOs are simi 阅读全文
posted @ 2014-03-20 20:18 samu 阅读(151) 评论(0) 推荐(0) 编辑
摘要: #include int thread_flag;pthread_cond_t thread_flag_cv;pthread_mutex_t thread_flag_mutex;void initialize_flag (){/* Initialize the mutex and condition variable.pthread_mutex_init (&thread_flag_mutex, NULL);pthread_cond_init (&thread_flag_cv, NULL);/* Initialize the flag value. */thread_flag 阅读全文
posted @ 2014-03-20 20:10 samu 阅读(187) 评论(0) 推荐(0) 编辑
摘要: #include #include #include struct job {/* Link field for linked list.struct job* next;*//* Other fields describing work to be done... */};/* A linked list of pending jobs.struct job* job_queue;*//* A mutex protecting job_queue. */pthread_mutex_t job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;/* A semap 阅读全文
posted @ 2014-03-20 20:07 samu 阅读(180) 评论(0) 推荐(0) 编辑
摘要: [(lambda x: x*x)(x) for x in range(10)]Or better yet:[x*x for x in range(10)] 阅读全文
posted @ 2014-03-19 19:47 samu 阅读(149) 评论(0) 推荐(0) 编辑