top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How a timer utility can be implemented in a system ?

0 votes
305 views

In interviews, it very common question. What things are considered to implement a timer ?

posted Sep 22, 2016 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

TIMER-
The basic idea of a timer is that they allow tasks to be run at sometime in the future. When their time arrives, they are scheduled to be run. The responsibility of actually running them is then turned over to someone else, such as the scheduler. In order to communicate with the scheduler, we'll set up a common data structure called a timer.
I've also included a few other miscellaneous definitions that will be needed later on. For instance, the TIME typedef is used to declare all relative time variables. You can complete this definition based on what your needs are.

By implementing the timers as a separate piece of software, we can reduce the complexity of the scheduler. Some people like this kind of modularization, and some don't. Similarly some operating systems do this, and some don't. I like it. It makes the code easier to write, to read, and to correct (oops).

include <stdio.h>

define TRUE 1

define FALSE 0

define MAX_TIMERS ... /* number of timers */

typedef ... TIME; /* how time is actually stored */

define VERY_LONG_TIME ... /* longest time possible */

struct timer {
int inuse; /* TRUE if in use */
TIME time; /* relative time to wait */
char *event; /* set to TRUE at timeout */
} timers[MAX_TIMERS]; /* set of timers
SOURCE-http://www.kohala.com/start/libes.timers.txt

answer Oct 2, 2016 by Devendra Bohre
Similar Questions
+4 votes

Lets assume I have a system with RAM of 1GB. and virtual memory is 500MB. That brings to 1.5GB i.e. 1500 MBytes.

I have read somewhere that when I process is created stack of 8MB is associated to that process. So, assuming that any of the process is not allocating any dynamic memory or anything, then does it mean that, Maximum number of process that i can create is 1500/8 and i.e. 187 Process.

Please clarify my understanding,

+1 vote

I can write a C program with strings and to pass it to the system to avoid the ugly bash syntax?
My Program

#include <stdlib.h>
#include <stdio.h>

static char command_name[2040] = {0};
int main(int argc, char** argv) {

    if ( argc != 2 ) {
        fprintf(stderr, "Provide command line argument for virtual image \n");
        exit(1);
    } else {
            sprintf(command_name, "qemu-kvm -m 1024 %s -netdev user,id=user.0 -device rtl8139,netdev=user.0",
                argv[1]);

    }

    return system(command_name);
}

Any suggestions are welcome?

0 votes

There are two files.

    #include <stdio.h>
    void fun1(int i)
    {
            int a;
            printf("Enter the number\n");
            scanf("%d",&a);
            if(a==i)
                    printf("Equal\n");
            else
                    printf("Equal\n");
    }

    int main()
    {
        fun1(18);
    }

/* INPUT  */    
    18

What will be in file in OUTPUT file both cases.
Case-1:

./a.out < INPUT  > OUTPUT 

Case-2: Entered value is "18" from key board.

./a.out > output
+3 votes

Please understand the scenario :

I have an embedded system, on which i do telnet and then i starts i run an application using ./binary_name &.
Now if i close the terminal, and if i do telnet from new terminal then i can see above process is still running.

Now, To check this, i have written a sample program,

#include<stdio.h>
main()
{
    while(1);
}

I just compiled it and i ran it on my linux PC using ./a.out &.
Then i just closed the terminal and from new terminal i checked using ps -elf command this process was killed.

My question is why different behavior for both process. i started both the process in background. Then why?

Any suggestion?

Thanks in advance

...