Linux编程技术详解——读书笔记

/* * my.c * * Created on: 2010-12-15 * Author: flybird */ #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> //进程间通信,无名管道(pipe),只能在父子进程或兄弟进程间通信,半双工通信 int main() { int f_des[2]; int pid; char msg[100]; scanf("%s", msg); //创建父子进程间的通信管道 if(pipe(f_des) == -1){ perror("can not create the IPC pipe!"); return 1; } //创建子进程 pid = fork(); if(pid == -1){ perror("can not create new process!"); return 1; }else if(pid == 0){ //关闭子进程的写入数据(父子间共享文件描述符) close(f_des[1]); //从管道中读取数据 if(read(f_des[0], msg, 100) == -1){ perror("child process cannot read data from pipe!"); return 1; }else{ printf("in child process, receiver message: %s/n", msg); _exit(0); } }else{ //关闭父进程的读取数据端 close(f_des[0]); //向管道写入数据 if(write(f_des[1], msg, strlen(msg)) == -1){ perror("parent process cannot write data to pipe!"); return 1; }else printf("in parent peocess, send message: %s/n", msg); //等待子进程结束 wait(NULL); _exit(0); } return 0; } /* * my.c * * Created on: 2010-12-15 * Author: flybird */ #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> /* 使用管道实现进程间的双向通信 Linux系统中,管道只能单向通信,因此,要实现双向通信,必须创建两个管道,一个作为读取数据的管道,一个作为写入数据的管道。 */ int main() { int f_des1[2]; int f_des2[2]; int pid; char msg[100]; char p_msg[100]; scanf("%s", msg); //f_des1用于父进程向子进程传输数据 if(pipe(f_des1) == -1){ perror("can not create the IPC pipe!"); return 1; } //f_des1用于父进程向子进程传输数据 if(pipe(f_des2) == -1){ perror("can not create the IPC pipe!"); return 1; } pid = fork(); if(pid == -1) { perror("can not create new process!"); return 1; } else if(pid == 0) { close(f_des1[1]); close(f_des2[0]); //从父进程中读取数据 if(read(f_des1[0], msg, 100) == -1) { perror("child process can not read data from pipe!"); return 1; } else printf("in child process, receive message:%s/n", msg); //向父进程中写入数据 if(write(f_des2[1], msg, strlen(msg)) == -1) { perror("child process can not write data from pipe!"); return 1; } else printf("in child process, send message back: %s/n", msg); _exit(0); } else { close(f_des1[0]); close(f_des2[1]); //向子进程写入数据 if(write(f_des1[1], msg, strlen(msg)) == -1) { perror("parent process cannot write data to pipe!"); return 1; } else printf("in parent process, receive message: %s/n", p_msg); //从子进程中读取数据 if(read(f_des2[0], p_msg, 100) == -1) { perror("parent process cannot read data from pipe!"); return 1; } else printf("in parent processs, receive message: %s/n", p_msg); wait(NULL); _exit(0); } return 0; } /* * 进程间通信的其它方式有:消息队列,信号量,共享内存。 * ipc关键字可以通过ftok函数获得。 * 基本你IPC命令: * ipcs用于显示系统中进程间通信相关的资源。 * ipcrm命令可以手工移除IPC资源。 * msgget函数用于创建消息队列,或者读一个已经存在的消息队列进行存取。 */ /* * my.c * * Created on: 2010-12-15 * Author: flybird */ #include <stdio.h> #include <pthread.h> /* * 多线程 * pthread_create函数来创建线程,系统首先创建一个新的进程,再在该进程内创建需要的线程。 * 新创建的进程与原有进程是共享内存的,因此,与调用fork()函数而产生的进程是有本质的区别的。在第一次调用 * pthread_create函数时,系统还会创建一个管理进程,用于对多线程管理。 */ void* thread_info(void) { fprintf(stderr, "in thread_info function!/n"); } int main() { pthread_t thread_id; int ret; //调用pthread_create函数创建线程,并判断创建结果 ret = pthread_create(&thread_id, NULL, (void*)thread_info, NULL); if(ret == -1) { perror("can not create new thread"); return 1; } //保证主线程会再创建线程执行完成之后结束 sleep(5); return 0; } /* * 由于pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,在编译中要加 -lpthread参数。 例如:在加了头文件#include <pthread.h>之后执行 pthread.c文件,需要使用如下命令: gcc thread.c -o thread -lpthread 这种情况类似于<math.h>的使用,需在编译时加 -m 参数。 * */  

你可能感兴趣的:(thread,多线程,编程,linux,null,math.h)