Myos
Myos
******
2. Write a C program to implement I/O System Calls of Linux.
******
******
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
int main() {
int c, fd, sz;
char c1[50];
while (1) {
printf("Enter choice: ");
scanf("%d", & c);
switch (c) {
case 1:
fd = creat("f1.txt", 0777);
printf("fd=%d\n", fd);
close(fd);
break;
case 2:
fd = open("f1.txt", O_RDONLY | O_CREAT, 0777);
printf("fd=%d\n", fd);
break;
case 3:
close(fd);
printf("fd=%d\n", fd);
break;
case 4:
fd = open("f1.txt", O_RDONLY | O_CREAT, 0777);
sz = read(fd, c1, 10);
printf("fd=%d\n", fd);
printf("sz=%d\n", sz);
close(fd);
//read();
break;
case 5:
fd = open("f1.txt", O_WRONLY | O_CREAT, 0777);
sz = write(fd, "hi,hello\n", strlen("hi,hello\n"));
printf("sz=%d\n", sz);
close(fd);
break;
case 6:
close(fd);
sz = unlink("f1.txt");
printf("sz=%d\n", sz);
break;
case 7:
fd = open("f1.txt", O_RDONLY);
sz = lseek(fd, -1, SEEK_END);
int i;
chars;
printf("sz=%d\n", sz);
for (i = sz; i > 0; i--) {}
break;
}
}
return 0;
}
******
******
3. Write a C program to simulate Directory management system calls of Linux.
******
******
p = opendir(cwd);
if (p == NULL) {
perror("Cannot find directory");
exit(-1);
}
while (d = readdir(p)) {
printf("%s\t", d->d_name);
}
return 0;
}
*******
*******
4. Demonstrate process control system calls: fork, vfork, exec, wait and sleep,
getpid and getppid
*******
*******
//fork
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
pid_t pd = fork();
if (pd < 0) {
exit(1);
}
if (pd == 0) {
printf("Inside child's process doing addition\n");
int c[10];
printf("\n");
if (pd2 == 0) {
printf("Inside child's child's process doing multiplication\n");
int c[10];
printf("\n");
} else {
pid_t ccpid = wait(NULL);
printf("child's child return and has process id %d\n", ccpid);
}
} else {
printf("Inside parent's process doing subtraction\n");
int c[10];
printf("\n");
if (pd2 == 0) {
printf("Inside parent's child's process doing division\n");
int c[10];
printf("\n");
}
}
return 0;
}
//vfork
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
pid_t pd = vfork();
if (pd < 0) {
exit(1);
}
if (pd == 0) {
printf("Inside child's process doing addition\n");
int c[10];
printf("\n");
if (pd2 == 0) {
printf("Inside child's child's process doing multiplication\n");
int c[10];
for (int i = 0; i < 10; i++) {
c[i] = a[i] * b[i];
}
printf("\n");
_exit(0);
} else {
pid_t ccpid = wait(NULL);
printf("child's child return and has process id %d\n", ccpid);
_exit(0);
}
} else {
printf("Inside parent's process doing subtraction\n");
int c[10];
printf("\n");
if (pd2 == 0) {
printf("Inside parent's child's process doing division\n");
int c[10];
printf("\n");
_exit(0);
}
}
return 0;
}
int main() {
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
pid_t pd = fork();
if (pd < 0) {
exit(1);
}
if (pd == 0) {
printf("Inside child's process doing addition\n");
printf("Getpid() = %d\n", getpid());
int c[10];
for (int i = 0; i < 10; i++) {
c[i] = a[i] + b[i];
}
printf("\n");
} else {
sleep(3);
printf("Inside parent's process doing subtraction\n");
int c[10];
for (int i = 0; i < 10; i++) {
c[i] = a[i] - b[i];
}
printf("\n");
}
return 0;
}
*******
*******
5. CPU Scheduling Algorithms: FCFS, SJF, SRTF, Round Robin(NP), Priority(NP)
*******
*******
//FIFO
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int pid;
int burst_time;
int waiting_time;
int turnaround_time;
} Process;
int main()
{
Process p[MAX];
int i, j, n;
int sum_waiting_time = 0, sum_turnaround_time=0;
printf("Enter total number of process: ");
scanf("%d", &n);
printf("Enter burst time for each process:\n");
for(i=0; i<n; i++) {
p[i].pid = i+1;
printf("P[%d] : ", i+1);
scanf("%d", &p[i].burst_time);
p[i].waiting_time = p[i].turnaround_time = 0;
}
p[0].turnaround_time = p[0].burst_time;
printf("\n");
print_table(p, n);
printf("\n");
printf("Total Waiting Time : %-2d\n", sum_waiting_time);
printf("Average Waiting Time : %-2.2lf\n", (double)sum_waiting_time /
(double) n);
printf("Total Turnaround Time : %-2d\n", sum_turnaround_time);
printf("Average Turnaround Time : %-2.2lf\n", (double)sum_turnaround_time /
(double) n);
printf("\nGANTT CHART:\n");
print_gantt_chart(p, n);
return 0;
}
void print_table(Process p[], int n)
{
int i;
//Non-Preemptive SJF
#include<stdio.h>
#include<stdlib.h>
*******
*******
6. Use of shared memory(with and without synchronization)
*******
*******
//WITHOUT SYNCHRONIZATION
//A.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
perror("shmget");
exit(1);
}
SharedData * sharedData = (SharedData * ) shmat(shmid, NULL, 0);
if (sharedData == (void * ) - 1) {
perror("shmat");
exit(1);
}
FILE * file = fopen("inpfile", "r");
if (file == NULL) {
perror("fopen");
exit(1);
}
int number;
int index = 0;
while (fscanf(file, "%d", & number) != EOF && index < MAX_SIZE) {
sharedData -> numbers[index] = number;
index++;
}
sharedData -> count = index;
fclose(file);
shmdt(sharedData);
sleep(5); // Delay to allow B.c to start after A.c has finished writing
return 0;
}
//B.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_SIZE 100
typedef struct {
int numbers[MAX_SIZE];
int count;
}
SharedData;
int main() {
key_t key = ftok("inpfile", 'R'); // Use the same key as A.c to access the
shared memory
int shmid = shmget(key, sizeof(SharedData), 0666);
if (shmid == -1) {
perror("shmget");
exit(1);
}
int * done = (int * ) shmat(shmid, NULL, 0);
// Synchronization: Wait until done becomes 1
while ( * done != 1) {
sleep(1);
}
shmdt(done);
SharedData * sharedData = (SharedData * ) shmat(shmid, NULL, 0);
if (sharedData == (void * ) - 1) {
perror("shmat");
exit(1);
}
// Sorting the numbers
int i, j, temp;
for (i = 0; i < sharedData -> count - 1; i++) {
for (j = 0; j < sharedData -> count - i - 1; j++) {
if (sharedData -> numbers[j] > sharedData -> numbers[j + 1]) {
temp = sharedData -> numbers[j];
sharedData -> numbers[j] = sharedData -> numbers[j + 1];
sharedData -> numbers[j + 1] = temp;
}
}
}
FILE * file = fopen("outfile", "w");
if (file == NULL) {
perror("fopen");
exit(1);
}
for (int i = 0; i < sharedData -> count; i++) {
//WITH SYNCHRONIZATION
//A1.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
//B1.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
perror("shmat");
exit(1);
}
// Sorting the numbers
int i, j, temp;
for (i = 0; i < sharedData -> count - 1; i++) {
for (j = 0; j < sharedData -> count - i - 1; j++) {
if (sharedData -> numbers[j] > sharedData -> numbers[j + 1]) {
temp = sharedData -> numbers[j];
sharedData -> numbers[j] = sharedData -> numbers[j + 1];
sharedData -> numbers[j + 1] = temp;
}
}
}
FILE * file = fopen("outfile", "w");
if (file == NULL) {
perror("fopen");
exit(1);
}
for (int i = 0; i < sharedData -> count; i++) {
fprintf(file, "%d\n", sharedData -> numbers[i]);
}
fclose(file);
shmdt(sharedData);
shmctl(shmid, IPC_RMID, NULL); // Delete shared memory segment
return 0;
}
*******
*******
7. Write C programs to implement threads and semaphores for process
synchronization.
*******
*******
pthread_create(&tid1,NULL,produce,NULL);
pthread_create(&tid2,NULL,consume,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
sem_destroy();
}
void *Producer();
void *Consumer();
int BufferIndex=-1;
char BUFFER[10];
pthread_cond_t Buffer_Empty=PTHREAD_COND_INITIALIZER;
pthread_cond_t Buffer_Full=PTHREAD_COND_INITIALIZER;
pthread_mutex_t mVar=PTHREAD_MUTEX_INITIALIZER;
int main()
{
pthread_t ptid,ctid;
pthread_create(&ptid,NULL,Producer,NULL);
pthread_create(&ctid,NULL,Consumer,NULL);
pthread_join(ptid,NULL);
pthread_join(ctid,NULL);
return 0;
}
//Producer Consumer
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
}while(1);
pthread_exit(0);
}
void *Producer()
{
//do
int i;
for(i=0; i<15 ; i++)
{
pthread_mutex_lock(&mVar);
if(BufferIndex==BufferSize-1)
pthread_cond_wait(&Buffer_Empty,&mVar);
BUFFER[++BufferIndex]='#';
printf("Produce : %d \n",BufferIndex);
pthread_mutex_unlock(&mVar);
pthread_cond_signal(&Buffer_Full);
}//while(1);
void *Consumer()
{
//do
int i;
for(i=0; i<15 ; i++)
{
pthread_mutex_lock(&mVar);
if(BufferIndex==-1)
pthread_cond_wait(&Buffer_Full,&mVar);
printf("Consume : %d \n",BufferIndex--);
pthread_mutex_unlock(&mVar);
pthread_cond_signal(&Buffer_Empty);
}//while(1);
}
//Reader Writer
/* Fibonacci and Prime using pipe - fibprime.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
int pipefd[2];
int i, j, flg, f1, f2, f3;
static unsigned int ar[25], br[25];
if (pipe(pipefd) == -1) {
printf("Error in pipe\n");
exit(-1);
}
pid = fork();
if (pid == 0) {
close(pipefd[0]);
printf("Child process generates Fibonacci series\n");
f1 = -1;
f2 = 1;
for (i = 0; i < 25; i++) {
f3 = f1 + f2;
printf("%d\t", f3);
f1 = f2;
f2 = f3;
ar[i] = f3;
}
write(pipefd[1], ar, 25 * sizeof(int));
} else if (pid > 0) {
close(pipefd[1]);
wait(NULL);
read(pipefd[0], br, 25 * sizeof(int));
printf("\nParent prints Fibonacci that are Prime\n");
for (i = 0; i < 25; i++) {
flg = 0;
if (br[i] <= 1)
flg = 1;
for (j = 2; j <= br[i] / 2; j++) {
if (br[i] % j == 0) {
flg = 1;
break;
}
}
if (flg == 0)
printf("%d\t", br[i]);
}
printf("\n");
} else {
printf("Process creation failed\n");
exit(-1);
}
return 0;
}
#define M 3
#define K 2
#define N 3
struct v {
int i; /* row */
int j; /* column */
};
int main() {
int i, j;
pthread_t tid; // Thread ID
pthread_attr_t attr; // Set of thread attributes
return 0;
}
*******
*******
8. Write C programs to demonstrate the Banker’s Algorithm.
*******
*******
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10
int available[MAX_RESOURCES];
int maximum[MAX_PROCESSES][MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
bool isSafeState(int num_processes, int num_resources, int request_process,
int request[])
{
int work[MAX_RESOURCES];
bool finish[MAX_PROCESSES];
// Create temporary arrays for safety check
int temp_allocation[MAX_PROCESSES][MAX_RESOURCES];
int temp_need[MAX_PROCESSES][MAX_RESOURCES];
// Initialize work and finish arrays
for (int i = 0; i < num_resources; i++)
work[i] = available[i];
for (int i = 0; i < num_processes; i++)
finish[i] = false;
// Create copies of allocation and need arrays
for (int i = 0; i < num_processes; i++)
{
for (int j = 0; j < num_resources; j++)
{
temp_allocation[i][j] = allocation[i][j];
temp_need[i][j] = need[i][j];
}
}
// Pretend allocation to the request
for (int i = 0; i < num_resources; i++)
{
work[i] -= request[i];
temp_allocation[request_process][i] += request[i];
temp_need[request_process][i] -= request[i];
}
int count = 0;
bool found;
// Find a process that can be executed
do
{
found = false;
for (int i = 0; i < num_processes; i++)
{
if (finish[i] == false)
{
int j;
for (j = 0; j < num_resources; j++)
{
if (temp_need[i][j] > work[j])
break;
}
if (j == num_resources)
{
for (int k = 0; k < num_resources; k++)
work[k] += temp_allocation[i][k];
finish[i] = true;
found = true;
count++;
}
}
}
} while (found && count < num_processes);
return (count == num_processes);
}
bool requestResources(int num_processes, int num_resources, int request_process,
int request[])
{
// Check if request exceeds the maximum need
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
int request_process;
int request[MAX_RESOURCES];
printf("Enter the process requesting resources: ");
scanf("%d", &request_process);
printf("Enter the requested resources:\n");
for (int i = 0; i < num_resources; i++)
scanf("%d", &request[i]);
if (requestResources(num_processes, num_resources,
request_process, request))
break;
}
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Try again.\n");
break;
}
} while (choice != 4);
return 0;
}
******
******
9. Write C programs to implement different disk scheduling algorithms.
******
******
if (next_request != -1) {
printf("Move from %d to %d\n", head, requests[next_request]);
total_movement += min_distance;
visited[next_request] = true;
head = requests[next_request];
}
}
printf("Total head movement: %d\n", total_movement);
}
int main() {
int n, head, direction;
int requests[MAX_REQUESTS];
fcfs(requests, n, head);
sstf(requests, n, head);
return 0;
}
//SCAN
#include<stdio.h>
int absoluteValue(int);
void main()
{
int queue[25], n, headposition, i, j, k, seek=0, maxrange,
difference, temp, queue1[20], queue2[20], temp1=0, temp2=0;
float averageSeekTime;
queue[i] = maxrange;
queue[i] = 0;
queue[0] = headposition;
int absoluteValue(int x)
{
if(x > 0)
{
return x;
}
else
{
return x * -1;
}
}
//CSAN
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek
%d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
//LOOK
#include <stdio.h>
#include <stdlib.h>
int main(){
int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
printf("Enter total disk size\n");
scanf("%d", &size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d", &move);
for (i = 0; i < n; i++){
for (j = 0; j < n - i - 1; j++){
if (RQ[j] > RQ[j + 1]){
int temp;
temp = RQ[j];
RQ[j] = RQ[j + 1];
RQ[j + 1] = temp;
}
}
}
int index;
for (i = 0; i < n; i++){
if (initial < RQ[i]){
index = i;
break;
}
}
if (move == 1){
for (i = index; i < n; i++){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
for (i = index - 1; i >= 0; i--){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
}
else{
for (i = index - 1; i >= 0; i--){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
for (i = index; i < n; i++){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
}
printf("Total head movement is %d \n", TotalHeadMoment);
return 0;
}
//CLOOK
#include <stdlib.h>
int main(){
int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
printf("Enter total disk size\n");
scanf("%d", &size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d", &move);
for (i = 0; i < n; i++){
for (j = 0; j < n - i - 1; j++){
if (RQ[j] > RQ[j + 1]){
int temp;
temp = RQ[j];
RQ[j] = RQ[j + 1];
RQ[j + 1] = temp;
}
}
}
int index;
for (i = 0; i < n; i++){
if (initial < RQ[i]){
index = i;
break;
}
}
if (move == 1){
for (i = index; i < n; i++){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
for (i = 0; i < index; i++){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
}
else{
for (i = index - 1; i >= 0; i--){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
for (i = n - 1; i >= index; i--){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
}
printf("Total head movement is %d\n", TotalHeadMoment);
return 0;
}
******
******
9. Write C programs to implement different page replacement algorithms.
******
******
//FIFO
#include<stdio.h>
int main()
{
int incomingStream[] = {4, 1, 2, 4, 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
//LRU
#include<stdio.h>
#include<limits.h>
return 0;
}
int main()
{
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printFrame(queue, occupied);
}
else{
if(queue[j] == incomingStream[k])
break;
}
printf("\n");
}
return 0;
}
//Optimal
#include <stdio.h>
// This function checks if current strea item(key) exists in any of the frames or
not
int search(int key, int frame_items[], int frame_occupied)
{
for (int i = 0; i < frame_occupied; i++)
if (frame_items[i] == key)
return 1;
return 0;
}
}
printf("\n\nHits: %d\n", hits);
printf("Misses: %d", refStrLen - hits);
}
// Driver Function
int main()
{
// int ref_str[] = {9, 0, 5, 1, 0, 3, 0, 4, 1, 3, 0, 3, 1, 3};
int ref_str[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1};
int refStrLen = sizeof(ref_str) / sizeof(ref_str[0]);
int max_frames = 3;
int frame_items[max_frames];
#include <stdio.h>
typedef struct {
int size;
int allocated;
} Block;
int main() {
Block blocks[MAX_BLOCKS];
int processSize[MAX_PROCESS];
int m, n;
return 0;
}