May 5, 2012

C program to implement SJF algorithm.

Write a C program to implement SJF algorithm.
Shortest Job First(SJF) is the CPU scheduling algorithm. In SJF, if the CPU is available it allocates the process which has smallest burst time, if the two process are same burst time it uses FCFS algorithm. Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>

void main()
{
    int i,j,n,brust_time[10],start_time[10],end_time[10],wait_time[10],temp,tot;
    float avg;
    clrscr();
    printf("Enter the No. of jobs:\n\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("\n \n Enter %d process burst time:\n",i);
        scanf("%d",&brust_time[i]);
    }
    
    for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if(brust_time[i]>brust_time[j])
            {
                temp=brust_time[i];
                brust_time[i]=brust_time[j];
                brust_time[j]=temp;
            }
        }
        
        if(i==1)
        {
            start_time[1]=0;
            end_time[1]=brust_time[1];
            wait_time[1]=0;
        }
        
        else
        {
            start_time[i]=end_time[i-1];
            end_time[i]=start_time[i]+brust_time[i];
            wait_time[i]=start_time[i];
        }
    }
    printf("\n\n BURST TIME \t STARTING TIME \t END TIME \t   WAIT TIME\n");
    printf("\n ********************************************************\n");
    for(i=1;i<=n;i++)
    {
        printf("\n %5d %15d %15d %15d",brust_time[i],start_time[i],end_time[i],wait_time[i]);
    }
    printf("\n ********************************************************\n");
    for(i=1,tot=0;i<=n;i++)
    tot+=wait_time[i];
    avg=(float)tot/n;
    printf("\n\n\n AVERAGE WAITING TIME=%f",avg);
    for(i=1,tot=0;i<=n;i++)
    tot+=end_time[i];
    avg=(float)tot/n;
    printf("\n\n AVERAGE TURNAROUND TIME=%f",avg);
    for(i=1,tot=0;i<=n;i++)
    tot+=start_time[i];
    avg=(float)tot/n;
    printf("\n\n AVERAGE RESPONSE TIME=%f\n\n",avg);
    getch();
}
Read more Similar C Programs
C Basic

Search Algorithms.
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

May 4, 2012

K & R C Chapter 5 Exercise Solutions.

We have already provided solutions to all the exercises in the book "C Programming Language (2nd Edition)" popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 5 of the book for easy reference.

Chapter 5: Pointers and Arrays

  1. Exercise 5-1. As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input.
    Solution to Exercise 5-1.

  2. Exercise 5-2.Write getfloat , the floating-point analog of getint . What type does getfloat return as its function value?
    Solution to Exercise 5-2.

  3. Exercise 5-3.GWrite a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies the string t to the end of s .
    Solution to Exercise 5-3.

  4. Exercise 5-4. Write the function strend(s,t) , which returns 1 if the string t occurs at the end of the string s , and zero otherwise.
    Solution to Exercise 5-4.

  5. Exercise 5-5. Write versions of the library functions strncpy , strncat , and strncmp , which operate on at most the first n characters of their argument strings. For example, strncpy(s,t,n) copies at most n characters of t to s . Full descriptions are in Appendix B.
    Solution to Exercise 5-5.

  6. Exercise 5-6. Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline (Chapters 1 and 4), atoi , itoa , and their variants (Chapters 2, 3, and 4), reverse (Chapter 3), and strindex and getop (Chapter 4).
    Solution to Exercise 5-6.
  7. Exercise 5-7. Rewrite readlines to store lines in an array supplied by main , rather than calling alloc to maintain storage. How much faster is the program?
    Solution to Exercise 5-7.

  8. Exercise 5-8.There is no error-checking in day_of_year or month_day. Remedy this defect.
    Solution to Exercise 5-8.

  9. Exercise 5-9.Rewrite the routines day_of_year and month_day with pointers instead of indexing.
    Solution to Exercise 5-9.

  10. Exercise 5-10. Write the program expr , which evaluates a reverse Polish expression from the command line, where each operator or operand is a separate argument. For example, expr 2 3 4 + * evaluates 2 X (3+4).
    Solution to Exercise 5-10.

  11. Exercise 5-11. Modify the programs entab and detab (written as exercises in Chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments.
    Solution to Exercise 5-11.

  12. Exercise 5-12. Extend entab and detab to accept the shorthand entab -m +n to mean tab stops every n columns, starting at column m . Choose convenient (for the user) default behavior.
    Solution to Exercise 5-12.

  13. Exercise 5-13. Write the program tail, which prints the last n lines of its input. By default, n is 10, say, but it can be changed by an optional argument, so that tail -n prints the last n lines. The program should behave rationally no matter how unreasonable the input or the value of n. Write the program so it makes the best use of available storage; lines should be stored as in the sorting program of Section 5.6, not in a two-dimensional array of fixed size.
    Solution to Exercise 5-13.

  14. Exercise 5-14. Modify the sort program to handle a -r flag, which indicates sorting in reverse (decreasing) order. Be sure that -r works with -n.
    Solution to Exercise 5-14.
  15. Exercise 5-15.Add the option -f to fold upper and lower case together, so that case distinctions are not made during sorting; for example, a and A compare equal.
    Solution to Exercise 5-15.

  16. Exercise 5-16.Add the -d ("directory order") option, which makes comparisons only on letters, numbers and blanks. Make sure it works in conjunction with -f .
    Solution to Exercise 5-16.

  17. Exercise 5-17. Add a field-handling capability, so sorting may be done on fields within lines, each field sorted according to an independent set of options. (The index for this book was sorted with -df for the index category and -n for the page numbers.)
    Solution to Exercise 5-10.

  18. Exercise 5-18. Make dcl recover from input errors.
    Solution to Exercise 5-18.

  19. Exercise 5-19. Modify undcl so that it does not add redundant parentheses to declarations.
    Solution to Exercise 5-19.

  20. Exercise 5-20. Expand dcl to handle declarations with function argument types, qualifiers like const , and so on.
    Solution to Exercise 5-20.
You can purchase the book from here or here.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

C Program to implement FCFS algorithm.

Write a C Program to implement FCFS algorithm.
First Come First Served(FCFS) algorithm is the CPU scheduling algorithm. In FCFS process is served when they are arrived in order, i.e First In First Out(FIFO). FCFS is the simple scheduling algorithm, but it takes typically long/varying waiting time.
Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/
#include<stdio.h>
#include<conio.h>
main()
{
    int n,i,j,sum=0;
    int arrv[10],ser[10],start[10];
    int finish[10],wait[10],turn[10];
    float avgturn=0.0,avgwait=0.0;
    start[0]=0;
    clrscr();
    printf("Enter the number of processes:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter the arriwal and service time of %d process:",i+1);
        scanf("%d%d",&arrv[i],&ser[i]);
    }
    for(i=0;i<n;i++)
    {
        sum=0;
        for(j=0;j<i;j++)
        sum=sum+ser[j];
        start[i]=sum;
    }
    for(i=0;i<n;i++)
    {
        finish[i]=ser[i]+start[i];
        wait[i]=start[i];
        turn[i]=ser[i]+wait[i];
    }
    for(i=0;i<n;i++)
    {
        avgwait+=wait[i];
        avgturn+=turn[i];
    }
    avgwait/=n;
    avgturn/=n;
    printf("\narraival service Start Finish Wait Turn\n");
    for(i=0;i<n;i++)
    printf("%d\t%d\t%d\t%d\t%d\t%d\n",arrv[i],ser[i],start[i],
    finish[i],wait[i],turn[i]);
    printf("\nAverage waiting time=%f",avgwait);
    printf("\nAverage turn around time=%f",avgturn);
    getch();
}
Read more Similar C Programs
C Basic

Search Algorithms.
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

May 3, 2012

K & R C Chapter 4 Exercise Solutions.

We have already provided solutions to all the exercises in the book "C Programming Language (2nd Edition)" popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 4 of the book for easy reference.

