commit 447e01fbc33de86a664ab157bea2b45fd1f9b73c Author: Wander_Lust Date: Thu May 14 00:24:19 2026 +0530 cooked diff --git a/1.c b/1.c new file mode 100644 index 0000000..199529b --- /dev/null +++ b/1.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#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; + + + + + +} diff --git a/2.c b/2.c new file mode 100644 index 0000000..e9027e5 --- /dev/null +++ b/2.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; + +} + + diff --git a/3.c b/3.c new file mode 100644 index 0000000..acebbd4 --- /dev/null +++ b/3.c @@ -0,0 +1,54 @@ +#include +#include +#include + +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; +} diff --git a/4.c b/4.c new file mode 100644 index 0000000..7868d17 --- /dev/null +++ b/4.c @@ -0,0 +1,80 @@ +#include +#include +#include + +int main(){ + int n, phil, ch; + printf("Enter number of philosophers: "); + scanf("%d", &n); + + int eating[n]; + for(int i =0;i +#include +#include + +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; +} diff --git a/dp b/dp new file mode 100755 index 0000000..cd586d6 Binary files /dev/null and b/dp differ diff --git a/mem b/mem new file mode 100755 index 0000000..5edd605 Binary files /dev/null and b/mem differ diff --git a/memory_allocation.c b/memory_allocation.c new file mode 100644 index 0000000..91c4c84 --- /dev/null +++ b/memory_allocation.c @@ -0,0 +1,112 @@ +#include +#include + +#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; +} diff --git a/pc b/pc new file mode 100755 index 0000000..681ec6a Binary files /dev/null and b/pc differ diff --git a/pc_2 b/pc_2 new file mode 100755 index 0000000..91c906e Binary files /dev/null and b/pc_2 differ diff --git a/pcmsg b/pcmsg new file mode 100755 index 0000000..582837f Binary files /dev/null and b/pcmsg differ diff --git a/rw b/rw new file mode 100755 index 0000000..b6ac903 Binary files /dev/null and b/rw differ