Exp 3 Os
Exp 3 Os
#include <stdio.h>
#include <stdlib.h>
struct PCB {
int procId;
int cpuBurst;
int waitingTime;
struct PCB *next;
struct PCB *prev;
};
void insertFCFS(struct PCB **head, struct PCB **tail, struct PCB *newPCB) {
if (*head == NULL) {
*head = newPCB;
*tail = newPCB;
} else {
(*tail)->next = newPCB;
newPCB->prev = *tail;
*tail = newPCB;
}
}
void insertRR(struct PCB **head, struct PCB **tail, struct PCB *newPCB) {
if (*head == NULL) {
*head = newPCB;
*tail = newPCB;
newPCB->next = newPCB;
newPCB->prev = newPCB;
} else {
(*tail)->next = newPCB;
newPCB->prev = *tail;
newPCB->next = *head;
(*head)->prev = newPCB;
*tail = newPCB;
}
}
int main() {
struct PCB *headFCFS = NULL;
struct PCB *tailFCFS = NULL;
struct PCB *headRR = NULL;
struct PCB *tailRR = NULL;
insertFCFS(&headFCFS, &tailFCFS, createPCB(1, 10));
insertFCFS(&headFCFS, &tailFCFS, createPCB(2, 5));
insertFCFS(&headFCFS, &tailFCFS, createPCB(3, 8));
insertRR(&headRR, &tailRR, createPCB(1, 10));
insertRR(&headRR, &tailRR, createPCB(2, 5));
insertRR(&headRR, &tailRR, createPCB(3, 8));
printf("Processes added to the queue:\n");
struct PCB *temp = headFCFS;
while (temp != NULL) {
printf("Process %d with CPU burst %d\n", temp->procId, temp->cpuBurst);
temp = temp->next;
}
printf("---\n");
printf("First Come First Serve\n");
executeFCFS(&headFCFS, &tailFCFS);
int timeQuantum = 3;
printf("\nRound Robin with time quantum = %d\n", timeQuantum);
executeRR(&headRR, &tailRR, timeQuantum);
return 0;
}
Multiplication game
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <signal.h>
#include <stdlib.h>
int p = 1;
void fun()
{
int rand_num = 0;
srand(time(0));
rand_num = (rand() % (9 - 2 + 1)) + 2;
p = p * rand_num;
printf("process = %d,rand_num = %d, p = %d\n", getpid(), rand_num, p);
}
int main()
{
pid_t pid;
int n = 0;
printf("Enter the integer n\n");
scanf("%d", &n);
pid = fork();
if (pid == -1)
{
perror("fork failed");
exit(EXIT_FAILURE);
}
if (pid == 0)
{