How to write a C Program for statistically calculating pi using a specific scheduling policy in C Programming Language ?
- /* Description:
- * This file contains a simple program for statistically
- * calculating pi using a specific scheduling policy.
- */
- /* Local Includes */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <math.h>
- #include <errno.h>
- #include <sched.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sched.h>
- #define DEFAULT_ITERATIONS 1000000
- #define RADIUS (RAND_MAX / 2)
- inline double dist(double x0, double y0, double x1, double y1){
- return sqrt(pow((x1-x0),2) + pow((y1-y0),2));
- }
- inline double zeroDist(double x, double y){
- return dist(0, 0, x, y);
- }
- int main(int argc, char* argv[]){
- long i;
- long iterations;
- struct sched_param param;
- int policy, numProcesses;
- double x, y;
- double inCircle = 0.0;
- double inSquare = 0.0;
- double pCircle = 0.0;
- double piCalc = 0.0;
- pid_t id;
- /* Process program arguments to select iterations and policy */
- /* Set iterations if supplied or set to DEFAULT_ITERATIONS*/
- if(argc > 2){
- iterations = atol(argv[1]);
- printf("iterations is: %d\n", iterations);
- if(iterations < 1){
- fprintf(stderr, "Bad iterations value\n");
- exit(EXIT_FAILURE);
- }
- }
- else{
- iterations = DEFAULT_ITERATIONS;
- }
- /* Set default policy if not supplied */
- if(argc < 3){
- policy = SCHED_OTHER;
- }
- else{
- if(!strcmp(argv[2], "SCHED_OTHER")){
- policy = SCHED_OTHER;
- }
- else if(!strcmp(argv[2], "SCHED_FIFO")){
- policy = SCHED_FIFO;
- }
- else if(!strcmp(argv[2], "SCHED_RR")){
- policy = SCHED_RR;
- }
- else{
- fprintf(stderr, "Unhandeled scheduling policy\n");
- exit(EXIT_FAILURE);
- }
- }
- // Set number of processes if not supplied
- if(argc < 4){
- numProcesses = 1; //set numProcesses = 1 if not set
- }
- else{
- numProcesses = atol(argv[3]);
- }
- /* Set policy if supplied */
- /*if(argc > 2){
- if(!strcmp(argv[2], "SCHED_OTHER")){
- policy = SCHED_OTHER;
- }
- else if(!strcmp(argv[2], "SCHED_FIFO")){
- policy = SCHED_FIFO;
- }
- else if(!strcmp(argv[2], "SCHED_RR")){
- policy = SCHED_RR;
- }
- else{
- fprintf(stderr, "Unhandeled scheduling policy\n");
- exit(EXIT_FAILURE);
- }
- }*/
- //set number of processes to be forke if supplied
- /*if(argc > 3){
- numProcesses = atol(argv[3]);
- }*/
- /* Set process to max priority for given scheduler */
- param.sched_priority = sched_get_priority_max(policy);
- /* Set new scheduler policy */
- fprintf(stdout, "Current Scheduling Policy: %d\n", sched_getscheduler(0));
- fprintf(stdout, "Setting Scheduling Policy to: %d\n", policy);
- printf("sched_setscheduler(0, policy, ¶m): %d\n", sched_setscheduler(id, policy, ¶m));
- if(sched_setscheduler(id, policy, ¶m)){
- perror("Error setting scheduler policy");
- exit(EXIT_FAILURE);
- }
- fprintf(stdout, "New Scheduling Policy: %d\n", sched_getscheduler(0));
- /* Fork N children */
- for (i = 0; i < numProcesses; i++){
- id = fork();
- if(id == 0){ //child process
- printf("Child process %d created\n", id);
- /* Calculate pi using statistical methode across all iterations*/
- for(i=0; i<iterations; i++){
- x = (random() % (RADIUS * 2)) - RADIUS;
- y = (random() % (RADIUS * 2)) - RADIUS;
- if(zeroDist(x,y) < RADIUS){
- inCircle++;
- }
- inSquare++;
- }
- /* Finish calculation */
- pCircle = inCircle/inSquare;
- piCalc = pCircle * 4.0;
- /* Print result */
- fprintf(stdout, "pi = %f\n", piCalc);
- break;
- }
- else if(id > 0){ //parent
- printf("Parent is %d\n", id);
- int status = 0;
- waitpid(id, &status, 0);
- }
- else{ //error
- printf("Error. pid %d is negative\n", id );
- }
- }
- return 0;
- }
Learn More :
Calculate
- C Programm zur Berechnung von Umfang und Flächeninhalt verschiedener geomatrischer Figuren
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program to Calculate GCD using recursion
- C Program To Calculate First Numbers Using Recursion
- C Program to Calculation of One Number Raised to Another
- C Program to calculate TT and Recharge Amount
- C Program to Calculate 2 numbers with 1 operation
- Un-sortiertes Array and Sortiertes Array
- C Program Calculation of Simple Interest
- C Program to Calculation of Total and Percentage
- C Program for calculation of Gross Salary
- C Program to calculate Area, Perimeter of Rectangle; Area, Circumference of Circle.
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program Number of Judge and Score From Each Judge
- C Program to Calculate the mathematical expression for the first n numbers
- C Program to Calculate the price of movie tickets
- C Program to calculate sum of two m*n matrices & store the result in 3 matrix
- C Program to calculate the sum of elements of upper triangle of a n*n matrix using DMA
- C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers
- Calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation
- Calculate sum of non-diagonal element in m*n matrix C Program
- Accept n number from user, store these number into an array and calculate the average of n number
- Calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation
Scheduling
Policy