- Implemented first time functions.
[planlOS.git] / programs / test1 / test1.c
blob056615c394840698945ab362340da89e445d77c5
2 #include <signal.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <sys/ioctl.h>
6 #include <termios.h>
7 #include <unistd.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <sys/wait.h>
11 #include <time.h>
12 #include <sys/time.h>
14 void size_changed(int sig)
16 struct winsize size;
17 int status = ioctl(0, TIOCGWINSZ, &size);
18 if (status != -1)
20 char tmp[30];
21 snprintf(tmp, 30, "\033[%d;%dH", size.ws_row, size.ws_col);
22 write(1, tmp, strlen(tmp));
23 write(1, "*", 1);
27 int main(int argc, char **argv)
29 signal(SIGWINCH, size_changed);
30 // Initialize screen
31 write(1, "\033[2J", 4);
32 write(1, "\033[0;0H", 6);
33 write(1, "\033[30;41m", 8);
34 write(1, "Hello ", 6);
35 write(1, "\033[33;40m", 8);
36 write(1, "World!\n", 7);
38 struct winsize size;
39 int status = ioctl(0, TIOCGWINSZ, &size);
40 if (status != -1)
42 write(1, "\033[37;40m", 8);
43 printf("Size: %d/%d.\n", size.ws_row, size.ws_col);
44 write(1, "\033[53;180H", 9);
45 write(1, "*", 1);
48 int time1 = time(0);
49 struct timeval tv1;
50 gettimeofday(&tv1, 0);
51 write(1, "\033[30;41m", 8);
52 write(1, "\033[4;0H", 6);
53 write(1, "fork()! ", 14);
54 int i;
55 for (i = 0; i < 1000; i++)
57 int pid = fork();
58 if (!pid)
60 exit(0);
62 waitpid(pid, 0, 0);
64 write(1, "\033[5;0H", 6);
65 write(1, "DONE! ", 14);
66 int time2 = time(0);
67 struct timeval tv2;
68 gettimeofday(&tv2, 0);
69 write(1, "\033[6;0H", 6);
70 printf("%d seconds (%d).\n", time2 - time1, time2);
72 if (tv1.tv_usec > tv2.tv_usec)
74 tv2.tv_usec += 1000000;
75 tv2.tv_sec -= 1;
77 int difference = tv2.tv_usec / 1000 - tv1.tv_usec / 1000 + tv2.tv_sec * 1000 - tv1.tv_sec * 1000;
78 write(1, "\033[7;0H", 6);
79 printf("%d seconds.\n", difference);
81 char c;
82 while ((c = getchar()) != 'q')
84 putchar(c);
86 return 0;