Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #define TRUNC_AT(str, reject) {\
- size_t s_len = strcspn(str, reject); \
- str[s_len] = '\0';\
- }
- #define TENTRY(x) {cmd[IDX_##x], IDX_##x}
- #define BUF_SIZE (1024*1024)
- #define IDX_SKB 0
- #define IDX_I3STATUS 1
- char buf[2][BUF_SIZE] = {"", ""};
- pthread_mutex_t m;
- const char *cmd[] = {
- "/home/dmitriy/bin/skb",
- "/usr/bin/python3 /home/dmitriy/bin/config.py"
- };
- struct tinfo {
- const char *cmd;
- int idx;
- };
- int first = 3;
- void *callback(void *arg){
- struct tinfo *ti = (struct tinfo *) arg;
- FILE *fd = popen(ti->cmd, "r");
- while (fgets(buf[ti->idx], BUF_SIZE, fd)){
- pthread_mutex_lock(&m);
- if (first) {
- if (ti->idx == IDX_I3STATUS) {
- printf("%s", buf[ti->idx]);
- fflush(stdout);
- first--;
- }
- } else {
- TRUNC_AT(buf[IDX_I3STATUS], "]");
- TRUNC_AT(buf[IDX_SKB], "\n");
- printf("%s,{\"name\": \"lang\", \"full_text\": \"%s\"}]\n",
- buf[IDX_I3STATUS], buf[IDX_SKB]);
- fflush(stdout);
- }
- pthread_mutex_unlock(&m);
- }
- return NULL;
- }
- int main(){
- pthread_t t[2];
- struct tinfo ti[2] = {
- TENTRY(SKB),
- TENTRY(I3STATUS)
- };
- pthread_mutex_init(&m, NULL);
- int i;
- for (i = 0; i < 2; i++){
- pthread_create(&t[i], NULL, callback, (void *) &ti[i]);
- }
- for (i = 0; i < 2; i++){
- pthread_join(t[i], NULL);
- }
- pthread_mutex_destroy(&m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment