Google Interview Question
Software Engineer / DevelopersTeam: youtube
Country: United States
Interview Type: Phone Interview
From "man alarm" -> " sleep(3) may be implemented using SIGALRM;"
#include <stdio.h>
#include <signal.h>
#include <limits.h>
#include <unistd.h>
void sighandler(int sig){
printf("Don't be alarmed!! \n");
}
void
sleep_alarm(unsigned int secs)
{
sigset_t new, susp;
signal(SIGALRM, sighandler);
sigemptyset(&new);
sigaddset(&new, SIGALRM);
sigprocmask(SIG_BLOCK, &new, &susp);
alarm(secs);
sigdelset(&susp, SIGALRM);
sigsuspend(&susp);
}
int main(int argc, char *argv[]){
int i;
for (i = 0; i <= ULLONG_MAX; i++){
sleep_alarm (100);
}
}
Set thread priority to lowest possible value (still settable in user space) and have a while loop that increments a large integer and exits when integer gets big enough. This doesn't hog the CPU from other running processes and never blocks. It's depedent on OS scheduling and system load, though.
Write a code to compute a very slow growing function like the Ackerman inverse. Since this function grows slowly it will take a long time before the variable's range is over when this is over we start over again. If we do this in nested loops then we could write a very long running program.
use all available mem on the machine to store a very very large integer.
Then, starting from 0, increment this integer by 1 in each loop iteration until
we get 0xfffffff ... ffffffff at the end
as an example, if you use 4 words to represent an int,
incrementing by one can be realized as follows:
int w[4];
w[0] += 1;
w[1] += (w[0] == 0);
w[2] += (w[1] == 0);
w[3] += (w[2] == 0);
We can make the parent process wait for child pid and start child suspended. After forking we can do sigalarm for parent and SIGCONT the child once we elapse the 'longtime'
// sigalarm proc
static int i;
i++;
if( i < longtime) {
alarm(5);
} else { // finish by waking up child
kill(pid, SIGCONT);
}
// main
pid = fork();
// child process
kill(getpid(), SIGSTOP);
return 0;
// parent process
// start timer
signal(SIGALRM, send_event);
alarm(5);
// wait for child; this will complete
// only when child is continued
waitpid(childpid, &status, 0);
Does this do it?
I thought of a blocking read on a resource in my process (so no system resources) with a timeout. Then I searched for some correct code... could have only generated psuedo code from memory. Don't know if I could get away with that... :-( my Google interview is coming up :0! eek!
From:
stackoverflow.com/questions/3711830/set-a-timeout-for-reading-stdin
// timeout structure passed into select
struct timeval tv;
// fd_set passed into select
fd_set fds;
// Set up the timeout. here we can wait for any number of hours.
tv.tv_sec = 60*60*24;
tv.tv_usec = 0;
// Zero out the fd_set - make sure it's pristine
FD_ZERO(&fds);
// Set the FD that we want to read
FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
// select takes the last file descriptor value + 1 in the fdset to check,
// the fdset for reads, writes, and errors. We are only passing in reads.
// the last parameter is the timeout. select will return if an FD is ready or
// the timeout has occurred
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
// return 0 if STDIN is not ready to be read.
return FD_ISSET(STDIN_FILENO, &fds);
}
I think solution should be some program using IO (but not one like scanf) or network connection or DB connection.
e.g.
1) program that will follow this,
a) write 1 byte at a time to a file upto XYZ MB or kb
b) delete that file
c) repeat steps a, b N number of times, depending on how long you want your prog to run.
2) Keep Sending XX bytes of data to some random ip address (not internal)
3) Assuming you have some RDB connection,
create small table in database and keep inserting and deleting single record in that table.
I think solution should be some program using IO (but not one like scanf) or network connection or DB connection.
e.g.
1) program that will follow this,
a) write 1 byte at a time to a file upto XYZ MB or kb
b) delete that file
c) repeat steps a, b N number of times, depending on how long you want your prog to run.
2) Keep Sending XX bytes of data to some random ip address (not internal)
3) Assuming you have some RDB connection,
create small table in database and keep inserting and deleting single record in that table.
this question will exmamine you knowledge of computer Arch and OS, here is some of my idea:
1) make your C program work as an IST for a never-happend Interrupt
2)make your C program work as an handler for a never-happend SWI entry
3)make your C program able to change his PCB to give up his CPU cycle
Well I guess this guy is in the funny mood to ask this kinda question. However, I have the humorous solution for him..
Why don't U give a code of any recursive or backtracking problem.
I would have given Tower of Hanoi with 512 disk.. Code will not take forever to finish yet it will have a great deal of time... haha
No hogging no wait no resource blocking
Keep on doing some I/O operations in a tight loop for a longer duration.
Is it enough ?
for(.......)
{
scanf("%d",&n);
scanf("%d",&n);
................
}
I might not be understanding the original question correctly, but scanf is a blocking call so that won't work - i.e. the program will not terminate. I think a better solution may be to use signals and threads, e.g. use an user defined signal that is triggered after a "very very very" long time that terminates a thread that is waiting or doing some low-CPU computation.
Just make the program read characters from stdin.
- Arun February 11, 2013exit only if a random sequence is entered.
will not hog cpu. will take a long long time to terminate :)