int main() { pthread_t thread; initialize_job_queue(); pthread_create(&thread, NULL, &thread_function, NULL); //pthread_join(thread, NULL); enqueue_job(); while(1) { enqueue_job(); } return 0; }
int producer(void* arg) { struct job *new_job; unsigned int i; allow_signal(SIGKILL); while (!kthread_should_stop()) { new_job=(struct job*)kmalloc(sizeof(struct job), GFP_KERNEL); if (new_job == NULL) { printk("Unable to allocate the job structure\n"); return -1; } enqueue_job(new_job); get_random_bytes(&i, sizeof(i)); ssleep(i % 5); if (signal_pending(current)) break; } printk(KERN_INFO "Producer Thread Stopping\n"); thread_prod = NULL; do_exit(0); }
int main() { const int SIZE = 3; pthread_t threads[SIZE]; int i; /* Initialize job queue. */ struct job *job_queue = NULL; /* Initialize semaphore for the job queue. */ sem_init (&job_count, 0, 0); #if 0 /* Create new jobs for a job queue. */ for (i = 0; i < 50; i++) { struct job *new_job = malloc (sizeof (struct job)); new_job->id = i; enqueue_job (&job_queue, new_job); } #endif for (i = 0; i < SIZE; i++) { pthread_create (&(threads[i]), NULL, thread_func, &job_queue); } /* Create new jobs for a job queue. */ for (i = 0; i < 10; i++) { struct job *new_job = malloc (sizeof (struct job)); new_job->id = i; enqueue_job (&job_queue, new_job); /* Use sleep to make enqueue slower than dequeue(thread processing).*/ sleep(1); } for (i = 0; i < SIZE; i++) { pthread_join (threads[i], NULL); } sem_destroy (&job_count); return 0; }
int main() { int job_size = 1000; int thread_num = 5; int i; pthread_t thread_id[thread_num]; for (i = 0; i < thread_num; i++) pthread_create(&thread_id[i], NULL, &thread_function, NULL); struct job* jobs; for (i = 1; i < job_size; i++) { enqueue_job(jobs, i); } for (i = 0; i < thread_num; i++) pthread_join(thread_id[i], NULL); return 0; }
static int __init init_consprod(void) { int i; char buff[20]; struct job *new_job; sema_init(&my_sem, 0); for (i = 0; i < 6; i++) { new_job=(struct job*)kmalloc(sizeof(struct job), GFP_KERNEL); if (new_job == NULL) { printk("Failed to allocate the memory for job\n"); return -1; } enqueue_job(new_job); } for (i = 0; i < 2; i++) { thread_id[i] = i; sprintf(buff, "thread_con%d", i); thread_cons[i] = kthread_run(thread_fn, &thread_id[i], buff); if (thread_cons[i]) printk(KERN_INFO "Thread %d created\n", i); else { printk(KERN_INFO "Thread creation failed\n"); return -1; } } thread_prod = kthread_run(producer, NULL, "myprod"); if (thread_prod) printk(KERN_INFO "Producer Thread created\n"); else { printk(KERN_INFO "Producer thread creation failed\n"); return -1; } return 0; }
void async_start_stop_task::set_should_work(bool s) { should_work = s; enqueue_job(); }