void Create (void * (* fn) (void *), void * ) { /* Initialize thread creation attributes */ int s = pthread_attr_init(&m_attr); if (s != 0) handle_error_en(s, "pthread_attr_init"); /*if (stack_size > 0) { s = pthread_attr_setstacksize(&m_attr, stack_size); if (s != 0) handle_error_en(s, "pthread_attr_setstacksize"); }*/ /* Create one thread for each command-line argument */ for (size_t t = 0; t < N; ++t) { m_tinfo[t].thread_num = t + 1; //m_tinfo[t].argv_string = argv[optind + t]; cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(t, &cpuset); //CPU_SET(atoi(m_tinfo[t].argv_string), &cpuset); pthread_attr_setaffinity_np(&m_attr, sizeof(cpuset), &cpuset); /* The pthread_create() call stores the thread ID into corresponding element of m_tinfo[] */ s = pthread_create(&m_tinfo[t].thread_id, &m_attr, fn, &m_tinfo[t]); if (s != 0) handle_error_en(s, "pthread_create"); } }
int main(int argc, char *argv[]) { int s; pthread_t thr; pthread_attr_t attr; pthread_attr_t *attrp = NULL; /* Set to &attr if we initialize a thread attributes object */ attrp = get_thread_attributes_from_cl(argc, argv, &attr); if (attrp != NULL) { printf("Thread attributes object after initializations:\n"); display_stack_related_attributes(attrp, "\t"); printf("\n"); } s = pthread_create(&thr, attrp, &thread_start, NULL); if (s != 0) handle_error_en(s, "pthread_create"); if (attrp != NULL) { s = pthread_attr_destroy(attrp); if (s != 0) handle_error_en(s, "pthread_attr_destroy"); } pause(); /* Terminates when other thread calls exit() */ }
void *sell_tickets(void *arg) { thread_info *tinfo = (thread_info *)arg; int rc; while (1) { rc = pthread_mutex_lock(tinfo->tmutex); if (rc != 0) handle_error_en(rc, "pthread_mutex_lock"); if (*(tinfo->num_tickets_to_sell) == 0) break; (*(tinfo->num_tickets_to_sell))--; printf("Agent %d prodava jizdenku.\n", tinfo->agent_id); if (random_chance(0.2)) sleep(1); rc = pthread_mutex_unlock(tinfo->tmutex); if (rc != 0) handle_error_en(rc, "pthread_mutex_unlock"); tinfo->num_tickets_sold++; if (random_chance(0.2)) sleep(1); } /* pokud se udela v predchozi smycce break je treba unlock */ rc = pthread_mutex_unlock(tinfo->tmutex); if (rc != 0) handle_error_en(rc, "pthread_mutex_unlock"); printf("Agent %d: hotovo.\n", tinfo->agent_id); return NULL; }
int main(int argc, char *argv[]) { int s, j; cpu_set_t cpuset; pthread_t thread; thread = pthread_self(); /* Set affinity mask to include CPUs 0 to 7 */ CPU_ZERO(&cpuset); for (j = 0; j < 8; j++) CPU_SET(j, &cpuset); s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_setaffinity_np"); /* Check the actual affinity mask assigned to the thread */ s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np"); printf("Set returned by pthread_getaffinity_np() contained:\n"); for (j = 0; j < CPU_SETSIZE; j++) if (CPU_ISSET(j, &cpuset)) printf(" CPU %d\n", j); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int s, tnum, opt, num_threads; struct thread_info *tinfo; pthread_attr_t attr; void *res; num_threads = atoi(argv[1]); /* Initialize thread creation attributes */ s = pthread_attr_init(&attr); if (s != 0) handle_error_en(s, "pthread_attr_init"); /* Allocate memory for pthread_create() arguments */ tinfo = calloc(num_threads, sizeof(struct thread_info)); if (tinfo == NULL) handle_error("calloc"); /* Create one thread for each command-line argument */ for (tnum = 0; tnum < num_threads; tnum++) { tinfo[tnum].thread_num = tnum + 1; tinfo[tnum].argv_string = argv[optind + tnum]; /* The pthread_create() call stores the thread ID into corresponding element of tinfo[] */ s = pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &tinfo[tnum]); if (s != 0) handle_error_en(s, "pthread_create"); } /* Destroy the thread attributes object, since it is no longer needed */ s = pthread_attr_destroy(&attr); if (s != 0) handle_error_en(s, "pthread_attr_destroy"); /* Now join with each thread, and display its returned value */ for (tnum = 0; tnum < num_threads; tnum++) { s = pthread_join(tinfo[tnum].thread_id, &res); if (s != 0) handle_error_en(s, "pthread_join"); printf("Joined with thread %d; returned value was %s\n", tinfo[tnum].thread_num, (char *) res); free(res); /* Free memory allocated by thread */ } free(tinfo); exit(EXIT_SUCCESS); }
static void * thread_func(void *ignored_argument) { int s; s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); if (s != 0) handle_error_en(s, "pthread_setcancelstate"); printf("thread_func(): started; cancellation disabled\n"); printf("thread_func(): about to enable cancellation\n"); s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); if (s != 0) handle_error_en(s, "pthread_setcancelstate"); char *p; size_t i = 0; while (i < 10000) { p = malloc(128); if (p) p[0] = 42; i++; } printf("thread_func(): not canceled!\n"); return NULL; }
int main(void) { pthread_t thr; void *res; int s; /* Start a thread and then send it a cancellation request */ s = pthread_create(&thr, NULL, &thread_func, NULL); if (s != 0) handle_error_en(s, "pthread_create"); sleep(2); /* Give thread a chance to get started */ printf("main(): sending cancellation request\n"); s = pthread_cancel(thr); if (s != 0) handle_error_en(s, "pthread_cancel"); /* Join with thread to see what its exit status was */ s = pthread_join(thr, &res); if (s != 0) handle_error_en(s, "pthread_join"); if (res == PTHREAD_CANCELED) printf("main(): thread was canceled\n"); else printf("main(): thread wasn't canceled (shouldn't happen!)\n"); exit(EXIT_SUCCESS); }
// $ ./a.out & // [1] 5423 // $ kill -QUIT %1 // Signal handling thread got signal 3 // $ kill -USR1 %1 // Signal handling thread got signal 10 // $ kill -TERM %1 // [1]+ Terminated ./a.out int main(int argc, char *argv[]) { pthread_t thread; sigset_t set; int s; /* Block SIGINT; other threads created by main() will inherit a copy of the signal mask. */ sigemptyset(&set); sigaddset(&set, SIGQUIT); sigaddset(&set, SIGUSR1); s = pthread_sigmask(SIG_BLOCK, &set, NULL); if (s != 0){ handle_error_en(s, "pthread_sigmask"); } s = pthread_create(&thread, NULL, &sig_thread, (void *) &set); if (s != 0){ handle_error_en(s, "pthread_create"); } /* Main thread carries on to create other threads and/or do other work */ pause(); /* Dummy pause so we can test program */ }
static void set_affinity() { int s, j; cpu_set_t cpuset; pthread_t thread; thread = pthread_self(); /* Set affinity mask to include CPUs 1 only */ CPU_ZERO(&cpuset); CPU_SET(RT_CPU, &cpuset); s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) { handle_error_en(s, "pthread_setaffinity_np"); } /* Check the actual affinity mask assigned to the thread */ s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) { handle_error_en(s, "pthread_getaffinity_np"); } char buff[64] = {0}; char *offset = buff; int blen = sizeof(buff); for (j = 0; j < CPU_SETSIZE; j++) { if (CPU_ISSET(j, &cpuset)) { snprintf(offset, blen, "CPU%d ", j); blen -= strlen(offset); offset += strlen(offset); } } syslog(LOG_DEBUG, "[RT thread %s] priority [%d] CPU affinity: %s", thread_info.thread_name, thread_info.thread_prio, buff); }
void measure_count(int sep) { int s; /* Timestamp */ unsigned long int total_time; uint64_t total_count = 0; uint64_t *counter; struct timeval start, end; gettimeofday(&start, NULL); /* Thread creation */ int num_threads = NUMCPUS; struct thread_info *tinfo; pthread_attr_t attr; s = pthread_attr_init(&attr); if (s != 0) handle_error_en(s, "pthread_attr_init"); tinfo = calloc(num_threads, sizeof(struct thread_info)); if (tinfo == NULL) handle_error("calloc"); for (int tnum = 0; tnum < num_threads; tnum++) { tinfo[tnum].thread_num = tnum + 1; tinfo[tnum].sep = sep; s = pthread_create(&tinfo[tnum].thread_id, &attr, &threadloop, &tinfo[tnum]); if (s != 0) handle_error_en(s, "pthread_create"); } while(!stop) sleep(2); for (int tnum = 0; tnum < num_threads; tnum++) { pthread_join(tinfo[tnum].thread_id, NULL); } stop = 0; gettimeofday(&end, NULL); total_time = (end.tv_sec - start.tv_sec); for(int i = 0; i < NUMCPUS; i++) { counter = (uint64_t *)(&(counters[i*sep])); total_count += *counter; } if (GPLOT) printf("%d %lu\n", sep, total_count/total_time); else printf("Separation: %3d, Total time: %3lu, ops: %lu, speed (ops/sec) = \t %lu\n", sep, total_time, total_count, total_count/total_time); }
static void display_thread_attr(pthread_t thread, char *prefix) { int s; pthread_attr_t attr; s = pthread_getattr_np(thread, &attr); if (s != 0) handle_error_en(s, "pthread_getattr_np"); display_stack_attr (&attr, prefix); s = pthread_attr_destroy(&attr); if (s != 0) handle_error_en(s, "pthread_attr_destroy"); }
int main(int argc, char *argv[]) { int err; void *res; struct tt_thread_info ti = { 0, .thread_name = "tt-test", .thread_prio = 0 }; if (argc == 2) { ti.dev = argv[1]; } else { ti.dev = NULL; } /* start & run thread for capture and interval processing */ err = tt_intervals_init(&ti); if (err) { handle_error_en(err, "tt intervals init"); } err = pthread_attr_init(&ti.attr); if (err) { handle_error_en(err, "pthread_attr_init"); } err = pthread_create(&ti.thread_id, &ti.attr, tt_intervals_run, &ti); if (err) { handle_error_en(err, "pthread_create"); } tt_update_ref_window_size(&ti, tt_intervals[0]); tt_update_ref_window_size(&ti, tt_intervals[INTERVAL_COUNT - 1]); /* check if the thread is still alive */ if (EBUSY != pthread_tryjoin_np(ti.thread_id, &res)) { handle_error_en(err, "pthread_tryjoin_np"); } pthread_cancel(ti.thread_id); pthread_join(ti.thread_id, &res); tt_intervals_free(&ti); return 0; }
int main(int argc, const char * argv[]) { int server_fd; int client_fd; int pid; if (argc != 4) { printf("Usage: SocketBridge listen_port dest_addr dest_port"); handle_error_en(1, "argc"); } server_fd = listen_on(atoi(argv[1])); while (1) { client_fd = accept_connection(server_fd); pid = fork(); if (pid < 0) { handle_error("fork"); } else if (pid == 0) { handle_request(client_fd, argv); } else { close(client_fd); } } }
int main(int argc,char*argv[]) { pthread_t thread; sigset_t set; int s; sigemptyset(&set); sigaddset(&set,SIGQUIT); sigaddset(&set,SIGUSR1); s = pthread_sigmask(SIG_BLOCK,&set,NULL); if(s != 0) handle_error_en(s,"pthread_sigmask"); s = pthread_create(&thread,NULL,&sig_thread,(void*)&set); if(s != 0) handle_error_en(s,"pthread_create"); pause(); }
static void * thread_func(void *ignored_argument) { int s; /* Disable cancellation for a while, so that we don't immediately react to a cancellation request */ //s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); /*if (s != 0) handle_error_en(s, "pthread_setcancelstate");*/ printf("thread_func(): started; cancellation disabled\n"); sleep(5); printf("thread_func(): about to enable cancellation\n"); s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); //s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); if (s != 0) handle_error_en(s, "pthread_setcancelstate"); /* sleep() is a cancellation point */ sleep(1000); /* Should get canceled while we sleep */ /* Should never get here */ printf("thread_func(): not canceled!\n"); return NULL; }
int set_single_core_affinity() { int s; int j=0; cpu_set_t cpuset; pthread_t thread; thread = pthread_self(); CPU_ZERO(&cpuset); CPU_SET(j, &cpuset); s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) { handle_error_en(s, "pthread_setaffinity_np"); } /* s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np"); printf("Set returned by pthread_getaffinity_np() contained:\n"); for (j = 0; j < CPU_SETSIZE; j++) if (CPU_ISSET(j, &cpuset)) printf(" CPU %d\n", j); */ return(EXIT_SUCCESS); }
static void * thread_start(void *arg) { struct thread_info *tinfo = arg; ticks tick, tick1, diff = 0, diff1; struct timespec ts, ts1; unsigned time =0; int s, j; /* Check the actual affinity mask assigned to the thread */ s = pthread_getaffinity_np(tinfo->thread_id, sizeof(cpu_set_t), &tinfo->cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np"); printf("Thread %d: \n", tinfo->thread_num); for (j = 0; j < CPU_SETSIZE; j++) { if (CPU_ISSET(j, &tinfo->cpuset)) { tinfo->cpu_bitmap |= j; printf(" CPU %d\n", j); } } for (j = 0; j <= 500000; j++) { bzero((void*) &ts, sizeof(struct timespec)); bzero((void*) &ts1, sizeof(struct timespec)); clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts1); tick = getticks(); tick1 = getticks(); diff1 = tick1 - tick; if (ts1.tv_sec <= ts.tv_sec && ts1.tv_nsec <= ts.tv_nsec) { printf("ERROR[%d:0x%lx]Time backwards %ld.%ld %ld.%ld\nTick %Lu %Lu (%Lu)\n", tinfo->thread_num, tinfo->cpu_bitmap, ts.tv_sec, ts.tv_nsec, ts1.tv_sec, ts1.tv_nsec, tick, tick1, tick - tick1); } if (tick1 <= tick) { printf("ERROR[%d:0x%lx][%ld.%ld]Tick backwards %Lu %Lu (%Lu)\n", tinfo->thread_num, tinfo->cpu_bitmap, ts1.tv_sec, ts1.tv_nsec, tick, tick1, tick - tick1); } if (diff1 > (diff * 1000)) { printf("[%d:0x%lx][%ld.%ld]Tick: %Ld %Ld, Diff: %Lu %Lu\n", tinfo->thread_num, tinfo->cpu_bitmap, ts1.tv_sec, ts1.tv_nsec, tick, tick1, diff, diff1); } diff = diff1; } return NULL; }
int main(int argc, char *argv[]) { int s, j, nprocs; cpu_set_t cpuset; pthread_t thread; thread = pthread_self(); nprocs = sysconf(_SC_NPROCESSORS_ONLN); /* Set affinity mask to include CPUs 0 to 7 */ CPU_ZERO(&cpuset); for (j = 0; j < nprocs; j++) CPU_SET(j, &cpuset); CPU_CLR(1, &cpuset); CPU_CLR(2, &cpuset); CPU_CLR(3, &cpuset); CPU_CLR(4, &cpuset); CPU_CLR(5, &cpuset); /* check if the cpu's have actually been set */ for (j = 0; j < nprocs; j++) fprintf(stdout, "CPU: %d, status: %d\n", j, CPU_ISSET(j, &cpuset)); s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_setaffinity_np"); /* Check the actual affinity mask assigned to the thread */ s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np"); printf("Set returned by pthread_getaffinity_np() contained:\n"); for (j = 0; j < CPU_SETSIZE; j++) if (CPU_ISSET(j, &cpuset)) printf(" CPU %d\n", j); exit(EXIT_SUCCESS); }
static void display_pthread_attr (pthread_attr_t *attr, char *prefix) { int s, i; size_t v; void *stkaddr; struct sched_param sp; s = pthread_attr_getdetachstate (attr, &i); if (s != 0) handle_error_en (s, "pthread_attr_getdetachstate"); printf ("%sDetach state = %s\n", prefix, (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" : (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" : "???"); s = pthread_attr_getscope (attr, &i); if (s != 0) handle_error_en (s, "pthread_attr_getscope"); printf ("%sScope = %s\n", prefix, (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" : (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" : "???"); s = pthread_attr_getinheritsched (attr, &i); if (s != 0) handle_error_en (s, "pthread_attr_getinheritsched"); printf ("%sInherit scheduler = %s\n", prefix, (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" : (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" : "???"); s = pthread_attr_getschedpolicy (attr, &i); if (s != 0) handle_error_en (s, "pthread_attr_getschedpolicy"); printf ("%sScheduling policy = %s\n", prefix, (i == SCHED_OTHER) ? "SCHED_OTHER" : (i == SCHED_FIFO) ? "SCHED_FIFO" : (i == SCHED_RR) ? "SCHED_RR" : "???"); s = pthread_attr_getschedparam (attr, &sp); if (s != 0) handle_error_en (s, "pthread_attr_getschedparam"); printf ("%sScheduling priority = %d\n", prefix, sp.sched_priority); s = pthread_attr_getguardsize (attr, &v); if (s != 0) handle_error_en (s, "pthread_attr_getguardsize"); printf ("%sGuard size = %ld bytes\n", prefix, v); s = pthread_attr_getstack (attr, &stkaddr, &v); if (s != 0) handle_error_en (s, "pthread_attr_getstack"); printf ("%sStack address = %p\n", prefix, stkaddr); printf ("%sStack size = 0x%zx bytes\n", prefix, v); }
int main (int argc, char *argv[]) { pthread_t thr; pthread_attr_t attr; pthread_attr_t *attrp; /* NULL or &attr */ int s; attrp = NULL; /* If a command-line argument was supplied, use it to set the stack-size attribute and set a few other thread attributes, and set attrp pointing to thread attributes object */ if (argc > 1) { int stack_size; void *sp; attrp = &attr; s = pthread_attr_init (&attr); if (s != 0) handle_error_en (s, "pthread_attr_init"); s = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); if (s != 0) handle_error_en (s, "pthread_attr_setdetachstate"); s = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED); if (s != 0) handle_error_en (s, "pthread_attr_setinheritsched"); stack_size = strtoul (argv[1], NULL, 0); s = posix_memalign (&sp, sysconf (_SC_PAGESIZE), stack_size); if (s != 0) handle_error_en (s, "posix_memalign"); printf ("posix_memalign() allocated at %p\n", sp); s = pthread_attr_setstack (&attr, sp, stack_size); if (s != 0) handle_error_en (s, "pthread_attr_setstack"); } s = pthread_create (&thr, attrp, &thread_start, NULL); if (s != 0) handle_error_en (s, "pthread_create"); if (attrp != NULL) { s = pthread_attr_destroy (attrp); if (s != 0) handle_error_en (s, "pthread_attr_destroy"); } pause (); /* Terminates when other thread calls exit() */ }
int main(void) { pthread_t thr; void *res; int s; s = pthread_create(&thr, NULL, &thread_func, NULL); if (s != 0) handle_error_en(s, "pthread_create"); char *p; size_t i = 1000; p = 0; while (i < 4000) { p = realloc(p, i); if (p) p[0] = 42; else { puts("no"); exit(1); } i++; } printf("main(): sending cancellation request\n"); s = pthread_cancel(thr); if (s != 0) handle_error_en(s, "pthread_cancel"); s = pthread_join(thr, &res); if (s != 0) handle_error_en(s, "pthread_join"); if (res == PTHREAD_CANCELED) printf("main(): thread was canceled\n"); else printf("main(): thread wasn't canceled (shouldn't happen!)\n"); show_alloc_mem(); exit(EXIT_SUCCESS); }
static void changeSelfThreadParam(int policy,int priority) { struct sched_param param; param.sched_priority=priority; /* scheduling parameters of target thread */ int s=pthread_setschedparam(pthread_self(), policy, ¶m); #ifdef CHECK_RST if (s != 0) handle_error_en(s, "pthread_setschedparam"); s=pthread_getschedparam(pthread_self(), &policy, ¶m); if (s != 0) handle_error_en(s, "pthread_getschedparam"); if(policy==SCHED_FIFO) printf("[Bruce] SCHED_FIFO\n"); else if(policy==SCHED_OTHER) printf("[Bruce] SCHED_OTHER\n"); #endif }
void WaitForTerminate () { /* Destroy the thread attributes object, since it is no longer needed */ int s = pthread_attr_destroy(&m_attr); if (s != 0) handle_error_en(s, "pthread_attr_destroy"); /* Now join with each thread, and display its returned value */ for (size_t t = 0; t < N; t++) { void * res = 0; s = pthread_join(m_tinfo[t].thread_id, &res); if (s != 0) handle_error_en(s, "pthread_join"); printf("Joined with thread %d; returned value was %s\n", m_tinfo[t].thread_num, (char *) res); free(res); /* Free memory allocated by thread */ } }
static void *sig_thread(void *arg) { sigset_t *set = (sigset_t *)arg; int s,sig; for(;;) { s = sigwait(set,&sig); if(s != 0) handle_error_en(s,"pthread_sigmask"); printf("signal handlinhg thread got signal %d\n",sig); } }
int main(int argc, char *argv[]) { int s; pthread_t thr; pthread_attr_t attr; s = pthread_create(&thr, NULL, &thread_start, NULL); if (s != 0) handle_error_en(s, "pthread_create"); pause(); /* Terminates when other thread calls exit() */ }
static void display_stack_related_attributes(pthread_attr_t *attr, char *prefix) { int s; size_t stack_size, guard_size; void *stack_addr; s = pthread_attr_getguardsize(attr, &guard_size); if (s != 0) handle_error_en(s, "pthread_attr_getguardsize"); printf("%sGuard size = %lu bytes\n", prefix, guard_size); s = pthread_attr_getstack(attr, &stack_addr, &stack_size); if (s != 0) handle_error_en(s, "pthread_attr_getstack"); printf("%sStack address = %p", prefix, stack_addr); if (stack_size > 0) printf(" (EOS = %p)", (char *) stack_addr + stack_size); printf("\n"); printf("%sStack size = 0x%lx (%lu) bytes\n", prefix, stack_size, stack_size); }
/* Creates a thread, and start the sqlFunction() in it with params parameter Returns with the thread descriptor */ pthread_t* createBackgroundQuery(SQLThreadParams *params) { pthread_t *sqlthread; int s; sqlthread = (pthread_t *)malloc(sizeof(pthread_t)); // Creating and starting the thread if ((s = pthread_create(sqlthread, NULL, sqlFunction, (void *) params)) != 0) { handle_error_en(s, "pthread_create"); } return sqlthread; }
static void getCurrentThreadPriority(int *p_policy, int *p_priority) { struct sched_param param; //int policy; int s=pthread_getschedparam(pthread_self(), p_policy, ¶m); *p_priority=param.sched_priority; #ifdef CHECK_RST if (s != 0) handle_error_en(s, "pthread_getschedparam"); else printf("[Bruce] policy:%s priority:%d\n",(*p_policy==SCHED_OTHER)?"SCHED_OTHER":"SCHED_FIFO",*p_priority); #endif }
/** * Function main * ------------- */ int main(int argc, char *argv[]) { pthread_mutex_t tmutex; int num_agents = 10; thread_info tinfo[10]; int num_tickets = 150; int agent; int rc; void *res; int sum = 0; rc = pthread_mutex_init(&tmutex, NULL); if (rc != 0) handle_error_en(rc, "pthread_mutex_init"); for (agent = 0; agent < num_agents; agent++) { tinfo[agent].tmutex = &tmutex; tinfo[agent].agent_id = agent; tinfo[agent].num_tickets_to_sell = &num_tickets; tinfo[agent].num_tickets_sold = 0; rc = pthread_create(&tinfo[agent].thread_id, NULL, &sell_tickets, &tinfo[agent]); if (rc != 0) handle_error_en(rc, "pthread_create"); } for (agent = 0; agent < num_agents; agent++) { rc = pthread_join(tinfo[agent].thread_id, &res); if (rc != 0) handle_error_en(rc, "pthread_join"); printf("Agent %d konci s rc %d. Prodal %d jizdenek.\n", agent, rc, tinfo[agent].num_tickets_sold); sum += tinfo[agent].num_tickets_sold; } printf("celkem prodano %d jizdenek.\n", sum); rc = pthread_mutex_destroy(&tmutex); if (rc != 0) handle_error_en(rc, "pthread_mutex_destroy"); return 0; }
static void display_stack_attr (pthread_attr_t *attr, char *prefix) { int s; size_t stack_size, guard_size, stack_size_check; void *stack_addr; s = pthread_attr_getguardsize(attr, &guard_size); if (s != 0) handle_error_en(s, "pthread_attr_getguardsize"); printf("%sGuard size = %d bytes\n", prefix, guard_size); s = pthread_attr_getstack(attr, &stack_addr, &stack_size); if (s != 0) handle_error_en(s, "pthread_attr_getstack"); printf("%sStack address = %p", prefix, stack_addr); if (stack_size > 0) printf(" (EOS = %p)", (char *) stack_addr + stack_size); printf("\n"); s = pthread_attr_getstacksize (attr, &stack_size_check); if (s != 0) handle_error_en(s, "pthread_attr_getstacksize"); assert (stack_size_check == stack_size); printf("%sStack size = 0x%x (%d) bytes\n", prefix, stack_size, stack_size); }