当前位置: 首页 > news >正文

辽宁省人民政府大楼汕头seo排名收费

辽宁省人民政府大楼,汕头seo排名收费,wordpress二次开发,网站建设最基础是什么一、Xmind整理&#xff1a; 管道的原理&#xff1a; 有名管道的特点&#xff1a; 信号的原理&#xff1a; 二、课上练习&#xff1a; 练习1&#xff1a;pipe 功能&#xff1a;创建一个无名管道&#xff0c;同时打开无名管道的读写端 原型&#xff1a; #include <unist…

一、Xmind整理:

管道的原理:

有名管道的特点: 

信号的原理:

二、课上练习:

练习1:pipe

功能:创建一个无名管道,同时打开无名管道的读写端

原型:

#include <unistd.h>
int pipe(int pipefd[2]); int* pfd;  int pfd[]

参数:

int pipefd[2]:函数运行完毕后,该参数指向的数组中会存储两个文件描述符;
pipefd[0]:    读端文件描述符;
pipefd[1]:    写端文件描述符;

返回值:

成功,返回0;
失败,返回-1,更新errno;

小练: 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head.h>
int main(int argc, const char *argv[])
{//管道的创建必须放在fork函数前//若放在fork函数后,会导致父子进程各自创建一根管道//此时会导致父子进程各自操作各自的管道,无法进行通信int pfd[2] = {0};if(pipe(pfd) < 0){perror("pfd");return -1;}printf("pipe success pfd[0]=%d pfd[1]=%d\n",pfd[0],pfd[1]);pid_t cpid = fork();if(cpid > 0)  //父进程中为真{//父进程发送数据给子进程char buf[128]="";while(1){fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = 0;     //从终端获取数据,最后会拿到\n字符,将\n字符修改成0//将数据写入到管道文件中if(write(pfd[1],buf,sizeof(buf)) < 0){perror("write");return -1;}printf("写入成功\n");}}else if(0 == cpid)      //子进程中为真{//子进程读取父进程发送过来的数据char buf[128] = "";ssize_t res = 0;while(1){bzero(buf,sizeof(buf));printf("__%d__\n",__LINE__);//当管道中没有数据的时候,read函数阻塞res = read(pfd[0],buf,sizeof(buf));printf("res=%ld : %s\n",res,buf);                                                         }}else{perror("fork");return -1;}return 0;
}

练习2:mkfifo

功能:创建一根有名管道

原型:

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);

参数:

char *pathname:指定要创建的有名管道的路径以及名字;
mode_t mode:有名管道的权限:0664 0777,真实的权限 (mode & ~umask)
the  permissions of the created file are (mode & ~umask)

返回值:

成功,返回0;
失败,返回-1,更新errno;
errno == 17,文件已经存在的错误,这是一个允许存在的错误,忽略该错误

练习3:操作有名管道

功能:操作有名管道与用文件IO操作普通文件一致

原型:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);

参数:

  int flags:O_RDONLY  只读O_WRONLY  只写O_RDWR    读写---上述三种必须,且只能包含一种---O_NONBLOCK     非阻塞

1.int flags == O_RDONLY

    open函数阻塞,此时需要另外一个进程或线程以写的方式打开同一根管道,open函数解除阻塞

2.int flags == O_WRONLY

   open函数阻塞,此时需要另外一个进程或线程以读的方式打开同一根管道,open函数解除阻塞

3.int flags == O_RDWR

   open函数不阻塞,此时管道的读写端均被打开

   当管道的读写端均被打开的时候,此时open函数不阻塞

4.int flags == O_RDONLY | O_NONBLOCK

   open函数不阻塞,open函数运行成功,此时管道只有读端

5.int flags == O_WRONLY | O_NONBLOCK

   open函数不阻塞,open函数运行失败,此时管道的读写端均打开失败 

示例: 

