Exp7 and Exp8
Exp7 and Exp8
==========================================
Experiment 7:
Ans1:
===================================================================================
==========================================
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int count = 0; // Number of items in the buffer
int in = 0; // Index for inserting into the buffer
int out = 0; // Index for removing from the buffer
pthread_mutex_lock(&mutex);
while (count == BUFFER_SIZE) {
pthread_cond_wait(&empty, &mutex);
}
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
count++;
pthread_cond_signal(&full);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
printf("Consumed item %d\n", item);
pthread_cond_signal(&empty);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main() {
pthread_t producer_thread, consumer_thread;
// Join threads
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
return 0;
}
Ans2:
===================================================================================
==========================================
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM_READERS 3
#define NUM_WRITERS 1
int readers_count = 0;
int shared_variable = 0;
// Reading operation
printf("Reader %d reads: %d\n", *((int *)arg), shared_variable);
pthread_mutex_lock(&mutex);
readers_count--;
if (readers_count == 0) {
sem_post(&writer_sem); // Signal writer to write
}
pthread_mutex_unlock(&mutex);
// Sleep to simulate reading time
sleep(rand() % 3);
}
pthread_exit(NULL);
}
// Writing operation
shared_variable++;
printf("Writer writes: %d\n", shared_variable);
int main() {
pthread_t readers[NUM_READERS], writers[NUM_WRITERS];
int reader_ids[NUM_READERS], writer_ids[NUM_WRITERS];
// Initialize semaphores
sem_init(&reader_sem, 0, NUM_READERS);
sem_init(&writer_sem, 0, 1);
// Destroy semaphores
sem_destroy(&reader_sem);
sem_destroy(&writer_sem);
return 0;
}
===================================================================================
==========================================
Experiment 8:
Ans1:
===================================================================================
==========================================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main() {
int sfd, rfd;
char *s_fifo = "/tmp/sender", *r_fifo = "/tmp/receiver", buf[BUF_SIZE];
printf("Receiver program\n");
read(rfd, buf, BUF_SIZE); printf("Received: %s", buf);
Ans2:
===================================================================================
==========================================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
int main() {
int shmid;
key_t key = 1234;
struct shared_data *shared_memory;
shmdt(shared_memory);
printf("\nConsumer process\n");
while (!shared_memory->written) { sleep(1); }
printf("Received message from shared memory: %s", shared_memory->message);
return 0;
}
Ans3:
===================================================================================
==========================================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
int main() {
pid_t pid;
key_t key = ftok("/tmp", 'A');
int msqid;
struct message msg;
pid = fork();
if (pid < 0) { perror("fork"); exit(EXIT_FAILURE); }
else if (pid == 0) { signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, sender_handler); sleep(1);
msg.mtype = 1; sprintf(msg.mtext, "Hello from sender!");
if (msgsnd(msqid, &msg, sizeof(msg.mtext), 0) == -1) { perror("msgsnd");
exit(EXIT_FAILURE); } }
else {
signal(SIGUSR1, receiver_handler);
signal(SIGUSR2, SIG_IGN);
printf("Receiver waiting for message...\n");
if (msgrcv(msqid, &msg, sizeof(msg.mtext), 1, 0) == -1) {
perror("msgrcv"); exit(EXIT_FAILURE); }
printf("Received message from sender: %s\n", msg.mtext);
if (msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl");
exit(EXIT_FAILURE); } }
return 0;
}