Study the following system calls
Semaphores sem_init, sem_wait, sem_post, sem_destroy POSIX, pthread semget , semctl, semop ( BSD for your understanding)
Shared memory shmget, shmat, shmdt, shmctl
Assignment 1:
Aim:
To write a C program to create parent/child processes to implement the producer/consumer problem using semaphores in pthread library.
Procedure:
- Create a Shared memory for buffer and semaphores empty, full, mutex
- Create a parent and a child process one acting as a producer and the other consumer.
- In the producer process, produce an item, place it in the buffer. Increment full and decrement empty using wait and signal operations appropriately.
- In the consumer process, consume an item from the buffer and display it on the terminal. Increment empty and decrement full using wait and signal operations appropriately.
- Compile the sample program with pthread library cc prg.c lpthread
Assignment 2:
Modify the program as separate client / server process programs to generate N random numbers in producer and write them into shared memory. Consumer process should read them from shared memory and display them in terminal
Sample Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include <pthread.h> // for semaphore operations sem_init,sem_wait,sem_post
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include <sys/errno.h> #include <sys/types.h> extern int errno;
#define SIZE 10 /* size of the shared buffer*/
#define VARSIZE 1 /* size of shared variable=1byte*/
#define INPUTSIZE 20
#define SHMPERM 0666 /* shared memory permissions */
int segid; /* id for shared memory bufer */ int empty_id; int full_id; int mutex_id; char * buff; char * input_string; sem_t *empty; sem_t *full; sem_t *mutex;
int p=0,c=0;
//
// Producer function
//
void produce()
{
int i=0; while (1)
{
if(i>=strlen(input_string))
{
printf(
Producer %d exited
,getpid());
wait(NULL);
exit(1);
}
printf(
Producer %d trying to aquire Semaphore Empty
,getpid());
sem_wait(empty);
printf(
Producer %d successfully aquired Semaphore Empty
,getpid());
printf(
Producer %d trying to aquire Semaphore Mutex
,getpid());
sem_wait(mutex);
printf(
Producer %d successfully aquired Semaphore Mutex
,getpid()); buff[p]=input_string[i];
printf(
Producer %d Produced Item [ %c ]
,getpid(),input_string[i]);
i++;
p++;
printf(
Items in Buffer %d
,p); sem_post(mutex);
printf(
Producer %d released Semaphore Mutex
,getpid());
sem_post(full);
printf(
Producer %d released Semaphore Full
,getpid()); sleep(2/random());
} //while
} //producer fn
//
// Consumer function
//
void consume()
{
int i=0; while (1)
{
if(i>=strlen(input_string))
{
printf(
Consumer %d exited
,getpid());
exit(1);
}
printf(
Consumer %d trying to aquire Semaphore Full
,getpid());
sem_wait(full);
printf(
Consumer %d successfully aquired Semaphore Full
,getpid());
printf(
Consumer %d trying to aquire Semaphore Mutex
,getpid());
sem_wait(mutex);
printf(
Consumer %d successfully aquired Semaphore Mutex
,getpid()); printf(
Consumer %d Consumed Item [ %c ]
,getpid(),buff[c]);
buff[c]= ;
c++;
printf(
Items in Buffer %d
,strlen(input_string)c);
i++;
sem_post(mutex);
printf(
Consumer %d released Semaphore Mutex
,getpid());
sem_post(empty);
printf(
Consumer %d released Semaphore Empty
,getpid()); sleep(1);
} //while
} //consumer fn
//
Main function
// int main()
{
int i=0; pid_t temp_pid; segid = shmget (IPC_PRIVATE, SIZE, IPC_CREAT | IPC_EXCL | SHMPERM );
empty_id=shmget(IPC_PRIVATE,sizeof(sem_t),IPC_CREAT|IPC_EXCL|
SHMPERM);
full_id=shmget(IPC_PRIVATE,sizeof(sem_t),IPC_CREAT|IPC_EXCL|
SHMPERM);
mutex_id=shmget(IPC_PRIVATE,sizeof(sem_t),IPC_CREAT|IPC_EXCL|
SHMPERM);
buff = shmat( segid, (char *)0, 0 ); empty = shmat(empty_id,(char *)0,0); full = shmat(full_id,(char *)0,0); mutex = shmat(mutex_id,(char *)0,0); // Initializing Semaphores Empty , Full & Mutex
sem_init(empty,1,SIZE); sem_init(full,1,0); sem_init(mutex,1,1); printf(
Main Process Started
); printf(
Enter the input string (20 characters MAX) : );
input_string=(char *)malloc(20); scanf(%s,input_string); printf(Entered string : %s,input_string); temp_pid=fork();
if(temp_pid>0) //parent
{
produce();
}
else //child
{ consume();
}
shmdt(buff); shmdt(empty); shmdt(full); shmdt(mutex); shmctl(segid, IPC_RMID, NULL); semctl( empty_id, 0, IPC_RMID, NULL); semctl( full_id, 0, IPC_RMID, NULL); semctl( mutex_id, 0, IPC_RMID, NULL);
sem_destroy(empty); sem_destroy(full);
sem_destroy(mutex);
printf(
Main process exited
); return(0);
} //main

![[Solved] UCS1411 Exercise 6-Producer/Consumer Problem using Semaphores](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip.jpg)

![[Solved] UCS1411 Exercise 12- File Organization Techniques](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip-1200x1200.jpg)
Reviews
There are no reviews yet.