Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Whenever the producer moves an item into the shared buffer, it sends a signal to

ID: 3825572 • 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();
}

2.

/*
Using structure write any no of records to a data file and read the records
to display on standard output
*/
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#define MAX 200
struct employee
{
   int eno;
   char ename[20],job[15];
   float sal,comm,np;
};
struct employee e[MAX];
void main(void)
{
   clrscr();
   fstream f;
   char fname[15];
   int i,n;
   cout << " Enter the filename: ";
   cin >> fname;
   cout << " How many records: ";
   cin >> n;
   cout << " Enter the employee information...!" << endl;
   for(i=0;i<n;i++)
   {
       cout << endl << "Record: " << (i+1) << endl;
       cout << "Empno: ";
       cin >> e[i].eno;
       cout << "Name: ";
       cin >> e[i].ename;
       cout << "Job: ";
       cin >> e[i].job;
       cout << "Salary: ";
       cin >> e[i].sal;
       if(strcmp(e[i].job,"Manager")==0)
       e[i].comm = 0;
       else
       e[i].comm=e[i].sal * (float).14;

       e[i].np=e[i].sal + e[i].comm;
   }
   cout << " Writing to the file.....!";
   f.open(fname,ios::out);
   for(i=0;i<n;i++)
   {
       f << e[i].eno << setw(20) << e[i].ename <<
       setw(15) << e[i].job << setprecision(3) << setw(10) <<
       e[i].sal << setprecision(3) << setw(10) <<
       e[i].comm << setprecision(3) << setw(10) << e[i].np << endl;
   }
   f.close();
   clrscr();
   cout << "Employee list.....!" << endl;
   f.open(fname,ios::in);
   i=0;
   while(!f.eof())
   {
       f >> e[i].eno >> setw(20) >> e[i].ename >> setw(15) >>
       e[i].job >> setw(10) >> e[i].sal >> setw(10) >> e[i].comm >>
       setw(10) >> e[i].np;
       ++i;
   }
   for(i=0;i<n;i++)
   {
       cout << (i+1) << ". " << e[i].eno << setw(20) <<
       e[i].ename << setw(15) <<
       e[i].job << setprecision(3) << setw(10) << e[i].sal <<
       setprecision(3) << setw(10) << e[i].comm << setw(10) <<
       setprecision(3) << e[i].np << endl;
   }
   f.close();
   getch();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote