c语言中创建子进程运行外部程序
答案:2 悬赏:60
解决时间 2021-11-27 05:20
- 提问者网友:但未伤到心
- 2021-11-26 12:34
我想通过自己的程序启动一个外部程序(比如aaa.o),然后在某些条件下关闭这个程序。光用system()不够吧,应该要创建子进程吧?请教关键几步应该怎么写啊?
最佳答案
- 二级知识专家网友:不服输就别哭
- 2021-11-26 12:59
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pid1, pid2, pid3, pid4, pid5, pid6;
pid1 = getpid();
printf("PID = %d\n", pid1);
pid2 = fork();
if (pid2 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
pid3 = fork();
if (pid3 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
pid5 = fork();
if (pid5 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
pid6 = fork();
if (pid6 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
sleep(30);
exit(0);
}
sleep(30);
exit(0);
}
pid4 = fork();
if (pid4 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
sleep(30);
return 0;
}
每一个进程都会在退出前 sleep 30秒,从而保证能够用 ps 看到,
编译 gcc testpid.c -o testpid
然后执行,可以看到
PID = 24913
PID = 24914, Parent PID = 24913
PID = 24916, Parent PID = 24913
PID = 24915, Parent PID = 24913
PID = 24917, Parent PID = 24915
PID = 24918, Parent PID = 24917
ps -ef 的结果
24913 24582 0 11:29 pts/19 00:00:00 ./testpid
24914 24913 0 11:29 pts/19 00:00:00 ./testpid
24915 24913 0 11:29 pts/19 00:00:00 ./testpid
24916 24913 0 11:29 pts/19 00:00:00 ./testpid
24917 24915 0 11:29 pts/19 00:00:00 ./testpid
24918 24917 0 11:29 pts/19 00:00:00 ./testpid
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pid1, pid2, pid3, pid4, pid5, pid6;
pid1 = getpid();
printf("PID = %d\n", pid1);
pid2 = fork();
if (pid2 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
pid3 = fork();
if (pid3 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
pid5 = fork();
if (pid5 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
pid6 = fork();
if (pid6 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
sleep(30);
exit(0);
}
sleep(30);
exit(0);
}
pid4 = fork();
if (pid4 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
sleep(30);
return 0;
}
每一个进程都会在退出前 sleep 30秒,从而保证能够用 ps 看到,
编译 gcc testpid.c -o testpid
然后执行,可以看到
PID = 24913
PID = 24914, Parent PID = 24913
PID = 24916, Parent PID = 24913
PID = 24915, Parent PID = 24913
PID = 24917, Parent PID = 24915
PID = 24918, Parent PID = 24917
ps -ef 的结果
24913 24582 0 11:29 pts/19 00:00:00 ./testpid
24914 24913 0 11:29 pts/19 00:00:00 ./testpid
24915 24913 0 11:29 pts/19 00:00:00 ./testpid
24916 24913 0 11:29 pts/19 00:00:00 ./testpid
24917 24915 0 11:29 pts/19 00:00:00 ./testpid
24918 24917 0 11:29 pts/19 00:00:00 ./testpid
全部回答
- 1楼网友:猖狂的痴情人
- 2021-11-26 13:19
#include
int main()
{
int pid;
pid=fork();
if(pid==0)
printf("i am son,my pid is %d.\n",getpid());
else if(pid>0)
{
printf("i'm father ,my pid is %d.\n",getpid());
pid=fork();
if(pid==0)
printf("i'm daughter process,my pid is %d.\n",getpid());
else if(pid>0)
printf("i'm father process,my pid is %d.\n",getpid());
else
printf("fork() error.\n");
}
else printf("fork() error.\n");
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