linux--进程(6)
2016-07-30 14:16
369 查看
fork()
进程:进程是程序的一次执行过程。1:父进程 0:子进程
fork():子、父进程顺序不定;拷贝父进程数据段
vfork():子先运行,然后父进程运行;与父进程共享数据段
//1.fork()函数 //子父进程并发进行(gcc编译) #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc,char **argv) { pid_t pid; pid = fork(); if (pid == 0) { printf("===child==="); } else { printf("===parent==="); } return 0; } /*结果:===child======parent=== 两个都打印出来,因为父子进程都进行*/
//2.fork()函数 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc,char **argv) { pid_t pid; int count = 0; pid = fork(); count++; printf("count == %d\n", count); return 0; } /*结果:count == 1 count == 1 拷贝父进程数据段*/
//3.vfork() #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc,char **argv) { pid_t pid; int count = 0; pid = vfork(); count++; sleep(2); printf("count == %d\n", count); return 0; } /*结果:count == 1 count == 2 与父进程共享数据段*/
popen
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc,char **argv) { FILE *fp; pid_t pid; char buf[1024]; fp=popen("ls", "r"); memset(buf, 0, sizeof(buf)); fread(buf, 1, 1024,fp); printf("==buf is%s\n", buf); printf("------------------"); return 0; } /*执行ls命令 也可用system("ls")*/
守护进程(Daemon)
1.创建子进程,父进程退出2.在子进程创建会话
3.改变当前目录文件
4.重设文件权限掩码
5.关闭文件描述符
//参考即可 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<sys/wait.h> #define MAXFILE 65535 void sigterm_handler(int arg); volatile sig_atomic_t _running = 1; int main(int argc, char **argv) { pid_t pc,pid; int i,fd,len,flag = 1; char *buf="this is a Dameon\n"; len = strlen(buf); pc = fork(); //第一步 if(pc<0){ printf("error fork\n"); exit(1); }else if(pc>0){ exit(0); } pid = setsid(); //第二步 if (pid < 0){ perror("setsid error"); } chdir("/"); //第三步 umask(0); //第四步 for(i=0;i<MAXFILE;i++){ //第五步 close(i); } signal(SIGTERM, sigterm_handler);/*第六步*/ while( _running ){ if( flag ==1 && (fd=open("/tmp/daemon.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0){ perror("open"); flag=0; exit(1); } write(fd,buf,len); close(fd); usleep(10*1000); //10毫秒 } return 0; } void sigterm_handler(int arg) { _running = 0; }
#include<stdlib.h> #include<string.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<sys/wait.h> #include <signal.h> #include <stdio.h> int main(int argc, char **argv) { pid_t pid; daemon(0,0);//daemon第一个参数,第二个参数表示不关闭的进程(如输出是1) while(1) { sleep(5); } return 0; } /*执行指令: gcc -o dasda m_pthread.c ./dasda ps -ef | grep dasda (查看守护进程) */
进程间通信
管道(pipe)和有名管道(name pipe)信号(signal)
队列(消息队列)
共享内存
信号量
套接口
管道
半双工、父子进程或者兄弟进程之间、先进先出
fd[0]写断、fd[1]读端
#include<stdlib.h> #include<string.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<sys/wait.h> #include <signal.h> #include <stdio.h> int main(int argc, char **argv) { pid_t pid; int pipe_fd[2]; int cnt = 0; pipe(pipe_fd); pid = fork(); if (pid == 0) { close(pipe_fd[1]);//只能留有一端,另一端要关闭 sleep(1); read(pipe_fd[0], &cnt, 4); printf("===cnt===%d\n", cnt); close(pipe_fd[0]); }else if(pid > 0) { close(pipe_fd[0]); cnt++; write(pipe_fd[1], &cnt, 4); close(pipe_fd[1]); }else { return -1; } return 0; }
相关文章推荐
- linux 监控之CACTI
- 关于Linux文件系统的的简单理解和认识
- linux 用户切换
- centos安装python2.7
- linux 之Mysql 同步复制
- Linux学习笔记10
- Linux快捷键--累积ing
- 安装telnet
- 对linux硬链接和软链接理解
- linux常用系统配置命令汇总
- linux自用命令
- Centos7安装Logwatch配合msmtp邮件客户端发送服务器监控分析日志
- 阿里云centOs下搭建Web应用步骤
- 【重装系统】线上Linux服务器(2TB)分区参考方案
- Linux 下设置防火墙白名单(RHEL 6 和 CentOS 7)
- Linux 命令总结
- lvm 2(逻辑卷管理)基础详解
- Linux Kernel Reading Guide - Configuring the Kernel
- centos bad ELF interpreter: No such file or directory
- Linux kernel configuration