void initiate(char ** argv){
  int key2 = ftok("makefile",'e');
    //creating
  if (strcmp(argv[1], "--create") == 0){
    createsharedmem(key2);
    createsem(key2);
    createfile("story.txt");
    printf("INITIATED\n");
  }
  //removing
  else if (strcmp(argv[1], "--remove") == 0) {
    //remove the shared memory (we need to remove pointers to memories separately or else shared memory will not disappear)
    removesharedmem(key2);
    //remove the semaphore
    removesem(key2);
    //display the file
    if (filesize() >0){
      int fd;
      fd = open("story.txt", O_RDWR | O_APPEND, 0644);
      write(fd, "\n",2);
    }
    //Display the full contents of the story 
    execlp("cat","cat","story.txt",NULL);
  }
  else{
   printf("INVALID INPUT: Please run control with either '--create' or '--remove'.\n");//
  } 
}
Beispiel #2
0
int removemem(void) {
  removesem();
  if (shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0) < 0) {
    perror("removemem");
    return 0;
  }
  return 1;
}