Access over 20 million homework & study documents

Consumer Producer

Content type
User Generated
Subject
C Programming
School
San Jose State University
Type
Homework
Rating
Showing Page:
1/5
/*
* consumer-producer.c
*
* Created on: 24 Νοε 2020
* Author: dkapsallas
*/
// C program of dual mode consumer-producer based on arguments
// possible arguments
// -p producer
// -c consumer
// -m "string" message from producer
// -q queue depth
// -u socket
// -s shared memory
// -e print the message
#include<stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include<stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define BUF_SIZE 1024
#define SHM_KEY 0x1234
struct shmseg {
int cnt;
int complete;
char buf[BUF_SIZE];
};
int fill_buffer(char * bufptr, int size);
// Taking argument as command line
int main(int argc, char *argv[])
{
// check if there are arguments
if (argc < 2)
{
printf("Enter one of the below arguments:\n-p for producer execution\n-c
for consumer execution\n-m \"string\" for message from producer");
printf("\n-q for queue \"queue depth integer\"\n-u for socket\n-s for
shared memory\n-e for printing the message");
return 0;
}
//read the arguments with getopt
int pflag = 0;
int cflag = 0;
int uflag = 0;
int sflag = 0;
int eflag = 0;

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/5
char *mvalue = NULL;
int qvalue = 0;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "pcm:q:use")) != -1)
switch (c)
{
case 'p':
pflag = 1;
break;
case 'c':
cflag = 1;
break;
case 'm':
mvalue = optarg;
break;
case 'q':
qvalue = atoi (optarg); // q is the depth of the queue
break;
case 'u':
uflag = 1;
break;
case 's':
sflag = 1;
break;
case 'e':
eflag = 1;
break;
case '?':
if (optopt == 'q' || optopt == 'm')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
printf ("pflag = %d, cflag = %d, mvalue = %s, qvalue= %d, uflag = %d, sflag =
%d, eflag = %d\n",
pflag, cflag, mvalue, qvalue, uflag, sflag, eflag);
//structure for shared memory, if qvalue is initial it get the defined
buff_size.
if (qvalue == 0)
{
struct shmseg {
int cnt;
int complete;

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/5

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 5 pages?
Access Now
Unformatted Attachment Preview
/* * consumer-producer.c * * Created on: 24 Νοε 2020 * Author: dkapsallas */ // C program of dual mode consumer-producer based on arguments // possible arguments // -p producer // -c consumer // -m "string" message from producer // -q queue depth // -u socket // -s shared memory // -e print the message #include #include #include #include #include #include #include #include #include #include #include #define BUF_SIZE 1024 #define SHM_KEY 0x1234 struct shmseg { int cnt; int complete; char buf[BUF_SIZE]; }; int fill_buffer(char * bufptr, int size); // Taking argument as command line int main(int argc, char *argv[]) { // check if there are arguments if (argc < 2) { printf("Enter one of the below arguments:\n-p for producer execution\n-c for consumer execution\n-m \"string\" for message from producer"); printf("\n-q for queue \"queue depth integer\"\n-u for socket\n-s for shared memory\n-e for printing the message"); return 0; } //read the arguments with getopt int pflag = 0; int cflag = 0; int uflag = 0; int sflag = 0; int eflag = 0; char *mvalue = NULL; int qvalue = 0; int index; int c; opterr = 0; while ((c = getopt (argc, argv, "pcm:q:use")) != -1) switch (c) { case 'p': pflag = 1; break; case 'c': cflag = 1; break; case 'm': mvalue = optarg; break; case 'q': qvalue = atoi (optarg); // q is the depth of the queue break; case 'u': uflag = 1; break; cas ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
I was stuck on this subject and a friend recommended Studypool. I'm so glad I checked it out!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4