mirror of
https://github.com/Manoj-HV30/sp-lab.git
synced 2026-05-16 19:35:27 +00:00
cooked
This commit is contained in:
@@ -0,0 +1,81 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
#include<unistd.h>
|
||||||
|
#include<sys/ipc.h>
|
||||||
|
#include<sys/wait.h>
|
||||||
|
#include<semaphore.h>
|
||||||
|
#include<sys/shm.h>
|
||||||
|
#include<fcntl.h>
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 5
|
||||||
|
|
||||||
|
struct shared{
|
||||||
|
int buffer[BUFFER_SIZE];
|
||||||
|
int in;
|
||||||
|
int out;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int n;
|
||||||
|
printf("\nhow many items to produce? ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
int shmid = shmget(IPC_PRIVATE, sizeof(struct shared), IPC_CREAT | 0666);
|
||||||
|
struct shared* s = (struct shared* )shmat(shmid, NULL, 0);
|
||||||
|
s->in = 0;
|
||||||
|
s->out = 0;
|
||||||
|
|
||||||
|
sem_unlink("/mutex");
|
||||||
|
sem_unlink("/empty");
|
||||||
|
sem_unlink("/full");
|
||||||
|
sem_t *mutex = sem_open("/mutex", O_CREAT, 0644, 1);
|
||||||
|
sem_t *empty = sem_open("/empty", O_CREAT, 0644, BUFFER_SIZE);
|
||||||
|
sem_t *full = sem_open("/full", O_CREAT, 0644, 0);
|
||||||
|
|
||||||
|
if(fork()==0){
|
||||||
|
for(int i =1;i<=n;i++){
|
||||||
|
sem_wait(empty);
|
||||||
|
sem_wait(mutex);
|
||||||
|
s->buffer[s->in] = i;
|
||||||
|
printf("produced: %d slot: %d\n", i, s->in);
|
||||||
|
s->in = (s->in+1)%BUFFER_SIZE;
|
||||||
|
|
||||||
|
sem_post(mutex);
|
||||||
|
sem_post(full);
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(fork()==0){
|
||||||
|
for(int i =1;i<=n;i++){
|
||||||
|
sem_wait(full);
|
||||||
|
sem_wait(mutex);
|
||||||
|
int item = s->buffer[s->out];
|
||||||
|
printf("consumed: %d slot: %d\n", item, s->out);
|
||||||
|
s->out = (s->out+1)%BUFFER_SIZE;
|
||||||
|
|
||||||
|
sem_post(mutex);
|
||||||
|
sem_post(empty);
|
||||||
|
sleep(2);
|
||||||
|
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
wait(NULL);
|
||||||
|
wait(NULL);
|
||||||
|
|
||||||
|
shmdt(s);
|
||||||
|
shmctl(shmid, IPC_RMID, NULL);
|
||||||
|
sem_unlink("/mutex");
|
||||||
|
sem_unlink("/empty");
|
||||||
|
sem_unlink("/full");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
#include<unistd.h>
|
||||||
|
#include<string.h>
|
||||||
|
#include<fcntl.h>
|
||||||
|
#include<sys/wait.h>
|
||||||
|
#include<sys/msg.h>
|
||||||
|
#include<sys/ipc.h>
|
||||||
|
#include<semaphore.h>
|
||||||
|
|
||||||
|
struct message{
|
||||||
|
long type;
|
||||||
|
int data;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int n;
|
||||||
|
printf("How many items to produce? ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
int msgid = msgget(IPC_PRIVATE, O_CREAT | 0666);
|
||||||
|
|
||||||
|
sem_unlink("/mutex");
|
||||||
|
sem_unlink("/empty");
|
||||||
|
sem_unlink("/full");
|
||||||
|
|
||||||
|
sem_t *mutex = sem_open("/mutex", O_CREAT, 0644,1);
|
||||||
|
sem_t *empty = sem_open("/empty", O_CREAT, 0644,n);
|
||||||
|
sem_t *full = sem_open("/full", O_CREAT, 0644,0);
|
||||||
|
|
||||||
|
if(fork() == 0){
|
||||||
|
struct message msg;
|
||||||
|
msg.type = 1;
|
||||||
|
|
||||||
|
for(int i =1;i<=n;i++){
|
||||||
|
sem_wait(empty);
|
||||||
|
sem_wait(mutex);
|
||||||
|
|
||||||
|
msg.data = i;
|
||||||
|
msgsnd(msgid, &msg, sizeof(msg.data), 0);
|
||||||
|
printf("Produced: %d\n", i);
|
||||||
|
|
||||||
|
sem_post(full);
|
||||||
|
sem_post(mutex);
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(fork() == 0){
|
||||||
|
struct message msg;
|
||||||
|
|
||||||
|
|
||||||
|
for(int i =1;i<=n;i++){
|
||||||
|
sem_wait(full);
|
||||||
|
sem_wait(mutex);
|
||||||
|
|
||||||
|
msgrcv(msgid, &msg, sizeof(msg.data),1, 0);
|
||||||
|
printf("Consumed: %d\n", msg.data);
|
||||||
|
|
||||||
|
sem_post(empty);
|
||||||
|
sem_post(mutex);
|
||||||
|
sleep(2);
|
||||||
|
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
wait(NULL);
|
||||||
|
wait(NULL);
|
||||||
|
|
||||||
|
msgctl(msgid, IPC_RMID, NULL);
|
||||||
|
sem_unlink("/mutex");
|
||||||
|
sem_unlink("/empty");
|
||||||
|
sem_unlink("/full");
|
||||||
|
|
||||||
|
printf("\nDOne\n");
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include<semaphore.h>
|
||||||
|
#include<fcntl.h>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int read_count = 0;
|
||||||
|
int data= 0;
|
||||||
|
|
||||||
|
sem_unlink("/mutex"); sem_unlink("/wrt");
|
||||||
|
sem_t *mutex = sem_open("/mutex", O_CREAT, 0644, 1);
|
||||||
|
sem_t *wrt = sem_open("/wrt", O_CREAT, 0644, 1);
|
||||||
|
|
||||||
|
int choice;
|
||||||
|
while(1){
|
||||||
|
printf("\n1.Read");
|
||||||
|
printf("\n2.Write");
|
||||||
|
printf("\n3.exit");
|
||||||
|
printf("\nEnter the choice: ");
|
||||||
|
scanf("%d", &choice);
|
||||||
|
|
||||||
|
if(choice ==1){
|
||||||
|
sem_wait(mutex);
|
||||||
|
read_count++;
|
||||||
|
if(read_count==1)
|
||||||
|
sem_wait(wrt);
|
||||||
|
sem_post(mutex);
|
||||||
|
|
||||||
|
printf("read data: %d | Active readers: %d", data, read_count);
|
||||||
|
|
||||||
|
sem_wait(mutex);
|
||||||
|
read_count--;
|
||||||
|
if(read_count==0)
|
||||||
|
sem_post(wrt);
|
||||||
|
sem_post(mutex);
|
||||||
|
}
|
||||||
|
else if(choice ==2){
|
||||||
|
sem_wait(wrt);
|
||||||
|
data+=10;
|
||||||
|
printf("wrote data: %d", data);
|
||||||
|
sem_post(wrt);
|
||||||
|
}else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
sem_close(mutex);
|
||||||
|
sem_close(wrt);
|
||||||
|
sem_unlink("/mutex");
|
||||||
|
sem_unlink("/wrt");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include<fcntl.h>
|
||||||
|
#include<semaphore.h>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int n, phil, ch;
|
||||||
|
printf("Enter number of philosophers: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
int eating[n];
|
||||||
|
for(int i =0;i<n;i++)
|
||||||
|
eating[i]=0;
|
||||||
|
sem_t *chopstick[n];
|
||||||
|
char name[20];
|
||||||
|
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
sprintf(name, "/c%d",i);
|
||||||
|
sem_unlink(name);
|
||||||
|
chopstick[i] = sem_open(name, O_CREAT, 0644, 1);
|
||||||
|
}
|
||||||
|
sem_unlink("/room");
|
||||||
|
sem_t *room = sem_open("/room", O_CREAT, 0644, n-1);
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
printf("---\nDIning philosopher problem----\n");
|
||||||
|
printf("\n1.Pick chopstick(eat)");
|
||||||
|
printf("\n2.Put chopstick(think)");
|
||||||
|
printf("\n3.Exit");
|
||||||
|
printf("\nENter your choice: ");
|
||||||
|
scanf("%d", &ch);
|
||||||
|
|
||||||
|
if(ch==1){
|
||||||
|
printf("\nEnter the philosopher number (0 to %d): ", n-1);
|
||||||
|
scanf("%d",&phil);
|
||||||
|
|
||||||
|
if(eating[phil]){
|
||||||
|
printf("\nprofessor already eating");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sem_wait(room);
|
||||||
|
sem_wait(chopstick[phil]);
|
||||||
|
sem_wait(chopstick[(phil+1)%n]);
|
||||||
|
eating[phil] = 1;
|
||||||
|
printf("\nprofessor %d eating", phil);
|
||||||
|
}
|
||||||
|
else if(ch ==2){
|
||||||
|
printf("\nEnter the philosopher number (0 to %d): ",n-1);
|
||||||
|
scanf("%d", &phil);
|
||||||
|
|
||||||
|
if(!eating[phil]){
|
||||||
|
printf("\nphilosopher not eating");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sem_post(chopstick[phil]);
|
||||||
|
sem_post(chopstick[(phil+1)%n]);
|
||||||
|
sem_post(room);
|
||||||
|
eating[phil] = 0;
|
||||||
|
|
||||||
|
printf("Philosopher %d finished eating\n",
|
||||||
|
phil);
|
||||||
|
}
|
||||||
|
else if (ch == 3) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
printf("Invalid choice\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i =0;i<n;i++){
|
||||||
|
sprintf(name, "/c%d", i);
|
||||||
|
sem_close(chopstick[i]);
|
||||||
|
sem_unlink(name);
|
||||||
|
}
|
||||||
|
sem_close(room);
|
||||||
|
sem_unlink("/room");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include<fcntl.h>
|
||||||
|
#include<semaphore.h>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int n;
|
||||||
|
printf("\nENter buffer size: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
int buffer[n];
|
||||||
|
int in =0, out= 0;
|
||||||
|
|
||||||
|
sem_unlink("/mutex");
|
||||||
|
sem_unlink("/empty");
|
||||||
|
sem_unlink("/full");
|
||||||
|
|
||||||
|
sem_t *mutex = sem_open("/mutex", O_CREAT, 0644, 1);
|
||||||
|
sem_t *empty = sem_open("/empty", O_CREAT, 0644, n);
|
||||||
|
sem_t *full = sem_open("/full", O_CREAT, 0644, 0);
|
||||||
|
|
||||||
|
int ch, item;
|
||||||
|
while(1){
|
||||||
|
printf("\n--- Producer Consumer ---");
|
||||||
|
printf("\n1. Produce (Write)");
|
||||||
|
printf("\n2. Consume (Read)");
|
||||||
|
printf("\n3. Exit");
|
||||||
|
printf("\nEnter choice: ");
|
||||||
|
scanf("%d", &ch);
|
||||||
|
|
||||||
|
if(ch==1){
|
||||||
|
printf("\nEnter item to write: ");
|
||||||
|
scanf("%d", &item);
|
||||||
|
|
||||||
|
sem_wait(empty);
|
||||||
|
sem_wait(mutex);
|
||||||
|
buffer[in]= item;
|
||||||
|
printf("\nProduced: %d at buffer[%d]", item, in);
|
||||||
|
in = (in+1)%n;
|
||||||
|
sem_post(mutex);
|
||||||
|
sem_post(full);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else if(ch==2){
|
||||||
|
|
||||||
|
sem_wait(full);
|
||||||
|
sem_wait(mutex);
|
||||||
|
item = buffer[out];
|
||||||
|
printf("\nconsumed: %d at buffer[%d]", item, out);
|
||||||
|
out = (out+1)%n;
|
||||||
|
sem_post(mutex);
|
||||||
|
sem_post(empty);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (ch == 3) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("Invalid choice\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
sem_close(mutex); sem_close(full); sem_close(empty);
|
||||||
|
sem_unlink("/mutex");
|
||||||
|
sem_unlink("/empty");
|
||||||
|
sem_unlink("/full");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX 25
|
||||||
|
|
||||||
|
void firstFit(int blocks[], int m, int process[], int n) {
|
||||||
|
int alloc[MAX];
|
||||||
|
memset(alloc, -1, sizeof(alloc));
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if (blocks[j] >= process[i]) {
|
||||||
|
alloc[i] = j;
|
||||||
|
blocks[j] -= process[i];
|
||||||
|
break; // first block that fits, stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n--- First Fit ---\n");
|
||||||
|
printf("Process\tSize\tBlock\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (alloc[i] == -1)
|
||||||
|
printf("P%d\t%d\tNot Allocated\n", i+1, process[i]);
|
||||||
|
else
|
||||||
|
printf("P%d\t%d\tBlock %d\n", i+1, process[i], alloc[i]+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bestFit(int blocks[], int m, int process[], int n) {
|
||||||
|
int alloc[MAX];
|
||||||
|
memset(alloc, -1, sizeof(alloc));
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int bestIdx = -1;
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if (blocks[j] >= process[i]) {
|
||||||
|
// pick the smallest block that still fits
|
||||||
|
if (bestIdx == -1 || blocks[j] < blocks[bestIdx])
|
||||||
|
bestIdx = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bestIdx != -1) {
|
||||||
|
alloc[i] = bestIdx;
|
||||||
|
blocks[bestIdx] -= process[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n--- Best Fit ---\n");
|
||||||
|
printf("Process\tSize\tBlock\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (alloc[i] == -1)
|
||||||
|
printf("P%d\t%d\tNot Allocated\n", i+1, process[i]);
|
||||||
|
else
|
||||||
|
printf("P%d\t%d\tBlock %d\n", i+1, process[i], alloc[i]+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void worstFit(int blocks[], int m, int process[], int n) {
|
||||||
|
int alloc[MAX];
|
||||||
|
memset(alloc, -1, sizeof(alloc));
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int worstIdx = -1;
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if (blocks[j] >= process[i]) {
|
||||||
|
// pick the largest block
|
||||||
|
if (worstIdx == -1 || blocks[j] > blocks[worstIdx])
|
||||||
|
worstIdx = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (worstIdx != -1) {
|
||||||
|
alloc[i] = worstIdx;
|
||||||
|
blocks[worstIdx] -= process[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n--- Worst Fit ---\n");
|
||||||
|
printf("Process\tSize\tBlock\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (alloc[i] == -1)
|
||||||
|
printf("P%d\t%d\tNot Allocated\n", i+1, process[i]);
|
||||||
|
else
|
||||||
|
printf("P%d\t%d\tBlock %d\n", i+1, process[i], alloc[i]+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int m, n;
|
||||||
|
int blocks[MAX], process[MAX];
|
||||||
|
int b1[MAX], b2[MAX], b3[MAX];
|
||||||
|
|
||||||
|
printf("Enter number of memory blocks: ");
|
||||||
|
scanf("%d", &m);
|
||||||
|
printf("Enter block sizes: ");
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
scanf("%d", &blocks[i]);
|
||||||
|
b1[i] = b2[i] = b3[i] = blocks[i]; // each strategy gets a fresh copy
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Enter number of processes: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
printf("Enter process sizes: ");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
scanf("%d", &process[i]);
|
||||||
|
|
||||||
|
firstFit(b1, m, process, n);
|
||||||
|
bestFit (b2, m, process, n);
|
||||||
|
worstFit(b3, m, process, n);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user