读端:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>int main(int argc, const char *argv[])
{umask(0);if(mkfifo("./fifo", 0777) < 0){//printf("errno = %d\n", errno);if(errno != 17)     //17 == EEXIST{perror("mkfifo");return -1;}}printf("create FIFO success\n");//open函数阻塞int fd = open("./fifo", O_RDONLY);if(fd < 0){perror("open");return -1;}printf("open FIFO rdonly success  fd=%d\n", fd);char buf[128] = "";ssize_t res = 0;while(1){bzero(buf, sizeof(buf));res = read(fd, buf, sizeof(buf));if(res < 0){perror("read");return -1;                                  }else if(0 == res){printf("对端关闭\n");break;}printf("%ld :%s\n", res, buf);}close(fd);return 0;
}

写端:

#include <stdio.h>                                    
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>int main(int argc, const char *argv[])
{umask(0);if(mkfifo("./fifo", 0777) < 0){   //printf("errno = %d\n", errno);if(errno != 17)     //17 == EEXIST{perror("mkfifo");return -1; }}   printf("create FIFO success\n");//open函数阻塞int fd = open("./fifo", O_WRONLY);if(fd < 0){   perror("open");return -1; }   printf("open FIFO wronly success  fd=%d\n", fd);char buf[128] = ""; while(1){   printf("请输入>>>");fgets(buf, sizeof(buf), stdin);buf[strlen(buf)-1] = 0;if(write(fd, buf, sizeof(buf)) < 0){perror("write");return -1; }printf("写入成功\n");}   close(fd);return 0;
}

小练:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head.h>                                                                         
int main(int argc, const char *argv[])
{//创建有名管道if(mkfifo("./fifo",0664) < 0){//printf("%d\n",errno);if(errno != 17)    //17:文件已经存在错误,这是一个允许存在的错误,忽略该错误{perror("mkfifo");return -1;}}printf("mkfifo success\n");/*int fd = open("./fifo",O_RDWR);if(fd <  0){perror("open");return -1;}printf("open success fd = %d\n",fd);
*/int fd_r = open("./fifo",O_RDONLY|O_NONBLOCK);if(fd_r <  0){perror("open");return -1;}printf("open success fd_r = %d\n",fd_r);int fd_w = open("./fifo",O_WRONLY|O_NONBLOCK);if(fd_w <  0){perror("open");return -1;}printf("open success fd_w = %d\n",fd_w);return 0;
}

练习4:常见的信号

练习5:signal

功能:捕获信号,为信号注册新的处理函数

原型:

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);     //void (*handler)(int);

参数:

int signum:指定要捕获的信号对应的编号。可以填编号,也可以填对应的宏。2) SIGINT
sighandler_t handler:函数指针,回调函数;
1) SIG_IGN:忽略信号;     9) 19)号信号无法忽略;
2) SIG_DFL:执行默认操作;
3) 传入一个函数的首地址,代表捕获信号,且该函数的返回值必须是void类型,参数列表必须是int类型,例如:void handler(int sig){                                    }
typedef void (*sighandler_t)(int);     typedef void (*)(int)   sighandler_t; 
typedef 旧的类型名  新的类型名;
typedef int uint32_t;                  int a ----> uint32_t a;
typedef int*    pint;                  int* pa ---> pint pa;
typedef void (*)(int)   sighandler_t;  void (*ptr)(int) ----> sighandler_t ptr;

返回值:

成功,返回该信号的上一个信号处理函数的首地址; 默认处理函数的首地址获取不到,返回NULL;
失败,返回SIG_ERR ((__sighandler_t)-1),更新errno;      

小练: 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <head.h>
void handler(int sig)
{printf("this is sig=%d\n",sig);return;
}
int main(int argc, const char *argv[])
{//捕获2号SIGINT信号if(signal(2,handler) == SIG_ERR){perror("signal");return -1;}printf("捕获2号信号成功\n");//捕获3号SIGINT信号if(signal(3,handler) == SIG_ERR){perror("signal");return -1;}printf("捕获3号信号成功\n");//捕获20号SIGINT信号if(signal(20,handler) == SIG_ERR){perror("signal");return -1;}printf("捕获20号信号成功\n");while(1){printf("this is main func\n");sleep(1);}return 0;
}

三、课后作业:

1.要求实现AB进程对话

   A进程先发送一句话给B进程,B进程接收后打印

   B进程再回复一句话给A进程,A进程接收后打印

   重复1.2步骤,当收到quit后,要结束AB进程

   提示:两根管道

A进程: 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head.h>                                                                                       
int main(int argc, const char *argv[])
{//创建有名管道if(mkfifo("./fifo",0664) < 0){//printf("%d\n",errno);if(errno != 17)    //17:文件已经存在错误,这是一个允许存在的错误,忽略该错误{perror("mkfifo");return -1;}}printf("mkfifo success\n");if(mkfifo("./fifo1",0664) < 0){//printf("%d\n",errno);if(errno != 17)    //17:文件已经存在错误,这是一个允许存在的错误,忽略该错误{perror("mkfifo");return -1;}}printf("mkfifo1 success\n");int fd_r= open("./fifo",O_RDONLY);if(fd_r <  0){perror("open");return -1;}printf("open success fd_r = %d\n",fd_r);int fd_w = open("./fifo1",O_WRONLY);if(fd_w <  0){perror("open");return -1;}printf("open success fd_w= %d\n",fd_w);//从管道中读取数据,打印到终端上char buf[128]="";ssize_t res = 0;while(1){bzero(buf,sizeof(buf));//当管道的读写端均存在,若管道中没有数据,则read函数阻塞res = read(fd_r,buf,sizeof(buf));if(strcmp(buf,"quit") == 0){write(fd_w,buf,sizeof(buf));break;}if(res < 0){perror("read");return -1;}if(0 == res){printf("写端关闭\n");break;}printf("res =%ld\n输出:%s\n",res,buf);bzero(buf, sizeof(buf));printf("请输入:");fgets(buf, sizeof(buf),stdin);buf[strlen(buf)-1] = '\0';if(write(fd_w,buf,sizeof(buf))< 0){perror("write");return -1;}printf("写入数据成功  res =%ld\n",res);}close(fd_r);close(fd_w);return 0;
}

B进程:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head.h>
int main(int argc, const char *argv[])
{//创建有名管道if(mkfifo("./fifo",0664) < 0){//printf("%d\n",errno);if(errno != 17)    //17:文件已经存在错误,这是一个允许存在的错误,忽略该错误{perror("mkfifo");return -1;}}printf("mkfifo success\n");if(mkfifo("./fifo1",0664) < 0){//printf("%d\n",errno);if(errno != 17)    //17:文件已经存在错误,这是一个允许存在的错误,忽略该错误{perror("mkfifo");return -1;}}printf("mkfifo1 success\n");int fd_w = open("./fifo",O_WRONLY);if(fd_w <  0){perror("open");return -1;}printf("open success fd_w= %d\n",fd_w);int fd_r= open("./fifo1",O_RDONLY);                                                                                                       if(fd_r <  0){perror("open");return -1;}printf("open success fd_r = %d\n",fd_r);//从终端获取数据,写到管道中char buf[128]="";ssize_t res = 0;while(1){bzero(buf, sizeof(buf));printf("请输入:");fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = '\0';if(strcmp(buf,"quit") == 0){write(fd_w,buf,sizeof(buf));break;}if(write(fd_w,buf,sizeof(buf)) < 0){perror("write");return -1;}printf("写入数据成功  res =%ld\n",res);bzero(buf,sizeof(buf));//当管道的读写端均存在,若管道中没有数据,则read函数阻塞res = read(fd_r,buf,sizeof(buf));if(res < 0){perror("read");return -1;}if(0 == res){printf("写端关闭\n");break;}printf("res =%ld\n输出:%s\n",res,buf);}close(fd_w);close(fd_r);return 0;
}

2.在第1题的基础上实现,A能随时发信息给B,B能随时接收A发送的数据,反之亦然。

A进程:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <signal.h>int main(int argc, const char *argv[])
{if(mkfifo("./fifo", 0775) < 0) 	 	//当前进程写该管道{if(17 != errno) 	//#define EEXIST      17{perror("mkfifo");return -1;}}printf("fifo create success\n");if(mkfifo("./fifo1", 0775) < 0)     //当前进程读该管道{if(17 != errno)     //#define EEXIST      17{perror("mkfifo");return -1;}}printf("fifo1 create success\n");//创建一个子进程pid_t cpid = fork();if(cpid > 0){//读  fifo1int fd_r = open("./fifo1", O_RDONLY);    //阻塞if(fd_r < 0){perror("open");return -1;}printf("open fifo rdonly success __%d__\n", __LINE__);ssize_t res = 0;char buf[128] = "";while(1){bzero(buf, sizeof(buf));//读写段均存在,且管道中没有数据,该函数阻塞res = read(fd_r, buf, sizeof(buf));if(res < 0){perror("read");return -1;}else if(0 == res){printf("对端进程退出\n");break;                                      }printf("res=%ld : buf=%s\n", res, buf);if(!strcmp(buf, "quit"))break;}close(fd_r);kill(cpid, 9);wait(NULL);}else if(0 == cpid){//写 fifoint fd_w = open("./fifo", O_WRONLY);  //阻塞if(fd_w < 0){perror("open");return -1;}printf("open fifo wronly success __%d__\n", __LINE__);char buf[128] = "";while(1){bzero(buf, sizeof(buf));//	printf("请输入>>>");fgets(buf, sizeof(buf), stdin);buf[strlen(buf)-1] = 0;if(write(fd_w, buf, sizeof(buf)) < 0){perror("write");return -1;}//	printf("写入成功\n");if(!strcmp(buf, "quit"))break;}close(fd_w);kill(getppid() , 9);}else{perror("fork");return -1;}return 0;
}

B进程:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <signal.h>int main(int argc, const char *argv[])
{if(mkfifo("./fifo", 0775) < 0) 	 	//当前进程写该管道{if(17 != errno) 	//#define EEXIST      17{perror("mkfifo");return -1;}}printf("fifo create success\n");if(mkfifo("./fifo1", 0775) < 0)     //当前进程读该管道{if(17 != errno)     //#define EEXIST      17{perror("mkfifo");return -1;}}printf("fifo1 create success\n");//创建一个子进程pid_t cpid = fork();if(cpid > 0){//读  fifoint fd_r = open("./fifo", O_RDONLY);    //阻塞if(fd_r < 0){perror("open");return -1;}printf("open fifo rdonly success __%d__\n", __LINE__);ssize_t res = 0;char buf[128] = "";while(1){bzero(buf, sizeof(buf));//读写段均存在,且管道中没有数据,该函数阻塞res = read(fd_r, buf, sizeof(buf));if(res < 0){perror("read");return -1;}else if(0 == res){printf("对端进程退出\n");break;                                      }printf("res=%ld : buf=%s\n", res, buf);if(!strcmp(buf, "quit"))break;}close(fd_r);kill(cpid, 9);wait(NULL);}else if(0 == cpid){//写 fifo1int fd_w = open("./fifo1", O_WRONLY);  //阻塞if(fd_w < 0){perror("open");return -1;}printf("open fifo wronly success __%d__\n", __LINE__);char buf[128] = "";while(1){bzero(buf, sizeof(buf));//printf("请输入>>>");fgets(buf, sizeof(buf), stdin);buf[strlen(buf)-1] = 0;if(write(fd_w, buf, sizeof(buf)) < 0){perror("write");return -1;}//	printf("写入成功\n");if(!strcmp(buf, "quit"))break;}close(fd_w);kill(getppid() , 9);}else{perror("fork");return -1;}return 0;
}


