Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <string.h>
- #include <sched.h>
- #define LINE_SIZE (1 << 12)
- static char target;
- void *routine(void *arg) {
- char *line = (char*)arg;
- int cnt = 0;
- for (int i = 0; line[i]; i++) {
- if (line[i] == target) {
- cnt++;
- }
- }
- printf("cnt=%d\n", cnt);
- return NULL;
- }
- int main(int argc, char *argv[]) {
- if (argc != 2) {
- fprintf(stderr, "WRONG USAGE!\n");
- exit(1);
- }
- target = argv[1][0];
- cpu_set_t cpuset;
- CPU_ZERO(&cpuset);
- CPU_SET(0, &cpuset);
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
- char line[LINE_SIZE];
- while (fgets(line, LINE_SIZE, stdin)) {
- pthread_t thread;
- int r = 0;
- if ((r = pthread_create(&thread, &attr, routine, line)) != 0) {
- fprintf(stderr, "pthread_create: %s\n", strerror(r));
- exit(1);
- }
- if ((r = pthread_join(thread, NULL)) != 0) {
- fprintf(stderr, "pthread_join: %s\n", strerror(r));
- exit(1);
- }
- }
- pthread_attr_destroy(&attr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement