摘要: 功能:主线程创建一个副线程,然后主线程负责将键盘输入的内容写到buffer,然后通知副线程将数据打印,打印完后,副线程通知主线程继续从输入缓冲区中将读到的字符写到buffer数组。当输入quit命令时,主线程退出,副线程也会随着主线程的结束而自动结束。 1 #include <stdio.h> 2 #include <pthread.h> 3 #include <semaphore.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 8 char buffer[128]; 9 sem_ 阅读全文
posted @ 2013-03-08 20:23 摩斯电码 阅读(167) 评论(0) 推荐(0) 编辑
摘要: 功能简述:一个程序运行时创建了一个子进程,子进程负责将键盘输入的内容写到pipe,父进程完成将从pipe中读到的内容输出到屏幕。当输入quit时,父子进程都退出。 #include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <strings.h>#include <string.h>#define N 64int main()... 阅读全文
posted @ 2013-03-08 19:47 摩斯电码 阅读(232) 评论(0) 推荐(0) 编辑
摘要: 下面的程序实现的功能是:writefifo.c完成从打开输入的文件名,然后将内容读取到管道readfifo.c完成将管道中读到的内容写到输入的文件名中。writefifo.c :#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <strings.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#includ 阅读全文
posted @ 2013-03-08 19:28 摩斯电码 阅读(267) 评论(0) 推荐(0) 编辑
摘要: #include <stdio.h>#include <stdlib.h>#include <signal.h>pid_t pid;void driver_handler(int signo) //司机的信号处理函数{ if (signo == SIGUSR1) printf("Let's go!\n"); if (signo == SIGUSR2) ... 阅读全文
posted @ 2013-03-08 18:49 摩斯电码 阅读(316) 评论(0) 推荐(0) 编辑
摘要: FILE *fp;int fd;fp = fopen(“1.c”, “r”);等价于 fd = open(“1.c”,O_RDONLY);fp = fopen(“1.c”, “r+“);等价于 fd = open(“1.c”, O_RDWR);fp = fopen(“1.c”, “w”); 等价于 fd = open(“1.c”, O_WRONLY | O_CREAT | O_TRUNC, 0666);fp = fopen(“1.c”, “w+”); 等价于 fd = open(“1.c”, O_RDWR | O_CREAT | O_TRUNC, 0666);fp = fopen(“1.c”, 阅读全文
posted @ 2013-03-08 18:43 摩斯电码 阅读(573) 评论(0) 推荐(0) 编辑
摘要: 假如在父进程中调用了标准IO,会在用户空间产生一个结构体,其中封装了文件IO返回的文件描述符fd,同时还有针对不同函数的输入输出缓冲区,与之对应的内核空间也有一个文件IO创建的结构体。所以,stdin、stdout和stderr都指向的是用户空间的那个结构体,类型是FILE *。当调用fork函数创建了子进程后,对于内核空间的那个结构体,子进程不会拷贝,但是对于用户空间的那个结构体,子进程会进行拷贝。即:子进程中的stdin、stdout和stderr的指向是与父进程中的stdin、stdout和stderr不同的(所以子进程的输入缓冲区与父进程的输入缓冲区不在一个位置,同理,他俩的输出缓冲区 阅读全文
posted @ 2013-03-08 12:35 摩斯电码 阅读(383) 评论(0) 推荐(0) 编辑