用C语言编写程序建立一个pipe

上传人:cn****1 文档编号:488201385 上传时间:2022-10-08 格式:DOCX 页数:8 大小:12.13KB
返回 下载 相关 举报
用C语言编写程序建立一个pipe_第1页
第1页 / 共8页
用C语言编写程序建立一个pipe_第2页
第2页 / 共8页
用C语言编写程序建立一个pipe_第3页
第3页 / 共8页
用C语言编写程序建立一个pipe_第4页
第4页 / 共8页
用C语言编写程序建立一个pipe_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《用C语言编写程序建立一个pipe》由会员分享,可在线阅读,更多相关《用C语言编写程序建立一个pipe(8页珍藏版)》请在金锄头文库上搜索。

1、1、用C语言编写程序,建立一个pipe,同时父进程生成一个子进程,子进程向pipe写入一 个字符串”Hello.”,父进程从pipe中读取该字符串。5#include #include #include #include #include #include int main()char buf20;int piledes2;pid_t pid;int ret;if(ret = pipe(piledes) = -1)perror(error on pipe:);exit(-1);elsepid = fork();if(pid 0)perror(error on fork:);exit(-1);e

2、lse if(pid = 0)close(piledes0);printf(to fu:);fgets(buf,sizeof(buf)-1,stdin);if(ret = write(piledes1,buf,strlen(buf) 0)perror(error on writing:);exit(-1);close(piledes1);else close(piledes1);if(ret = read(piledes0,buf,sizeof(buf)-1) 0)perror(error on reading:);exit(-1);bufret = 0;printf(from zi:%sn,

3、buf);close(piledes0);return 0;2、编写一个C语言程序lswc,使之功能能和ls | wc等价。(也就是利用,fork,exec,以及 ls,wc,以及重定向,或者是管道的系统调用)13#include #include #include #include #include #include #include #include #include #include #include #include #include #include int main(int argc,char *argv)int pildes2;int ret;char *argument3;pi

4、d_t pid;char buf1000;if(argc 2)printf(que shao can shun); exit(0);argument0 = ls;argument1 = /;argument2 = NULL;if(ret = pipe(pildes) 0)/ 、 perror(error on pipe:); exit(-1);pid = fork();if(pid 0)printf(fork errorn); exit(-1);if(pid = 0)close(pildes0);dup2(pildes1,1); execvp(ls,argument);close(pildes

5、1);elseclose(pildes1);/ dup2(fildes0,0); if(strcmp(argv1),wc) = 0) bzero(buf,sizeof(buf);if(ret = read(pildes0,buf,sizeof(buf) 0) printf(error on reading:); exit(-1); bufret = 0; printf(%s,buf); close(pildes0);return 0;3、实现一个具有头节点的链表。要求具有crea te, inser t, dele te, search功能,能对链 表。编写一个应用程序,使用上面的函数。 10

6、#include #include #include #include #define PASSWD 111111struct studentint num;char name20;float grade;struct student *next;typedef struct student stuNode;typedef struct student *stuPtr;int stuwrite(stuPtr head)FILE *fp;CC*/.、fp = fopen(stu,w); while(head-next != NULL)head = head-next; fwrite(head,s

7、izeof(stuNode),1,fp);close(fp);return 0;stuPtr sturead()FILE *fp;stuPtr head,p1,p2;int a;head = (stuPtr)malloc(sizeof(stuNode);head-next = NULL;p1 = head;CC*/.、fp = fopen(stu,r);dop2 = (stuPtr)malloc(sizeof(stuNode); p2-next = NULL;a = fread(head,sizeof(stuNode),1,fp); p1-next = p2;p1 = p2;while(a =

8、 1);fclose(fp);return head;void creat()stuPtr head,p;head = (stuPtr)malloc(sizeof(stuNode); p = (stuPtr)malloc(sizeof(stuNode); p-next = NULL;head-next = p;printf(学号:);scanf(%d,&(p-num);getchar();printf(姓名:);scanf(%s,p-name);getchar();printf(成绩:); scanf(%f,&(p-grade); getchar();stuwrite(head);stuPtr

9、 insert(stuPtr head)stuPtr p,q;p = (stuPtr)malloc(sizeof(stuNode);printf(学号:);scanf(%d,&(p-num); getchar();printf(姓名:);scanf(%s,p-name);getchar();printf(成绩:); scanf(%f,&(p-grade);getchar();q = head;while(q-next != NULL & p-num q-next-num) q = q-next;p-next = q-next;q-next = p;stuwrite(head);p = stur

10、ead();return p;stuPtr search(stuPtr head,int num)stuPtr p1,p2;int flag;p1 = (stuPtr)malloc(sizeof(stuNode);p2 = (stuPtr)malloc(sizeof(stuNode);p1 = head; while(p1-next != NULL) flag = 0;p2 = p1;p1 = p1-next;if(p1-num = num)flag = 1; break;if(flag = 1)p1 = p2;return p1;elseprin tf(没有此人n);return NULL;

11、stuPtr delete(stuPtr head, int num) stuPtr p1,p2;int flag;p1 = search(head,num);p2 = p1-next;p1-next = p2-next;free(p2);stuwrite(head);p1 = sturead();return p1;void print(stuPtr head)stuPtr p; int i;prin tf(学号t姓名t成绩n);p = head-next;while(p != NULL)if(p-num 0 & p-grade 0) printf(%dt%st%.1fn,p-num,p-n

12、ame,p-grade);p = p-next;void print1(stuPtr p)prin tf(学号t姓名t成绩n);while(p != NULL)if(p-num 0 & p-grade 0) printf(%dt%st%.1fn,p-num,p-name,p-grade);p = p-next;int main()int choose,num;stuPtr head,p;head = (stuPtr)malloc(sizeof(stuNode);p = (stuPtr)malloc(sizeof(stuNode);while(1)prin tf(l t创建链表n2 t添加节点n3t删除节点n4t查找学生n5t查看所有 学生信息nOt退出n);scanf(%d,&choose);switch(choose)case 1:creat();break;case 2: head = sturead(); insert(head);break;case 3:head = sturead();printf(删除学生的学号:); scanf(%d,&num);delete(head,num);break;case 4:head = sturead();printf(学生学号:

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号