Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Simple program demonstrating shared memory in POSIX systems.
- *
- * This is the producer process that writes to the shared memory region.
- *
- * Figure 3.17
- *
- * @author Silberschatz, Galvin, and Gagne
- * Operating System Concepts - Ninth Edition
- * Copyright John Wiley & Sons - 2013
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/shm.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- int main(int argc, char **argv)
- {
- const int SIZE = 4096;
- const char *name = "OS";
- const char *message0= "Studying ";
- const char *message1= "Operating Systems ";
- const char *message2= "Is Fun!";
- int BUFFER_SIZE = 0;
- int NUM_PRODUCERS = 0;
- int NUM_MESSAGES = 0;
- int shm_fd;
- void *ptr;
- checkArgs(argc,argv);
- /* create the shared memory segment */
- shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
- /* configure the size of the shared memory segment */
- ftruncate(shm_fd,SIZE);
- /* now map the shared memory segment in the address space of the process */
- ptr = mmap(0,SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
- if (ptr == MAP_FAILED) {
- printf("Map failed\n");
- return -1;
- }
- /**
- * Now write to the shared memory region.
- *
- * Note we must increment the value of ptr after each write.
- */
- sprintf(ptr,"%s",message0);
- ptr += strlen(message0);
- sprintf(ptr,"%s",message1);
- ptr += strlen(message1);
- sprintf(ptr,"%s",message2);
- ptr += strlen(message2);
- return 0;
- }
- void produce()
- {
- }
- void checkArgs(int argc, char **argv)
- {
- if(argc == 1){
- BUFFER_SIZE = 10;
- NUM_PRODUCERS = 5;
- NUM_MESSAGES = 100;
- }
- if(argc == 2 && (argv[0] == "-h")){
- displayHelp();
- }
- if(argc == 4 && (numCheck(argv[0])) && (numCheck(argv[1))
- && (numCheck(argv[2]))){
- BUFFER_SIZE = argv[0];
- NUM_PRODUCERS = argv[1];
- NUM_MESSAGES = argv[2];
- }
- else {
- displayHelp();
- }
- }
- void displayHelp()
- {
- printf("help");
- }
- int numCheck(int x)
- {
- if((x >= 1) && (x <= 100)){
- return x;
- } else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement