int main(int argc, char *argv[]) { pid_t process_id; long int long_int; char *end_ptr; if(argc != 2) { printf("\nInvalid input.\nFormat is: a.out <sequence size>\n\n"); return -1; } long_int = strtol(argv[1],&end_ptr,10); if((!((atoi(argv[1]) >= 0) && (atoi(argv[1]) <= (MAX_SEQUENCE - 1)))) || (end_ptr == argv[1]) || (*end_ptr != '\0')) { printf("\nPlease enter Fibonacci Sequence size between 0 - 9.\n\n"); return -1; } gfib_series.sequence_size = atoi(argv[1]); process_id = fork(); if(process_id < 0) { /* Error during child process creation. */ fprintf(stderr, "Fork failed"); return -1; } else if(process_id == 0) { /* Child process */ computeFibonacci(&gfib_series); printf("\nOutput from child process:"); printFibonacci(&gfib_series); } else { /* Parent process */ /* Wait for the child to complete. */ wait(NULL); printf("\nOutput from parent process:"); printFibonacci(&gfib_series); } return 0; }
void printFibonacci(int n) { static long int first=0,second=1,sum; if(n>0) { sum = first + second; first = second; second = sum; printf("%ld ",sum); printFibonacci(n-1); } }
/* Número máximo de valores */ void printFibonacci(int max_index, int atual) { if (atual <= max_index) { if (atual == max_index) printf("%d.\n", Fibonacci(atual)); else printf("%d, ", Fibonacci(atual)); printFibonacci(max_index, atual + 1); } }
int main() { int op, x; printf("Pretende usar numero maximo de valores (1) ou valor maximo (2)? "); scanf("%d", &op); switch (op) { case 1: printf("Introduza um numero maximo de valores: "); scanf("%d", &x); printf("Sequencia: "); printFibonacci(x - 1, 0); /*Até um máximo X valores da sequência, por outras palavras, F(0),...,F(x-1)*/ break; case 2: printf("Introduza o valor maximo: "); scanf("%d", &x); printf("Sequencia: "); printFibonacci_val(x, 0); break; default: printf("Não introduziu um valor válido.\n"); exit(0); } }
int main(int argc,char*argv[]) { if( argc < 4 ) { printf("Insufficient arguments.\n"); printf("Syntax:<filename> <No of elements to sort> <The range for fibonacci series> <No of iterations in the Buffon needle problem simulation>\n"); exit(0); } if(argc>4) { printf("Too many arguments\n"); printf("Syntax:<filename> <No of elements to sort> <The range for fibonacci series> <No of iterations in the Buffon needle problem simulation>\n"); exit(0); } m=atoi(argv[1]); n=atoi(argv[2]); r=atoi(argv[3]); pid_t pid1,pid2,pid3,pid,k; pid1=fork(); sleep(1); if(pid1==0)//In the first child process { pid1=getpid(); printf("\nThe process id of first child process is:%d\n",pid1); int i,arr[50],j; printf("The generated random numbers are:\n"); srand(time(NULL)); for(i=0;i<m;i++) { arr[i]=rand()%100; printf("%d\n",arr[i]); } heapsort(arr,m); printf("\nThe Sorted Elements Are:\n"); for(i=0;i<m;i++) printf("%d\t",arr[i]); printf("\n"); exit(0); } else//In the parent process { pid2=fork(); sleep(1); } if(pid2==0)//In the second child process { pid2=getpid(); printf("\nThe process id of the second process is:%d\n",pid2); long int i=0,j=1,f; printf("The range is:%d\n",n); printf("Fibonacci Series: \n"); printf("%d %d ",0,1); printFibonacci(n); printf("\n"); return 0; exit(0); } else//In the parent process { pid3=fork(); sleep(1); } if(pid3==0)//In the third child process { pid3=getpid(); printf("\nThe process id of the third process is:%d\n",pid3); int i,t=0; printf("The number of iterations is:%d\n",r); srand(time(NULL)); for(i=1;i<=r;i++) { //printf("Iteration:%d\n",i); iterate(&t); } j=(float)t/r; p=(2/M_PI)*(L/G); printf("Exact probability is:%lf.\n",p); printf("An approximation of the exact probability is:%lf.\n\n",j); exit(0); } else//In the parent process { //printf("Waiting for child process 1 to terminate..\n"); pid1=waitpid(pid1,&s,WNOHANG); if(WIFEXITED(s)) printf("The exit status of the first child process:%d",WEXITSTATUS(s)); sleep(1); //printf("\nWaiting for child process 2 to terminate..\n"); pid2=waitpid(pid2,&s,WNOHANG); if(WIFEXITED(s)) printf("\nThe exit status of the second child process:%d\n",WEXITSTATUS(s)); sleep(1); //printf("\nWaiting for child process 3 to terminate..\n"); pid3=waitpid(pid3,&s,WNOHANG); if(WIFEXITED(s)) printf("The exit status of the third child process:%d\n",WEXITSTATUS(s)); } }