Created
July 3, 2016 18:09
-
-
Save amanuel2/9d39c3978b13f8f8393e80858698ad48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include<stdlib.h> | |
#include<stdio.h> | |
//64 BITS -ALL 32 bit Arch | |
//32 BIT - UNIX 64 bit arch | |
//64 BIT - WINDOWS 64 bit arch | |
//long long sum = 0; | |
static enum turn | |
{ | |
PING, | |
PONG | |
}def; | |
struct threads_util | |
{ | |
pthread_t *t_id; | |
pthread_attr_t *attr; | |
void (*init_attr)(pthread_attr_t *); | |
}; | |
typedef struct | |
{ | |
long long limit; | |
long long anwser; | |
}runner_struct; | |
void init_attr_fn(pthread_attr_t *attr) | |
{ | |
pthread_attr_init(attr); | |
} | |
void* sum_runner(void* arg) | |
{ | |
runner_struct *arg_struct = | |
(runner_struct*) arg; | |
long long sum = 0; | |
for (long long i = 0; i <= arg_struct->limit; i++) { | |
sum += i; | |
} | |
arg_struct->anwser = sum; | |
printf("%lld \n",arg_struct->anwser); | |
pthread_exit(0); | |
} | |
void ping() | |
{ | |
puts("Ping"); | |
def = PONG; | |
} | |
void pong() | |
{ | |
puts("Pong"); | |
def = PING; | |
} | |
pthread_t * all_thread(pthread_t *t_id) | |
{ | |
t_id = malloc(sizeof(pthread_t)); | |
return t_id; | |
} | |
int main(int argc, char **argv) | |
{ | |
if(argc<2) | |
{ | |
puts("Usage ./objFile <num 1> <num 2> .. <num n>"); | |
exit(1); | |
} | |
int args = argc-1; | |
long long limit = atoll(argv[1]); | |
def = PING; | |
struct threads_util *threads = malloc(sizeof(struct threads_util) * args); | |
runner_struct rs[args]; | |
for (int i=0; i<args; i++) | |
threads[i].t_id = all_thread(threads[i].t_id); | |
for(int i=0; i<args; i++) | |
{ | |
rs[i].limit = atoll(argv[i + 1]); | |
threads[i].init_attr = init_attr_fn; | |
pthread_attr_init(&threads[i].attr); | |
pthread_create(&threads[i].t_id,&threads[i].attr,sum_runner,&rs[i]); | |
} | |
//Begin -Main Functions | |
for(int i=0; i<= 10; i++) | |
{ | |
if(def == PING) | |
ping(); | |
else if(def == PONG) | |
pong(); | |
else | |
puts("UNKOWN PI-PO"); | |
} | |
//End - Main Functions | |
for(int i=0; i<args; i++) | |
{ | |
pthread_join(threads[i].t_id,NULL); | |
// printf("%lld \n", rs[i].anwser); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment