Add lab 1
This commit is contained in:
commit
e359896da0
10 changed files with 166 additions and 0 deletions
14
lab1/demos/chdir.c
Normal file
14
lab1/demos/chdir.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/limits.h>
|
||||
|
||||
int main(void) {
|
||||
char s[PATH_MAX];
|
||||
|
||||
// printing current working directory
|
||||
printf("%s\n", getcwd(s, 100));
|
||||
// using the command
|
||||
chdir("..");
|
||||
// printing current working directory after chdir is executed
|
||||
printf("%s\n", getcwd(s, 100));
|
||||
}
|
10
lab1/demos/execve.c
Normal file
10
lab1/demos/execve.c
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
char *args[] = {"fork", NULL};
|
||||
|
||||
execve("fork", args, NULL);
|
||||
|
||||
printf("End of execve\n");
|
||||
}
|
8
lab1/demos/exit1.c
Normal file
8
lab1/demos/exit1.c
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
printf("start of program\n");
|
||||
_exit(3);
|
||||
printf("End of program\n");
|
||||
}
|
13
lab1/demos/exit2.c
Normal file
13
lab1/demos/exit2.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (fork() == 0) {
|
||||
printf("Child %d with pid %d\n", i, getpid());
|
||||
_exit(0);
|
||||
}
|
||||
}
|
||||
printf("hello\n");
|
||||
}
|
9
lab1/demos/fork.c
Normal file
9
lab1/demos/fork.c
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
fork(); // 2
|
||||
fork(); // 4
|
||||
fork(); // 8
|
||||
printf("hello\n");
|
||||
}
|
19
lab1/demos/kill.c
Normal file
19
lab1/demos/kill.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(void) {
|
||||
int pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
printf("Child with pid %d\n", getpid());
|
||||
while (1) {
|
||||
printf("*");
|
||||
}
|
||||
} else {
|
||||
sleep(1);
|
||||
kill(pid, SIGKILL);
|
||||
printf("\nParent killed pid %d with signal %d\n", pid, SIGKILL);
|
||||
}
|
||||
}
|
6
lab1/demos/leak.c
Normal file
6
lab1/demos/leak.c
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
int *p = malloc(64);
|
||||
(void) p;
|
||||
}
|
30
lab1/demos/timing.c
Normal file
30
lab1/demos/timing.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include <sys/times.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(void) {
|
||||
struct tms start_times;
|
||||
struct tms cease_times;
|
||||
|
||||
times(&start_times);
|
||||
|
||||
int pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
printf("This is a child process!\n");
|
||||
|
||||
for (int i = 0; i < 300000; i += 1) {
|
||||
printf("Got a number %d\n", i);
|
||||
}
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
waitpid(pid, NULL, 0);
|
||||
times(&cease_times);
|
||||
|
||||
printf(" user time: %lf\n", (double) (cease_times.tms_utime - start_times.tms_utime) / sysconf(_SC_CLK_TCK));
|
||||
printf("system time: %lf\n", (double) (cease_times.tms_stime - start_times.tms_stime) / sysconf(_SC_CLK_TCK));
|
||||
printf(" user time: %lf\n", (double) (cease_times.tms_cutime - start_times.tms_cutime) / sysconf(_SC_CLK_TCK));
|
||||
printf("system time: %lf\n", (double) (cease_times.tms_cstime - start_times.tms_cstime) / sysconf(_SC_CLK_TCK));
|
||||
}
|
21
lab1/demos/wait.c
Normal file
21
lab1/demos/wait.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int f = fork();
|
||||
|
||||
if (f == 0) {
|
||||
printf("Child %d with pid %d\n", i, getpid());
|
||||
_exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
while (wait(NULL) > 0); // This waits until all children are terminated
|
||||
// while (waitpid(-1, NULL, 0) > 0); // Same effect
|
||||
|
||||
printf("parent\n");
|
||||
return 0;
|
||||
}
|
36
lab1/zombies.c
Normal file
36
lab1/zombies.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
// Write a program that creates a zombie, and then call system() from stdlib.h
|
||||
// (for executing a command from within a program) to execute the ps(1) command
|
||||
// to verify that the process is a zombie.
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
|
||||
void create_zombie() {
|
||||
int pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
sleep(100000);
|
||||
} else {
|
||||
_exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
void get_ps(int pid) {
|
||||
char spid[1000];
|
||||
sprintf(spid, "ps aux | awk '$2 == %d { print }'", pid);
|
||||
system(spid);
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
int pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
create_zombie();
|
||||
} else {
|
||||
get_ps(pid);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
Loading…
Reference in a new issue