Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views18 pages

3

The document contains multiple C programming code snippets that demonstrate various concepts in operating systems and concurrent programming. Topics include process creation using fork, matrix multiplication with threads, scheduling algorithms like FCFS and Round Robin, inter-process communication using shared memory and pipes, producer-consumer problem, memory allocation strategies, page replacement algorithms, and the Banker's algorithm for deadlock avoidance. Each code snippet is designed to illustrate specific functionalities and algorithms relevant to system programming.

Uploaded by

ganesh.n.b001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

3

The document contains multiple C programming code snippets that demonstrate various concepts in operating systems and concurrent programming. Topics include process creation using fork, matrix multiplication with threads, scheduling algorithms like FCFS and Round Robin, inter-process communication using shared memory and pipes, producer-consumer problem, memory allocation strategies, page replacement algorithms, and the Banker's algorithm for deadlock avoidance. Each code snippet is designed to illustrate specific functionalities and algorithms relevant to system programming.

Uploaded by

ganesh.n.b001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

3]]#include <stdio.

h>
#include <unistd.h>

int main() {
pid_t pid = fork();

if (pid < 0) {
printf("\nFork failed! Child process not created.\n");
return 1;
}
else if (pid == 0) {
printf("\nChild process is running");
printf("\nChild process id = %d\n", getpid());
}
else {
printf("\nParent process is running");
printf("\nParent process id = %d\n", getpid());
}

return 0;
}
4]multiplication
#include <stdio.h>
#include <pthread.h>

#define SIZE 3

int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];

void* multiplyRow(void* arg) {


int row = (int)arg;
for (int j = 0; j < SIZE; j++) {
C[row][j] = 0;
for (int k = 0; k < SIZE; k++) {
C[row][j] += A[row][k] * B[k][j];
}
}
return NULL;
}

int main() {
pthread_t threads[SIZE];
int rows[SIZE];

printf("Enter Matrix A (3x3):\n");


for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
scanf("%d", &A[i][j]);

printf("Enter Matrix B (3x3):\n");


for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
scanf("%d", &B[i][j]);

for (int i = 0; i < SIZE; i++) {


rows[i] = i;
pthread_create(&threads[i], NULL, multiplyRow, &rows[i]);
}

for (int i = 0; i < SIZE; i++) {


pthread_join(threads[i], NULL);
}

printf("Result Matrix C:\n");


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++)
printf("%d ", C[i][j]);
printf("\n");
}

return 0;
}

5) fcfs
#include <stdio.h>

int main() {
int i, j, n;
int bt[20], at[20], wt[20], tat[20], start[20], pid[20];
float awt = 0, atat = 0;

printf("Enter the number of processes: ");


scanf("%d", &n);

for (i = 0; i < n; i++) {


pid[i] = i + 1;
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &bt[i]);
printf("Enter arrival time for process %d: ", i + 1);
scanf("%d", &at[i]);
}

for (i = 0; i < n - 1; i++) {


for (j = i + 1; j < n; j++) {
if (at[i] > at[j]) {
int tempVal;
tempVal = at[i]; at[i] = at[j]; at[j] = tempVal;
tempVal = bt[i]; bt[i] = bt[j]; bt[j] = tempVal;
tempVal = pid[i]; pid[i] = pid[j]; pid[j] = tempVal;
}
}
}

start[0] = at[0];

printf("\nProcess\tBT\tAT\tStart\tWT\tTAT\n");

for (i = 0; i < n; i++) {


if (start[i] < at[i])
start[i] = at[i];
wt[i] = start[i] - at[i];
tat[i] = wt[i] + bt[i];
start[i + 1] = start[i] + bt[i];
awt += wt[i];
atat += tat[i];
printf("%d\t%d\t%d\t%d\t%d\t%d\n", pid[i], bt[i], at[i], start[i], wt[i], tat[i]);
}

printf("\nAverage Waiting Time = %.2f", awt / n);


printf("\nAverage Turnaround Time = %.2f\n", atat / n);

return 0;
}
5)round Robin preemptive
#include <stdio.h>

