Whenever the producer moves an item into the shared buffer, it sends a signal to
ID: 3825570 • Letter: W
Question
Whenever the producer moves an item into the shared buffer, it sends a signal to main process. Main process enters a “P” in the monitoring table. Similarly, whenever the consumer moves an item from the shared buffer, it sends a signal to main process. Main process then enters a “C” in the monitoring table. When producer and consumer have copied the entire file1, consumer sends a signal to main process. Main process outputs the monitoring table.
Implement the above in the UNIX environment using C or C++. You may need system calls: signal, kill, pause, sleep, semget, semctl, semop, shmget, shmat, shmctl, etc. When sending signals, you should use the two signals defined for users: SIGUSR1 and SIGUSR2 for the producer and consumer respectively
#define N 100 number of slots in the buffer semaphores are a special kind of int typedef int semaphore; Semaphore mutex 1; controls access to critical region Semaphore empty N, counts empty buffer slots Semaphore ful 0; counts full buffer slots void producer(void) int item; while TRUE) TRUE is the constant 1 item produce em() generate something to put in buffer down (); decrement empty count down (&mutex;); enter critical region insert item (item); put new item in buffer up(&mutex;); leave critical region up(&full;); increment count of full slots void consumer (void) int item; while TRUE) infinite loop down(&full;); decrement full count down(&mutex;); enter critical region take item from buffer item remove item up(&mutex;: leave critical region up(); increment count of empty slots consume item(item); do something with the item Figure 2.28. The producer-consumer problem using semaphores.Explanation / Answer
/*
A program to copy the file contents of one file to another using
command line arguments
*/
#include <iostream.h>
#include <fstream.h>
#include <process.h>
#include <conio.h>
void main(int argc, char *argv[])
{
clrscr();
char ch;
ofstream out;
ifstream in,tmp;
if(argc != 3)
{
cout << " Wrong no of command line arguments...!";
getch();
exit(0);
}
in.open(argv[1]);
if(in.fail())
{
cerr << " Source file not exists....!";
getch();
exit(0);
}
tmp.open (argv[2]);
if(!tmp.fail())
{
cerr << " Target file exists.....!";
getch();
exit(0);
}
out.open(argv[2]);
long int nc=0;
while(!in.eof())
{
ch=in.get();
out.put(ch);
nc=nc+1;
}
in.close();
out.close();
cout << " Copied sucessfully....!" << endl;
cout << " No of characters written is: "<< nc << endl;
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.