feat: update structure

This commit is contained in:
2024-01-22 14:27:40 +08:00
parent 7836c9185c
commit 3544a28a2e
559 changed files with 120846 additions and 4102 deletions

View File

@@ -0,0 +1,2 @@
This is a text file.
CS2106 Introduction to Operating Systems is a cool module!

View File

@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int slow(char *name, int time, int n) {
for(int i=n; i<=n+4; i++) {
printf("%s: i = %d\n", name, i);
sleep(time);
}
}
sdf
asdf
as
dfas
f
int main() {
int id;
if((id = fork()) != 0) {
int stat;
int my_id = getpid();
int parent_id = getppid();
printf("\nI am the parent.\n");
printf("My ID is %d\n", my_id);
printf("My parent's ID is %d\n", parent_id);
printf("My child's ID is %d\n\n", id);
slow("Parent", 1, 5);
printf("\nWaiting for child to exit.\n");
wait(&stat);
printf("CHILD HAS EXITED WITH STATUS %d\n", WEXITSTATUS(stat));
}
else
{
id = getpid();
int parent_id = getppid();
printf("\nI am the child.\n");
printf("My ID is %d\n", id);
printf("My parent's ID is %d\n\n", parent_id);
slow("Child", 2, 10);
exit(25);
}
}

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
int main(int ac, char **av, char **vp) {
printf("ac = %d\n", ac);
printf("Arguments:\n");
int i;
for(i=0; i<ac; i++)
printf("Arg %d is %s\n", i, av[i]);
i=0;
while(vp[i] != NULL) {
printf("Env %d is %s\n", i, vp[i]);
i++;
}
}

View File

@@ -0,0 +1,12 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
int main() {
if(fork() == 0) {
execlp("cat", "cat", "file.txt", NULL);
}
else
wait(NULL);
}

View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
int fp_in = open("./file.txt", O_RDONLY);
int fp_out = open("./talk.out", O_CREAT | O_WRONLY);
if(fork() == 0) {
dup2(fp_in, STDIN_FILENO);
dup2(fp_out, STDOUT_FILENO);
execlp("./talk", "talk", (char *) 0);
close(fp_in);
close(fp_out);
}
else
wait(NULL);
}

View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
int main() {
int p[2];
char str[] = "Hello this is the parent.";
// This creates a pipe. p[0] is the reading end,
// p[1] is the writing end.
if(pipe(p) < 0)
perror("lab2p2e: ");
// We will send a message from father to child
if(fork() != 0) {
close(p[0]); // The the end we are not using.
write(p[1], str, strlen(str));
close(p[1]);
wait(NULL);
}
else
{
char buffer[128];
close(p[1]); // Close the writing end
read(p[0], buffer, 127);
printf("Child got the message \"%s\"\n", buffer);
close(p[0]);
}
}

View File

@@ -0,0 +1,20 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
int main() {
printf("Be patient, the program will take around 7 seconds to run.\n");
printf("At the end you can do \"cat results.out\" to see the result.\n");
//
// Add code here to pipe from ./slow 5 to ./talk and redirect
// output of ./talk to results.out
// I.e. your program should do the equivalent of ./slow 5 | talk > results.out
// WITHOUT using | and > from the shell.
//
}

View File

@@ -0,0 +1,29 @@
#include <stdio.h>
// For the sleep functiopn
#include <unistd.h>
// For the atoi function that converts
// strings to integers
#include <stdlib.h>
int main(int ac, char **av) {
if(ac != 2) {
printf("\nCounts from specified integer n to n + 5\n");
printf("Usage: %s <integer>\n\n", av[0]);
exit(-1);
}
int n = atoi(av[1]);
int i;
for(i=n; i<=n+5; i++) {
printf("%d\n", i);
sleep(1);
}
printf("\nFinal value of i is %d\n\n", i);
exit(i);
}

View File

@@ -0,0 +1,13 @@
#include <stdio.h>
#define BUF_SIZE 128
int main() {
char input[128];
char *ret;
while(!feof(stdin)) {
ret = fgets(input, BUF_SIZE - 1, stdin);
if(ret != NULL)
printf("This is what I read: %s\n", input);
}
}