Posts

Showing posts from February, 2024

program 2

 //FCFS #include<stdio.h> int main() { int bt[20], wt[20], tat[20], i, n; float wtavg, tatavg; printf("\nEnter the number of processes -- "); scanf("%d", &n); for(i=0;i<n;i++) { printf("\nEnter Burst Time for Process %d -- ", i); scanf("%d", &bt[i]); } wt[0] = wtavg = 0; tat[0] = tatavg = bt[0]; for(i=1;i<n;i++) { wt[i] = wt[i-1] +bt[i-1]; tat[i] = tat[i-1] +bt[i]; wtavg = wtavg + wt[i]; tatavg = tatavg + tat[i]; } printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n"); for(i=0;i<n;i++) printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]); printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\nAverage Turnaround Time -- %f", tatavg/n); } //SJF #include<stdio.h> int main() { int p[20], bt[20], wt[20], tat[20], i, k, n, temp; float wtavg, tatavg; printf("\nEnter the number of processes -- "); scanf("%d", &n); for(i=0;i<n...

program 5

 #include <stdio.h> #define MAX_PROCESS 10 #define MAX_RESOURCES 10 int available[MAX_RESOURCES]; int maximum[MAX_PROCESS][MAX_RESOURCES]; int allocation[MAX_PROCESS][MAX_RESOURCES]; int need[MAX_PROCESS][MAX_RESOURCES]; int n, m; // Number of processes and resources void initialize() { // Input the number of processes and resources printf("Enter the number of processes: "); scanf("%d", &n); printf("Enter the number of resources: "); scanf("%d", &m); // Input the available resources printf("Enter the available resources:\n"); for (int i = 0; i < m; i++) { printf("Resource %d: ", i + 1); scanf("%d", &available[i]); } // Input maximum resources needed by each process printf("Enter the maximum resources needed by each process:\n"); for (int i = 0; i < n; i++) { printf("Process %d:\n", i + 1); for (int j = 0; j < m; j++) { printf("Resource %d: ", j + 1); scanf(...

program 4

//WRITER  #include <stdio.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> int main() { int fd; char buf[1024] = "Hello World"; char *myfifo = "Absolute path to a folder"; // Create the FIFO (named pipe) mkfifo(myfifo, 0666); printf("Run Reader process to read the FIFO File\n"); // Open the FIFO for writing fd = open(myfifo, O_WRONLY); if (fd == -1) { perror("open"); return 1; } // Write data to the FIFO if (write(fd, buf, sizeof(buf)) == -1) { perror("write"); close(fd); return 1; } // Close the FIFO close(fd); return 0; } //READER  #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> #define MAX_BUF 1024 int main() { int fd; char *myfifo = "Absolute path to a folder"; char buf[MAX_BUF]; // Open the FIFO for reading fd = open(myfifo, O_RDONLY); if (fd == -1) { perror("open...

program 3

 #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> #define BUFFER_SIZE 5 sem_t empty, full; pthread_mutex_t mutex; int buffer[BUFFER_SIZE]; int in = 0, out = 0; void *producer(void *arg) { int item; for (int i = 0; i < BUFFER_SIZE; i++) { item = rand() % 100; // Produce an item sem_wait(&empty); pthread_mutex_lock(&mutex); buffer[in] = item; printf("Producer produced item %d at position %d\n", item, in); in = (in + 1) % BUFFER_SIZE; pthread_mutex_unlock(&mutex); sem_post(&full); sleep(1); } pthread_exit(NULL); } void *consumer(void *arg) { int item; for (int i = 0; i < BUFFER_SIZE; i++) { sem_wait(&full); pthread_mutex_lock(&mutex); item = buffer[out]; printf("Consumer consumed item %d from position %d\n", item, out); out = (out + 1) % BUFFER_SIZE; pthread_mutex_unlock(&mutex); sem_post(&empty); sleep(2); } pthread_exit(NULL); } int main() { pthre...

program 1

 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() { // Create a child process pid_t child_pid = fork(); if (child_pid < 0) { perror("Fork failed"); exit(1); } if (child_pid == 0) { // This code will be executed by the child process // Execute a different program using exec char *args[] = {"ls", "-l", NULL}; execvp("ls", args); // If execvp fails, this code will be executed perror("Exec failed"); exit(1); } else { // This code will be executed by the parent process // Wait for the child process to finish int status; wait(&status); if (WIFEXITED(status)) { printf("Child process exited with status: %d\n", WEXITSTATUS(status)); } else { perror("Child process didn't exit normally"); } } return 0; }