int main () { int semaphore_id; int segment_id; char* shared_memory; struct shmid_ds shmbuffer; int segment_size, i, j; pid_t child_pid; segment_id = shmget (IPC_PRIVATE, getpagesize(), IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); semaphore_id = binary_semaphore_allocation(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); binary_semaphore_initialize(semaphore_id); shared_memory = (char*) shmat (segment_id, 0, 0); sprintf (shared_memory, "%d", 100); printf("%s\n", shared_memory); child_pid = fork (); if (child_pid != 0) { for (i = 0; i < 1000000; ++i) { binary_semaphore_wait(semaphore_id); sprintf (shared_memory, "%d", atoi(shared_memory) + 1); sprintf (shared_memory, "%d", atoi(shared_memory) - 1); printf("%s\n", shared_memory); binary_semaphore_post(semaphore_id); } } else { for (i = 0; i < 1000000; ++i) { binary_semaphore_wait(semaphore_id); sprintf (shared_memory, "%d", atoi(shared_memory) + 1); sprintf (shared_memory, "%d", atoi(shared_memory) - 1); printf("%s\n", shared_memory); binary_semaphore_post(semaphore_id); } return 0; } printf("\nValor final: %s\n", shared_memory); shmdt (shared_memory); shmctl (segment_id, IPC_RMID, 0); binary_semaphore_deallocate(semaphore_id); return 0; }
void sigint_handler(int signal_number) { int status=0; system(CLEAR_SC); printf(" -->Waiting for last clients to be attended\n"); while(wait(&status)!=-1); /*Waits until there is no more childs*/ /* Save changes in the database to the disk */ printf(" -->Terminating the server correctly\n"); printf(" -->Closing the database\n"); data_person * users = (data_person *) shmat(users_shared_memory,0,0); int * newid = (int *) shmat(newid_shared_memory,0,0); write_database(users,newid); shmdt(users); shmdt(newid); /* Deallocate Shared Memory Segment */ shmctl(users_shared_memory, IPC_RMID, 0); shmctl(newid_shared_memory, IPC_RMID, 0); /* Deallocate Semaphore */ binary_semaphore_deallocate(semaphore); /* Release the lock. */ lock.l_type = F_UNLCK; fcntl (database, F_SETLKW, &lock); close(database); printf(" -->Have a nice day\n"); exit(status); }
int main() { char buf[BUF_SIZE]; int nread; int flag = 0; int nwrite; int fdin,fdout; int shmid; const int shared_segment_size = BUF_SIZE + 1; char* shared_memory; key_t key = ftok("prog1.c", 1); key_t keySem = ftok("tdf", 1); key_t keySem1 = ftok("file", 1); key_t keySem2 = ftok("rgv", 1); int semid = binary_semaphore_allocation(keySem, 0666|IPC_CREAT); printf("semid: %d\n ",semid); int semid1 = binary_semaphore_allocation(keySem1, 0666|IPC_CREAT); printf("semid1: %d\n ",semid1); int semid2 = binary_semaphore_allocation(keySem2, 0666|IPC_CREAT); printf("semid2: %d\n ",semid2); binary_semaphore_initialize(semid); binary_semaphore_initialize(semid1); binary_semaphore_initialize_0(semid2); shmid = shmget(key, shared_segment_size, 0666| IPC_CREAT); if (shmid < 0) printf("Shared memory get fail\n"); shared_memory = shmat(shmid, 0, 0); shared_memory = "4555"; shared_memory[4] = '\0'; binary_semaphore_free(semid2); sleep(10); binary_semaphore_deallocate(semid); binary_semaphore_deallocate(semid1); binary_semaphore_deallocate(semid2); return 0; shared_memory[BUF_SIZE+1] = '0'; fdin = open("100mb_file", O_RDONLY); //fdout = open("file.out", O_RDWR|O_CREAT, //S_IRUSR|S_IWUSR); int i = 0; while ((nread = read(fdin, buf, BUF_SIZE)) > 0) { printf("nread after while: %d\n", nread); binary_semaphore_take(semid1); printf("Take sem1\n"); binary_semaphore_take(semid); printf("Take sem\n"); printf("iter: %d\n", i++); if (nread < BUF_SIZE) { buf[nread] = '\0'; //printf("buf last sem = %s \n", buf); //shared_memory[BUF_SIZE+1] = '1'; } //write(fdout, buf, BUF_SIZE); printf("BUF_SIZE+1 = %c\n", shared_memory[BUF_SIZE +1]); printf("read: %d\n", nread); strcpy(shared_memory, buf); //sleep(5); binary_semaphore_free(semid); printf("Free sem\n"); binary_semaphore_free(semid2); printf("Free sem2\n"); //if (flag) //shared_memory[0] = '\0'; } sleep(2); if (shmdt(shared_memory) == -1) printf("shmdt fail\n"); else printf("shmdt success\n"); if (shmctl(shmid, IPC_RMID, 0) == -1) printf("shmctl fail\n"); else printf("shmctl success\n"); binary_semaphore_deallocate(semid); binary_semaphore_deallocate(semid1); binary_semaphore_deallocate(semid2); close(fdin); //close(fdout); return 0; }