feat: update structure
This commit is contained in:
2
cs2106/labs/lab2/part2/file.txt
Normal file
2
cs2106/labs/lab2/part2/file.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
This is a text file.
|
||||
CS2106 Introduction to Operating Systems is a cool module!
|
||||
47
cs2106/labs/lab2/part2/lab2p2a.c
Normal file
47
cs2106/labs/lab2/part2/lab2p2a.c
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
17
cs2106/labs/lab2/part2/lab2p2b.c
Normal file
17
cs2106/labs/lab2/part2/lab2p2b.c
Normal 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++;
|
||||
}
|
||||
}
|
||||
12
cs2106/labs/lab2/part2/lab2p2c.c
Normal file
12
cs2106/labs/lab2/part2/lab2p2c.c
Normal 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);
|
||||
}
|
||||
|
||||
24
cs2106/labs/lab2/part2/lab2p2d.c
Normal file
24
cs2106/labs/lab2/part2/lab2p2d.c
Normal 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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
34
cs2106/labs/lab2/part2/lab2p2e.c
Normal file
34
cs2106/labs/lab2/part2/lab2p2e.c
Normal 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]);
|
||||
}
|
||||
}
|
||||
|
||||
20
cs2106/labs/lab2/part2/lab2p2f.c
Normal file
20
cs2106/labs/lab2/part2/lab2p2f.c
Normal 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.
|
||||
//
|
||||
|
||||
}
|
||||
|
||||
29
cs2106/labs/lab2/part2/slow.c
Normal file
29
cs2106/labs/lab2/part2/slow.c
Normal 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);
|
||||
}
|
||||
|
||||
|
||||
13
cs2106/labs/lab2/part2/talk.c
Normal file
13
cs2106/labs/lab2/part2/talk.c
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user