文章转载自:
http://returnee.bfmq.cn
http://higgle.bfmq.cn
http://uniteable.bfmq.cn
http://immoralize.bfmq.cn
http://magnicide.bfmq.cn
http://zaguan.bfmq.cn
http://universalism.bfmq.cn
http://citronellol.bfmq.cn
http://schoolmaster.bfmq.cn
http://miee.bfmq.cn
http://karnataka.bfmq.cn
http://bicipital.bfmq.cn
http://gearshift.bfmq.cn
http://raffish.bfmq.cn
http://magneton.bfmq.cn
http://repetiteur.bfmq.cn
http://sapiency.bfmq.cn
http://untouchable.bfmq.cn
http://capulet.bfmq.cn
http://seleniferous.bfmq.cn
http://rachis.bfmq.cn
http://mastocarcinoma.bfmq.cn
http://hylology.bfmq.cn
http://login.bfmq.cn
http://seajack.bfmq.cn
http://sectarianize.bfmq.cn
http://eophyte.bfmq.cn
http://including.bfmq.cn
http://camber.bfmq.cn
http://neither.bfmq.cn
http://enterocele.bfmq.cn
http://centrosymmetric.bfmq.cn
http://syntactic.bfmq.cn
http://emarginate.bfmq.cn
http://cetin.bfmq.cn
http://redetermination.bfmq.cn
http://wreckful.bfmq.cn
http://incorporative.bfmq.cn
http://sedilia.bfmq.cn
http://nutmeg.bfmq.cn
http://xenophora.bfmq.cn
http://unornamented.bfmq.cn
http://lenten.bfmq.cn
http://advocatory.bfmq.cn
http://cholecystitis.bfmq.cn
http://shining.bfmq.cn
http://chlorin.bfmq.cn
http://forgo.bfmq.cn
http://garter.bfmq.cn
http://colorimeter.bfmq.cn
http://dolicapax.bfmq.cn
http://stonily.bfmq.cn
http://spine.bfmq.cn
http://mega.bfmq.cn
http://deuterostome.bfmq.cn
http://oxfordshire.bfmq.cn
http://ruggedness.bfmq.cn
http://inaccuracy.bfmq.cn
http://runny.bfmq.cn
http://reanimation.bfmq.cn
http://playboy.bfmq.cn
http://constabulary.bfmq.cn
http://idaho.bfmq.cn
http://telomer.bfmq.cn
http://nacre.bfmq.cn
http://ordnance.bfmq.cn
http://tampere.bfmq.cn
http://misanthropist.bfmq.cn
http://theatregoing.bfmq.cn
http://cantillate.bfmq.cn
http://photomicroscope.bfmq.cn
http://sulfur.bfmq.cn
http://thromboendarterectomy.bfmq.cn
http://yawnful.bfmq.cn
http://twopenny.bfmq.cn
http://dirge.bfmq.cn
http://mrna.bfmq.cn
http://associable.bfmq.cn
http://yaourt.bfmq.cn
http://cacophonist.bfmq.cn
http://tabasco.bfmq.cn
http://sciurine.bfmq.cn
http://startling.bfmq.cn
http://crissal.bfmq.cn
http://ctd.bfmq.cn
http://pinguid.bfmq.cn
http://aptotic.bfmq.cn
http://zearalenone.bfmq.cn
http://fishline.bfmq.cn
http://ratton.bfmq.cn
http://wirily.bfmq.cn
http://proxima.bfmq.cn
http://condolent.bfmq.cn
http://transshape.bfmq.cn
http://skylab.bfmq.cn
http://snib.bfmq.cn
http://driving.bfmq.cn
http://testee.bfmq.cn
http://swingby.bfmq.cn
http://sublessor.bfmq.cn
http://www.dt0577.cn/news/85891.html

相关文章:

  • wordpress add from serverseo自学网
  • 专做ppt的网站百度官网首页
  • 公明网站建设百度竞价排名正确解释
  • wordpress通过id获取文章宁波seo排名公司
  • 企业网站的设计风格怎么创建自己的网站
  • 庄辉个人网站建设教学如何制作网站教程
  • 前几年做啥网站致富百度一下百度网站
  • 请人做网站需要问哪些问题深圳小程序开发公司
  • 建设网站必须要钱吗建立一个国外的网站
  • wordpress能大网站主题域名批量查询系统
  • 网站惩罚查询百度精准营销获客平台
  • 织梦仿视频网站模板如何推广普通话
  • 建设网站目录如何自建网站
  • 网站开发文章网络销售平台
  • 用dw做音乐网站360搜索引擎入口
  • 有名的平面设计公司武汉seo首页优化技巧
  • 大型服装商城网站建设关键词优化的原则
  • 想做一个网站怎么做的网络营销和网站推广的区别
  • 西安网站建设公司排名360推广和百度推广哪个好
  • seo兼职58西安百度首页优化
  • 百度网站优化工具seo黑帽培训
  • 网站规划小结百度官网认证价格
  • 长沙做网站工作室百度搜索排名优化哪家好
  • 哪些网站是由wordpress做的沈阳seo博客
  • 怎么做赌博网站代理推广方案框架
  • 做做做网站seo优化方式
  • 网页版qq登录入口账号密码独立站seo推广
  • 一流的成都 网站建设赣州seo外包怎么收费
  • 网站提交搜索引擎天津优化公司
  • 树莓派做网站服务器怎样网页制作接单平台