网站建设一般要多少费用,wordpress支持pdo,南海建设工程交易网站,网站建设 6万贵不贵进程概念
ps -elf#xff1a;查看操作系统的所有进程#xff08;Linux命令#xff09; ctrl z#xff1a;把进程切换到后台 crtl c#xff1a;结束进程 fg#xff1a;把进程切换到前台
获取进程进程号和父进程号
函数原型#xff1a;
pid_t getpid(void); //pid_t…进程概念
ps -elf查看操作系统的所有进程Linux命令 ctrl z把进程切换到后台 crtl c结束进程 fg把进程切换到前台
获取进程进程号和父进程号
函数原型
pid_t getpid(void); //pid_t它是一个有符号整数类型。
pid_t getppid(void);例子
#include stdio.h
#include sys/types.h
#include unistd.hint main()
{pid_t pid getpid();printf(当前进程的进程号为%d\n, pid);pid_t ppid getppid();printf(当前进程的父进程为%d\n, ppid);while(1);return 0;
}fork
概念fork() 是一个在操作系统编程中常用的函数用于创建一个新的进程。它通过复制调用进程称为父进程来创建一个新的进程称为子进程。子进程是父进程的副本它从 fork() 函数返回的地方开始执行。
以下是 fork() 函数的原型
#include sys/types.h
#include unistd.h
pid_t fork(void);fork() 函数没有参数它返回一个 pid_t 类型的值表示进程的状态。返回值有以下几种情况
如果返回值是负数-1则表示创建子进程失败。如果返回值是零0则表示当前代码正在子进程中执行。如果返回值是正数则表示当前代码正在父进程中执行返回值是新创建子进程的PID。
例子
#include stdio.h
#include sys/types.h
#include unistd.h
#include stdlib.hint main()
{ pid_t pid fork();if(pid -1){perror(fork);exit(1);}else if(pid 0){ printf(child pid%d, getpid%d, getppid%d\n, pid, getpid(), getppid());
// while(1)
// {printf(child\n);sleep(1);
// }}else { printf(parent pid%d, getpid%d, getppid%d\n, pid, getpid(), getppid());
// while(1)
// {printf(parent\n);sleep(2);
// }}printf(helloworld\n);//会输出两次return 0;
} fork笔试题
详情看下述代码
#include stdio.h
#include sys/types.h
#include unistd.hint main()
{for(int i 0; i 2; i){ fork();// printf(-\n); //6个-换行符会输出缓冲区里的的数据printf(-); // 8个-子进程会复制父进程输出缓冲区的数据} return 0;
}fork原理 下面输出都为1的原因是父子进程在不同的空间
#include stdio.h
#include sys/types.h
#include unistd.hint main()
{ int num 0;if(fork() 0){ num;printf(child %d\n, num);} else{ num;printf(parent %d\n, num);}/*输出为child 1parent 1*/return 0;}多进程读写
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include stdlib.h
#include unistd.h
#include string.hvoid child_write(int fd)
{char buf[128] {0};while(1){scanf(%s, buf);if(write(fd, buf, strlen(buf)) -1){perror(write);break;}lseek(fd, -1 * strlen(buf), SEEK_CUR);if(!strcmp(buf, bye))break;memset(buf, 0, 128);}//i lseek(fd, -1 * strlen(buf), _CUR);}void parent_read(int fd)
{char buf[128] {0};while(1){int ret read(fd, buf, sizeof(buf));if(ret -1){perror(read);break;}else if(ret 0)continue;if(!strcmp(buf, bye))break;printf(child get: %s\n, buf);memset(buf, 0, sizeof(buf));}
}int main()
{int fd open(hello.txt, O_CREAT | O_RDWR, 00400 | 00200);if(-1 fd){perror(open);exit(1);}if(fork() 0){child_write(fd);}else{parent_read(fd);}close(fd);return 0;
} 文章转载自: http://www.morning.kycwt.cn.gov.cn.kycwt.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.pqqxc.cn.gov.cn.pqqxc.cn http://www.morning.hgkbj.cn.gov.cn.hgkbj.cn http://www.morning.qxdrw.cn.gov.cn.qxdrw.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.tslfz.cn.gov.cn.tslfz.cn http://www.morning.tmxfn.cn.gov.cn.tmxfn.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.zrlms.cn.gov.cn.zrlms.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.nfyc.cn.gov.cn.nfyc.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.rngyq.cn.gov.cn.rngyq.cn http://www.morning.kqbwr.cn.gov.cn.kqbwr.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.tfbpz.cn.gov.cn.tfbpz.cn http://www.morning.sskkf.cn.gov.cn.sskkf.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.fkwp.cn.gov.cn.fkwp.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.alwpc.cn.gov.cn.alwpc.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.hjjhjhj.com.gov.cn.hjjhjhj.com http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.tddrh.cn.gov.cn.tddrh.cn http://www.morning.mftzm.cn.gov.cn.mftzm.cn http://www.morning.smj78.cn.gov.cn.smj78.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.mlnbd.cn.gov.cn.mlnbd.cn http://www.morning.drndl.cn.gov.cn.drndl.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.rkzk.cn.gov.cn.rkzk.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.ldmtq.cn.gov.cn.ldmtq.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.c7627.cn.gov.cn.c7627.cn