失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > linux函数进程撤销 Linux进程控制函数

linux函数进程撤销 Linux进程控制函数

时间:2021-03-06 23:29:00

相关推荐

linux函数进程撤销 Linux进程控制函数

1. fork()

#include

#include

pid_t fork(void);

@一个进程可以调用fork函数创建一个新进程,由fork创建的进程被称为子进程,子进程是父进程的副本,它将获得父进程的数据空间,队,栈等资源副本

@pid_t 是一个宏定义,其实质是int,定义中

@返回值:若成功调用一次则返回两个值,子进程返回0,父进程返回子进程的ID;否则返回1

@注意:子进程持有的是父进程的副本,父子进程间不共享存储空间。

示例代码:

#include

#include

#include

int main()

{

int pid=fork();

if(pid==-1){

printf(""error);

}else if(pid==0){

prinf("this is the child process");

}else{

prinf("this is the parent process!childprocess is %d",pid);}

return 0;

}

2.system()

#include

int system(const char * string)

@system()会调用fork()产生子进程,由子进程来调用 /bin/sh/ -c string 来执行参数string字符串所代表的命令,此命令执行完成后随即返回原调用进程

@如果system在调用 /bin/sh 时失败则返回127,其他失败原因返回-1.如果system调用成功则最后返回执行shell命令后的返回值,但此返回值也有可能为system()调用 /bin/sh 失败所返回的127,因此最好再检查errno来确认执行成功。

代码示例:

#include

main(){

system("ls -a/etc/password/etc/shadow");

}

如果觉得《linux函数进程撤销 Linux进程控制函数》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。