int main() {
int n, i, time = 0, tq, count = 0;
int at[20], bt[20], rt[20], wt[20], tat[20];
float awt = 0, atat = 0;

printf("Enter number of processes: ");


scanf("%d", &n);

for(i = 0; i < n; i++) {


printf("Enter arrival time of process %d: ", i + 1);
scanf("%d", &at[i]);
printf("Enter burst time of process %d: ", i + 1);
scanf("%d", &bt[i]);
rt[i] = bt[i]; // Remaining time initially equals burst time
}

printf("Enter Time Quantum: ");


scanf("%d", &tq);

int completed = 0;
int flag;

while(completed < n) {
flag = 0;
for(i = 0; i < n; i++) {
if(at[i] <= time && rt[i] > 0) {
if(rt[i] > tq) {
time += tq;
rt[i] -= tq;
} else {
time += rt[i];
wt[i] = time - at[i] - bt[i]; // Waiting Time
tat[i] = time - at[i]; // Turnaround Time
awt += wt[i];
atat += tat[i];
rt[i] = 0;
completed++;
}
flag = 1;
}
}
if(flag == 0) {
time++;
}
}

printf("\nProcess\tArrival\tBurst\tWaiting\tTurnaround\n");
for(i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], wt[i], tat[i]);
}

printf("\nAverage Waiting Time = %.2f", awt / n);


printf("\nAverage Turnaround Time = %.2f\n", atat / n);

return 0;
}
6) using shared memory
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

int main() {
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
char str = (char) shmat(shmid, (void*)0, 0);
printf("Write Data: ");
fgets(str, 1024, stdin);
printf("Data written: %s", str);
printf("Data read: %s", str);
shmdt(str);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
6) using pipes

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main() {
int fd[2];
char write_msg[100];
char read_msg[100];

pipe(fd);

if (fork() == 0) {
close(fd[1]);
read(fd[0], read_msg, sizeof(read_msg));
printf("Child received: %s\n", read_msg);
close(fd[0]);
} else {
close(fd[0]);
printf("Parent: Enter message: ");
fgets(write_msg, sizeof(write_msg), stdin);
write(fd[1], write_msg, strlen(write_msg) + 1);
close(fd[1]);
}

return 0;
}

7]producer consumer
#include <stdio.h>
#include <stdlib.h>

#define SIZE 5

int buffer[SIZE];
int in = 0, out = 0;

int mutex = 1;
int full = 0;
int empty = SIZE;

int wait(int s) {
return --s;
}

int signal(int s) {
return ++s;
}

void displayBuffer() {
printf("Buffer: [");
for (int i = 0; i < SIZE; i++) {
printf("%d ", buffer[i]);
}
printf("]\n");
}

void producer() {
if (mutex == 1 && empty > 0) {
mutex = wait(mutex);
empty = wait(empty);
full = signal(full);

int item;
printf("Enter item to produce: ");
scanf("%d", &item);

buffer[in] = item;
printf("Producer produced %d at position %d\n", item, in);
in = (in + 1) % SIZE;

displayBuffer();

mutex = signal(mutex);
} else {
printf("Buffer is full, producer cannot produce\n");
}
}

void consumer() {
if (mutex == 1 && full > 0) {
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);

int item = buffer[out];


buffer[out] = 0;
printf("Consumer consumed %d from position %d\n", item, out);
out = (out + 1) % SIZE;

displayBuffer();

mutex = signal(mutex);
} else {
printf("Buffer is empty, consumer cannot consume\n");
}
}

int main() {
int choice;

while (1) {
printf("\n1. Produce\n2. Consume\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1: producer(); break;
case 2: consumer(); break;
case 3: exit(0);
default: printf("Invalid choice. Try again.\n");
}
}

return 0;
}
8) first fit static
#include <stdio.h>
#define MAX_BLOCKS 10
#define MAX_PROCESSES 10

