【LittleXi】【MIT6.S081-2022Fall】Lab0
【LittleXi】【MIT6.S081-2022Fall】Lab0
report
part1
实验描述:重定向输出
实现
1、read_stdin
函数中,我们可以调用read方法,读取输入到buf中
int bytesRead = read(0, buf, sizeof(buf));
buf[bytesRead] = '\0';
2、log_stdout
函数中,可能处理比较麻烦,我们首先要将i转化为字符串,然后拼接为i.log
的形式
然后再调用open函数,并保存文件描述符fd
3、最后将fprintf
重定向到fd描述的文件上
实验结果
实现代码
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/fcntl.h"
#include "user/user.h"
#include <stdarg.h>
int fd;
char buf[1024];
int read_stdin(char* buf) {
/*
Description: Read stdin into buf
Example:
- read_stdin(); // Read the stdin into buf
Parameters:
- buf (char*): A buffer to store all characters
Return:
- 0 (int)
*/
// Your code here
int bytesRead = read(0, buf, sizeof(buf));
buf[bytesRead] = '\0';
// End
return 0;
}
int log_stdout(uint i) {
/*
Description: Redirect stdout to a log file named i.log
Example:
- log_stdout(1); // Redirect the stdout to 1.log and return 0
Parameters:
- i (uint): A number
Return:
- 0 (int)
*/
// Your code here
// 拼接字符串
char log_name[15];
char sint[10];
int m=0;
while(i)
{
sint[m]= i % 10 + '0';
m++;
i/=10;
}
for(int k=m-1;k>=0;k--)
log_name[m-1-k] = sint[k];
log_name[m]='.';
log_name[m+1]='l';
log_name[m+2]='o';
log_name[m+3]='g';
// 打开文件并获取文件描述符
fd = open(log_name, O_WRONLY | O_CREATE | O_TRUNC);
// End
return 0;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(2, "Usage: log_stdout number1n");
exit(1);
}
if (log_stdout(atoi(argv[1])) != 0) {
fprintf(2, "log_stdout: log_stdout failed\n");
exit(1);
}
if (read_stdin(buf) != 0) {
fprintf(2, "log_stdout: read_stdin failed\n");
exit(1);
}
//将输出重定向到fd指定的文件
fprintf(fd, buf);
exit(0);
}
part2
实验描述:多线程筛埃氏筛筛质数,并将每次筛出的数字输出到文件中
实现
1、该实验的重点是实现sub_process()
函数,在这里我们可以利用pile()
和fork
函数进行,首先接受一个管道p_left
和想要输出到的文件i.log
,然后在该函数中,我们可以新建一个p_right
管道,首先将p_left
中的数据读取出来,根据埃氏筛原理,第一个肯定是质数,打印质数,然后循环遍历,如果是该质数的倍数,那么可以直接输出到文件中,否则传入到下一个管道,并在结束的时候新fork
一个进程,将p_right
传入
2、在composites()
函数中,第一步首先fork
创建一个线程,将2~35写入管道p_right
中,然后在下面调用sub_process()
就行
3、实验中的所有等待操作可以创建信号量status
和用wait
函数等待子进程释放
实验结果
实验代码
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/fcntl.h"
#include "user/user.h"
#include <stdarg.h>
int log_stdout(uint i)
{
/*
Description: Redirect stdout to a log file named i.log.
Example:
- log_stdout(1); // Redirect the stdout to 1.log and return 0.
Parameters:
- i (uint): A number
Return:
- 0 (int)
*/
char log_name[15] = "0.log";
// Your code here
uint base = 1, i_temp;
if (i != 0)
{
for (base = 0, i_temp = i; i_temp != 0; ++base, i_temp /= 10)
;
for (uint base_temp = 0, i_temp = i; i_temp != 0; ++base_temp, i_temp /= 10)
{
log_name[base - base_temp - 1] = '0' + i_temp % 10;
}
strcpy(log_name + base, ".log");
}
close(1);
// printf("sa%s", log_name);
if (open(log_name, O_CREATE | O_WRONLY) != 1)
{
fprintf(2, "log_stdout: open failed\n");
return -1;
}
// End
return 0;
}
void sub_process(int p_left[2], int i)
{
/*
Description:
- Pseudocode:
prime = get a number from left neighbor
print prime m
loop:
m = get a number from left neighbor
if (p does not divide m)
send m to right neighbor
else
print composite m
- Be careful to close file descriptors that a process doesn't need,
because otherwise your program will run xv6 out of resources before the first process reaches 35.
- Hint: read returns zero when the write-side of a pipe is closed.
- It's simplest to directly write 8-bit (1-byte) chars to the pipes,
rather than using formatted ASCII I/O.
- Use pipe and fork to recursively set up and run the next sub_process if necessary
- Once the write-side of left neighbor is closed,
it should wait until the entire pipeline terminates, including all children, grandchildren, &c.
Example:
- sub_process(4); // Run the 4th sub_process.
Parameters:
- i (int): A number
Return:
- (void)
*/
if (log_stdout(i) < 0)
{
fprintf(2, "composites: log_stdout %d failed\n", i);
exit(1);
}
char m, prime;
int p_right[2], pid = 0;
pipe(p_right);
close(p_left[1]);
char nums[36] = {0};
read(p_left[0], nums, sizeof(nums));
close(p_left[0]);
prime = nums[0];
if (prime == 0)
{
exit(1);
}
int status;
// End
printf("prime %d\n", prime);
int p = 0;
while (1)
{
// m = get a number from left neighbor
m = nums[p++];
// End
// Use pipe and fork to recursively set up and run the next sub_process if necessary
if (m == 0)
{
pid = fork();
if (pid == -1)
{
fprintf(2, "composites: fork failed\n");
exit(1);
}
if (pid == 0)
{
sub_process(p_right, i + 1);
close(p_right[1]);
exit(0);
}
close(p_right[1]);
exit(0);
}
// End
if (m % prime != 0)
{
// send m to right neighbor
write(p_right[1], &m, sizeof(m));
// End
}
else
{
printf("composite %d\n", m);
}
}
// Once the write-side of left neighbor is closed, it should wait until the entire pipeline terminates,
// including all children, grandchildren, &c.
wait(&status);
// End
exit(0);
}
void composites()
{
/*
Description:
- A generating process can feed the numbers 2, 3, 4, ..., 35 into the left end of the pipeline:
the first process in the line eliminates the multiples of 2,
the second eliminates the multiples of 3,
the third eliminates the multiples of 5, and so on:
+---------+ +---------+ +---------+ +---------+
-2->| print 2 | | | | | | |
-3->| |-3->| print 3 | | | | |
-4->| print 4 | | | | | | |
-5->| |-5->| |- 5->| print 5 | | |
-6->| print 6 | | | | | | |
-7->| |-7->| |- 7->| |- 7->| print 7 |
-8->| print 8 | | | | | | |
-9->| |-9->| print 9 | | | | |
+---------+ +---------+ +---------+ +---------+
- Be careful to close file descriptors that a process doesn't need,
because otherwise your program will run xv6 out of resources before the first process reaches 35.
- Once the first process reaches 35, it should wait until the entire pipeline terminates,
including all children, grandchildren, &c.
Thus the main composites process should only exit after all the output has been printed,
and after all the other composites processes have exited.
- You should create the processes in the pipeline only as they are needed.
Example:
- sub_process(4); // Run the 4th sub_process.
Parameters:
Return:
- (void)
*/
int p_right[2];
int pid;
// int i = 0;
pipe(p_right);
int status = 0;
// Use pipe and fork to recursively set up and run the first sub_process
if ((pid = fork()) < 0)
{
fprintf(2, "composites: fork failed\n");
exit(1);
}
else if (pid == 0)
{
for (char i = 2; i <= 35; i++)
write(p_right[1], &i, sizeof(i));
exit(0);
}
// End
// The first process feeds the numbers 2 through 35 into the pipeline.
// for (char i = 2; i <= 35; i++)
// write(p_right[1], &i, sizeof(i));
wait(&status);
sub_process(p_right, 0);
// End
// Once the first process reaches 35, it should wait until the entire pipeline terminates,
// including all children, grandchildren, &c.
// Thus the main primes process should only exit after all the output has been printed,
// and after all the other primes processes have exited.
wait(&status);
// End
exit(0);
}
int main(int argc, char *argv[])
{
if (argc != 1)
{
fprintf(2, "Usage: composites\n");
exit(1);
}
composites();
exit(0);
}
part 3
实验描述:实现简略xargs.c
实现
1、利用read
函数读取前面的参数,然后利用字符串拼接,将当前的argv
和参数拼接在一起,形成新参数表new_argvs
2、利用fork
函数,新开一个进程,将之前拼接到的参数传入exec
函数中进行执行,并且在父进程中一直等待子进程完成命名执行
实验结果
实验代码
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/fcntl.h"
#include "user/user.h"
#include <stdarg.h>
char buf[1024];
char *new_argv[100];
int main(int argc, char *argv[])
{
int pid = fork();
if (pid == 0)
exec(argv[1], argv + 1);
while (wait(0) != pid)
{
}
char byte;
int l = 0, r = 0, new_argc = 0;
new_argv[new_argc] = argv[1];
new_argc += 1;
int num_read = 1;
while (num_read)
{
num_read = read(0, &byte, sizeof(byte));
if (num_read == 0)
break;
if (byte == '\n')
{
buf[r] = 0;
new_argv[new_argc] = buf + l;
new_argv[new_argc + 1] = 0;
pid = fork();
if (pid == 0)
exec(argv[1], new_argv);
while (wait(0) != pid)
{
}
l = r = 0;
new_argc = 1;
}
else if (byte == ' ')
{
buf[r] = 0;
new_argv[new_argc] = buf + l;
new_argc++;
r = l = r + 1;
}
else
buf[r++] = byte;
}
if (r > 0)
{
new_argv[new_argc] = buf + l;
buf[r] = 0;
new_argv[new_argc + 1] = 0;
pid = fork();
if (pid == 0)
exec(argv[1], new_argv);
}
else if (new_argc > 0)
{
buf[r] = 0;
new_argv[new_argc] = 0;
if (fork() == 0)
exec(argv[1], new_argv);
}
while (wait(0) != -1)
{
}
return 0;
}