Operating System Lab Programs
Table of Content:
Lab Lab Title
No.:
1. Install RHEL/KALI/LINUX server with GUI using VMware
2. Program to create multiple threaded processes.
3. Implement IPC mechanism using shared memory & message passing
4. Program to simulate producer consumer problem using Semaphore.
5. Program to simulate and find Average TAT & Waiting Time for Preemptive & Non
Preemptive Scheduling Algorithms: FCFS, SJF, Priority & Round-Robin.
6. Program to simulate Contagious Memory Allocation technique: Worst Fit, Best Fit, &
First Fit.
7. Program to simulate Pare Replacement Algorithms: FIFO, LRU, & LFU"
8. Program to simulate Disk Scheduling Algorithms: FCFS, SCAN, & C-SCAN
Lab 2: Program to create Multi-Threaded Process
Source Code:
For Single Thread
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void *myThreadFun(void *vargp)
{
sleep(1); // simulate some work
printf("Printing NIST College from Thread\n");
return NULL;
}
int main()
{
pthread_t thread_id;
printf("Before Thread\n");
pthread_create(&thread_id, NULL, myThreadFun, NULL);
// Wait for the thread to finish
pthread_join(thread_id, NULL);
printf("After Thread\n");
return 0;
}
Output:
Before Thread
Printing NIST College from Thread
After Thread
--------------------------------
Process exited after 1.111 seconds with return value 0
Press any key to continue . . .
Multi Thread:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
//Let us create a global variable to change it in threads
int g = 0; // Global variable
//The function to be executed by all threads
void *myThreadFun(void *vargp) {
//Store the value argument passed to this thread
int myid = *((int *)vargp); // Thread-specific ID
//Let us create a static variable to observe its changes
static int s = 0; // Shared across all threads
// Change static and global variables
s++;
g++;
//Print the argument static and global variables
printf("ThreadID: %d, Static: %d, Global: %d\n", myid, ++s, ++g);
}
int main() {
int i;
pthread_t tid;
//Let us create three threads
for (i = 0; i < 3; i++)
pthread_create(&tid[i], NULL, myThreadFun, (void *)&ids);
pthread_exit(NULL);
Return 0;
}
Output:
ThreadID: 3, Static: 2, Global: 2
ThreadID: 3, Static: 4, Global: 4
ThreadID: 3, Static: 6, Global: 6
--------------------------------
Process exited after 0.08158 seconds with return value 34
Press any key to continue . . .