int main() {
int blockSize[MAX_BLOCKS], processSize[MAX_PROCESSES];
int blockCount, processCount;
int allocation[MAX_PROCESSES];

printf("Enter number of memory blocks: ");


scanf("%d", &blockCount);

printf("Enter sizes of %d memory blocks:\n", blockCount);


for (int i = 0; i < blockCount; i++) {
scanf("%d", &blockSize[i]);
}

printf("\nEnter number of processes: ");


scanf("%d", &processCount);

printf("Enter sizes of %d processes:\n", processCount);


for (int i = 0; i < processCount; i++) {
scanf("%d", &processSize[i]);
allocation[i] = -1;
}

for (int i = 0; i < processCount; i++) {


for (int j = 0; j < blockCount; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}

printf("\nFirst Fit Memory Allocation:\n");


printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < processCount; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}

return 0;
}

Worst fit (dyanamic)


#include <stdio.h>

#define MAX_BLOCKS 10
#define MAX_PROCESSES 10

int main() {
int blockSize[MAX_BLOCKS], processSize[MAX_PROCESSES];
int blockCount, processCount;
int allocation[MAX_PROCESSES];

printf("Enter number of memory blocks: ");


scanf("%d", &blockCount);

printf("Enter sizes of %d memory blocks:\n", blockCount);


for (int i = 0; i < blockCount; i++) {
scanf("%d", &blockSize[i]);
}

printf("\nEnter number of processes: ");


scanf("%d", &processCount);

printf("Enter sizes of %d processes:\n", processCount);


for (int i = 0; i < processCount; i++) {
scanf("%d", &processSize[i]);
allocation[i] = -1; // default: not allocated
}

// First Fit Allocation


for (int i = 0; i < processCount; i++) {
for (int j = 0; j < blockCount; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i]; // reduce available memory
break;
}
}
}

printf("\nFirst Fit Memory Allocation:\n");


printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < processCount; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1); // block no starts from 1
else
printf("Not Allocated\n");
}
return 0;
}
9)page replacement fifo
#include <stdio.h>

int main() {
int frames, pages[100], n, i, j, k, pageFaults = 0;
int memory[10], pointer = 0, found;

printf("Enter number of frames: ");


scanf("%d", &frames);

printf("Enter number of pages: ");


scanf("%d", &n);

printf("Enter page reference string:\n");


for (i = 0; i < n; i++) {
scanf("%d", &pages[i]);
}

for (i = 0; i < frames; i++) {


memory[i] = -1;
}

printf("\nPage\tFrames\n");

for (i = 0; i < n; i++) {


found = 0;
for (j = 0; j < frames; j++) {
if (memory[j] == pages[i]) {
found = 1;
break;
}
}

if (!found) {
memory[pointer] = pages[i];
pointer = (pointer + 1) % frames;
pageFaults++;
}

printf("%d\t", pages[i]);
for (k = 0; k < frames; k++) {
if (memory[k] != -1)
printf("%d ", memory[k]);
else
printf("- ");
}
printf("\n");
}

printf("\nTotal Page Faults = %d\n", pageFaults);


return 0;
}

10)banker's
#include <stdio.h>
#include <stdbool.h>

#define P 5 // Number of processes


#define R 3 // Number of resources

int available[R] = {3, 3, 2};


int max[P][R] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};
int allocation[P][R] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};
int need[P][R];

void calculateNeed() {
for (int i = 0; i < P; i++)
for (int j = 0; j < R; j++)
need[i][j] = max[i][j] - allocation[i][j];
}

bool isSafe() {
int work[R];
bool finish[P] = {false};

for (int i = 0; i < R; i++)


work[i] = available[i];

int safeSeq[P];
int count = 0;
while (count < P) {
bool found = false;

for (int p = 0; p < P; p++) {


if (!finish[p]) {
bool possible = true;
for (int r = 0; r < R; r++) {
if (need[p][r] > work[r]) {
possible = false;
break;
}
}

if (possible) {
for (int r = 0; r < R; r++)
work[r] += allocation[p][r];

safeSeq[count++] = p;
finish[p] = true;
found = true;
}
}
}

if (!found) {
printf("System is not in a safe state.\n");
return false;
}
}

printf("System is in a safe state.\nSafe sequence: ");


for (int i = 0; i < P; i++)
printf("P%d ", safeSeq[i]);
printf("\n");
return true;
}

int main() {
calculateNeed();
isSafe();
return 0;
}

You might also like