Linux创建进程

你的名字 2022-10-01 04:00 265阅读 0赞

创建进程:Unix—fork();windows—CreateProcess(…);

终止进程:Unix—exit(EXIT_SUCCESS),kill();windows—ExitProcess(…),TerminateProcess(…);

Linux启动新进程
方法有三个:
1)system函数
#include
int system(const char *string);//运行以字符串形式传递给他的命令。局限性是必须等待system函数启动的Shell返回,
前台进程执执行完时shell返回,后台进程已启动shell返回。
才能继续。system函数在启动进城之前必须启动一个shell,所以system函数效率不高。

eg:

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. int main()
  4. {
  5. printf("Running ps with system\n");
  6. system("ps ax &");
  7. printf("Done.\n");
  8. exit(0);
  9. }

2)替换进程映像——exec系列函数,将当前进程替换为一个新进程,新进程由path或file参数指定,原程序不再执行。
#include
char **environ;

int execl(const char *path,const char *arg0,…,NULL);
int execlp(const char *file,const char *arg0,…,NULL);
int execle(const char *path,const char *arg0,…,NULL,char *const envp[]);
int execv(const char *path,const char *const argv[],NULL);//argv以NULL结尾
int execvp(const char *file,const char *const argv[],NULL);
int execve(const char *path,const char *const argv[],NULL,char *const envp[]);//基础系统调用,其它函数可由他实现。
l—list,参数列表;v—vector,参数数组;p—path,$PATH变量;e—environment环境变量

eg:

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main()
  5. {
  6. printf("Running ps with execlp\n");
  7. execlp("ps", "ps", "ax", 0);
  8. printf("Done.\n");
  9. exit(0);
  10. }

3)复制进程映像-fork()
#include
#include
pid_t fork();
创建一个新进程,复制当前进程,进程表中创建一个新的表项,新表项的许多属性与当前进程相同,新进程有自己的
数据空间,环境,文件描述符。

发表评论

表情:
评论列表 (有 0 条评论,265人围观)

还没有评论,来说两句吧...

相关阅读

    相关 linux 进程创建 进程启动 监控

    0x00 简介 在入侵检测的过程中,进程创建监控是必不可少的一点,因为攻击者的绝大多数攻击行为都是以进程的方式呈现,所以及时获取到新进程创建的信息能帮助我们快速地定位攻击行为

    相关 Linux进程创建过程

    进程 进程的产生极大地提高的cpu的利用率,进程是cpu的执行单元。cpu可以通过进程切换提高使用率。那么Linux的进程是如何创建的呢? 这里一副图,我们先看个大概