c语言循环创建5个子进程
时间: 2024-03-10 07:49:22 浏览: 18
好的,这里是一个循环创建5个子进程的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int i;
pid_t pid;
for (i = 0; i < 5; i++) {
pid = fork();
if (pid == 0) {
printf("这是子进程%d,进程ID为%d\n", i+1, getpid());
exit(0);
}
}
printf("这是父进程,进程ID为%d\n", getpid());
return 0;
}
```
这段代码使用了C语言的fork()函数来创建子进程。在循环中,每次调用fork()函数都会创建一个子进程,直到创建5个子进程为止。在子进程中,我们输出了子进程的进程ID和进程号,然后使用exit(0)退出子进程,以避免子进程继续执行父进程的代码。在父进程中,我们输出了父进程的进程ID和进程号。
希望这段代码对你有所帮助。如果你还有其他问题,可以继续问我。
相关问题
C语言 创建4个子进程,分别求2500个浮点数之和
可以回答这个问题。C语言可以使用fork()函数创建子进程,然后在子进程中使用循环计算每个子进程需要计算的2500个浮点数之和,最后将结果返回给父进程。具体实现可以参考以下代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define NUM_CHILDREN 4
#define NUM_FLOATS 2500
float floats[NUM_CHILDREN][NUM_FLOATS];
float sum(float *arr, int size) {
float s = 0.0;
for (int i = 0; i < size; i++) {
s += arr[i];
}
return s;
}
int main() {
pid_t pid;
int status;
float total = 0.0;
// Generate random floats
for (int i = 0; i < NUM_CHILDREN; i++) {
for (int j = 0; j < NUM_FLOATS; j++) {
floats[i][j] = (float) rand() / RAND_MAX;
}
}
// Fork child processes
for (int i = 0; i < NUM_CHILDREN; i++) {
pid = fork();
if (pid == 0) {
// Child process
float s = sum(floats[i], NUM_FLOATS);
exit(s);
} else if (pid < 0) {
// Fork error
perror("fork");
exit(1);
}
}
// Wait for child processes to finish
for (int i = 0; i < NUM_CHILDREN; i++) {
pid = wait(&status);
if (WIFEXITED(status)) {
// Child process exited normally
total += WEXITSTATUS(status);
} else {
// Child process exited abnormally
fprintf(stderr, "Child process %d exited abnormally\n", pid);
}
}
printf("Total sum: %f\n", total);
return 0;
}
```
c语言 TCP多进程创建代码
下面是一个简单的TCP多进程创建代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8888
int main(void) {
int sockfd, newsockfd, pid;
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
char buffer[256];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
listen(sockfd, 5);
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &addr, &addrlen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
pid = fork();
if (pid < 0) {
perror("ERROR on fork");
exit(1);
}
if (pid == 0) {
close(sockfd);
memset(buffer, 0, sizeof(buffer));
if (read(newsockfd, buffer, sizeof(buffer)) < 0) {
perror("ERROR reading from socket");
exit(1);
}
printf("Here is the message: %s\n", buffer);
if (write(newsockfd, "I got your message", 18) < 0) {
perror("ERROR writing to socket");
exit(1);
}
exit(0);
}
else {
close(newsockfd);
}
}
close(sockfd);
return 0;
}
```
该代码创建了一个TCP服务器,通过多进程实现并发处理多个客户端请求。主进程负责监听客户端连接请求,并通过fork()函数创建一个子进程来处理客户端请求。子进程负责与客户端通信,读取客户端发送的消息并回复一个固定的消息。
在主循环中,每当有新的客户端连接请求被接受,就会创建一个子进程来处理该请求。子进程通过关闭sockfd来避免竞争条件,并在处理完请求后通过exit(0)退出。父进程则关闭newsockfd,继续监听新的客户端连接请求。