int create_num(int num) { int ret; int tmp; while (1) { ret = mylock(); if (ret != 0) { fprintf(stderr, "mylock fail!\n"); exit(1); } tmp = getnum(PRIMER); if (tmp != 0) { ret = myunlock(); if (ret != 0) { fprintf(stderr, "myunlock fail!\n"); exit(1); } continue; } ret = setnum(PRIMER, num); if (ret != 0) { fprintf(stderr, "setnum() error!\n"); exit(1); } ret = myunlock(); if (ret != 0) { fprintf(stderr, "myunlock fail!\n"); exit(1); } } return 0; }
int do_child(void) { int ret; int tmp; int i; while (1) { ret = mylock(); if (ret != 0) { fprintf(stderr, "mylock fail!\n"); exit(1); } if ((tmp = getnum(PRIMER)) == 0) { ret = myunlock(); if (ret != 0) { fprintf(stderr, "myunlock fail!\n"); exit(1); } continue; } if (tmp == END) { ret = myunlock(); if (ret != 0) { fprintf(stderr, "myunlock fail!\n"); exit(1); } break; } ret = setnum(PRIMER, 0); if (ret != 0) { fprintf(stderr, "setnum() error!\n"); exit(1); } ret = myunlock(); if (ret != 0) { fprintf(stderr, "myunlock fail!\n"); exit(1); } for (i=2;i<tmp/2;i++) { if (tmp%i == 0) { ret = 0; break; } ret = 1; continue; } if (ret == 1) { printf("%d\n", tmp); } } exit(0); }
// Continually access a critical section for the provided amount of seconds void* bakery(void *x) { // The thread id of this thread int thread_num = *(int*)x; // printf("Hereaaa: %d\n", thread_num); // Run program for provided seconds time_t start, end; double elapsed; start = time(NULL); end = time(NULL); while(1) { end = time(NULL); elapsed = difftime(end, start); if (elapsed >= seconds) break; // Lock the lock mylock(thread_num); // Critical section thread_incs[thread_num]++; assert (in_cs==0); in_cs++; assert (in_cs==1); in_cs++; assert (in_cs==2); in_cs++; assert (in_cs==3); in_cs=0; // Unlock the lock myunlock(thread_num); } return NULL; }
void * increment_counter(void *arg) { int cpu = get_myid(); unsigned long myid = (unsigned long)pthread_self(); //unsigned long myid = tno++; int i = 0; for (i = 0; i < 2; ++i) { POLL(); mylock(&L); POLL(); count++; POLL(); myunlock(&L); POLL(); sleep(interval); } POLL(); sleep(2); // Sleep to give time for other threads to send messages POLL(); pthread_exit(NULL); return NULL; }
void * increment_counter(void *arg) { int i = 0; threadId = (long)arg; init_uli(0); dprintLine("IC called: %d\n", threadId); /* * Setup ULI stack. */ void *my_stack = (void *) malloc(4096); if(my_stack == NULL) { exit(1); } SETUPULI(my_stack); for (i = 0; i < 100; ++i) { POLL(); mylock(&L); POLL(); count++; POLL(); myunlock(&L); POLL(); } POLL(); sleep(2); // Sleep to give time for other threads to send messages POLL(); pthread_exit(NULL); return NULL; }
static void add(int i) { int tmp; mylock(&lock); get_num(&tmp); add_num(&tmp, i); put_num(&tmp); myunlock(&lock); }
int do_child(int proj_id) { int interval; int *shm_p, shm_id; key_t shm_key; // call fork to generate shmkey if ((shm_key = ftok(PATHNAME, proj_id)) == -1) { perror("ftok()"); exit(1); } // use shm_id in child process which was got from parent process shm_id = shmget(shm_key, sizeof(int), 0); if (shm_id < 0) { perror("shmget()"); exit(1); } // shmat function maps shared memroy in local process shm_p = (int *)shmat(shm_id, NULL, 0); if ((void *)shm_p == (void *)-1) { perror("shmat()"); exit(1); } // critical section if (mylock(lockid) == -1) { exit(1); } interval = *shm_p; interval++; usleep(1); *shm_p = interval; if (myunlock(lockid) == -1) { perror("shmdt()"); exit(1); } // critical section if (shmdt(shm_p) < 0) { perror("shmdt()"); exit(1); } exit(0); }
int rlSharedMemory::read(unsigned long offset, void *buf, int len) { void *ptr; if(status != OK) return -1; if(len <= 0) return -1; if(offset+len > _size) return -1; ptr = user_adr + offset; #ifdef RLWIN32 LockFileEx(hSharedFile,LOCKFILE_EXCLUSIVE_LOCK,0,_size,0,&overlapped); // Changed by FMakkinga 18-03-2013 #elif defined(RLUNIX) flock(fdlock,LOCK_EX); #else mylock(mutex,1); #endif memcpy(buf,ptr,len); #ifdef RLWIN32 UnlockFileEx(hSharedFile,0,_size,0,&overlapped); // Changed by FMakkinga 18-03-2013 #elif defined(RLUNIX) flock(fdlock,LOCK_UN); #else myunlock(mutex); #endif return len; }