Skip to content

Commit c1bf01f

Browse files
committed
Upload files
1 parent 32cd624 commit c1bf01f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2666
-0
lines changed

00_copying/00_copying.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
//#include <sys/types.h>
4+
#include <fcntl.h> // for all O_ and S_
5+
6+
7+
/*
8+
int open(const char *pathname, int flags); or
9+
int open(const char *pathname, int flags, int mode);
10+
The parameter flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-only, write-only, or read/write, respectively.
11+
In addition, zero or more file creation flags and file status flags can be bitwise-or’d in flags. The file creation flags are O_CREAT, O_EXCL, O_NOCTTY, and O_TRUNC.
12+
open() return the new file descriptor, or -1 if an error occurred.
13+
*/
14+
/*
15+
ssize_t read(int fd, void *buf, size_t count); // under unistd.h
16+
read() attempts to read up to `count` bytes from file descriptor `fd` into the buffer starting at `buf`. On success, the number of bytes read is returned. Else -1.
17+
*/
18+
19+
void main()
20+
{
21+
char buf;
22+
int fd_one, fd_two;
23+
24+
fd_one = open("first_file", O_RDONLY);
25+
26+
//fd_two = open("second_file", O_WRONLY | O_CREAT);
27+
// creates second_file with perms ---sr-x--T
28+
// so, specify mode explicitly
29+
30+
fd_two = open("second_file",
31+
O_WRONLY | O_CREAT,
32+
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
33+
// -rw-r--r-- is the default permission for new files
34+
// From the table given here: https://www.tutorialspoint.com/unix_system_calls/open.htm
35+
// irusr or iwusr or irgrp or iroth = rw-r--r--
36+
37+
while(read(fd_one, &buf, 1))
38+
{
39+
write(fd_two, &buf, 1);
40+
}
41+
42+
printf("Successful copy");
43+
44+
close(fd_one);
45+
close(fd_two);
46+
47+
}

00_copying/first_file

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is the first file.
2+

00_copying/second_file

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is the first file.
2+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
4+
#define max(one, two) ((one < two) ? two: one)
5+
typedef struct Process
6+
{
7+
int process_number;
8+
float burst;
9+
float arrival;
10+
float waiting;
11+
float turnaround;
12+
} Process;
13+
14+
void print_process(Process* proc)
15+
{
16+
printf("(%d, %f, %f, %f, %f)\n",
17+
proc->process_number,
18+
proc->burst,
19+
proc->arrival,
20+
proc->waiting,
21+
proc->turnaround);
22+
}
23+
24+
void print_process_array(int nf, Process* arr)
25+
{
26+
for (int i = 0; i < nf; ++i)
27+
{
28+
print_process(&arr[i]);
29+
}
30+
printf("\n");
31+
}
32+
33+
void read_process(Process* proc)
34+
{
35+
scanf("%f %f",
36+
&proc->burst,
37+
&proc->arrival);
38+
}
39+
40+
void read_process_array(int nf, Process* arr)
41+
{
42+
for (int i = 0; i < nf; ++i)
43+
{
44+
arr[i].process_number = i + 1;
45+
read_process(&arr[i]);
46+
}
47+
}
48+
49+
bool cmp_by_arrival_then_burst(Process* proc_one, Process* proc_two)
50+
{
51+
return proc_one->arrival < proc_two->arrival ||
52+
(proc_one->arrival == proc_two->arrival &&
53+
proc_one->burst < proc_two->burst);
54+
}
55+
56+
bool cmp_by_process_no(Process* proc_one, Process* proc_two)
57+
{
58+
return proc_one->process_number < proc_two->process_number;
59+
}
60+
61+
void swap(Process *xp, Process *yp)
62+
{
63+
Process temp = *xp;
64+
*xp = *yp;
65+
*yp = temp;
66+
}
67+
68+
void bubble_sort(int nf, Process* arr, bool (*lt)(Process*, Process*))
69+
{
70+
int i, j;
71+
for (i = 0; i < nf - 1; i++)
72+
{
73+
for (j = 0; j < nf - i - 1; j++)
74+
{
75+
if (lt(&arr[j + 1], &arr[j]))
76+
{
77+
swap(&arr[j], &arr[j + 1]);
78+
}
79+
}
80+
}
81+
}
82+
83+
void fcfs(int nf, Process* arr, float* avg_waiting, float* avg_turnaround)
84+
{
85+
float time_elapsed = 0, sum_waiting, sum_turnaround;
86+
for (int i = 0; i < nf; ++i)
87+
{
88+
time_elapsed = max(time_elapsed, arr[i].arrival);
89+
arr[i].waiting = time_elapsed;
90+
time_elapsed += arr[i].burst;
91+
arr[i].turnaround = time_elapsed;
92+
93+
sum_waiting += arr[i].waiting;
94+
sum_turnaround += arr[i].turnaround;
95+
}
96+
97+
*avg_waiting = sum_waiting / nf;
98+
*avg_turnaround = sum_turnaround / nf;
99+
}
100+
101+
void main()
102+
{
103+
int nf;
104+
scanf("%d", &nf);
105+
106+
Process arr[nf];
107+
read_process_array(nf, arr);
108+
109+
bubble_sort(nf, arr, cmp_by_arrival_then_burst);
110+
111+
float avg_waiting, avg_turnaround;
112+
fcfs(nf, arr, &avg_waiting, &avg_turnaround);
113+
114+
bubble_sort(nf, arr, cmp_by_process_no);
115+
print_process_array(nf, arr);
116+
printf("=> (%f, %f)",
117+
avg_waiting, avg_turnaround);
118+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
4
2+
8 0
3+
4 1
4+
5 3
5+
9 2
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
8 0
3+
6 0
4+
1 0
5+
9 0
6+
3 0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
8 0
3+
6 1
4+
1 2
5+
9 3
6+
3 4
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
4 3
3+
3 5
4+
2 0
5+
1 5
6+
3 4
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
#include <limits.h>
4+
5+
#define max(one, two) ((one < two) ? two: one)
6+
typedef struct Process
7+
{
8+
int process_number;
9+
float burst;
10+
float arrival;
11+
float waiting;
12+
float turnaround;
13+
} Process;
14+
15+
void print_process(Process* proc)
16+
{
17+
printf("(%d, %f, %f, %f, %f)\n",
18+
proc->process_number,
19+
proc->burst,
20+
proc->arrival,
21+
proc->waiting,
22+
proc->turnaround);
23+
}
24+
25+
void print_process_array(int nf, Process* arr)
26+
{
27+
for (int i = 0; i < nf; ++i)
28+
{
29+
print_process(&arr[i]);
30+
}
31+
printf("\n");
32+
}
33+
34+
void read_process(Process* proc)
35+
{
36+
scanf("%f %f",
37+
&proc->burst,
38+
&proc->arrival);
39+
}
40+
41+
void read_process_array(int nf, Process* arr)
42+
{
43+
for (int i = 0; i < nf; ++i)
44+
{
45+
arr[i].process_number = i + 1;
46+
read_process(&arr[i]);
47+
}
48+
}
49+
50+
bool cmp_by_arrival_then_burst(Process* proc_one, Process* proc_two)
51+
{
52+
return proc_one->arrival < proc_two->arrival ||
53+
(proc_one->arrival == proc_two->arrival &&
54+
proc_one->burst < proc_two->burst);
55+
}
56+
57+
bool cmp_by_process_no(Process* proc_one, Process* proc_two)
58+
{
59+
return proc_one->process_number < proc_two->process_number;
60+
}
61+
62+
void swap(Process *xp, Process *yp)
63+
{
64+
Process temp = *xp;
65+
*xp = *yp;
66+
*yp = temp;
67+
}
68+
69+
void bubble_sort(int nf, Process* arr, bool (*lt)(Process*, Process*))
70+
{
71+
int i, j;
72+
for (i = 0; i < nf - 1; i++)
73+
{
74+
for (j = 0; j < nf - i - 1; j++)
75+
{
76+
if (lt(&arr[j + 1], &arr[j]))
77+
{
78+
swap(&arr[j], &arr[j + 1]);
79+
}
80+
}
81+
}
82+
}
83+
84+
void sjf(int nf, Process* arr, float* avg_waiting, float* avg_turnaround)
85+
{
86+
float time_elapsed = 0, sum_waiting, sum_turnaround;
87+
int left = 0, right = 0;
88+
89+
for (int i = 0; i < nf; ++i)
90+
{
91+
float minn = FLOAT_MAX;
92+
int pos = -1;
93+
94+
for (int j = left; j <= right; ++j)
95+
{
96+
if (arr[j].burst < minn)
97+
{
98+
minn = arr[j].burst;
99+
pos = j;
100+
}
101+
}
102+
103+
if (pos != -1) //INCOMPLETE
104+
{
105+
swap(&arr[pos], &arr[left]);
106+
left++;
107+
}
108+
}
109+
for (int i = 0; i < nf; ++i)
110+
{
111+
time_elapsed = max(time_elapsed, arr[i].arrival);
112+
arr[i].waiting = time_elapsed;
113+
time_elapsed += arr[i].burst;
114+
arr[i].turnaround = time_elapsed;
115+
116+
sum_waiting += arr[i].waiting;
117+
sum_turnaround += arr[i].turnaround;
118+
}
119+
120+
*avg_waiting = sum_waiting / nf;
121+
*avg_turnaround = sum_turnaround / nf;
122+
}
123+
124+
void main()
125+
{
126+
int nf;
127+
scanf("%d", &nf);
128+
129+
Process arr[nf];
130+
read_process_array(nf, arr);
131+
132+
bubble_sort(nf, arr, cmp_by_arrival_then_burst);
133+
134+
float avg_waiting, avg_turnaround;
135+
sjf(nf, arr, &avg_waiting, &avg_turnaround);
136+
137+
bubble_sort(nf, arr, cmp_by_process_no);
138+
print_process_array(nf, arr);
139+
printf("=> (%f, %f)",
140+
avg_waiting, avg_turnaround);N
141+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
4
2+
8 0
3+
4 1
4+
5 3
5+
9 2
6+

0 commit comments

Comments
 (0)