該無腦計(jì)算機(jī)嗎?一個(gè)視頻講透計(jì)算機(jī)類所有專業(yè)!【框框的b站大學(xué)-計(jì)算機(jī)類專...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#define BUFFER_SIZE 64
int main() {
pid_t pid;
int pipefd[2];
char username1[] = "user1";
char username2[] = "user2";
char buffer[BUFFER_SIZE];
if (pipe(pipefd) == -1) {
printf("管道創(chuàng)建失??!\n");
return 1;
}
pid = fork();
if (pid == 0) {
// 子進(jìn)程
close(pipefd[0]); // 關(guān)閉讀端
// 創(chuàng)建用戶
system("useradd -m user1");
system("useradd -m user2");
// 將用戶名寫入管道
write(pipefd[1], username1, strlen(username1)+1);
write(pipefd[1], username2, strlen(username2)+1);
close(pipefd[1]); // 關(guān)閉寫端
exit(0);
} else if (pid > 0) {
// 父進(jìn)程
close(pipefd[1]); // 關(guān)閉寫端
// 從管道讀取用戶名
read(pipefd[0], buffer, BUFFER_SIZE);
printf("收到的用戶名: %s\n", buffer);
read(pipefd[0], buffer, BUFFER_SIZE);
printf("收到的用戶名: %s\n", buffer);
close(pipefd[0]); // 關(guān)閉讀端
wait(NULL); // 等待子進(jìn)程結(jié)束
} else {
printf("創(chuàng)建子進(jìn)程失??!\n");
return 1;
}
return 0;
}