int main() { int semid = binary_semaphore_allocation(KEY, 0666); fprintf(stderr, "binary_semaphore_allocation success! semid: %d\n", semid); binary_semaphore_post(semid); fprintf(stderr, "post successful\n"); return 0; }
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; }
int main(){ key_t key = ftok("file1.c", 1); int semid = binary_semaphore_allocation(key, 0666 | IPC_CREAT); binary_semaphore_initialize(semid); binary_semaphore_take(semid); printf("pr1: proc1 took the semaphore\n"); sleep(20); binary_semaphore_free(semid); printf("pr1: proc1 release the semaphore\n"); exit(0); }
int main(){ key_t key = ftok("file1.c", 1); int semid = binary_semaphore_allocation(key, 0666 | IPC_CREAT); if (semid > 0) { printf("pr2: Try take the sem\n"); binary_semaphore_take(semid); printf("pr2: Yeeeh, we have taken it!\n"); binary_semaphore_free(semid); printf("pr2: And released....\n"); exit(0); } printf("pr2: Unable to get sem id\n"); exit(1); }
int main(void) { system(CLEAR_SC); /* Creation/Load of the database */ database = open ("database", O_RDWR | O_CREAT,S_IRWXU); if(database==ERROR) return ERROR; /* Initialize the flock structure. */ lock.l_type = F_WRLCK; /* A write lock also blocks readers */ lock.l_start = 0; lock.l_len = 0; lock.l_whence = SEEK_SET; /* Lock the database */ printf("Accesing the database\n"); if(fcntl (database, F_SETLKW, &lock)==ERROR) return ERROR; printf("Access granted\n"); /* Assign handler for SIGINT (AKA Ctl+C) */ struct sigaction sa; memset (&sa, 0, sizeof (sa)); sa.sa_handler=&sigint_handler; if(sigaction(SIGINT,&sa,NULL)==-1) return SIGNAL_ERROR; /*Binary semaphore creation - MUTEX*/ semaphore=binary_semaphore_allocation(IPC_PRIVATE,IPC_CREAT); binary_semaphore_initialize(semaphore); /*Shared Memory Allocation*/ users_shared_memory = shmget(IPC_PRIVATE,sizeof(data_person)*MAX_USERS,IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR ); newid_shared_memory = shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR ); /* Shared Memory Attach */ int * newid = (int *) shmat(newid_shared_memory,0,0); data_person * users = (data_person *) shmat(users_shared_memory,0,0); /* Shared Memory initialization */ *newid=0; /* Load data from the database */ load_database(users,newid); /* Shared Memory Detach */ shmdt(newid); shmdt(users); /*Client-Server comunication*/ if(initialize()==ERROR) { printf("The port 49500 was not available\n"); sigint_handler(0); } start_connection(&application_request,MAX_QUEUE); return 0; }
/******************************************************************************* Function name : EWLInit Description : Allocate resources and setup the wrapper module Return type : ewl_ret Argument : void *******************************************************************************/ const void *EWLInit(EWLInitParam_t * param) { hx280ewl_t *enc = NULL; PTRACE(("EWLInit: Start\n")); /* Check for NULL pointer */ if(param == NULL || param->clientType > 4) { PTRACE(("EWLInit: Bad calling parameters!\n")); return NULL; } /* Allocate instance */ if((enc = (hx280ewl_t *) EWLmalloc(sizeof(hx280ewl_t))) == NULL) { PTRACE("------>EWLInit: failed to alloc hx280ewl_t struct\n"); return NULL; } enc->clientType = param->clientType; enc->fd_mem = enc->fd_enc = enc->fd_memalloc = enc->fd_dec = -1; /* New instance allocated */ enc->fd_mem = open("/dev/mem", O_RDWR | O_SYNC); if(enc->fd_mem == -1) { PTRACE("------>EWLInit: failed to open: %s\n", "/dev/mem"); goto err; } enc->fd_enc = open(ENC_MODULE_PATH, O_RDWR); if(enc->fd_enc == -1) { PTRACE("------>EWLInit: failed to open: %s\n", ENC_MODULE_PATH); goto err; } enc->fd_memalloc = open(MEMALLOC_MODULE_PATH, O_RDWR); if(enc->fd_memalloc == -1) { PTRACE("------>EWLInit: failed to open: %s\n", MEMALLOC_MODULE_PATH); goto err; } /* map hw registers to user space */ if(MapAsicRegisters(enc) != 0) { goto err; } PTRACE("------>EWLInit: mmap regs %d bytes --> %p\n", enc->regSize, enc->pRegBase); #ifdef USE_LINUX_LOCK_IOCTL /* Use ioctls to the decoder driver to hold/release locks */ enc->fd_dec = open(DEC_MODULE_PATH, O_RDWR); if(enc->fd_dec == -1) { PTRACE("------>EWLInit: failed to open: %s\n", DEC_MODULE_PATH); goto err; } #else { /* common semaphore with the decoder */ key_t key = 0x8070; int semid; if((semid = binary_semaphore_allocation(key, 2, O_RDWR)) != -1) { PTRACE("------>EWLInit: HW lock sem aquired (key=%x, id=%d)\n", key, semid); } else if(errno == ENOENT) { semid = binary_semaphore_allocation(key, 2, IPC_CREAT | O_RDWR); if(binary_semaphore_initialize(semid, 0) != 0) { PTRACE("------>binary_semaphore_initialize error: %s\n", strerror(errno)); } /* init PP semaphore also */ if(binary_semaphore_initialize(semid, 1) != 0) { PTRACE("------>binary_semaphore_initialize error: %s\n", strerror(errno)); } PTRACE("------>EWLInit: HW lock sem created (key=%x, id=%d)\n", key, semid); } else { PTRACE("------>binary_semaphore_allocation error: %s\n", strerror(errno)); goto err; } enc->semid = semid; } #endif #ifndef USE_LINUX_LOCK_IOCTL /* asynchronus notification handler (when needed) */ HandleSIGIO(enc); #endif PTRACE("------>EWLInit: Return %0xd\n", (u32) enc); return enc; err: EWLRelease(enc); PTRACE("------>EWLInit: Return NULL\n"); return NULL; }
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; }