0% found this document useful (0 votes)
2 views20 pages

Os 5,6,7,9

The document contains multiple programs related to inter-process communication and memory management in C, including producer-consumer problems, semaphore usage, pipes, FIFOs, message queues, and shared memory. Each section provides code examples along with explanations of their functionality and expected outputs. The document is structured by weeks, indicating a progression of topics covered in a course at Aditya University.

Uploaded by

jahnavisindhu673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Os 5,6,7,9

The document contains multiple programs related to inter-process communication and memory management in C, including producer-consumer problems, semaphore usage, pipes, FIFOs, message queues, and shared memory. Each section provides code examples along with explanations of their functionality and expected outputs. The document is structured by weeks, indicating a progression of topics covered in a course at Aditya University.

Uploaded by

jahnavisindhu673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Exp No:

Date: Page No:

Week-5
Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int mutex = 1, full = 0, empty = 3, x = 0;
void producer() {
mutex--; full++; empty--; x++;
printf("\nProducer produces item %d", x);
mutex++;
}
void consumer() {
mutex--; full--; empty++;
printf("\nConsumer consumes item %d", x--);
mutex++;
}
int main() {
int n;
while (1) {
printf("\n1. Producer\n2. Consumer\n3. Exit\nEnter choice: ");
scanf("%d", &n);
if (n == 1 && mutex == 1 && empty != 0) producer();
else if (n == 2 && mutex == 1 && full != 0) consumer();
else if (n == 3) exit(0);
else printf("Buffer is full or empty\n");
}
return 0;
}

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Week-6
Program:
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
sem_t x, y;
pthread_t writerthreads[100], readerthreads[100];
int readercount = 0;
void *reader(void *param) {
sem_wait(&x);
readercount++;
if (readercount == 1)
sem_wait(&y);
sem_post(&x);
printf("\nReader %d is inside", readercount);
sleep(1);
sem_wait(&x);
readercount--;
if (readercount == 0)
sem_post(&y);
sem_post(&x);
printf("\nReader %d is leaving", readercount + 1);
return NULL;
}
void *writer(void *param) {
printf("\nWriter is trying to enter");
sem_wait(&y);
printf("\nWriter has entered");

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

sleep(1);
sem_post(&y);
printf("\nWriter is leaving");
return NULL;
}
int main() {
int n;
printf("Enter the number of readers and writers: ");
scanf("%d", &n);
sem_init(&x, 0, 1);
sem_init(&y, 0, 1);
for (int i = 0; i < n; i++) {
pthread_create(&readerthreads[i], NULL, reader, NULL);
pthread_create(&writerthreads[i], NULL, writer, NULL);
}
for (int i = 0; i < n; i++) {
pthread_join(readerthreads[i], NULL);
pthread_join(writerthreads[i], NULL);
}
sem_destroy(&x);
sem_destroy(&y);
return 0;
}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Week-7
Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main() {
int pipe_fd[2];
char data[100];
if (pipe(pipe_fd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid > 0) {
close(pipe_fd[0]);
printf("Enter data to send: ");
fgets(data, sizeof(data), stdin);
write(pipe_fd[1], data, strlen(data) + 1);
close(pipe_fd[1]);
} else {
close(pipe_fd[1]);
read(pipe_fd[0], data, sizeof(data));
printf("Received: %s", data);
close(pipe_fd[0]);
}

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

return 0;
}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#define FIFO_FILE "myfifo"
int main() {
char buffer[80];
mkfifo(FIFO_FILE, 0666);
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
if (pid > 0) {
int fd = open(FIFO_FILE, O_WRONLY);
printf("Enter messages (type 'end' to stop):\n");

while (fgets(buffer, sizeof(buffer), stdin)) {


if (strncmp(buffer, "end", 3) == 0) {
break;
}
write(fd, buffer, strlen(buffer) + 1);
}
close(fd);
wait(NULL);
unlink(FIFO_FILE);

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

} else {
int fd = open(FIFO_FILE, O_RDONLY);
printf("Received messages:\n");
while (read(fd, buffer, sizeof(buffer)) > 0) {
printf("%s", buffer);
}
close(fd);
}
return 0;
}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <sys/wait.h>
#define MSG_KEY 1234
struct message {
long msg_type;
char msg_text[100];
};
int main() {
int msgid;
struct message msg;
msgid = msgget(MSG_KEY, 0666 | IPC_CREAT);
if (msgid == -1) {
perror("Error creating message queue");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid > 0) {
while (1) {
printf("Writer: ");
fflush(stdout);
fgets(msg.msg_text, sizeof(msg.msg_text), stdin);
msg.msg_text[strcspn(msg.msg_text, "\n")] = 0;
msg.msg_type = 1;
msgsnd(msgid, &msg, sizeof(msg.msg_text), 0);

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

if (strcmp(msg.msg_text, "end") == 0) {
break;
}
}
wait(NULL);
msgctl(msgid, IPC_RMID, NULL);
} else {
sleep(10);
printf("\nReader is waiting for messages...\n");
while (1) {
msgrcv(msgid, &msg, sizeof(msg.msg_text), 1, 0);
printf("Reader: %s\n", msg.msg_text);
fflush(stdout);
if (strcmp(msg.msg_text, "end") == 0) {
break;
}
}
}
return 0;
}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/wait.h>
#define SHM_KEY 1234
#define SHM_SIZE 1024
int main() {
int shmid;
char *shared_memory;
shmid = shmget(SHM_KEY, SHM_SIZE, 0666 | IPC_CREAT);
if (shmid == -1) {
perror("Error creating shared memory");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid > 0) {
shared_memory = (char *)shmat(shmid, NULL, 0);
if (shared_memory == (char *)(-1)) {
perror("Error attaching shared memory");
exit(EXIT_FAILURE);
}
while (1) {
printf("Writer: ");
fflush(stdout);
fgets(shared_memory, SHM_SIZE, stdin);
shared_memory[strcspn(shared_memory, "\n")] = 0;

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

if (strcmp(shared_memory, "end") == 0) {
break;
}
sleep(1);
}
wait(NULL);
shmdt(shared_memory);
shmctl(shmid, IPC_RMID, NULL);
} else {
sleep(1);
shared_memory = (char *)shmat(shmid, NULL, 0);
if (shared_memory == (char *)(-1)) {
perror("Error attaching shared memory");
exit(EXIT_FAILURE);
}
printf("\nReader is waiting for messages...\n");
while (1) {
if (strlen(shared_memory) > 0) {
printf("Reader: %s\n", shared_memory);
fflush(stdout);
if (strcmp(shared_memory, "end") == 0) {
break;
}
memset(shared_memory, 0, SHM_SIZE);
}
sleep(1);
}
shmdt(shared_memory); }
return 0;
}

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Week-9
Program:
#include <stdio.h>
int main() {
int pages, frames, m, n, s, pageFaults = 0;
printf("Enter the number of pages: ");
scanf("%d", &pages);
int incomingStream[pages];
printf("Enter the page reference string: ");
for (m = 0; m < pages; m++) {
scanf("%d", &incomingStream[m]);
}
printf("Enter the number of frames: ");
scanf("%d", &frames);
int temp[frames];
for (m = 0; m < frames; m++) {
temp[m] = -1;
}
printf("\nIncoming\tFrame 1\tFrame 2\tFrame 3\n");
for (m = 0; m < pages; m++) {
s = 0;
for (n = 0; n < frames; n++) {
if (incomingStream[m] == temp[n]) {
s++;
pageFaults--;
break;
}
}
pageFaults++;
if ((pageFaults <= frames) && (s == 0)) {

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

temp[m] = incomingStream[m];
} else if (s == 0) {
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
printf("%d\t\t", incomingStream[m]);
for (n = 0; n < frames; n++) {
if (temp[n] != -1)
printf("%d\t", temp[n]);
else
printf("-\t");
}
printf("\n");
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Program:
#include <stdio.h>
int findLRU(int time[], int frames) {
int min = time[0], pos = 0;
for (int i = 1; i < frames; i++) {
if (time[i] < min) {
min = time[i];
pos = i;
}
}
return pos;
}
int main() {
int pages = 12, frames = 4, pageFaults = 0, time[10], counter = 0;
int incomingStream[] = {3, 1, 4, 2, 5, 1, 3, 4, 2, 5, 6, 2};
int temp[frames];
for (int i = 0; i < frames; i++) temp[i] = -1;
printf("\nIncoming\t");
for (int i = 0; i < frames; i++) printf("Frame %d\t", i + 1);
printf("\n");
for (int i = 0; i < pages; i++) {
int found = 0;
for (int j = 0; j < frames; j++) {
if (temp[j] == incomingStream[i]) {
found = 1;
time[j] = counter++;
break; } }
if (!found) {
int pos;
if (i < frames)

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

pos = i;
else
pos = findLRU(time, frames);
temp[pos] = incomingStream[i];
time[pos] = counter++;
pageFaults++; }
printf("%d\t\t", incomingStream[i]);
for (int j = 0; j < frames; j++) {
if (temp[j] != -1)
printf("%d\t", temp[j]);
else
printf("-\t"); }
printf("\n"); }
printf("\nTotal Page Faults (LRU):\t%d\n", pageFaults);
return 0;
}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Program:
#include <stdio.h>
int predict(int incomingStream[], int temp[], int pages, int index, int frames) {
int farthest = index, pos = -1;
for (int i = 0; i < frames; i++) {
int j;
for (j = index; j < pages; j++) {
if (temp[i] == incomingStream[j]) {
if (j > farthest) {
farthest = j;
pos = i;
}
break;
}
}
if (j == pages) return i;
}
return (pos == -1) ? 0 : pos;
}
int main() {
int pages = 12, frames = 4, pageFaults = 0;
int incomingStream[] = {3, 1, 4, 2, 5, 1, 3, 4, 2, 5, 6, 2};
int temp[frames];
for (int i = 0; i < frames; i++) temp[i] = -1;
printf("\nIncoming\t");
for (int i = 0; i < frames; i++) printf("Frame %d\t", i + 1);
printf("\n");
for (int i = 0; i < pages; i++) {
int found = 0;
for (int j = 0; j < frames; j++) {

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

if (temp[j] == incomingStream[i]) {
found = 1;
break;
}
}
if (!found) {
int pos;
if (i < frames)
pos = i;
else
pos = predict(incomingStream, temp, pages, i, frames);
temp[pos] = incomingStream[i];
pageFaults++;
}
printf("%d\t\t", incomingStream[i]);
for (int j = 0; j < frames; j++) {
if (temp[j] != -1)
printf("%d\t", temp[j]);
else
printf("-\t");
}
printf("\n");
}
printf("\nTotal Page Faults (Optimal):\t%d\n", pageFaults);
return 0;
}

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543

You might also like