OS - LAB Manual
OS - LAB Manual
int main()
{
int n, fd;
char buff[50]; // declaring buffer
return 0;
}
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t b; // note that the actual return type of fork is
// pid_t, though it's probably just an int typedef'd or macro'd
b = fork();
if (b <0)
{
perror("Fork failed");
}
else if (b == 0) {
printf("Hello from Child \n"); // child process
else
printf("Hello from Parent \n"); // parent process
#include <stdio.h>
#include <unistd.h>
#define MSGSIZE 16
char* msg1 = "hello, world #1";
char* msg2 = "hello, world #2";
char* msg3 = "hello, world #3";
int main()
{
char inbuf[MSGSIZE];
int p[2], i;
if (pipe(p) < 0)
exit(1);
/* continued */
/* write pipe */
// thread_routine is the function the thread invokes right after its creation. The
thread ends at the end of this function.
int main(void)
{
pthread_t tid1; // First thread's ID
pthread_t tid2; // Second thread's ID
// creating the first thread that will go execute its thread routine function.
// creating the second thread that will also execute thread routine.
// the main thread waits for the new threads to end with pthread_join.
pthread_join(tid1, NULL);
printf("Main: Joining first thread [%ld]\n", tid1);
pthread_join(tid2, NULL);
printf("Main: Joining second thread [%ld]\n", tid2);
return (0);
}
#include <stdio.h>
int main()
{
int pid[15];
int bt[15];
int n;
printf("Enter the number of processes: ");
scanf("%d",&n);
int i, wt[n];
wt[0]=0;
SJF
#include <stdio.h>
int main()
{
// Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
// User Input Burst Time and alloting Process Id.
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
// Sorting process according to their Burst Time.
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
// Calculation of Turn Around Time and printing the
// data.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],A[i][1], A[i][2], A[i][3]);
}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
}
Round Robin
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
struct P
{
int AT, BT, ST[20], WT, FT, TAT, pos;
};
int quant;
int main()
{
int n, i, j;
// Taking Input
printf("Enter the no. of processes :");
scanf("%d", &n);
struct P p[n];
// Declaring variables
int c = n, s[n][20];
float time = 0, mini = INT_MAX, b[n], a[n];
while (c != 0)
{
mini = INT_MAX;
flag = false;
if (s[index][j] == -1)
{
s[index][j] = time;
p[index].ST[j] = time;
}
if (b[index] > 0)
{
a[index] = time + 0.1;
}
// Printing output
printf("Process number ");
printf("Arrival time ");
printf("Burst time ");
printf("\tStart time");
j = 0;
while (j != 10)
{
j += 1;
printf(" ");
}
printf("\t\tFinal time");
printf("\tWait Time ");
printf("\tTurnAround Time \n");
return 0;
}
Pipes
#include<stdio.h>
#include<unistd.h>
int main() {
intpipefds[2];
intreturnstatus;
charwritemessages[2][20]={"Hi", "Hello"};
charreadmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1) {
printf("Unable to create pipe\n");
return 1;
}
Messages Queues
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// structure for message queue
structmsg_buffer {
longmsg_type;
charmsg[100];
} message;
main() {
key_tmy_key;
intmsg_id;
my_key = ftok("progfile", 65); //create unique key
msg_id = msgget(my_key, 0666 | IPC_CREAT); //create message queue and
return id
message.msg_type = 1;
printf("Write Message : ");
fgets(message.msg, 100, stdin);
msgsnd(msg_id, &message, sizeof(message), 0); //send message
printf("Sent message is : %s \n", message.msg);
}
Shared Memory
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
intshmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);
//creates shared memory segment with key 2345, having size 1024 bytes.
IPC_CREAT is used to create the shared segment if it does not exist. 0666 are
the permissions on the shared segment
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
//process attached to shared memory segment
printf("Process attached at %p\n",shared_memory);
//this prints the address where the segment is attached with this process
printf("Enter some data to write to shared memory\n");
read(0,buff,100); //get some input from user
strcpy(shared_memory,buff); //data written to shared memory
printf("You wrote : %s\n",(char *)shared_memory);
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM_PHILOSOPHERS 5
sem_t forks[NUM_PHILOSOPHERS];
sem_t mutex; // For mutual exclusion while printing
while (1) {
think(philosopher);
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int philosopher_numbers[NUM_PHILOSOPHERS];
// Initialize semaphores
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_init(&forks[i], 0, 1); // Each fork is initially available
}
sem_init(&mutex, 0, 1); // Mutex for critical section
// Join threads (in real systems, this would be done with proper exit
conditions)
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_join(philosophers[i], NULL);
}
// Destroy semaphores
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_destroy(&forks[i]);
}
sem_destroy(&mutex);
return 0;
}
Producer – Consumer
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
sem_t empty, full, mutex; // Semaphores for empty slots, full slots, and mutual
exclusion
sleep(1);
}
return NULL;
}
sleep(1);
}
return NULL;
}
int main() {
pthread_t prod_thread, cons_thread;
// Initialize semaphores
sem_init(&empty, 0, BUFFER_SIZE); // Empty slots in buffer
sem_init(&full, 0, 0); // Full slots in buffer
sem_init(&mutex, 0, 1); // Mutex for critical section
// Join threads (in real systems, this would be done with proper exit
conditions)
pthread_join(prod_thread, NULL);
pthread_join(cons_thread, NULL);
// Destroy semaphores
sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);
return 0;
}
Readers – Writers
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
/*
This program provides a possible solution for first readers writers problem
using mutex and semaphore.
I have used 10 readers and 5 producers to demonstrate the solution. You can
always play with these values.
*/
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
}
void *reader(void *rno)
{
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt); // If this id the first reader, then it will block the writer
}
pthread_mutex_unlock(&mutex);
// Reading Section
printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader--;
if(numreader == 0) {
sem_post(&wrt); // If this is the last reader, it will wake up the writer.
}
pthread_mutex_unlock(&mutex);
}
int main()
{
pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
int a[10] = {1,2,3,4,5,6,7,8,9,10}; //Just used for numbering the producer and
consumer
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
}
VII Write a C program to simulate Bankers Algorithm for Deadlock
Avoidance.
#include <stdio.h>
#include <stdbool.h>
int main() {
int processes[] = {0, 1, 2, 3, 4}; // Process IDs