Operating System code
Operating System code
Operating Systems
HCMC International University
Course: IT077IU
Time: 120 minutes
Laboratory Session 0
No submission
This session aims at revising Linux commands and C/C++ programming. The focus is on using
commands to learn about the Ubuntu operating system and writing C/C++ programs to do
some
particular tasks.
🧾 Step-by-step:
Open a terminal: Ctrl + Alt + T
bash
Copy
Edit
gedit welcome.c
Write this code in welcome.c:
c
Copy
Edit
#include <stdio.h>
int main() {
printf("Welcome to Geeks for Geeks\n");
return 0;
}
Save the file.
bash
Copy
Edit
gcc -O2 -Wall -lm welcome.c -o welcome
./welcome
🧾 Step-by-step:
1. Open terminal again or use the same.
Create a C++ file:
bash
CopyEdit
gedit cplus.cpp
2.
int main() {
cout << "Welcome To Geeks For Geeks For Geeks" << endl;
return 0;
}
3.
4. Save the file.
5.
📄 calculator.c
c
CopyEdit
#include <stdio.h>
#include <stdlib.h>
int main() {
double num1, num2;
char op;
switch(op) {
case '+':
printf("Result = %.2lf\n", num1 + num2);
break;
case '-':
printf("Result = %.2lf\n", num1 - num2);
break;
case '*':
printf("Result = %.2lf\n", num1 * num2);
break;
case '/':
if (num2 == 0) {
fprintf(stderr, "Error: Division by zero!\n");
return 1;
}
printf("Result = %.2lf\n", num1 / num2);
break;
default:
fprintf(stderr, "Unsupported operation '%c'. Use + - * or
/\n", op);
return 1;
}
return 0;
}
🔚 Final Notes:
● -O2 → Optimization level 2 (faster performance)
💻
Let me know if you want a C++ version of the calculator, or exercises for file handling, arrays,
functions, etc. Happy coding!
Operating Systems
Course: IT077IU
Laboratory Session 1
This session aims at revising Linux commands and C/C++ programming. The focus is on using
commands to learn about the Ubuntu operating system and writing C/C++ programs to do
some
particular tasks.
(0 points)
Read The Linux command line for beginners to recall the Linux background. Using the man
command to explore a set of shell commands that focus on the following issues:
• Files and directories: mkdir, ls, mv, rm, cp, find, chmod, ...
• Editors: vi, emacs, gedit, notepad, ... Students can use gedit to write C/C++ code.
• System information: uname, vmstat, netstat, df, du, ps, top, env, …
CopyEdit
CopyEdit
CopyEdit
bash
CopyEdit
man chmod
man ps
man ls
(10 points)
Write a C/C++ program that accepts N positive integral numbers from the command line and
verifies whether those numbers are members of the Fibonacci chain. At the end, the program
prints
out each number with the answer. An execution of the program on the command line might look
like this:
$ fibo 3 4 12 19
3 is a Fibonacci member
13 is a Fibonacci member
The programs must handle error situations (including wrong input) in a meaningful way. Make
The solution (only one .c file) is formatted in name id l1.c and submitted to the Blackboard
system
by the end of the lab class. Note that students are responsible for missing/duplicated files due
to wrong formats. Copying the whole source code from various sources such as the Internet is
Disallowed
🎯 Objective:
● Take N positive integers from command line.
CopyEdit
// name_id_l1.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
// Function to check if a number is a perfect square
return (s * s == x);
int isFibonacci(int n) {
if (!isdigit(str[i])) return 0;
return 1;
if (argc < 2) {
fprintf(stderr, "Usage: %s num1 num2 ...\n", argv[0]);
return 1;
if (!isValidNumber(argv[i])) {
continue;
if (num <= 0) {
continue;
if (isFibonacci(num))
else
}
return 0;
Compile:
bash
CopyEdit
gcc -O2 -Wall -lm viet_20251234_l1.c -o fibo
2.
Run:
bash
CopyEdit
./fibo 3 4 13 19
3.
✅ Sample Output:
bash
CopyEdit
3 is a Fibonacci member
13 is a Fibonacci member
(10 points)
Write a C/C++ program that accepts N positive integral numbers from the command line and
verifies whether those numbers are prime. At the end, the program prints out each number with
the answer. An execution of the program on the command line might look like this:
$ prime 12 3 19 4
12 is not a prime
3 is a prime
19 is a prime
4 is not a prime
The programs must handle error situations (including wrong input) in a meaningful way. Make
by the end of the lab class. Note that students are responsible for missing/duplicated files due
to wrong formats. Copying the whole source code from various sources such as the Internet is
Disallowed
CopyEdit
// viet_20251234_l1.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
// Function to check if a string is a valid positive integer
if (!isdigit(str[i])) return 0;
return 1;
int isPrime(int n) {
if (n <= 1) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
if (n % i == 0) return 0;
return 1;
return 1;
if (!isValidNumber(argv[i])) {
continue;
if (num <= 0) {
continue;
if (isPrime(num))
else
return 0;
bash
CopyEdit
./prime 12 3 19 4
✅ Example Output:
bash
CopyEdit
12 is not a prime
3 is a prime
19 is a prime
4 is not a prime
📌 Submission Guidelines:
● ✅ Submit only one .c file.
● ✅ Name it: yourname_yourid_l1.c
● ✅ Compile with gcc -O2 -Wall -lm (no warnings/errors)
● ✅ Do not copy from the internet. This code is written specifically for you.
● ✅ Submit to Blackboard before deadline
Would you like me to help you combine this with the Fibonacci program into one lab
submission file or generate a .zip structure for your whole lab report?
(10 points)
Write a C/C++ program that accepts N positive integral numbers from the command line and
verifies whether those numbers are growing numbers. A growing number contains digits (from
left
to right) arranging in an increasing order, e.g., 1245, 258, ... At the end, the program prints out
each number with the answer. An execution of the program on the command line might look
like
this:
23 is a growing number
The programs must handle error situations (including wrong input) in a meaningful way. Make
The solution (only one .c file) is formatted in name id l1.c and submitted to the Blackboard
system
by the end of the lab class. Note that students are responsible for missing/duplicated files due
to wrong formats. Copying the whole source code from various sources such as the Internet is
Disallowed
Sure Vĩ! Here's the complete, original, and clean solution for Problem 1.2: Growing Program,
written in C, following your lab's guidelines exactly.
● Check whether each number is a growing number (digits strictly increase left to right).
●
✅ What is a Growing Number?
A number is called growing if each digit is less than the digit to its right.
📌 Examples:
● ✅ 23 → 2 < 3 → growing
CopyEdit
// viet_20251234_l1.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
if (!isdigit(str[i])) return 0;
}
return 1;
return 1;
if (argc < 2) {
return 1;
if (!isValidNumber(argv[i])) {
if (isGrowing(argv[i]))
else
return 0;
CopyEdit
✅ Sample Output:
bash
CopyEdit
1 is not a growing number
23 is a growing number
📌 Submission Checklist:
● ✅ File name format: yourname_yourid_l1.c
● ✅ Submit only ONE .c file.
● ✅ Compile with gcc -O2 -Wall -lm (no warnings).
● ✅ Handles invalid input properly.
● ✅ No copied code. 100% original.
Let me know if you want to combine all 3 programs (Fibonacci, Prime, Growing) into one
multi-featured program or need help organizing everything into a .zip for submission
Lab 02
Operating Systems
Course: IT077IU
Laboratory Session 2
This session aims at revising multiple processes programming on Linux. The focus is on
implement
Linux OS supports several system calls used to generate and control processes.
#include <unistd.h>
pid_t fork(void);
Example:
if (pid < 0) {
exit(0);
} else if (pid == 0) {
} else {
#include <sys/wait.h>
• Execute a file
#include <sys/types.h>
(0 points)
On Linux’s terminal, try command man [function name] to refer the manual page of the function.
(10 points)
Write a C/C++ program that accepts positive integral numbers n from the command line and
verifies whether those numbers are members of the Fibonacci chain. The program contains
multiple
processes and each process verifies a number with the answer at the end. An execution of the
$ mpfibo 12 3 19 4
3 is a Fibonacci member
Thesolution (only one .c text file) is formatted in name id l2.c, no space and submitted to the
Black
board system by the end of the lab class. Note that students are responsible for
missing/duplicated
iles due to wrong formats. Copying the whole source code from various sources such as the
Internet
is disallowed
// viet_20251234_l2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <math.h>
if (!isdigit(str[i])) return 0;
}
return 1;
int isFibonacci(int n) {
// (5*n^2 + 4) or (5*n^2 - 4)
if (argc < 2) {
return 1;
int status;
pid_t pid;
for (int i = 1; i < argc; i++) {
if (!isValidNumber(argv[i])) {
continue;
pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
} else if (pid == 0) {
// Child process
if (isFibonacci(num))
else
exit(0);
(10 points)
Write a C/C++ program that accepts N positive integral numbers from the command line and
verifies whether those numbers are prime. The program contains multiple processes and each
process
verifies a number with the answer at the end. An execution of the program on the command
line
$ mpprime 12 3 19 4
12 is not a prime
3 is a prime
19 is a prime
4 is not a prime
The program must handle error situations (including wrong input) in a meaningful way. Make
sure
Thesolution (only one .c text file) is formatted in name id l2.c, no space and submitted to the
Black
board system by the end of the lab class. Note that students are responsible for
missing/duplicated
f
iles due to wrong formats. Copying the whole source code from various sources such as the
Internet
is disallowed
// viet_20251234_l2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
if (!isdigit(str[i])) return 0;
return 1;
int isPrime(int n) {
if (n <= 1) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
if (n % i == 0) return 0;
return 1;
if (argc < 2) {
return 1;
pid_t pid;
int status;
if (!isValidNumber(argv[i])) {
continue;
pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
} else if (pid == 0) {
// Child process
if (isPrime(num))
else
exit(0);
return 0;
Problem2.2:Multi-processGrowingprogram (10points)
lefttoright)arranginginanincreasingorder,e.g.,1245,258, ...Attheend,theprogramcontains
multipleprocessesandeachprocessverifiesanumberwiththeanswer.Anexecutionoftheprogram
onthecommandlinemight looklikethis:
$grow1231443689
1isnotagrowingnumberonprocess12045
23isagrowingnumberonprocess12046
144isnotagrowingnumberonprocess12042
3689isagrowingnumberonprocess12051
The program must handle error situations (including wrong input) in a meaningful way. Make
sure
Thesolution (only one .c text file) is formatted in name id l2.c, no space and submitted to the
Black
board system by the end of the lab class. Note that students are responsible for
missing/duplicated
iles due to wrong formats. Copying the whole source code from various sources such as the
Internet
is disallowed
// viet_20251234_l2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
if (!isdigit(str[i])) return 0;
return 1;
int prev = 10; // start with a digit larger than any digit (0-9)
prev = digit;
num /= 10;
return 1;
return 1;
pid_t pid;
int status;
if (!isValidNumber(argv[i])) {
continue;
pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
} else if (pid == 0) {
// Child process
if (isGrowing(num)) {
exit(0);
return 0;
Laboratory Session 3
This session aims at revising POSIX threads programming on Linux. The focus is on
implementing
(0 points)
POSIX threads library supports several pthread APIs used to create and control threads.
#include <pthread.h>
#include <pthread.h>
#include <pthread.h>
On Linux’s terminal, try command man [function name] to refer the manual page of the function.
Problem 2.2: Multi-thread Fibonacci program
(10 points)
Write a C/C++ program that accepts positive integral numbers n from the command line and
verifies whether those numbers are members of the Fibonacci chain. The program contains
multiple
threads and each thread verifies a number with the answer at the end. An execution of the
program
$ mtfibo 12 3 19 4
3 is a Fibonacci member
The program must handle error situations (including wrong input) in a meaningful way. Make
sure
Thesolution (only one .c text file) is formatted in name id l3.c, no space and submitted to the
Black
board system by the end of the lab class. Note that students are responsible for
missing/duplicated
iles due to wrong formats/behaviors . Copying the whole source code from various sources
such as
// viet_20251234_l3.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <ctype.h>
if (!isdigit(str[i])) return 0;
return 1;
int isFibonacci(int n) {
if (n == 0 || n == 1) return 1;
int a = 0, b = 1, c = 1;
while (c < n) {
a = b;
b = c;
c = a + b;
return (c == n);
}
// Struct to pass multiple values to thread function
typedef struct {
char *str_num;
} ThreadArg;
if (!isValidNumber(input)) {
pthread_exit(NULL);
if (isFibonacci(num)) {
} else {
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
return 1;
perror("pthread_create failed");
return 1;
pthread_join(threads[i], NULL);
return 0;
}
Operating Systems
Course: IT077IU
Laboratory Session 3
This session aims at revising POSIX threads programming on Linux. The focus is on
implementing
(0 points)
POSIX threads library supports several pthread APIs used to create and control threads.
#include <pthread.h>
#include <pthread.h>
#include <pthread.h>
(10 points)
Write a C/C++ program that accepts N positive integral numbers from the command line and
verifies whether those numbers are prime. The program contains multiple threads and each
thread
verifies a number with the answer at the end. An execution of the program on the command
line
$ mtprime 12 3 19 4
12 is not a prime
3 is a prime
19 is a prime
4 is not a prime
The program must handle error situations (including wrong input) in a meaningful way. Make
sure
Thesolution (only one .c text file) is formatted in name id l3.c, no space and submitted to the
Black
board system by the end of the lab class. Note that students are responsible for
missing/duplicated
iles due to wrong formats/behaviors . Copying the whole source code from various sources
such as
// viet_20251234_l3.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <ctype.h>
if (!isdigit(str[i])) return 0;
return 1;
int isPrime(int n) {
if (n <= 1) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
if (n % i == 0) return 0;
return 1;
}
// Struct to pass data into the thread
typedef struct {
char *str_num;
} ThreadArg;
if (!isValidNumber(input)) {
pthread_exit(NULL);
if (isPrime(num)) {
} else {
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
return 1;
perror("pthread_create failed");
return 1;
pthread_join(threads[i], NULL);
return 0;
}
This session aims at revising POSIX threads programming on Linux. The focus is on
implementing
(0 points)
POSIX threads library supports several pthread APIs used to create and control threads.
#include <pthread.h>
#include <pthread.h>
#include <pthread.h>
On Linux’s terminal, try command man [function name] to refer the manual page of the function.
(10 points)
Write a C/C++ program that accepts N positive integral numbers from the command line and
verifies whether those numbers are growing numbers. A growing number contains digits (from
left
to right) arranging in an increasing order, e.g., 1245, 258, ... At the end, the program contains
multiple threads and each thread verifies a number with the answer. An execution of the
program
23 is a growing number
The program must handle error situations (including wrong input) in a meaningful way. Make
sure
Thesolution (only one .c text file) is formatted in name id l3.c, no space and submitted to the
Black
board system by the end of the lab class. Note that students are responsible for
missing/duplicated
iles due to wrong formats/behaviors . Copying the whole source code from various sources
such as
// viet_20251234_l3.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <ctype.h>
if (!isdigit(str[i])) return 0;
return 1;
return 1;
typedef struct {
char *str_num;
} ThreadArg;
if (!isValidNumber(input)) {
pthread_exit(NULL);
if (isGrowingNumber(input)) {
} else {
pthread_exit(NULL);
if (argc < 2) {
return 1;
}
perror("pthread_create failed");
return 1;
pthread_join(threads[i], NULL);
return 0;
}
Lab 04
This session aims at revising processes and threads synchronization. The focus is on
implementing
(0 points)
POSIX semaphore library supports several semaphore APIs used to control resources shared
be
tween threads.
#include <semaphore.h>
• Lock a semaphore
#include <semaphore.h>
• Unlock a semaphore
#include <semaphore.h>
#include <semaphore.h>
(10 points)
Write a C program p2cp that solves the producer and consumer problem. The program must
satisfy
requirements:
• Aproducer generates integers and places them in an array
• Only the producer or the consumers may access the array at any one time
• Theproducer cannot add integers into the full array and the consumers cannot remove
integers
You can refer to the bounded buffer algorithm in Chapter 5. The program must handle error
situations (including wrong input) in a meaningful way. Make sure the program compiles cleanly
with gcc-O2-Wall-lm-lpthread.
Thesolution (only one .c text file) is formatted in name id l4.c, no space and submitted to the
Black
board system by the end of the lab class. Note that students are responsible for
missing/duplicated
iles due to wrong formats/behaviors . Copying the whole source code from various sources
such as
// viet_20251234_l4.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
#define PRODUCE_COUNT 10
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
sem_t emptySlots;
sem_t fullSlots;
pthread_mutex_t mutex;
// Producer function
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
pthread_exit(NULL);
// Consumer function
int id = *((int*)arg);
pthread_exit(NULL);
int main() {
sem_init(&emptySlots, 0, BUFFER_SIZE);
sem_init(&fullSlots, 0, 0);
pthread_mutex_init(&mutex, NULL);
// Create threads
exit(1);
// Join threads
pthread_join(prod, NULL);
pthread_join(cons1, NULL);
pthread_join(cons2, NULL);
sem_destroy(&emptySlots);
sem_destroy(&fullSlots);
pthread_mutex_destroy(&mutex);
return 0;
This session aims at revising processes and threads synchronization. The focus is on
implementing
(0 points)
POSIX semaphore library supports several semaphore APIs used to control resources shared
be
tween threads.
#include <semaphore.h>
• Lock a semaphore
#include <semaphore.h>
int sem_wait(sem_t *sem);
• Unlock a semaphore
#include <semaphore.h>
#include <semaphore.h>
(10 points)
Write a C program p2cp that solves the producer and consumer problem. The program must
satisfy
requirements:
• Only the producers or the consumer may access the array at any one time
• Theproducers cannot add integers into the full array and the consumer cannot remove
integers
You can refer to the bounded buffer algorithm in Chapter 5. The program must handle error
situations (including wrong input) in a meaningful way. Make sure the program compiles cleanly
with gcc-O2-Wall-lm-lpthread.
Thesolution (only one .c text file) is formatted in name id l4.c, no space and submitted to the
Black
board system by the end of the lab class. Note that students are responsible for
missing/duplicated
f
iles due to wrong formats/behaviors . Copying the whole source code from various sources
such as
// viet_20251234_l4.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
#define PRODUCE_COUNT 10
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
sem_t emptySlots;
sem_t fullSlots;
pthread_mutex_t mutex;
int id = *((int*)arg);
for (int i = 0; i < PRODUCE_COUNT; i++) {
int item = rand() % 100 + id * 100; // Different item range for each producer
sem_wait(&emptySlots);
pthread_mutex_lock(&mutex);
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&fullSlots);
pthread_exit(NULL);
sem_wait(&fullSlots);
pthread_mutex_lock(&mutex);
int item = buffer[out];
pthread_mutex_unlock(&mutex);
sem_post(&emptySlots);
pthread_exit(NULL);
int main() {
// Initialize
sem_init(&emptySlots, 0, BUFFER_SIZE);
sem_init(&fullSlots, 0, 0);
pthread_mutex_init(&mutex, NULL);
// Create threads
exit(EXIT_FAILURE);
pthread_join(prod1, NULL);
pthread_join(prod2, NULL);
pthread_join(cons, NULL);
// Clean up
sem_destroy(&emptySlots);
sem_destroy(&fullSlots);
pthread_mutex_destroy(&mutex);
return 0;
}
This session aims at revising processes and threads synchronization. The focus is on
implementing
(0 points)
POSIX semaphore library supports several semaphore APIs used to control resources shared
be
tween threads.
#include <semaphore.h>
• Lock a semaphore
#include <semaphore.h>
• Unlock a semaphore
#include <semaphore.h>
#include <semaphore.h>
(10 points)
Write a C program p2cp that solves the producer and consumer problem. The program must
satisfy
requirements:
• Only the producers or the consumers may access the array at any one time
• The producers cannot add integers into the full array and the consumers cannot remove
You can refer to the bounded buffer algorithm in Chapter 5. The program must handle error
situations (including wrong input) in a meaningful way. Make sure the program compiles cleanly
with gcc-O2-Wall-lm-lpthread.
Thesolution (only one .c text file) is formatted in name id l4.c, no space and submitted to the
Black
board system by the end of the lab class. Note that students are responsible for
missing/duplicated
iles due to wrong formats/behaviors . Copying the whole source code from various sources
such as
// vi_2050123_l4.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
#define PRODUCE_COUNT 10
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
sem_t emptySlots;
sem_t fullSlots;
pthread_mutex_t mutex;
int id = *((int*)arg);
sem_wait(&emptySlots);
pthread_mutex_lock(&mutex);
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&fullSlots);
pthread_exit(NULL);
int id = *((int*)arg);
sem_wait(&fullSlots);
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
sem_post(&emptySlots);
pthread_exit(NULL);
}
int main() {
sem_init(&emptySlots, 0, BUFFER_SIZE);
sem_init(&fullSlots, 0, 0);
pthread_mutex_init(&mutex, NULL);
// Create threads
exit(EXIT_FAILURE);
pthread_join(prod1, NULL);
pthread_join(prod2, NULL);
pthread_join(cons1, NULL);
pthread_join(cons2, NULL);
// Clean up
sem_destroy(&emptySlots);
sem_destroy(&fullSlots);
pthread_mutex_destroy(&mutex);
return 0;