Chapter 4: Functions and Program Structure

  1. Exercise 4-1. Write the function strrindex(s,t) , which returns the position of the rightmost occurrence of t in s , or -1 if there is none.
    Solution to Exercise 4-1.

  2. Exercise 4-2.Extend atof to handle scientific notation of the form 123.45e-6 where a floating-point number may be followed by e or E and an optionally signed exponent.
    Solution to Exercise 4-2.

  3. Exercise 4-3.Given the basic framework, it's straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.
    Solution to Exercise 4-3.

  4. Exercise 4-4. Add commands to print the top element of the stack without popping, to duplicate it, and to swap the top two elements. Add a command to clear the stack.
    Solution to Exercise 4-4.

  5. Exercise 4-5. Add access to library functions like sin , exp , and pow . See in Appendix B, Section 4.
    Solution to Exercise 4-5.

  6. Exercise 4-6. Add commands for handling variables. (It's easy to provide twenty-six variables with single-letter names.) Add a variable for the most recently printed value.
    Solution to Exercise 4-6.
  7. Exercise 4-7. Write a routine ungets(s) that will push back an entire string onto the input. Should ungets know about buf and bufp , or should it just use ungetch ?
    Solution to Exercise 4-7.

  8. Exercise 4-8.Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.
    Solution to Exercise 4-8.

  9. Exercise 4-9.Our getch and ungetch do not handle a pushed-back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement your design
    Solution to Exercise 4-9.

  10. Exercise 4-10. An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. Revise the calculator to use this approach.
    Solution to Exercise 4-10.

  11. Exercise 4-11. Modify getop so that it doesn't need to use ungetch. Hint: use an internal static variable.
    Solution to Exercise 4-11.

  12. Exercise 4-12. Adapt the ideas of printd to write a recursive version of itoa ; that is, convert an integer into a string by calling a recursive routine.
    Solution to Exercise 4-12.

  13. Exercise 4-13. Write a recursive version of the function reverse(s) , which reverses the string s in place.
    Solution to Exercise 4-13.

  14. Exercise 4-14. Define a macro swap(t,x,y) that interchanges two arguments of type t . (Block structure will help.)
    Solution to Exercise 4-14.
You can purchase the book from here or here.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link

May 2, 2012

C Program to solve equations using Jordan elimination method.

Write a C Program to solve equations using Jordan elimination method.
Gauss-Jordan elimination method is used to solve the linear equations. In this method, We find the inverse matrix of the coefficients of equations by elementary row operations. Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
void solution( int a[][], int var );
int main()
{
    
    int a[ 20 ][ 20 ], var, i, j, k, l;
    clrcsr();
    printf( "\nEnter the number of variables:\n" );
    scanf( "%d", &var );
    
    for ( i = 0;i < var;i++ )
    {
        printf( "\nEnter the equation%d:\n", i + 1 );
        
        for ( j = 0;j < var;j++ )
        {
            printf( "Enter the coefficient of  x%d:\n", j + 1 );
            scanf( "%d", &a[ i ][ j ] );
        }
        
        printf( "\nEnter the constant:\n" );
        scanf( "%d", &a[ i ][ n ] );
    }
    
    solution( a, var );
    return 0;
}



void solution( int a[ 20 ][ 20 ], int var )
{
    int k, i, l, j;
    
    for ( k = 0;k < var;k++ )
    {
        for ( i = 0;i <= var;i++ )
        {
            l = a[ i ][ k ];
            
            for ( j = 0;j <= var;j++ )
            {
                if ( i != k )
                a[ i ][ j ] = a[ k ][ k ] * a[ i ][ j ] – l * a[ k ][ j ];
            }
        }
    }
    
    printf( "\nSolutions:" );
    
    for ( i = 0;i < n;i++ )
    {
        printf( "\nTHE VALUE OF x%d IS %f\n", i + 1, ( float ) a[ i ][ n ] / ( float ) a[ i ][ i ] );
    }
    
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


To browse more C Programs visit this link

Apr 30, 2012

C Program to lock file using semaphores.

Write a C Program to lock file using semaphores.
Using semaphores, We can control access to files, shared memory and other things. The basic functionality of a semaphore is that you can either set it, check it, or wait until it clears then set it ("test-n-set"). In C semaphores functions defined in the sys/sem library. Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

 

#include <stdio.h>
#include <sys/file.h>
#include <error.h>
#include <sys/sem.h>
#define MAXBUF 100
#define KEY 1216
#define SEQFILE "seq_file"
int semid,fd;
void my_lock(int);
void my_unlock(int);
union semnum
{
    int val;
    struct semid_ds *buf;
    short *array;
}arg;
int main()
{
    int child, i,n, pid, seqno;
    char buff[MAXBUF+1];
    pid=getpid();
    if((semid=semget(KEY, 1, IPC_CREAT | 0666))= = -1)
    {
        perror("semget");
        exit(1);
    }
    arg.val=1;
    if(semctl(semid,0,SETVAL,arg)<0)
    perror("semctl");
    if((fd=open(SEQFILE,2))<0)
    {
        perror("open");
        exit(1);
    }
    pid=getpid();
    for(i=0;i<2;i++)
    {
        my_lock(fd);
        lseek(fd,01,0);
        if((n=read(fd,buff,MAXBUF))<0)
        {
            perror("read");
            exit(1);
        }
        printf("pid:%d, Seq no:%d\n", pid, seqno);
        seqno++;
        sprintf(buff,"%d\n", seqno);
        n=strlen(buff);
        lseek(fd,01,0);
        if(write(fd,buff,n)!=n)
        {
            perror("write");
            exit(1);
        }
        sleep(1);
        my_unlock(fd);
    }
}
void my_lock(int fd)
{
    struct sembuff sbuf=(0, -1, 0);
    if(semop(semid, &sbuf, 1)= =0)
    printf("Locking: Resource…\n");
    else
    printf("Error in Lock\n");
}
void my_unlock(int fd)
{
    struct sembuff sbuf=(0, 1, 0);
    if(semop(semid, &sbuf, 1)= =0)
    printf("UnLocking: Resource…\n");
    else
    printf("Error in Unlock\n");
} 
Read more c programs
C Basic
C Strings
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



To browse more C Programs visit this link