我之前在Perl中制作了以下程序:
my $db = DBconnection with DB2
if ($pid = fork()) {
#parent
} else {
#child
$db->execute("SELECT ****");
exit;
}
wait();
$db->execute("SELECT ****");
Run Code Online (Sandbox Code Playgroud)
我认为它等待子进程的结束想要这样做并且将通过pro-process为DB操作它.
此外,DB未连接到错误的内容.
怎么了?
我的c ++程序将使用fork()和execv()生成几个子进程.我该如何查询这些流程?如果我想关闭一个,我该怎么做?
可能重复:
fork()如何返回两个值?
我是C的新手,我对该fork()函数的返回值结构感到困惑.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
pid_t childPid;
childPid = fork();
printf("%d\n",childPid);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
28501
0
Run Code Online (Sandbox Code Playgroud)
由于pid_t是一个int类型,如何childPid有2个值?
如何~fork()函数调用不同然后简单的fork()..我已经尝试了下面的代码..在这个偶然的孩子正在执行父节的代码.
main()
{
pid_t pid = ~fork();
int a = 2;
int *ptr = (int*)malloc(sizeof(int));
*ptr = 2;
if(pid == 0)
{
a = a + 3;
*ptr = *ptr + 2;
printf("value at a and ptr in child process : %d and %d\n" , a , *ptr);
printf("address a and ptr in child process : %p and %p\n" , &a , ptr);
}
else
{
printf("value at a and ptr in parent process : %d and %d\n" , a …Run Code Online (Sandbox Code Playgroud) 我需要能够为一个小项目使用fork().问题是示例代码不起作用:
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
pid_t pID = fork();
if (pID == 0) // child
{
printf("CHILD\n");
// Code only executed by child process
}
else if (pID < 0) // failed to fork
{
printf("FAIL\n");
}
else // parent
{
printf("PARENT\n"); // Code only executed by parent process
}
// Code executed by both parent and child.
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器说:"20 D:\ Untitled1.cpp`forb'unclaclared(首先使用这个函数)"
但我已经在互联网上看到它应该位于#include <unistd.h>.
有任何想法吗?谢谢!
vfork可以在父进程中更改变量,但为什么不能增加堆栈呢?
void f1()
{
vfork();
}
Run Code Online (Sandbox Code Playgroud)
f2()导致崩溃.
void f2()
{
char buf[100];
}
int main()
{
f1();
f2();
_exit(0);
}
Run Code Online (Sandbox Code Playgroud)
如果我将vfork()更改为fork(),则不会发生崩溃.
在以下链接中,以及许多其他类似链接,exec通常被描述为:
exec调用是一种基本上用新程序替换整个当前进程的方法.它将程序加载到当前进程空间并从入口点运行它.
它真的取代了整个程序吗?它不仅仅是执行一个恰好体现main()所选程序的子程序/函数调用吗? - 在返回自己的背景和操作之前?
所以不是更恰当的描述,只是一个体现的子程序main()?(例如,将所选择的流程代码纳入当前的流程中?与完全"替换它"相反?
# original taken from the provided link
+--------+
| pid=7 |
| ppid=4 |
| bash |
+--------+
|
| calls fork
V
+--------+ +--------+
| pid=7 | forks | pid=22 |
| ppid=4 | ----------> | ppid=7 |
| bash | | bash |
+--------+ +--------+
| |
| waits for pid 22 | calls exec to run ls
| V
| +--------+
| | pid=22 |
| …Run Code Online (Sandbox Code Playgroud) 我想用他们的pid和打印一系列进程burst time.为此,我生成pid使用fork()然后pid使用它getpid().但是,由于fork创建了一个与父进程隔离运行的子进程,因此我没有得到预期的行为.程序应该做的是生成给定的进程number_of_process,然后在特定的结构元素内存储pid和随机burst time值.这是我的代码: -
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
struct process{
int pid;
int bt;
};
int main()
{
int no_of_process,i,new_process;
printf("Enter the number of process\n");
scanf("%d",&no_of_process);
struct process p[no_of_process];
for(i=0;i<no_of_process;i++){
new_process=fork();
p[i].pid = getpid();
p[i].bt = rand()%10;
//kill(getpid(),SIGKILL);
}
for(i=0;i<no_of_process;i++){
printf("process %d and bt %d\n",p[i].pid,p[i].bt);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图杀死子进程,但这会停止整个程序.进程数的输出= 2
process 6373 and bt 3
process 6373 and bt 6
process …Run Code Online (Sandbox Code Playgroud) 有谁能解释我这是如何工作的?
如果我的代码中有类似的东西
int pids[N];
for(int i = 0; i < N; i++){
pids[i] = fork();
if(pids[i]==0){
//do something
break;
}
}
Run Code Online (Sandbox Code Playgroud)
难道我不会创造N个孩子,然后在每个孩子中再次出现N个孩子并最终成为一个循环吗?
以下是我的代码的主要部分(采用"高级linux编程",清单3.7):
void clean_up_child_process ( int signal_number ) {
/* Clean up the child process. */
int status ;
wait ( &status ) ;
printf ( " wait finished \n" ) ;
/* Store its exit status in a global variable. */
child_exit_status = status ;
}
int main() {
/* Handle SIGCHLD by calling clean_up_child_process. */
pid_t child_pid ;
struct sigaction sigchld_action ;
memset ( &sigchld_action, 0, sizeof(sigchld_action) ) ;
sigchld_action.sa_handler = &clean_up_child_process ;
sigaction ( SIGCHLD, &sigchld_action, NULL ) …Run Code Online (Sandbox Code Playgroud)