C语言 在 C 中引用 fork() 的库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13643278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:37:12  来源:igfitidea点击:

Library that has reference to fork() in C

cfork

提问by cipher

What is the library that defines fork(). I am learning to use fork(). I found out that the Standard I/O Library : stdio.his enough for fork() to work but that does not apply in my case.

什么是定义 fork() 的库。我正在学习使用 fork()。我发现标准 I/O 库 :stdio.h足以让 fork() 工作,但这不适用于我的情况。

I am using gccin Code::Blockson Windows 8 Pro

我使用gccCode::BlocksWindows 8 Pro

My Code is:

我的代码是:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
#include <time.h>

int main(void)
{

    pid_t process;
    process = fork();

    if(process< 0)
    {
        printf("fork failed");
    }
    if(process > 0)
    {
        printf("\nParent Process Executed");
    }

    if(process == 0)
    {
        printf("\nChild Process Executed");
    }
    return 0 ;
}

The Exact Error I get is:

我得到的确切错误是:

useoffork.o:useoffork.c:(.text+0xf): undefined reference to `fork'

useoffork.o:useoffork.c:(.text+0xf): 对`fork'的未定义引用

采纳答案by iabdalkader

The C standard library (glibc) implements fork()which calls a UNIX/Linux-specific system call eventually to create a process, on Windows, you should use the winapi CreateProcess()see this example in MSDN.

C 标准库 (glibc) 实现fork()它最终调用特定于 UNIX/Linux 的系统调用以创建进程,在 Windows 上,您应该使用 winapi,CreateProcess()请参阅MSDN 中的此示例。

Note: Cygwin fork()is just a wrapper around CreateProcess()see How is fork() implemented?

注意:Cygwinfork()只是一个包装器,CreateProcess()参见Fork() 是如何实现的?

回答by cnicutar

I am using gcc in Code::Blocks on Windows 8 Pro

我在 Windows 8 Pro 上的 Code::Blocks 中使用 gcc

You don't have forkon windows. You can use cygwin or something like that though.

fork在窗户上没有。你可以使用 cygwin 或类似的东西。