举一个fork函数使用例子
时间: 2024-06-10 10:07:22 浏览: 10
好的,下面是一个fork函数的使用例子:
```
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid; //pid_t 是Linux系统中进程id的数据类型
pid = fork(); //创建一个新的子进程,复制父进程的所有变量和资源
if (pid < 0) { //如果fork函数返回负数,说明出错了
printf("Fork failed.\n");
return 1;
}
if (pid == 0) { //如果pid等于0,说明是子进程
printf("This is child process.\n");
} else { //如果pid大于0,说明是父进程
printf("This is parent process, child pid is: %d\n", pid);
}
return 0;
}
```
这段代码的作用是创建一个新的子进程,并打印出当前进程的类型和进程ID。如果您对代码中的fork函数不太了解,可以阅读这篇CSDN博客:https://blog.csdn.net/weixin_44966950/article/details/104593803。
相关问题
举一个fork和select函数一起用的例子
好的,以下是一个使用 fork 和 select 函数的例子:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
int main(int argc, char** argv)
{
fd_set rfds;
struct timeval tv;
int retval, child_pid;
// 创建管道
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(1);
}
// 创建子进程
child_pid = fork();
if (child_pid == -1) {
perror("fork");
exit(1);
} else if (child_pid == 0) {
// 子进程关闭写端
close(pipefd[1]);
char buffer[100];
int len;
// 从管道读取数据,并输出到终端
while ((len = read(pipefd[0], buffer, sizeof(buffer))) > 0) {
write(STDOUT_FILENO, buffer, len);
}
// 关闭读端
close(pipefd[0]);
exit(0);
} else {
// 父进程关闭读端
close(pipefd[0]);
// 等待 3 秒钟
sleep(3);
// 向管道写入数据
write(pipefd[1], "Hello, world!", 13);
// 关闭写端
close(pipefd[1]);
exit(0);
}
return 0;
}
```
这个例子中,父进程创建了一个管道,并创建了一个子进程。父进程等待 3 秒钟之后,向管道写入了一条字符串,然后关闭了写端。子进程从管道中读取数据,并输出到标准输出。父进程和子进程之间使用 select 函数进行通信,以确保管道中有数据之后再进行读取。
Windows使用fork函数的c++例子
很抱歉,Windows系统并不支持fork函数,因为它是基于Unix系统的概念。在Windows系统中,可以使用CreateProcess函数来创建进程。下面是一个简单的C++程序,演示如何使用CreateProcess函数创建一个新进程:
```c++
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess(
NULL, // Application name
"notepad.exe", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
))
{
cout << "CreateProcess failed: " << GetLastError() << endl;
return 1;
}
cout << "New process created with ID: " << pi.dwProcessId << endl;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
```
该程序创建一个新的进程,启动记事本(notepad.exe),然后输出新进程的ID。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)