0% found this document useful (0 votes)
18 views8 pages

Lab7 Os

The document contains C code implementations for the Producer-Consumer problem using shared memory and semaphores, as well as file-based communication. It also includes a solution for the Dining Philosophers problem using semaphores for synchronization. Each section provides code for the producer and consumer processes, along with explanations of their functionality.

Uploaded by

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

Lab7 Os

The document contains C code implementations for the Producer-Consumer problem using shared memory and semaphores, as well as file-based communication. It also includes a solution for the Dining Philosophers problem using semaphores for synchronization. Each section provides code for the producer and consumer processes, along with explanations of their functionality.

Uploaded by

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

Lab Experiment – 7

Operating System Lab


Name : Gaurav Singh
Reg. No.: 22BLC1081
Q1. Implement Producer Consumer Problem as parent child process. Also
implement Producer and Consumer as different process.

Producer C Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <time.h>
#define BUFFERSIZE 5
#define PRODUCE_COUNT 10
struct shmseg {
int buffer[BUFFERSIZE];
int in;
int out;
};
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
};
void sem_wait(int semid, int semnum) {
struct sembuf op;
op.sem_num = semnum;
op.sem_op = -1;
op.sem_flg = 0;
semop(semid, &op, 1);
}
void sem_signal(int semid, int semnum) {
struct sembuf op;
op.sem_num = semnum;
op.sem_op = 1;
op.sem_flg = 0;
semop(semid, &op, 1);
}
int main() {
key_t key = ftok("pc", 'x');
int shmid = shmget(key, sizeof(struct shmseg), IPC_CREAT | 0666);
struct shmseg *shmp = shmat(shmid, NULL, 0);
shmp->in = 0;
shmp->out = 0;
int semid = semget(key, 3, IPC_CREAT | 0666);
union semun arg;
arg.val = 1;
semctl(semid, 0, SETVAL, arg);
arg.val = BUFFERSIZE;
semctl(semid, 1, SETVAL, arg);
arg.val = 0;
semctl(semid, 2, SETVAL, arg);
int i, item;
for(i = 0; i < PRODUCE_COUNT; i++) {
sem_wait(semid, 1);
sem_wait(semid, 0);
item = rand() % 100;
shmp->buffer[shmp->in] = item;
shmp->in = (shmp->in + 1) % BUFFERSIZE;
printf("Produced: %d\n", item);
fflush(stdout);
sem_signal(semid, 0);
sem_signal(semid, 2);
sleep(1);
}
shmdt(shmp);
return 0;
}
Consumer C Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define BUFFERSIZE 5
#define PRODUCE_COUNT 10
struct shmseg {
int buffer[BUFFERSIZE];
int in;
int out;
};
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
};
void sem_wait(int semid, int semnum) {
struct sembuf op;
op.sem_num = semnum;
op.sem_op = -1;
op.sem_flg = 0;
semop(semid, &op, 1);
}
void sem_signal(int semid, int semnum) {
struct sembuf op;
op.sem_num = semnum;
op.sem_op = 1;
op.sem_flg = 0;
semop(semid, &op, 1);
}
int main() {
key_t key = ftok("pc", 'x');
int shmid = shmget(key, sizeof(struct shmseg), 0666);
struct shmseg *shmp = shmat(shmid, NULL, 0);
int semid = semget(key, 3, 0666);
int i, item;
for(i = 0; i < PRODUCE_COUNT; i++) {
sem_wait(semid, 2);
sem_wait(semid, 0);
item = shmp->buffer[shmp->out];
shmp->out = (shmp->out + 1) % BUFFERSIZE;
printf("Consumed: %d\n", item);
fflush(stdout);
sem_signal(semid, 0);
sem_signal(semid, 1);
sleep(1);
}
shmdt(shmp);
return 0;
}

OUTPUT:
Q2. Implement Producer Consumer Problem where producer writes a file
and consumer reads a file using semaphores.

Producer C Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <semaphore.h>
#include <time.h>
#define PRODUCE_COUNT 10
int main(){
sem_t *sem = sem_open("/full_sem", O_CREAT, 0644, 0);
FILE *fp = fopen("buffer.txt", "a");
int i, item;
for(i = 0; i < PRODUCE_COUNT; i++){
item = rand() % 100;
fprintf(fp, "%d\n", item);
fflush(fp);
sem_post(sem);
sleep(1);
}
fclose(fp);
sem_close(sem);
return 0;
}
Consumer C Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <semaphore.h>
#define PRODUCE_COUNT 10
int main(){
sem_t *sem = sem_open("/full_sem", O_CREAT, 0644, 0);
FILE *fp = fopen("buffer.txt", "r+");
if(fp == NULL) fp = fopen("buffer.txt", "w+");
char line[256];
int i;
for(i = 0; i < PRODUCE_COUNT; i++){
sem_wait(sem);
if(fgets(line, sizeof(line), fp) != NULL)
printf("Consumed: %s", line);
sleep(1);
}
fclose(fp);
sem_close(sem);
sem_unlink("/full_sem");
return 0;
}

OUTPUT:
Q3. Write a C code to implement dining philosopher's problem: Consider
each philosopher attempts to grab both the forks (left and right)
simultaneously. If he can, he eats. If not, the philsopher waits. If a
philosopher sees that only on eof the forks is available he does not grab it.
Use semaphores to synchronize the think-and-eat loops run by the
philosophers. The program should print suitable messages. Get the number
of philosophers from the user.

C CODE:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <time.h>
int num_philosophers;
sem_t *forks;
void* philosopher(void *arg) {
int id = *(int*)arg;
int left = id;
int right = (id + 1) % num_philosophers;
while(1) {
printf("Philosopher %d is thinking\n", id);
sleep(rand() % 3 + 1);
if(sem_trywait(&forks[left]) == 0) {
if(sem_trywait(&forks[right]) == 0) {
printf("Philosopher %d is eating\n", id);
sleep(rand() % 3 + 1);
printf("Philosopher %d finished eating\n", id);
sem_post(&forks[right]);
sem_post(&forks[left]);
} else {
printf("Philosopher %d could not acquire right fork\n", id);
sem_post(&forks[left]);
}
} else {
printf("Philosopher %d could not acquire left fork\n", id);
}
sleep(1);
}
return NULL;
}
int main(){
srand(time(NULL));
printf("Enter number of philosophers: ");
scanf("%d", &num_philosophers);
forks = malloc(num_philosophers * sizeof(sem_t));
pthread_t *threads = malloc(num_philosophers * sizeof(pthread_t));
int *ids = malloc(num_philosophers * sizeof(int));
for(int i = 0; i < num_philosophers; i++){
sem_init(&forks[i], 0, 1);
}
for(int i = 0; i < num_philosophers; i++){
ids[i] = i;
pthread_create(&threads[i], NULL, philosopher, &ids[i]);
}
for(int i = 0; i < num_philosophers; i++){
pthread_join(threads[i], NULL);
}
free(threads);
free(ids);
free(forks);
return 0;
}
OUTPUT:

You might also like