欢迎光临散文网 会员登陆 & 注册

该无脑计算机吗?一个视频讲透计算机类所有专业!【框框的b站大学-计算机类专...

2023-07-03 11:19 作者:doer清醒的重生  | 我要投稿

#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("管道创建失败!\n");

return 1;

}


pid = fork();


if (pid == 0) {

// 子进程

close(pipefd[0]); // 关闭读端


// 创建用户

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]); // 关闭写端


exit(0);

} else if (pid > 0) {

// 父进程

close(pipefd[1]); // 关闭写端


// 从管道读取用户名

read(pipefd[0], buffer, BUFFER_SIZE);

printf("收到的用户名: %s\n", buffer);


read(pipefd[0], buffer, BUFFER_SIZE);

printf("收到的用户名: %s\n", buffer);


close(pipefd[0]); // 关闭读端


wait(NULL); // 等待子进程结束

} else {

printf("创建子进程失败!\n");

return 1;

}


return 0;

}

该无脑计算机吗?一个视频讲透计算机类所有专业!【框框的b站大学-计算机类专...的评论 (共 条)

分享到微博请遵守国家法律